Math
The Math module provides mathematical functions and constants for numeric computations.
Most functions are available both as bare functions and with the Math. prefix.
Constants
Math.pi
Float
The mathematical constant pi (3.14159...).
println Math.pi
# => 3.141592653589793
Math.e
Float
Euler's number (2.71828...).
println Math.e
# => 2.718281828459045
Math.tau
Float
Tau, equal to 2 * pi (6.28318...).
Math.phi
Float
The golden ratio (1.61803...).
Math.infinity
Float
Positive infinity.
Math.neg-infinity
Float
Negative infinity.
println Math.neg-infinity
# => -inf
Math.euler-gamma
Float
The Euler-Mascheroni constant (0.57721...).
println Math.euler-gamma
# => 0.5772156649015329
Math.sqrt2
Float
The square root of 2 (1.41421...).
println Math.sqrt2
# => 1.4142135623730951
Math.sqrt3
Float
The square root of 3 (1.73205...).
println Math.sqrt3
# => 1.7320508075688772
Math.ln2
Float
The natural logarithm of 2 (0.69314...).
println Math.ln2
# => 0.6931471805599453
Math.ln10
Float
The natural logarithm of 10 (2.30258...).
println Math.ln10
# => 2.302585092994046
Basic Functions
Math.abs
Int -> Int
Returns the absolute value of an integer.
println (Math.abs -42)
# => 42
Math.abs-float
Float -> Float
Returns the absolute value of a float.
Math.min
Int -> Int -> Int
Returns the smaller of two integers.
println (Math.min 3 7)
# => 3
Math.max
Int -> Int -> Int
Returns the larger of two integers.
println (Math.max 3 7)
# => 7
Math.sqrt
Float -> Float
Returns the square root of a number.
println (Math.sqrt 16.0)
# => 4.0
Math.pow / Math.power
Float -> Float -> Float
Raises a number to a power.
println (Math.pow 2.0 10.0)
# => 1024.0
Math.floor
Float -> Int
Rounds down to the nearest integer.
println (Math.floor 3.7)
# => 3
Math.ceil
Float -> Int
Rounds up to the nearest integer.
println (Math.ceil 3.2)
# => 4
Math.round
Float -> Int
Rounds to the nearest integer.
println (Math.round 3.5)
# => 4
Math.trunc
Float -> Int
Truncates toward zero.
Math.sum
List Int -> Int
Returns the sum of a list of numbers.
numbers = [1, 2, 3, 4, 5]
println (Math.sum numbers)
# => 15
Math.product
List Int -> Int
Returns the product of a list of numbers.
numbers = [2, 3, 4]
println (Math.product numbers)
# => 24
Math.mul-div
Int -> Int -> Int -> Int
Multiplies two integers then divides by a third with higher precision.
This is useful to avoid overflow when computing (a * b) / c.
# Calculate (1000 * 2000) / 500 with higher precision
result = Math.mul-div 1000 2000 500
println result
# => 4000
Trigonometry
Math.sin
Float -> Float
Returns the sine of an angle in radians.
println (Math.sin (Math.pi / 2.0))
# => 1.0
Math.cos
Float -> Float
Returns the cosine of an angle in radians.
Math.tan
Float -> Float
Returns the tangent of an angle in radians.
Exponential and Logarithmic
Math.exp
Float -> Float
Returns e raised to the given power.
println (Math.exp 1.0)
# => 2.718281828459045
Math.log
Float -> Float
Returns the natural logarithm (base e).
println (Math.log Math.e)
# => 1.0
Number Theory
Math.gcd
Int -> Int -> Int
Returns the greatest common divisor of two integers.
println (Math.gcd 48 18)
# => 6
Math.lcm
Int -> Int -> Int
Returns the least common multiple of two integers.
println (Math.lcm 4 6)
# => 12
Math.is-prime?
Int -> Bool
Returns true if the integer is prime.
println (Math.is-prime? 17)
# => true
Math.factorial
Int -> Int
Returns the factorial of a non-negative integer.
println (Math.factorial 5)
# => 120
Math.binomial / Math.choose
Int -> Int -> Int
Returns the binomial coefficient "n choose k".
println (Math.binomial 5 2)
# => 10
Random
Math.random
() -> Float
Returns a random float between 0 and 1.
r = Math.random
println r
# => 0.7234... (random value)
Math.random-int
Int -> Int
Returns a random integer from 0 to n-1.
dice = Math.random-int 6 + 1
println dice
# => 1-6 (random die roll)