Math Module
Mathematical functions and constants.
The Math module provides standard mathematical operations. Many functions are polymorphic over numeric types using the number type variable, which accepts both Int and Float. Functions like sqrt and sin require Float arguments.
Common patterns
import Math
-- Basic arithmetic
Math.abs (-5) -- 5
Math.max 3 7 -- 7
Math.clamp 0 100 150 -- 100
-- Rounding (Float -> Int)
Math.floor 3.7 -- 3
Math.ceil 3.2 -- 4
Math.round 3.5 -- 4
-- Powers and roots
Math.sqrt 16.0 -- 4.0
Math.pow 2.0 10.0 -- 1024.0
-- Trigonometry (radians)
Math.sin (Math.pi / 2.0) -- 1.0
Math.atan2 1.0 1.0 -- 0.785... (pi/4)
-- Number theory (Int -> Int)
Math.gcd 12 8 -- 4
Math.lcm 4 6 -- 12
Type variables
number—IntorFloat(e.g.abs : number -> number)comparable— types that support ordering (e.g.min : comparable -> comparable -> comparable)
Rounding functions (floor, ceil, round, truncate) take Float and return Int. Use toFloat : Int -> Float to convert in the other direction.
Functions
Arithmetic
Math.abs
number -> number
Returns the absolute value of a number.
import Math
Math.abs (-5)Try itNotes: Works with both Int and Float.
See also: Math.negate, Math.sign
Math.negate
number -> number
Negate a number.
import Math
Math.negate 5Try itMath.sign
number -> Int
Returns -1, 0, or 1 based on the sign of the number.
import Math
Math.sign 5Try itSee also: Math.abs, Math.negate
Math.rem
Int -> Int -> Int
Returns the remainder of integer division (has the sign of the dividend).
import Math
Math.rem 7 3Try itNotes: Errors on division by zero.
See also: Math.gcd
Math.gcd
Int -> Int -> Int
Returns the greatest common divisor of two integers.
import Math
Math.gcd 12 8Try itNotes: Always returns non-negative value.
See also: Math.lcm
Math.lcm
Int -> Int -> Int
Returns the least common multiple of two integers.
import Math
Math.lcm 4 6Try itNotes: Returns 0 if either input is 0.
See also: Math.gcd
Bounds
Math.min
number -> number -> number
Returns the smaller of two values.
import Math
Math.min 3 5
-- 3Try itNotes: Works with Int, Float, or mixed types.
See also: Math.max, Math.clamp, List.minimum
Math.max
number -> number -> number
Returns the larger of two values.
import Math
Math.max 3 5
-- 5Try itNotes: Works with Int, Float, or mixed types.
See also: Math.min, Math.clamp, List.maximum
Math.clamp
number -> number -> number -> number
Clamp a value to be within the range [min, max].
import Math
Math.clamp 0 10 15Try itRounding
Math.floor
Float -> Int
Rounds a float down to the nearest integer.
import Math
Math.floor 3.7Try itSee also: Math.ceil, Math.round, Math.truncate
Math.ceil
Float -> Int
Rounds a float up to the nearest integer.
import Math
Math.ceil 3.2Try itSee also: Math.floor, Math.round, Math.truncate
Math.round
Float -> Int
Rounds a float to the nearest integer.
import Math
Math.round 3.7Try itNotes: Rounds half away from zero (0.5 -> 1, -0.5 -> -1).
See also: Math.floor, Math.ceil, Math.truncate
Math.truncate
Float -> Int
Truncate a float towards zero.
import Math
Math.truncate 3.9Try itSee also: Math.floor, Math.ceil, Math.round
Powers
Math.sqrt
Float -> Float
Returns the square root of a number.
import Math
Math.sqrt 4.0Try itNotes: Returns NaN for negative inputs.
See also: Math.pow
Math.pow
Float -> Float -> Float
Returns base raised to the power of exponent.
import Math
Math.pow 2.0 3.0
-- 8.0Try itMath.exp
Float -> Float
Returns e raised to the given power.
import Math
Math.exp 1.0Try itMath.log
Float -> Float
Returns the natural logarithm of a number.
import Math
Math.log Math.eTry itNotes: Returns -Infinity for 0, NaN for negative inputs.
See also: Math.log10, Math.log2, Math.exp
Math.log2
Float -> Float
Returns the base-2 logarithm of a number.
import Math
Math.log2 8.0
-- 3.0Try itSee also: Math.log, Math.log10
Math.log10
Float -> Float
Returns the base-10 logarithm of a number.
import Math
Math.log10 100.0
-- 2.0Try itTrigonometry
Math.sin
Float -> Float
Returns the sine of an angle in radians.
import Math
Math.sin (Math.pi / 2.0)Try itMath.cos
Float -> Float
Returns the cosine of an angle in radians.
import Math
Math.cos 0.0
-- 1.0Try itMath.tan
Float -> Float
Returns the tangent of an angle in radians.
import Math
Math.tan 0.0
-- 0.0Try itMath.asin
Float -> Float
Returns the arc sine in radians.
import Math
Math.asin 1.0Try itNotes: Input must be in range [-1, 1].
Math.acos
Float -> Float
Returns the arc cosine in radians.
import Math
Math.acos 1.0
-- 0.0Try itNotes: Input must be in range [-1, 1].
Math.atan
Float -> Float
Returns the arc tangent in radians.
import Math
Math.atan 1.0Try itSee also: Math.tan, Math.atan2
Math.atan2
Float -> Float -> Float
Returns the arc tangent of y/x in radians, using signs to determine quadrant.
import Math
Math.atan2 1.0 1.0
-- 0.7853... (pi/4)Try itNotes: More robust than atan(y/x) for determining angle.
See also: Math.atan
Hyperbolic
Math.sinh
Float -> Float
Returns the hyperbolic sine.
import Math
Math.sinh 0.0
-- 0.0Try itMath.cosh
Float -> Float
Returns the hyperbolic cosine.
import Math
Math.cosh 0.0
-- 1.0Try itMath.tanh
Float -> Float
Returns the hyperbolic tangent.
import Math
Math.tanh 0.0
-- 0.0Try itConstants
Math.pi
Float
The mathematical constant π (3.14159...).
import Math
Math.piTry itSee also: Math.e
Math.e
Float
Euler's number e (2.71828...).
import Math
Math.eTry itSee also: Math.pi
Checks
Math.isNaN
Float -> Bool
Returns true if the value is NaN (Not a Number).
import Math
Math.isNaN 1.0Try itSee also: Math.isInfinite, Math.isFinite
Math.isInfinite
Float -> Bool
Returns true if the value is positive or negative infinity.
import Math
Math.isInfinite 1.0Try itSee also: Math.isNaN, Math.isFinite
Math.isFinite
Float -> Bool
Returns true if the value is neither infinite nor NaN.
import Math
Math.isFinite 1.0Try itSee also: Math.isNaN, Math.isInfinite
Converting
Math.toFloat
Int -> Float
Convert an integer to a float.
import Math
Math.toFloat 42
-- 42.0Try itSee also: Math.floor, Math.round
Math.degrees
Float -> Float
Convert radians to degrees.
import Math
Math.degrees Math.piTry itSee also: Math.radians
Math.radians
Float -> Float
Convert degrees to radians.
import Math
Math.radians 180.0
-- 3.14159... (pi)Try itSee also: Math.degrees