Lesson 3: Arithmetic and Primitives

Beginner

Primitives are built-in operations provided by the Phograph runtime. They cover arithmetic, comparison, logic, string manipulation, and more. This lesson focuses on the arithmetic and comparison primitives.

Arithmetic primitives

PrimitiveInputsOutputDescription
+a, ba + bAddition
-a, ba - bSubtraction
*a, ba * bMultiplication
/a, ba / bDivision
moda, ba mod bModulo (remainder)
absa|a|Absolute value
roundaround(a)Round to nearest integer

Walkthrough: Basic Arithmetic

Load the Basic Arithmetic example. It computes (3 + 4) * 2 = 14.

Inputs 3 4 a b + 3 + 4 = 7 2 sum b * 7 * 2 = 14 result Outputs returns 14

Execution order

  1. Constants 3, 4, and 2 fire immediately.
  2. + receives both inputs (3 and 4), fires, outputs 7.
  3. * receives 7 from + and 2 from the constant, fires, outputs 14.
  4. 14 flows to the output bar.

Notice that the constant 2 fired at the same time as 3 and 4. It didn’t need to wait — it has no inputs. The * node waited for both its inputs before firing. This is the data-driven firing rule.

Comparison primitives

PrimitiveInputsOutputDescription
=a, bBooleanEqual
!=a, bBooleanNot equal
<a, bBooleanLess than
>a, bBooleanGreater than
<=a, bBooleanLess than or equal
>=a, bBooleanGreater than or equal

Comparisons output true or false. They are essential for control flow, which we’ll cover in Lesson 5.

Logic primitives

PrimitiveInputsOutputDescription
anda, bBooleanBoth must be true
ora, bBooleanEither can be true
notaBooleanInvert

Exercise 3.1 — Temperature converter

Build a method that converts Fahrenheit to Celsius: C = (F - 32) * 5 / 9

  1. Create a new project.
  2. Add a method main with 0 inputs and 1 output.
  3. Add a constant 212 (boiling point of water in °F).
  4. Add a constant 32.
  5. Wire both to a - primitive: 212 - 32 = 180.
  6. Add constants 5 and 9. Wire them with * and / to complete the formula.
  7. Wire the final result to the output bar.
  8. Run. The result should be 100.
Solution diagram 212 32 a b - 5 diff * 9 / result Outputs (100)

Exercise 3.2 — Area of a circle

Compute the area of a circle with radius 7: A = pi * r * r.

  1. Use constant 7 for the radius.
  2. Use constant 3.14159 for pi.
  3. Use two * nodes to compute r * r then pi * r^2.
  4. Wire the result to an inspect node (so it prints and passes through).
  5. Run. You should see approximately 153.938 in the console.
Hint

Use fan-out from the radius constant — connect two wires from its output to both inputs of the first * node.

Exercise 3.3 — Is it even?

  1. Add a constant 17.
  2. Use mod with constant 2 to get the remainder.
  3. Use = with constant 0 to check if the remainder is zero.
  4. Wire the result to log.
  5. Run. Console should show [log] false (17 is odd).
  6. Change the constant to 42 and run again — should be true.

Key Concepts