std.math
API summary (generated from the Beans source by npm run coverage): 2 package functions.
std.math is a small package of integer helpers written in Beans. 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}See also
Section titled “See also”- Numbers and decimal, the integer and float types and their own methods.