March Docs

Math

Math module: mathematical functions and constants.

Transcendental functions (sin, cos, exp, etc.) delegate to the math_* builtins which wrap libm. Pure functions (min, max, clamp, lerp) are implemented in March.

Functions

fnabsabs(x : Float) : Float#

Absolute value of a float.

Examples

march> Math.abs(-2.5)
2.5
march> (Math.abs(3.0)) == 3.0
true
fnacosacos(x : Float) : Float#

Arc cosine, returns radians in [0, pi]. NaN for x outside [-1, 1].

Examples

march> (Math.acos(1.0)) == 0.0
true
fnasinasin(x : Float) : Float#

Arc sine, returns radians in [-pi/2, pi/2]. NaN for x outside [-1, 1].

Examples

march> Math.asin(1.0) == Math.pi() /. 2.0
true
fnatanatan(x : Float) : Float#

Arc tangent, returns radians in (-pi/2, pi/2).

Examples

march> (Math.atan(0.0)) == 0.0
true
fnatan2atan2(y : Float, x : Float) : Float#

Arc tangent of y/x, using the signs of both arguments to pick the quadrant. Returns radians in [-pi, pi]. Note the argument order: y first.

Examples

march> Math.atan2(0.0, -1.0) == Math.pi()
true
march> Math.atan2(1.0, 1.0) == Math.pi() /. 4.0
true
fncbrtcbrt(x : Float) : Float#

Cube root. Defined for negative x too. The result comes from the platform's C library and need not be exact: glibc's cbrt(27.0) is one unit in the last place above 3.0, so compare with a tolerance.

Examples

march> Math.abs(Math.cbrt(27.0) - 3.0) < 0.000000001
true
march> (Math.cbrt(-8.0)) == -2.0
true
fnceilceil(x : Float) : Float#

Smallest integer not less than x, as a Float. Same Int round-trip caveat as floor.

Examples

march> (Math.ceil(2.1)) == 3.0
true
march> (Math.ceil(-2.7)) == -2.0
true
fnchecked_divchecked_div(a : Int, b : Int) : Option(Int)#

Divide a by b, returning None if b is zero. Division truncates toward zero.

Examples

march> Math.checked_div(7, 2)
Some(3)
march> Math.checked_div(-7, 2)
Some(-3)
march> Math.checked_div(7, 0)
None
fnchecked_modchecked_mod(a : Int, b : Int) : Option(Int)#

Compute a % b, returning None if b is zero. The remainder takes the sign of a.

Examples

march> Math.checked_mod(7, 3)
Some(1)
march> Math.checked_mod(-7, 3)
Some(-1)
march> Math.checked_mod(7, 0)
None
fnclamp_floatclamp_float(x : Float, low : Float, high : Float) : Float#

Clamps x to the range [low, high]: returns low if x < low, high if x > high, else x (so a NaN x is returned unchanged). Expects low <= high; the bounds are not checked.

Examples

march> (Math.clamp_float(1.5, 0.0, 1.0)) == 1.0
true
march> Math.clamp_float(0.25, 0.0, 1.0)
0.25
fnclamp_intclamp_int(x : Int, low : Int, high : Int) : Int#

Clamps x to the range [low, high]: returns low if x < low, high if x > high, else x. Expects low <= high; the bounds are not checked.

Examples

march> Math.clamp_int(15, 0, 10)
10
march> Math.clamp_int(-3, 0, 10)
0
march> Math.clamp_int(7, 0, 10)
7
fncoscos(x : Float) : Float#

Cosine of x (in radians).

Examples

march> (Math.cos(0.0)) == 1.0
true
fncoshcosh(x : Float) : Float#

Hyperbolic cosine.

Examples

march> (Math.cosh(0.0)) == 1.0
true
fnee() : Float#

Euler's number e (2.71828...).

Examples

march> (Math.log(Math.e())) == 1.0
true
fnexpexp(x : Float) : Float#

e raised to the power x.

Examples

march> (Math.exp(0.0)) == 1.0
true
fnexp2exp2(x : Float) : Float#

2 raised to the power x.

Examples

march> (Math.exp2(10.0)) == 1024.0
true
march> Math.exp2(-1.0)
0.5
fnfloorfloor(x : Float) : Float#

Largest integer not greater than x, as a Float. Computed via an Int round-trip, so it is only correct when the result fits in an Int (NaN, infinities and huge magnitudes are not preserved).

Examples

march> (Math.floor(2.7)) == 2.0
true
march> (Math.floor(-2.3)) == -3.0
true
fnlerplerp(a : Float, b : Float, t : Float) : Float#

Linear interpolation: lerp(a, b, t) = a + t*(b-a). Returns a when t=0, b when t=1. t is not clamped, so values outside [0, 1] extrapolate.

Examples

march> (Math.lerp(0.0, 10.0, 0.5)) == 5.0
true
march> (Math.lerp(0.0, 10.0, 2.0)) == 20.0
true
fnloglog(x : Float) : Float#

Natural logarithm (base e). Returns negative infinity for 0.0 and NaN for negative x.

Examples

march> (Math.log(1.0)) == 0.0
true
fnlog10log10(x : Float) : Float#

Logarithm base 10. Same domain as log.

Examples

march> (Math.log10(1000.0)) == 3.0
true
fnlog2log2(x : Float) : Float#

Logarithm base 2. Same domain as log.

Examples

march> (Math.log2(1024.0)) == 10.0
true
fnmax_floatmax_float(a : Float, b : Float) : Float#

Returns the larger of two floats. If either argument is NaN the result is b (the comparison a > b is false).

Examples

march> (Math.max_float(1.5, 2.0)) == 2.0
true
fnmax_intmax_int(a : Int, b : Int) : Int#

Returns the larger of two Ints.

Examples

march> Math.max_int(3, -1)
3
fnmin_floatmin_float(a : Float, b : Float) : Float#

Returns the smaller of two floats. If either argument is NaN the result is b (the comparison a < b is false).

Examples

march> Math.min_float(1.5, 2.0)
1.5
fnmin_intmin_int(a : Int, b : Int) : Int#

Returns the smaller of two Ints.

Examples

march> Math.min_int(3, -1)
-1
fnpipi() : Float#

The mathematical constant pi (3.14159...).

Examples

march> Math.pi() > 3.14159 && Math.pi() < 3.1416
true
fnpowpow(base : Float, exp : Float) : Float#

Raises base to the power exp (both Float), as C pow.

Examples

march> (Math.pow(2.0, 10.0)) == 1024.0
true
march> Math.pow(2.0, -1.0)
0.5
march> (Math.pow(4.0, 0.5)) == 2.0
true
fnroundround(x : Float) : Float#

Rounds to the nearest integer, as a Float; halves round away from zero. Same Int round-trip caveat as floor.

Examples

march> (Math.round(2.5)) == 3.0
true
march> (Math.round(-2.5)) == -3.0
true
march> (Math.round(2.4)) == 2.0
true
fnsinsin(x : Float) : Float#

Sine of x (in radians).

Examples

march> (Math.sin(0.0)) == 0.0
true
fnsinhsinh(x : Float) : Float#

Hyperbolic sine.

Examples

march> (Math.sinh(0.0)) == 0.0
true
fnsqrtsqrt(x : Float) : Float#

Square root. Returns NaN for negative x.

Examples

march> (Math.sqrt(16.0)) == 4.0
true
march> (Math.sqrt(0.0)) == 0.0
true
fntantan(x : Float) : Float#

Tangent of x (in radians).

Examples

march> (Math.tan(0.0)) == 0.0
true
fntanhtanh(x : Float) : Float#

Hyperbolic tangent, in (-1, 1).

Examples

march> (Math.tanh(0.0)) == 0.0
true
fntautau() : Float#

Tau = 2*pi (6.28318...), one full turn in radians.

Examples

march> Math.tau() == 2.0 *. Math.pi()
true
fntruncatetruncate(x : Float) : Float#

Rounds toward zero, as a Float. Same Int round-trip caveat as floor.

Examples

march> (Math.truncate(-2.7)) == -2.0
true
march> (Math.truncate(2.7)) == 2.0
true