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.
Remainders, integer division, and powers
Section titled “Remainders, integer division, and powers”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 3total integer divided by 12principal times decimal 1.05 to the power of yearsside squaredside 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.
Exact numbers: decimals and fractions
Section titled “Exact numbers: decimals and fractions”Plain numbers are floats (with whole numbers kept as integers). For money and other exact arithmetic, use decimals and fractions:
decimal 19.99price times 3round total times decimal 0.0825 to 2 decimal placesfraction 1 over 3sum of list of decimal 0.1, decimal 0.2, decimal 0.3decimal of some_numbernumeric value of totaldecimal <digits>is exact from the source text:decimal 19.99is exactly 19.99, anddecimal 19.99 times 3is exactly 59.97.decimal of Xconverts a number or string at runtime; a bad literal is a compile error.fraction A over Bis an exact rational, always reduced (fraction 2 over 6is 1/3). Read parts back withnumerator 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 Xornumeric value of Xso precision loss is a decision, not an accident. Comparisons across kinds are allowed and compare quantities (decimal 5.0 equals 5is true). round X to N decimal placesrounds exactly, defaulting to banker’s rounding (half even). Addrounding half up(orhalf down,up,down,ceiling,floor) for other modes. The result is a decimal.sum of,average of,minimum of,maximum of, andsortare exact over lists containing decimals or fractions.- Decimals print and concatenate as their quantity (
"total: " plus decimal 19.99is"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.