math - numeric functions and constants
Enable with use math;. A small set of frequently-needed numeric functions
plus the constants math.PI and math.E. The library is strict on undefined
inputs - anything that would produce NaN or Infinity in IEEE arithmetic
instead produces a positioned runtime error.
use io;
use math;
io.printf("%f\n", math.PI); # 3.141592653589793
io.printf("%d\n", math.abs(0 - 42)); # 42
io.printf("%d\n", math.min(3, 7)); # 3
io.printf("%f\n", math.sqrt(2)); # 1.4142135623730951
io.printf("%f\n", math.pow(2, 10)); # 1024.0
io.printf("%d\n", math.floor(3.7)); # 3
io.printf("%d\n", math.ceil(3.2)); # 4
io.printf("%d\n", math.round(2.5)); # 3 (half away from zero)
Functions
| Call | Returns | Notes |
|---|---|---|
math.abs(x) | same type as x | int → int, float → float; errors on math.abs(MinInt64) (no representable result) |
math.min(a, b) | int or float | int+int → int; mixed → float |
math.max(a, b) | int or float | same rule as min |
math.sqrt(x) | float | errors on negative input |
math.pow(x, y) | float | errors if the result would be NaN or Infinity |
math.floor(x) | int | toward -∞; accepts int (identity); errors if the result does not fit in a 64-bit int (NaN / Inf / out of range) |
math.ceil(x) | int | toward +∞; same int-range error as floor |
math.round(x) | int | half-away-from-zero (math.round(2.5) = 3); same int-range error as floor |
min/max follow the same numeric-promotion rule as +: same-type
arguments return that type; any float involved produces a float.
Constants
| Name | Kind | Value |
|---|---|---|
math.PI | float | 3.141592653589793… |
math.E | float | 2.718281828459045… |
Constants are referenced through the math. namespace prefix like
every other library name; the bare identifiers PI and E
are not in scope. With use math as m; the alias takes over
(m.PI, m.E).
Strictness
The library refuses to produce floating-point edge values:
math.sqrt(-1)- undefined for negative input.math.pow(0, -1)- division-by-zero territory; result would be Infinity.math.pow(-1, 0.5)- would be NaN.
If a future use case needs the NaN/Infinity values, a math.NAN / math.INF
constant (or dedicated check functions) can be added later. For now Jennifer
treats them as errors at the boundary - consistent with how the language
already refuses to silently coerce types.
See also: ../user-guide/index.md, ../technical/interpreter.md, index.md.