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 -> Maybe Int
Returns the remainder of integer division (has the sign of the dividend). Returns Nothing when the divisor is 0.
import Math
import Maybe
Math.rem 7 3 |> Maybe.withDefault 0Try itNotes: Returns Nothing instead of erroring when the divisor is 0.
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 -> Maybe Float
Returns the square root of a number, or Nothing if the input is negative.
import Math
import Maybe
Math.sqrt 4.0 |> Maybe.withDefault 0.0Try itSee also: Math.pow
Math.pow
Float -> Float -> Maybe Float
Returns base raised to the power of exponent, or Nothing if the result is undefined (e.g. negative base with non-integer exponent).
import Math
import Maybe
Math.pow 2.0 3.0 |> Maybe.withDefault 0.0
-- 8.0Try itMath.exp
Float -> Maybe Float
Returns e raised to the given power, or Nothing if the result overflows to infinity.
import Math
import Maybe
Math.exp 1.0 |> Maybe.withDefault 0.0Try itMath.log
Float -> Maybe Float
Returns the natural logarithm of a number, or Nothing if the input is not positive.
import Math
import Maybe
Math.log Math.e |> Maybe.withDefault 0.0Try itSee also: Math.log10, Math.log2, Math.exp
Math.log2
Float -> Maybe Float
Returns the base-2 logarithm of a number, or Nothing if the input is not positive.
import Math
import Maybe
Math.log2 8.0 |> Maybe.withDefault 0.0
-- 3.0Try itSee also: Math.log, Math.log10
Math.log10
Float -> Maybe Float
Returns the base-10 logarithm of a number, or Nothing if the input is not positive.
import Math
import Maybe
Math.log10 100.0 |> Maybe.withDefault 0.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 -> Maybe Float
Returns the arc sine in radians, or Nothing if the input is outside [-1, 1].
import Math
import Maybe
Math.asin 1.0 |> Maybe.withDefault 0.0Try itMath.acos
Float -> Maybe Float
Returns the arc cosine in radians, or Nothing if the input is outside [-1, 1].
import Math
import Maybe
Math.acos 1.0 |> Maybe.withDefault 0.0
-- 0.0Try itMath.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 -> Maybe Float
Returns the hyperbolic sine, or Nothing if the result overflows to infinity.
import Math
import Maybe
Math.sinh 0.0 |> Maybe.withDefault 0.0
-- 0.0Try itMath.cosh
Float -> Maybe Float
Returns the hyperbolic cosine, or Nothing if the result overflows to infinity.
import Math
import Maybe
Math.cosh 0.0 |> Maybe.withDefault 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
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