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
Absolute value of a float.
Examples
march> Math.abs(-2.5)
2.5
march> (Math.abs(3.0)) == 3.0
trueArc cosine, returns radians in [0, pi]. NaN for x outside [-1, 1].
Examples
march> (Math.acos(1.0)) == 0.0
trueArc sine, returns radians in [-pi/2, pi/2]. NaN for x outside [-1, 1].
Examples
march> Math.asin(1.0) == Math.pi() /. 2.0
trueArc tangent, returns radians in (-pi/2, pi/2).
Examples
march> (Math.atan(0.0)) == 0.0
trueArc 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
trueCube 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
trueSmallest 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
trueDivide 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)
NoneCompute 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)
NoneClamps 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.25Clamps 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)
7Cosine of x (in radians).
Examples
march> (Math.cos(0.0)) == 1.0
trueHyperbolic cosine.
Examples
march> (Math.cosh(0.0)) == 1.0
trueEuler's number e (2.71828...).
Examples
march> (Math.log(Math.e())) == 1.0
truee raised to the power x.
Examples
march> (Math.exp(0.0)) == 1.0
true2 raised to the power x.
Examples
march> (Math.exp2(10.0)) == 1024.0
true
march> Math.exp2(-1.0)
0.5Largest 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
trueLinear 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
trueNatural logarithm (base e). Returns negative infinity for 0.0 and NaN for negative x.
Examples
march> (Math.log(1.0)) == 0.0
trueLogarithm base 10. Same domain as log.
Examples
march> (Math.log10(1000.0)) == 3.0
trueLogarithm base 2. Same domain as log.
Examples
march> (Math.log2(1024.0)) == 10.0
trueReturns 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
trueReturns the larger of two Ints.
Examples
march> Math.max_int(3, -1)
3Returns 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.5Returns the smaller of two Ints.
Examples
march> Math.min_int(3, -1)
-1The mathematical constant pi (3.14159...).
Examples
march> Math.pi() > 3.14159 && Math.pi() < 3.1416
trueRaises 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
trueRounds 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
trueSine of x (in radians).
Examples
march> (Math.sin(0.0)) == 0.0
trueHyperbolic sine.
Examples
march> (Math.sinh(0.0)) == 0.0
trueSquare root. Returns NaN for negative x.
Examples
march> (Math.sqrt(16.0)) == 4.0
true
march> (Math.sqrt(0.0)) == 0.0
trueTangent of x (in radians).
Examples
march> (Math.tan(0.0)) == 0.0
trueHyperbolic tangent, in (-1, 1).
Examples
march> (Math.tanh(0.0)) == 0.0
trueTau = 2*pi (6.28318...), one full turn in radians.
Examples
march> Math.tau() == 2.0 *. Math.pi()
trueRounds 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