Skip to content

Arithmetic and Exact Numbers

Numbers support calculations such as totals, rates, and comparisons. The choice of number representation matters: ordinary numeric literals use integers or floating-point values, while explicit decimals and fractions preserve exact quantities for calculations such as money or shares.

This page extends the basic arithmetic in Syntax Basics with remainders, powers, exact numbers, and rounding. Snippets show expressions; use them after equals, Print, or Respond with in a program.

Beyond basic arithmetic, modulo returns a remainder, integer division returns a whole quotient, and powers apply repeated multiplication. The examples below show each operation on existing values:

total modulo 3
total integer divided by 12
principal times decimal 1.05 to the power of years
side squared
side cubed

%, //, and ** or ^ are symbolic alternatives. Powers bind more tightly than multiplication. For negative inputs, modulo and integer division have different sign and rounding behavior for decimals than for other numeric kinds; use explicit number types when those details matter. Dividing or taking a remainder by zero fails.

Plain numbers are floats (with whole numbers kept as integers). For money and other exact arithmetic, use decimals and fractions:

decimal 19.99
price times 3
round total times decimal 0.0825 to 2 decimal places
fraction 1 over 3
sum of list of decimal 0.1, decimal 0.2, decimal 0.3
decimal of some_number
numeric value of total
  • decimal <digits> is exact from the source text: decimal 19.99 is exactly 19.99, and decimal 19.99 times 3 is exactly 59.97. decimal of X converts a number or string at runtime; a bad literal is a compile error.
  • fraction A over B is an exact rational, always reduced (fraction 2 over 6 is 1/3). Read parts back with numerator of f / denominator of f. A zero denominator is a compile error when literal, a loud runtime error otherwise.
  • Integers combine exactly with decimals and fractions. Mixing a decimal or fraction with a float in arithmetic is a loud error; convert explicitly with decimal of X or numeric value of X so precision loss is a decision, not an accident. Comparisons across kinds are allowed and compare quantities (decimal 5.0 equals 5 is true).
  • round X to N decimal places rounds exactly, defaulting to banker’s rounding (half even). Add rounding half up (or half down, up, down, ceiling, floor) for other modes. The result is a decimal.
  • sum of, average of, minimum of, maximum of, and sort are exact over lists containing decimals or fractions.
  • Decimals print and concatenate as their quantity ("total: " plus decimal 19.99 is "total: 19.99"); in JSON output they appear as tagged records ({"__type": "decimal", "value": "59.97"}), so exactness survives journaling, checkpoints, and the browser runtime.