math
The math module provides mathematical functions and constants. It is written in Koji (std/math.kj), the functions of floats and decimals included (in the runtime, std/koji/floats.kj and std/koji/decimal.kj): the results are the same on every system.
Functions
Every function takes an int, a decimal or a float. A decimal (or an int) gives a decimal and a float gives a float; a function of two arguments does not mix a decimal and a float.
abs(x: int) -> int,abs(x: float) -> float,abs(x: decimal) -> decimal
Returns the absolute value ofx, with the same type asx.sqrt(x: decimal) -> Result[decimal, MathError],sqrt(x: float) -> Result[float, MathError]
Returns the square root ofx, correctly rounded (for a decimal, to 34 digits; the root of anintis a decimal). Fails withComplexwhenxis negative.pow(x: decimal, y: decimal) -> Result[decimal, MathError],pow(x: float, y: float) -> Result[float, MathError]
Returnsxraised to the power ofy. For decimals, anintexponent gives the exact power rounded once, and another exponent is computed with 64 digits and rounded to 34; for floats, the float nearest to the exact power. Fails withComplexwhenxis negative andyis not an integer (e.g.pow(-8, 0.5)), and withInfinitewhenxis0andyis negative (e.g.pow(0, -1), which is1 / 0).pow(0, 0)is1: that is the value of the expression (only the limit form 0⁰ is indeterminate).floor(x: decimal) -> int
Returns the largest integer less than or equal tox.ceil(x: decimal) -> int
Returns the smallest integer greater than or equal tox.round(x: decimal) -> int
Roundsxto the nearest integer (halfway cases away from zero:round(2.5)is3).
Errors
The math module follows one rule:
- Domain problems return a
MathError. The result does not exist in the real numbers. - Representation limits panic. The result exists, but does not fit in an
int, adecimalor afloat(e.g.math.pow(10, 7000), ormath.round(1e300), which does not fit in anint).
type MathError = {
Complex // the result exists only in the complex numbers: sqrt(-1)
Indeterminate // indeterminate form (no current function returns it)
Infinite // the result is infinite: pow(0, -1)
}
Infinite is not an overflow. To tell them apart, ask whether a larger type would fit the result: pow(10, 7000) is a finite number too large for a decimal (overflow, it panics), while pow(0, -1) is infinite, so no type could represent it (Err(Infinite)).
Operators cannot return a Result, so they panic instead: 1 / 0 and 0.0 ** -1.0 are division by zero (see Operators).
decimal and float have no NaN and no infinity, so these cases cannot be represented as a value (see Primitive Types).
Constants
pi
The mathematical constant π, as a decimal of 34 digits:3.141592653589793238462643383279503.e
Euler's number, as a decimal of 34 digits:2.718281828459045235360287471352662.
Example
import math
match (math.pow(5, 2)) {
Ok(squared) => print(math.pi * squared)
Err(error) => print(error)
}
match (math.sqrt(-1)) {
Ok(root) => print(root)
Err(Complex) => print('no real square root')
Err(Indeterminate) => print('indeterminate')
Err(Infinite) => print('infinite')
}