aiken/math

This module contains some basic Math utilities. Standard arithmetic operations on integers are available through native operators:

OperatorDescription
+Arithmetic sum
-Arithmetic difference
/Whole division
*Arithmetic multiplication
%Remainder by whole division

Here are a few examples:

1 + 1   // 2
10 - 2  // 8
40 / 14 // 2
3 * 4   // 12
10 % 3  // 1

Functions

abs(self: Int) -> Int

Calculate the absolute value of an integer.

math.abs(-42) == 42
math.abs(14) == 14

clamp(self: Int, min: Int, max: Int) -> Int

Restrict the value of an integer between two min and max bounds

math.clamp(14, min: 0, max: 10) == 10

gcd(x: Int, y: Int) -> Int

The greatest common divisor of two integers.

math.gcd(42, 14) == 14
math.gcd(14, 42) == 14
math.gcd(0, 0) == 0

log(self: Int, base: Int) -> Int

The logarithm in base b of an element using integer divisions.

math.log(10, base: 2) == 3
math.log(42, base: 2) == 5
math.log(42, base: 3) == 3
math.log(5, base: 0) == 0
math.log(4, base: 4) == 1
math.log(4, base: 42) == 0

max(a: Int, b: Int) -> Int

Return the maximum of two integers.

min(a: Int, b: Int) -> Int

Return the minimum of two integers.

pow(self: Int, e: Int) -> Int

Calculates a number to the power of e using the exponentiation by squaring method.

math.pow(3, 5) == 243
math.pow(7, 2) == 49
math.pow(3, -4) == 0
math.pow(0, 0) == 1
math.pow(513, 3) == 135005697

pow2(e: Int) -> Int

Calculates the power of 2 for a given exponent e. Much cheaper than using pow(2, _) for small exponents (0 < e < 256).

math.pow2(-2) == 0
math.pow2(0) == 1
math.pow2(1) == 2
math.pow2(4) == 16
math.pow2(42) == 4398046511104

sqrt(self: Int) -> Option<Int>

Calculates the square root of an integer using the Babylonian method. This returns either the exact result or the smallest integer nearest to the square root.

Returns None for negative values.

math.sqrt(0) == Some(0)
math.sqrt(25) == Some(5)
math.sqrt(44203) == Some(210)
math.sqrt(-42) == None
Search Document