Skip to content
Halyard
Project
Source code on Codeberg (opens in new tab)

Search

Menu

Math Module

Mathematical functions and constants.

The Math module provides standard mathematical operations. sqrt takes a Real value (Float or Decimal) and returns Maybe (Nothing for a negative input); sin and the other trigonometric functions take a Float, and an Int argument widens to it.

Common patterns

halyard
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                 -- Just 4.0
Math.pow 2.0 10.0              -- Just 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

Numeric a => a -> a

Returns the absolute value of a number.

Example:
halyard
import Math

Math.abs (-5)

Notes: Works with Int, Float, and Decimal.

See also: Math.negate, Math.sign

Math.negate

Numeric a => a -> a

Negate a number.

Example:
halyard
import Math

Math.negate 5

See also: Math.abs, Math.sign

Math.sign

Numeric a => a -> Int

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

Example:
halyard
import Math

Math.sign 5

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:
halyard
import Math
import Maybe

Math.rem 7 3 |> Maybe.withDefault 0

Notes: Returns Nothing instead of erroring when the divisor is 0 at run time; a literal 0 divisor (Math.rem 7 0) is the compile-time error RemDivisorIsZero.

See also: Math.gcd

Math.gcd

Int -> Int -> Int

Returns the greatest common divisor of two integers.

Example:
halyard
import Math

Math.gcd 12 8

Notes: Always returns non-negative value.

See also: Math.lcm

Math.lcm

Int -> Int -> Int

Returns the least common multiple of two integers.

Example:
halyard
import Math

Math.lcm 4 6

Notes: Returns 0 if either input is 0.

See also: Math.gcd

Bounds

Math.min

Ord a => a -> a -> a

Returns the smaller of two values.

Example:
halyard
import Math

Math.min 3 5

-- 3

Notes: Works with any Ord type -- Int, Float, Decimal, String -- and both values are of one type; an Int beside a Float widens (Math.min 2 1.5 is 1.5).

See also: Math.max, Math.clamp, List.minimum

Math.max

Ord a => a -> a -> a

Returns the larger of two values.

Example:
halyard
import Math

Math.max 3 5

-- 5

Notes: Works with any Ord type -- Int, Float, Decimal, String -- and both values are of one type; an Int beside a Float widens (Math.max 2 1.5 is 2.0).

See also: Math.min, Math.clamp, List.maximum

Math.clamp

Ord a => a -> a -> a -> a

Clamp a value to be within the range [min, max].

Example:
halyard
import Math

Math.clamp 0 10 15

See also: Math.min, Math.max

Rounding

Math.floor

Numeric a => a -> Int

Rounds a number down to the nearest integer.

Example:
halyard
import Math

Math.floor 3.7

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

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

Math.ceil

Numeric a => a -> Int

Rounds a number up to the nearest integer.

Example:
halyard
import Math

Math.ceil 3.2

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

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

Math.round

Numeric a => a -> Int

Rounds a number to the nearest integer.

Example:
halyard
import Math

Math.round 3.7

Notes: Accepts Int (identity), Float, or Decimal. A Float rounds half away from zero (Math.round 0.5 is 1, Math.round (-2.5) is -3); a Decimal rounds half to even (Math.round 2.5d is 2, Math.round 3.5d is 4).

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

Math.truncate

Numeric a => a -> Int

Truncate a number toward zero.

Example:
halyard
import Math

Math.truncate 3.9

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:
halyard
import Math
import Maybe

Math.sqrt 4.0 |> Maybe.withDefault 0.0

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:
halyard
import Math
import Maybe

Math.pow 2.0 3.0 |> Maybe.withDefault 0.0

-- 8.0

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:
halyard
import Math
import Maybe

Math.exp 1.0 |> Maybe.withDefault 0.0

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:
halyard
import Math
import Maybe

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

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:
halyard
import Math
import Maybe

Math.log2 8.0 |> Maybe.withDefault 0.0

-- 3.0

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:
halyard
import Math
import Maybe

Math.log10 100.0 |> Maybe.withDefault 0.0

-- 2.0

See also: Math.log, Math.log2

Trigonometry

Math.sin

Float -> Float

Returns the sine of an angle in radians.

Example:
halyard
import Math

Math.sin (Math.pi / 2.0)

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

Math.cos

Float -> Float

Returns the cosine of an angle in radians.

Example:
halyard
import Math

Math.cos 0.0

-- 1.0

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

Math.tan

Float -> Float

Returns the tangent of an angle in radians.

Example:
halyard
import Math

Math.tan 0.0

-- 0.0

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:
halyard
import Math
import Maybe

Math.asin 1.0 |> Maybe.withDefault 0.0

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:
halyard
import Math
import Maybe

Math.acos 1.0 |> Maybe.withDefault 0.0

-- 0.0

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

Math.atan

Float -> Float

Returns the arc tangent in radians.

Example:
halyard
import Math

Math.atan 1.0

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:
halyard
import Math

Math.atan2 1.0 1.0

-- 0.7853... (pi/4)

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:
halyard
import Math
import Maybe

Math.sinh 0.0 |> Maybe.withDefault 0.0

-- 0.0

See also: Math.cosh, Math.tanh

Math.cosh

Float -> Maybe Float

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

Example:
halyard
import Math
import Maybe

Math.cosh 0.0 |> Maybe.withDefault 0.0

-- 1.0

See also: Math.sinh, Math.tanh

Math.tanh

Float -> Float

Returns the hyperbolic tangent.

Example:
halyard
import Math

Math.tanh 0.0

-- 0.0

See also: Math.sinh, Math.cosh

Constants

Math.pi

Float

The mathematical constant π (3.14159...).

Example:
halyard
import Math

Math.pi

See also: Math.e

Math.e

Float

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

Example:
halyard
import Math

Math.e

See also: Math.pi

Converting

Math.toFloat

Int -> Float

Convert an integer to a float.

Example:
halyard
import Math

Math.toFloat 42

-- 42.0

See also: Math.floor, Math.round

Math.degrees

Float -> Float

Convert radians to degrees.

Example:
halyard
import Math

Math.degrees Math.pi

See also: Math.radians

Math.radians

Float -> Float

Convert degrees to radians.

Example:
halyard
import Math

Math.radians 180.0

-- 3.14159... (pi)

See also: Math.degrees