Skip to content

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 x

Constants

aivi
pi : Float
tau : Float
e : Float
inf : Float
nan : Float
phi : Float
sqrt2 : Float
ln2 : Float
ln10 : Float

Angles

Angles are represented by a dedicated domain so trigonometric functions are not called with raw Float values.

aivi
Angle = { radians: Float }
FunctionExplanation
radians value
Float -> Angle
Creates an Angle from a raw radians value.
FunctionExplanation
degrees value
Float -> Angle
Creates an Angle from a raw degrees value.
FunctionExplanation
toRadians angle
Angle -> Float
Extracts the radians value from an Angle.
FunctionExplanation
toDegrees angle
Angle -> Float
Extracts the degrees value from an Angle.

Basic helpers

FunctionExplanation
abs value
Int -> Int
Returns the absolute value of value.
abs value
Float -> Float
Returns the absolute value of value.
FunctionExplanation
sign x
Float -> Float
Returns -1.0, 0.0, or 1.0 based on the sign of x.
copysign mag sign
Float -> Float -> Float
Returns mag with the sign of sign.
FunctionExplanation
min a b
Float -> Float -> Float
Returns the smaller of a and b.
max a b
Float -> Float -> Float
Returns the larger of a and b.
minAll values
List Float -> Option Float
Returns the minimum of values or None when empty.
maxAll values
List Float -> Option Float
Returns the maximum of values or None when empty.
FunctionExplanation
clamp low high x
Float -> Float -> Float -> Float
Limits x to the closed interval [low, high].
sum values
List Float -> Float
Sums values (empty list yields 0.0).
sumInt values
List Int -> Int
Sums values (empty list yields 0).

Rounding and decomposition

FunctionExplanation
floor x
Float -> Float
Rounds toward -inf.
ceil x
Float -> Float
Rounds toward +inf.
trunc x
Float -> Float
Rounds toward 0.
round x
Float -> Float
Uses banker's rounding (ties to even).
fract x
Float -> Float
Returns the fractional part with the same sign as x.
FunctionExplanation
modf x
Float -> (Float, Float)
Returns (intPart, fracPart) where x = intPart + fracPart.
frexp x
Float -> (Float, Int)
Returns (mantissa, exponent) such that x = mantissa * 2^exponent.
ldexp mantissa exponent
Float -> Int -> Float
Computes mantissa * 2^exponent.

Powers, roots, and logs

FunctionExplanation
pow base exp
Float -> Float -> Float
Raises base to exp.
sqrt x
Float -> Float
Computes the square root.
cbrt x
Float -> Float
Computes the cube root.
hypot x y
Float -> Float -> Float
Computes sqrt(x*x + y*y) with reduced overflow/underflow.
FunctionExplanation
exp x
Float -> Float
Computes e^x.
exp2 x
Float -> Float
Computes 2^x.
expm1 x
Float -> Float
Computes e^x - 1 with improved precision near zero.
FunctionExplanation
log x
Float -> Float
Computes the natural log.
log10 x
Float -> Float
Computes the base-10 log.
log2 x
Float -> Float
Computes the base-2 log.
log1p x
Float -> Float
Computes log(1 + x) with improved precision near zero.

Trigonometry

FunctionExplanation
sin angle
Angle -> Float
Computes the sine ratio for angle.
cos angle
Angle -> Float
Computes the cosine ratio for angle.
tan angle
Angle -> Float
Computes the tangent ratio for angle.
FunctionExplanation
asin x
Float -> Angle
Returns the angle whose sine is x.
acos x
Float -> Angle
Returns the angle whose cosine is x.
atan x
Float -> Angle
Returns the angle whose tangent is x.
atan2 y x
Float -> Float -> Angle
Returns the angle of the vector (x, y) from the positive x-axis.

Hyperbolic functions

FunctionExplanation
sinh x
Float -> Float
Computes hyperbolic sine.
cosh x
Float -> Float
Computes hyperbolic cosine.
tanh x
Float -> Float
Computes hyperbolic tangent.
asinh x
Float -> Float
Computes inverse hyperbolic sine.
acosh x
Float -> Float
Computes inverse hyperbolic cosine.
atanh x
Float -> Float
Computes inverse hyperbolic tangent.

Integer math

FunctionExplanation
gcd a b
Int -> Int -> Int
Computes the greatest common divisor.
lcm a b
Int -> Int -> Int
Computes the least common multiple.
gcdAll values
List Int -> Option Int
Returns the gcd of all values or None when empty.
lcmAll values
List Int -> Option Int
Returns the lcm of all values or None when empty.
FunctionExplanation
factorial n
Int -> BigInt
Computes n!.
comb n k
Int -> Int -> BigInt
Computes combinations ("n choose k").
perm n k
Int -> Int -> BigInt
Computes permutations ("n P k").
FunctionExplanation
divmodInt -> Int -> (Int, Int)
modPowInt -> Int -> Int -> Int

Notes:

  • BigInt is from aivi.number.bigint and is re-exported by aivi.math.

Floating-point checks

FunctionExplanation
isFinite x
Float -> Bool
Returns whether x is finite.
isInf x
Float -> Bool
Returns whether x is infinite.
isNaN x
Float -> Bool
Returns whether x is NaN.
nextAfter from to
Float -> Float -> Float
Returns the next representable float after from toward to.
ulp x
Float -> Float
Returns the size of one unit-in-the-last-place at x.

Remainders

FunctionExplanation
fmod a b
Float -> Float -> Float
Returns the remainder using truncation toward zero.
remainder a b
Float -> Float -> Float
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