Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

math

std › math

Integer math, and fixed-point math on Decimal and Fraction, all evaluated at compile time.

from std.math import *

macro show(value: int) {
    @emit value
}

macro demo() {
    show gcd(12, 18)
    show div_floor(-7, 2)
    show sqrt(2, 4).value
    show sin(PI(10), 6).value
}

demo
6 -4 14142 0

Functions that give a Decimal take a precision: the number of decimal places to keep, DEFAULT_PRECISION unless given. Results are truncated to that many places, so sqrt(2, 4) is 1.4142. Angles are in radians.

Re-exports std.decimal.

Macros

abs

SyntaxParametersResultDescription
abs(x)x: intreturns intThe absolute value of x.
abs(x)x: Decimalreturns DecimalThe absolute value of x.
abs(x)x: Fractionreturns FractionThe absolute value of x.

min

The smaller of a and b.

SyntaxParametersResultDescription
min(a, b)a: int, b: intreturns int

max

The larger of a and b.

SyntaxParametersResultDescription
max(a, b)a: int, b: intreturns int

sign

SyntaxParametersResultDescription
sign(x)x: intreturns int-1, 0 or 1, as x is negative, zero or positive.
sign(x)x: Decimalreturns int-1, 0 or 1, as x is negative, zero or positive.
sign(x)x: Fractionreturns int-1, 0 or 1, as x is negative, zero or positive.

copysign

abs(x) with the sign of y: abs(x) * sign(y), so 0 when y is 0.

SyntaxParametersResultDescription
copysign(x, y)x: int, y: intreturns int

clamp

x, moved into lo..=hi if it’s outside. lo can’t be more than hi.

SyntaxParametersResultDescription
clamp(x, lo, hi)x: int, lo: int, hi: intreturns int

pow

SyntaxParametersResultDescription
pow(base, exponent)base: int, exponent: intreturns intbase to the power exponent, which can’t be negative.
pow(base, exponent, precision)base: Decimal, exponent: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimalbase to the power exponent. base must be positive.
pow(base, exponent, precision)base: Decimal, exponent: int, precision: int = DEFAULT_PRECISIONreturns Decimalbase to an integer power. A negative exponent needs a nonzero base.

gcd

The greatest common divisor of a and b, never negative. gcd(0, 0) is 0.

SyntaxParametersResultDescription
gcd(a, b)a: int, b: intreturns int

lcm

The least common multiple of a and b, never negative.

SyntaxParametersResultDescription
lcm(a, b)a: int, b: intreturns int

div_floor

a / b rounded down: div_floor(-7, 2) is -4, where / gives -3.

SyntaxParametersResultDescription
div_floor(a, b)a: int, b: intreturns int

div_ceil

a / b rounded up: div_ceil(7, 2) is 4.

SyntaxParametersResultDescription
div_ceil(a, b)a: int, b: intreturns int

rem_euclid

The remainder of a / b that’s never negative: rem_euclid(-7, 3) is 2, where % gives -1.

SyntaxParametersResultDescription
rem_euclid(a, b)a: int, b: intreturns int

div_euclid

The quotient that goes with rem_euclid, so div_euclid(a, b) * b + rem_euclid(a, b) == a.

SyntaxParametersResultDescription
div_euclid(a, b)a: int, b: intreturns int

pow_mod

base^exponent % modulus, without computing base^exponent in full. exponent can’t be negative, and modulus must be positive.

SyntaxParametersResultDescription
pow_mod(base, exponent, modulus)base: int, exponent: int, modulus: intreturns int

isqrt

The square root of x, rounded down. x can’t be negative.

SyntaxParametersResultDescription
isqrt(x)x: intreturns int

popcount

How many bits of x are 1. x can’t be negative.

SyntaxParametersResultDescription
popcount(x)x: intreturns int

ctz

How many 0 bits come below x’s lowest 1 bit. ctz(0) is 0.

SyntaxParametersResultDescription
ctz(x)x: intreturns int

clz

How many 0 bits come above x’s highest 1 bit, in a width-bit value. x must fit in width bits.

SyntaxParametersResultDescription
clz(x, width)x: int, width: intreturns int

rotate_left

x rotated left by amount bits within a width-bit value: bits shifted out at the top come back in at the bottom.

SyntaxParametersResultDescription
rotate_left(x, amount, width)x: int, amount: int, width: intreturns int

rotate_right

x rotated right by amount bits within a width-bit value: bits shifted out at the bottom come back in at the top.

SyntaxParametersResultDescription
rotate_right(x, amount, width)x: int, amount: int, width: intreturns int

is_pow_of_two

1 if x is a power of two, else 0. 0 isn’t one.

SyntaxParametersResultDescription
is_pow_of_two(x)x: intreturns int

next_pow_of_two

The smallest power of two that’s at least x: next_pow_of_two(5) is 8, and next_pow_of_two(0) is 1.

SyntaxParametersResultDescription
next_pow_of_two(x)x: intreturns int

floor

SyntaxParametersResultDescription
floor(x)x: intreturns intThe largest integer not above x.
floor(x)x: Decimalreturns int
floor(x)x: Fractionreturns int

ceil

SyntaxParametersResultDescription
ceil(x)x: intreturns intThe smallest integer not below x.
ceil(x)x: Decimalreturns int
ceil(x)x: Fractionreturns int

trunc

SyntaxParametersResultDescription
trunc(x)x: intreturns intx with its fractional part dropped, rounding toward zero.
trunc(x)x: Decimalreturns int
trunc(x)x: Fractionreturns int

round

SyntaxParametersResultDescription
round(x)x: intreturns intThe nearest integer to x. Halves round away from zero: 2.5 becomes 3, and -2.5 becomes -3.
round(x)x: Decimalreturns int
round(x)x: Fractionreturns int

fract

SyntaxParametersResultDescription
fract(x)x: Decimalreturns Decimalx - floor(x), always between 0 and 1: fract(-2.5) is 0.5.
fract(x)x: Fractionreturns Fraction

PI

π to precision decimal places, at most 60.

SyntaxParametersResultDescription
PI(precision)precision: int = DEFAULT_PRECISIONreturns Decimal

TAU

τ = 2π to precision decimal places, at most 60.

SyntaxParametersResultDescription
TAU(precision)precision: int = DEFAULT_PRECISIONreturns Decimal

E

e to precision decimal places, at most 60.

SyntaxParametersResultDescription
E(precision)precision: int = DEFAULT_PRECISIONreturns Decimal

SQRT2

√2 to precision decimal places, at most 60.

SyntaxParametersResultDescription
SQRT2(precision)precision: int = DEFAULT_PRECISIONreturns Decimal

LN2

ln 2 to precision decimal places, at most 60.

SyntaxParametersResultDescription
LN2(precision)precision: int = DEFAULT_PRECISIONreturns Decimal

LN10

ln 10 to precision decimal places, at most 60.

SyntaxParametersResultDescription
LN10(precision)precision: int = DEFAULT_PRECISIONreturns Decimal

root

SyntaxParametersResultDescription
root(x, degree, precision)x: Decimal, degree: int, precision: int = DEFAULT_PRECISIONreturns DecimalThe degreeth root of x. degree must be positive, and a negative x needs an odd degree: root(-8, 3) is -2.
root(x, degree, precision)x: int, degree: int, precision: int = DEFAULT_PRECISIONreturns Decimal
root(x, degree, precision)x: Fraction, degree: int, precision: int = DEFAULT_PRECISIONreturns Decimal

sqrt

SyntaxParametersResultDescription
sqrt(x, precision)x: Decimal, precision: int = DEFAULT_PRECISIONreturns DecimalThe square root of x, which can’t be negative.
sqrt(x, precision)x: int, precision: int = DEFAULT_PRECISIONreturns Decimal

hypot

SyntaxParametersResultDescription
hypot(x, y, precision)x: Decimal, y: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimalsqrt(x^2 + y^2), the length of the hypotenuse.
hypot(x, y, precision)x: int, y: int, precision: int = DEFAULT_PRECISIONreturns Decimal

exp

e to the power x.

SyntaxParametersResultDescription
exp(x, precision)x: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimal

exp2

2 to the power x.

SyntaxParametersResultDescription
exp2(x, precision)x: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimal

expm1

exp(x) - 1.

SyntaxParametersResultDescription
expm1(x, precision)x: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimal

ln

The natural logarithm of x, which must be positive.

SyntaxParametersResultDescription
ln(x, precision)x: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimal

log2

The base-2 logarithm of x, which must be positive.

SyntaxParametersResultDescription
log2(x, precision)x: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimal

log10

The base-10 logarithm of x, which must be positive.

SyntaxParametersResultDescription
log10(x, precision)x: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimal

log1p

ln(1 + x).

SyntaxParametersResultDescription
log1p(x, precision)x: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimal

sin

The sine of x radians.

SyntaxParametersResultDescription
sin(x, precision)x: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimal

cos

The cosine of x radians.

SyntaxParametersResultDescription
cos(x, precision)x: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimal

tan

The tangent of x radians. It’s an error where the cosine is 0.

SyntaxParametersResultDescription
tan(x, precision)x: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimal

atan

The arctangent of x, in radians from -π/2 to π/2.

SyntaxParametersResultDescription
atan(x, precision)x: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimal

atan2

The angle of the point (x, y) from the positive x axis, in radians from -π to π, using both signs to pick the quadrant. atan2(0, 0) is an error.

SyntaxParametersResultDescription
atan2(y, x, precision)y: Decimal, x: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimal

asin

The arcsine of x, in radians. x must be between -1 and 1.

SyntaxParametersResultDescription
asin(x, precision)x: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimal

acos

The arccosine of x, in radians. x must be between -1 and 1.

SyntaxParametersResultDescription
acos(x, precision)x: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimal

sinh

The hyperbolic sine of x.

SyntaxParametersResultDescription
sinh(x, precision)x: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimal

cosh

The hyperbolic cosine of x.

SyntaxParametersResultDescription
cosh(x, precision)x: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimal

tanh

The hyperbolic tangent of x.

SyntaxParametersResultDescription
tanh(x, precision)x: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimal

asinh

The inverse hyperbolic sine of x.

SyntaxParametersResultDescription
asinh(x, precision)x: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimal

acosh

The inverse hyperbolic cosine of x, which must be at least 1.

SyntaxParametersResultDescription
acosh(x, precision)x: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimal

atanh

The inverse hyperbolic tangent of x, which must be between -1 and 1, exclusive.

SyntaxParametersResultDescription
atanh(x, precision)x: Decimal, precision: int = DEFAULT_PRECISIONreturns Decimal

simplify

x in lowest terms, with a positive denominator: 6/-4 becomes -3/2.

SyntaxParametersResultDescription
simplify(x)x: Fractionreturns Fraction

Constants

ConstantTypeValueDescription
DEFAULT_PRECISIONint16The number of decimal places a Decimal result keeps when no precision is given.
GUARD_DIGITSint4Extra decimal places used inside a calculation, and dropped from its result.
CONSTANT_DIGITSint60The most decimal places PI, E and the other constants can give.

Re-exported

From std.decimal: Decimal, Fraction.