std.math
API summary (generated from the Beans source by npm run coverage): 25 package functions.
std.math is the numeric helper package, written in Beans rather than bound to
a C library. Every float function has an f32 twin with a 32 suffix that
works in single precision throughout rather than rounding a double at the end.
Read the source at
stdlib/std/math/math.b.
import std.mathpub fn clamp(value: int, low: int, high: int) -> intpub fn gcd(a: int, b: int) -> intclampforcesvalueinto the inclusive range[low, high]. It returnslowifvalueis belowlow,highif it is abovehigh, andvalueotherwise.gcdis the greatest common divisor, using Euclid’s algorithm on the absolute values ofaandb, so the signs of the inputs do not matter.gcd(0, 0)is0.
import std.ioimport std.math
fn main() { io.println(math.clamp(15, 0, 10)) // 10 io.println(math.clamp(-3, 0, 10)) // 0 io.println(math.gcd(12, 18)) // 6 io.println(math.gcd(0, 0)) // 0}Float helpers
Section titled “Float helpers”pub fn sqrt(value: float) -> floatpub fn hypot(x: float, y: float) -> floatpub fn exp(power: float) -> floatpub fn sin(radians: float) -> floatpub fn cos(radians: float) -> floatpub fn fmin(left: float, right: float) -> floatpub fn fmax(left: float, right: float) -> floatpub fn fclamp(value: float, low: float, high: float) -> floatpub fn rem_euclid(value: float, divisor: float) -> floatpub fn infinity() -> floatpub fn is_finite(value: float) -> boolpub fn angle_limit() -> floatsqrtis the hardware instruction, so it is correctly rounded. The wrapper exists so you need nounsafeblock.hypotis the length of the vector(x, y)and does not overflow on the squares.expraisesetopower.sinandcostake radians.fmin,fmaxandfclampare the float versions of the integer helpers. A NaN is not ordered against anything, so a NaN argument tofmin/fmaxanswers whichever side the comparison falls through to — test withis_nanfirst when that matters.rem_euclidis the Euclidean remainder and is never negative.%is the truncated remainder and keeps the sign of the dividend, so wrapping an angle or a hue wants this one.infinityis positive infinity — negate it for the other one.is_finiteis false for both infinities and for NaN.angle_limitis the largest|radians|thatsinandcoscan reduce accurately (1.0e15). Both return NaN when the absolute input exceeds this limit.
import std.ioimport std.math
fn main() { io.println(math.sqrt(2.0)) // 1.4142135623730951 io.println(math.hypot(3.0, 4.0)) // 5 io.println(math.fclamp(1.5, 0.0, 1.0)) // 1 io.println(math.rem_euclid(-1.0, 3.0)) // 2 io.println(math.is_finite(math.infinity())) // false}f32 helpers
Section titled “f32 helpers”pub fn sqrt32(value: f32) -> f32pub fn hypot32(x: f32, y: f32) -> f32pub fn exp32(power: f32) -> f32pub fn sin32(radians: f32) -> f32pub fn cos32(radians: f32) -> f32pub fn fmin32(left: f32, right: f32) -> f32pub fn fmax32(left: f32, right: f32) -> f32pub fn fclamp32(value: f32, low: f32, high: f32) -> f32pub fn rem_euclid32(value: f32, divisor: f32) -> f32pub fn infinity32() -> f32pub fn is_finite32(value: f32) -> boolEach is its float counterpart done in single precision from end to end, not a
float result rounded down at the last step. Use them when the values around
them are already f32 — there are no implicit numeric conversions, so mixing
the two widths needs an explicit as either way.
See also
Section titled “See also”- Numbers and decimal, the integer and float types and their own methods, including how floats order and compare.