Skip to content

Math ​

Math holds numeric functions and constants. The functions over float64 follow IEEE 754 and never panic: Math.log(0.0) is -Infinity, and Math.sqrt(-1.0) is NaN.

Details ​

  • Most functions take and return float64 only. Numeric types never mix, so an int argument is a compile error: convert it with float64(n). Math.abs, Math.min, Math.max, and Math.clamp are generic and keep their argument's type.
  • The float64 functions follow IEEE 754 and never panic: outside its domain a function returns NaN or an infinity, as Math.sqrt(-1.0) is NaN and Math.log(0.0) is -Infinity. Three functions do panic: Math.abs on the most negative value of an integer type, Math.clamp when lo > hi or a bound is NaN, and Math.randomInt when to <= from.
  • NaN is not equal to anything, itself included. Test for it with Math.isNaN.
  • Angles are in radians.
  • Math.random and Math.randomInt cannot be seeded. Use Random for numbers you can repeat, and Crypto.randomBytes for keys and tokens.

Basic examples ​

Call a function through the namespace:

ant
Math.sqrt(2.0)      // 1.4142135623730951
Math.pow(2.0, 10.0) // 1024.0
Math.abs(-3)        // 3
Math.max(2, 7)      // 7

Round a float64 four ways:

ant
Math.floor(-2.5) // -3.0
Math.ceil(-2.5)  // -2.0
Math.round(-2.5) // -3.0
Math.trunc(-2.5) // -2.0

The constants are values of the namespace:

ant
Math.PI     // 3.141592653589793
Math.E      // 2.718281828459045
Math.maxInt // 9223372036854775807

Scope ​

Math.abs, Math.min, Math.max, and Math.clamp take any one numeric type, and the result has it:

ant
Math.abs(-2.5)        // 2.5
Math.abs(int8(-5))    // 5
Math.clamp(15, 0, 10) // 10

Math.min, Math.max, and Math.clamp also take strings, which they compare as < does:

ant
Math.min("pear", "apple") // "apple"
Math.clamp("m", "a", "f") // "f"

An int goes through float64 on its way into a float64 function, and through Math.toInt or int on its way back:

ant
let total = 7
let parts = 3
Math.ceil(float64(total) / float64(parts))             // 3.0
Math.toInt(Math.ceil(float64(total) / float64(parts))) // 3
int(Math.trunc(-2.7))                                  // -2

Results outside a function's domain are NaN or an infinity:

ant
Math.sqrt(-1.0)               // NaN
Math.log(0.0)                 // -Infinity
Math.exp(1000.0)              // Infinity
Math.maxFloat64 * 2.0         // Infinity
Math.Infinity - Math.Infinity // NaN

Math.random is uniform in [0, 1) and cannot be seeded:

ant
let r = Math.random()
r >= 0.0 && r < 1.0 // true

Calls chain through a pipe:

ant
16.0 | Math.sqrt() | Math.log2() // 2.0

Functions by job ​

Sign, size, and bounds ​

Rounding ​

  • Math.floor: the greatest whole number not above x
  • Math.ceil: the least whole number not below x
  • Math.round: the nearest whole number, halves away from zero
  • Math.trunc: x with its fraction dropped, toward zero
  • Math.toInt: a whole float64 as an int, or null

Powers, roots, and logarithms ​

Trigonometry ​

Testing floats ​

Random numbers ​

  • Math.random: a uniformly random float64 in [0, 1)
  • Math.randomInt: a uniformly random int from from, inclusive, to to, exclusive

Constants ​

ConstantTypeValue
Math.PIfloat64π, 3.141592653589793
Math.Efloat64e, 2.718281828459045
Math.NaNfloat64not-a-number
Math.Infinityfloat64positive infinity; -Math.Infinity is negative
Math.maxFloat64float64the largest finite float64
Math.minIntint-2^63, which no literal writes
Math.maxIntint2^63 - 1
Math.maxUint64uint642^64 - 1, which no literal writes

Type bounds ​

  • Number: any integer or floating-point type
  • Integer: any integer type
  • Ordered: any number type or string, the types < compares

Applications ​

Convert degrees to radians before a trigonometric call:

ant
func radians(degrees: float64): float64 {
  degrees * Math.PI / 180.0
}
Math.sin(radians(90.0)) // 1.0

Measure the distance between two points:

ant
type Point = { x: float64, y: float64 }
func distance(a: Point, b: Point): float64 {
  Math.hypot(b.x - a.x, b.y - a.y)
}
distance(Point{ x: 1.0, y: 1.0 }, Point{ x: 4.0, y: 5.0 }) // 5.0

Round to two decimal places:

ant
let x = 3.14159
Math.round(x * 100.0) / 100.0 // 3.14

Grow a deposit by compound interest:

ant
let deposit = 1000.0
let rate = 0.05
deposit * Math.pow(1.0 + rate, 10.0) // 1628.8946267774415

Keep a percentage within range:

ant
let percents = [-12, 40, 130]
percents.map({ Math.clamp(x, 0, 100) }) // [0, 40, 100]

Properties and relations ​

Math.floor and Math.ceil bracket x, and Math.trunc is one of them by sign:

ant
for x in [2.5, -2.5, 3.0] {
  assert(Math.floor(x) <= x && x <= Math.ceil(x))
  assert(Math.trunc(x) == (if x < 0.0 { Math.ceil(x) } else { Math.floor(x) }))
}

Math.clamp is Math.max then Math.min:

ant
for x in [-5, 3, 99] {
  assert(Math.clamp(x, 0, 10) == Math.min(Math.max(x, 0), 10))
}

Outside its domain a function gives NaN or an infinity, never a panic:

ant
assert(Math.isNaN(Math.sqrt(-1.0)))
assert(Math.log(0.0) == -Math.Infinity)

Possible issues ​

An int literal is not a float64. Write 2.0, or convert a variable with float64:

ant
let n = 2
Math.sqrt(n) // error: cannot use int where float64 is expected

The two arguments of Math.max must share one type:

ant
Math.max(2, 3.5) // error: cannot use float64 where int is expected

NaN is not equal to itself, so == never finds it:

ant
let x = Math.sqrt(-1.0)
x == Math.NaN // false
Math.isNaN(x) // true

Math.minInt has no absolute value in an int:

ant
Math.abs(Math.minInt) // panic: Math.abs: integer overflow

The integer constants are limits, so arithmetic past them panics:

ant
Math.maxInt + 1 // panic: integer overflow

Math.clamp with the bounds swapped panics:

ant
Math.clamp(5, 10, 0) // panic: Math.clamp needs lo <= hi, got 10 and 0

See also ​