Math Module
The aivi.math module provides standard numeric functions and constants for Int and Float. It is intentionally small, predictable, and aligned with common math libraries across languages.
Overview
aivi
use aivi.math
area = pi * r * r
clamped = clamp 0.0 1.0 xConstants
aivi
pi : Float
tau : Float
e : Float
inf : Float
nan : Float
phi : Float
sqrt2 : Float
ln2 : Float
ln10 : FloatAngles
Angles are represented by a dedicated domain so trigonometric functions are not called with raw Float values.
aivi
Angle = { radians: Float }| Function | Explanation |
|---|---|
radians value | Creates an Angle from a raw radians value. |
| Function | Explanation |
|---|---|
degrees value | Creates an Angle from a raw degrees value. |
| Function | Explanation |
|---|---|
toRadians angle | Extracts the radians value from an Angle. |
| Function | Explanation |
|---|---|
toDegrees angle | Extracts the degrees value from an Angle. |
Basic helpers
| Function | Explanation |
|---|---|
abs value | Returns the absolute value of value. |
abs value | Returns the absolute value of value. |
| Function | Explanation |
|---|---|
sign x | Returns -1.0, 0.0, or 1.0 based on the sign of x. |
copysign mag sign | Returns mag with the sign of sign. |
| Function | Explanation |
|---|---|
min a b | Returns the smaller of a and b. |
max a b | Returns the larger of a and b. |
minAll values | Returns the minimum of values or None when empty. |
maxAll values | Returns the maximum of values or None when empty. |
| Function | Explanation |
|---|---|
clamp low high x | Limits x to the closed interval [low, high]. |
sum values | Sums values (empty list yields 0.0). |
sumInt values | Sums values (empty list yields 0). |
Rounding and decomposition
| Function | Explanation |
|---|---|
floor x | Rounds toward -inf. |
ceil x | Rounds toward +inf. |
trunc x | Rounds toward 0. |
round x | Uses banker's rounding (ties to even). |
fract x | Returns the fractional part with the same sign as x. |
| Function | Explanation |
|---|---|
modf x | Returns (intPart, fracPart) where x = intPart + fracPart. |
frexp x | Returns (mantissa, exponent) such that x = mantissa * 2^exponent. |
ldexp mantissa exponent | Computes mantissa * 2^exponent. |
Powers, roots, and logs
| Function | Explanation |
|---|---|
pow base exp | Raises base to exp. |
sqrt x | Computes the square root. |
cbrt x | Computes the cube root. |
hypot x y | Computes sqrt(x*x + y*y) with reduced overflow/underflow. |
| Function | Explanation |
|---|---|
exp x | Computes e^x. |
exp2 x | Computes 2^x. |
expm1 x | Computes e^x - 1 with improved precision near zero. |
| Function | Explanation |
|---|---|
log x | Computes the natural log. |
log10 x | Computes the base-10 log. |
log2 x | Computes the base-2 log. |
log1p x | Computes log(1 + x) with improved precision near zero. |
Trigonometry
| Function | Explanation |
|---|---|
sin angle | Computes the sine ratio for angle. |
cos angle | Computes the cosine ratio for angle. |
tan angle | Computes the tangent ratio for angle. |
| Function | Explanation |
|---|---|
asin x | Returns the angle whose sine is x. |
acos x | Returns the angle whose cosine is x. |
atan x | Returns the angle whose tangent is x. |
atan2 y x | Returns the angle of the vector (x, y) from the positive x-axis. |
Hyperbolic functions
| Function | Explanation |
|---|---|
sinh x | Computes hyperbolic sine. |
cosh x | Computes hyperbolic cosine. |
tanh x | Computes hyperbolic tangent. |
asinh x | Computes inverse hyperbolic sine. |
acosh x | Computes inverse hyperbolic cosine. |
atanh x | Computes inverse hyperbolic tangent. |
Integer math
| Function | Explanation |
|---|---|
gcd a b | Computes the greatest common divisor. |
lcm a b | Computes the least common multiple. |
gcdAll values | Returns the gcd of all values or None when empty. |
lcmAll values | Returns the lcm of all values or None when empty. |
| Function | Explanation |
|---|---|
factorial n | Computes n!. |
comb n k | Computes combinations ("n choose k"). |
perm n k | Computes permutations ("n P k"). |
| Function | Explanation |
|---|---|
| divmod | Int -> Int -> (Int, Int) |
| modPow | Int -> Int -> Int -> Int |
Notes:
BigIntis fromaivi.number.bigintand is re-exported byaivi.math.
Floating-point checks
| Function | Explanation |
|---|---|
isFinite x | Returns whether x is finite. |
isInf x | Returns whether x is infinite. |
isNaN x | Returns whether x is NaN. |
nextAfter from to | Returns the next representable float after from toward to. |
ulp x | Returns the size of one unit-in-the-last-place at x. |
Remainders
| Function | Explanation |
|---|---|
fmod a b | Returns the remainder using truncation toward zero. |
remainder a b | Returns the IEEE-754 remainder (round-to-nearest quotient). |
Usage Examples
aivi
use aivi.math
angle = degrees 90.0
unit = sin angle
digits = [1.0, 2.0, 3.0] |> sum