Esc
Start typing to search...

Math Module

Mathematical functions and constants.

The Math module provides standard mathematical operations. 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 (any numeric -> 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

Trait constraints

abs, negate, and sign are constrained by the Numeric trait, accepting Int, Float, or Decimal (e.g. abs : Numeric a => a -> a). Passing a String or Bool is a compile-time error.

Ordering functions min, max, and clamp are constrained by Ord a (e.g. min : Ord a => a -> a -> a). See the Traits guide for details.

Rounding functions (floor, ceil, round, truncate) accept any numeric type (Int, Float, or Decimal) and return Int. For Int input, the result is the identity.

Functions

Arithmetic

Math.abs

number -> number

Returns the absolute value of a number.

Example:
import Math

Math.abs (-5)
Try it

Notes: Works with Int, Float, and Decimal.

See also: Math.negate, Math.sign

Math.negate

number -> number

Negate a number.

Example:
import Math

Math.negate 5
Try it

See also: Math.abs, Math.sign

Math.sign

number -> Int

Returns -1, 0, or 1 based on the sign of the number.

Example:
import Math

Math.sign 5
Try it

See 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.

Example:
import Math
import Maybe

Math.rem 7 3 |> Maybe.withDefault 0
Try it

Notes: 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.

Example:
import Math

Math.gcd 12 8
Try it

Notes: Always returns non-negative value.

See also: Math.lcm

Math.lcm

Int -> Int -> Int

Returns the least common multiple of two integers.

Example:
import Math

Math.lcm 4 6
Try it

Notes: Returns 0 if either input is 0.

See also: Math.gcd

Bounds

Math.min

number -> number -> number

Returns the smaller of two values.

Example:
import Math

Math.min 3 5

-- 3
Try it

Notes: 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.

Example:
import Math

Math.max 3 5

-- 5
Try it

Notes: 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].

Example:
import Math

Math.clamp 0 10 15
Try it

See also: Math.min, Math.max

Rounding

Math.floor

number -> Int

Rounds a number down to the nearest integer.

Example:
import Math

Math.floor 3.7
Try it

Notes: Accepts Int (identity), Float, or Decimal.

See also: Math.ceil, Math.round, Math.truncate

Math.ceil

number -> Int

Rounds a number up to the nearest integer.

Example:
import Math

Math.ceil 3.2
Try it

Notes: Accepts Int (identity), Float, or Decimal.

See also: Math.floor, Math.round, Math.truncate

Math.round

number -> Int

Rounds a number to the nearest integer.

Example:
import Math

Math.round 3.7
Try it

Notes: Rounds half away from zero (0.5 -> 1, -0.5 -> -1). Accepts Int (identity), Float, or Decimal.

See also: Math.floor, Math.ceil, Math.truncate

Math.truncate

number -> Int

Truncate a number toward zero.

Example:
import Math

Math.truncate 3.9
Try it

Notes: Accepts Int (identity), Float, or Decimal.

See also: Math.floor, Math.ceil, Math.round

Powers

Math.sqrt

Real a => a -> Maybe a

Returns the square root of a number, or Nothing if the input is negative. Accepts Float or Decimal.

Example:
import Math
import Maybe

Math.sqrt 4.0 |> Maybe.withDefault 0.0
Try it

See also: Math.pow

Math.pow

Real a => a -> a -> Maybe a

Returns base raised to the power of exponent, or Nothing if the result is undefined. Accepts Float or Decimal.

Example:
import Math
import Maybe

Math.pow 2.0 3.0 |> Maybe.withDefault 0.0

-- 8.0
Try it

See also: Math.sqrt, Math.exp

Math.exp

Real a => a -> Maybe a

Returns e raised to the given power, or Nothing if the result overflows. Accepts Float or Decimal.

Example:
import Math
import Maybe

Math.exp 1.0 |> Maybe.withDefault 0.0
Try it

See also: Math.log, Math.e

Math.log

Real a => a -> Maybe a

Returns the natural logarithm of a number, or Nothing if the input is not positive. Accepts Float or Decimal.

Example:
import Math
import Maybe

Math.log Math.e |> Maybe.withDefault 0.0
Try it

See also: Math.log10, Math.log2, Math.exp

Math.log2

Real a => a -> Maybe a

Returns the base-2 logarithm of a number, or Nothing if the input is not positive. Accepts Float or Decimal.

Example:
import Math
import Maybe

Math.log2 8.0 |> Maybe.withDefault 0.0

-- 3.0
Try it

See also: Math.log, Math.log10

Math.log10

Real a => a -> Maybe a

Returns the base-10 logarithm of a number, or Nothing if the input is not positive. Accepts Float or Decimal.

Example:
import Math
import Maybe

Math.log10 100.0 |> Maybe.withDefault 0.0

-- 2.0
Try it

See also: Math.log, Math.log2

Trigonometry

Math.sin

Float -> Float

Returns the sine of an angle in radians.

Example:
import Math

Math.sin (Math.pi / 2.0)
Try it

See also: Math.cos, Math.tan, Math.asin

Math.cos

Float -> Float

Returns the cosine of an angle in radians.

Example:
import Math

Math.cos 0.0

-- 1.0
Try it

See also: Math.sin, Math.tan, Math.acos

Math.tan

Float -> Float

Returns the tangent of an angle in radians.

Example:
import Math

Math.tan 0.0

-- 0.0
Try it

See also: Math.sin, Math.cos, Math.atan

Math.asin

Float -> Maybe Float

Returns the arc sine in radians, or Nothing if the input is outside [-1, 1].

Example:
import Math
import Maybe

Math.asin 1.0 |> Maybe.withDefault 0.0
Try it

See also: Math.sin, Math.acos, Math.atan

Math.acos

Float -> Maybe Float

Returns the arc cosine in radians, or Nothing if the input is outside [-1, 1].

Example:
import Math
import Maybe

Math.acos 1.0 |> Maybe.withDefault 0.0

-- 0.0
Try it

See also: Math.cos, Math.asin, Math.atan

Math.atan

Float -> Float

Returns the arc tangent in radians.

Example:
import Math

Math.atan 1.0
Try it

See also: Math.tan, Math.atan2

Math.atan2

Float -> Float -> Float

Returns the arc tangent of y/x in radians, using signs to determine quadrant.

Example:
import Math

Math.atan2 1.0 1.0

-- 0.7853... (pi/4)
Try it

Notes: 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.

Example:
import Math
import Maybe

Math.sinh 0.0 |> Maybe.withDefault 0.0

-- 0.0
Try it

See also: Math.cosh, Math.tanh

Math.cosh

Float -> Maybe Float

Returns the hyperbolic cosine, or Nothing if the result overflows to infinity.

Example:
import Math
import Maybe

Math.cosh 0.0 |> Maybe.withDefault 0.0

-- 1.0
Try it

See also: Math.sinh, Math.tanh

Math.tanh

Float -> Float

Returns the hyperbolic tangent.

Example:
import Math

Math.tanh 0.0

-- 0.0
Try it

See also: Math.sinh, Math.cosh

Constants

Math.pi

Float

The mathematical constant π (3.14159...).

Example:
import Math

Math.pi
Try it

See also: Math.e

Math.e

Float

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

Example:
import Math

Math.e
Try it

See also: Math.pi

Converting

Math.toFloat

Int -> Float

Convert an integer to a float.

Example:
import Math

Math.toFloat 42

-- 42.0
Try it

See also: Math.floor, Math.round

Math.degrees

Float -> Float

Convert radians to degrees.

Example:
import Math

Math.degrees Math.pi
Try it

See also: Math.radians

Math.radians

Float -> Float

Convert degrees to radians.

Example:
import Math

Math.radians 180.0

-- 3.14159... (pi)
Try it

See also: Math.degrees