Skip to content

std.math

std.math is the libm scalar functions over float64, added in 1.4.0. It binds 22 of libm’s functions straight across the foreign boundary, with no C shim of its own: every dusk binary already links -lm, so the symbols are there for the taking. Two constants, pi and e, come back as plain Dusk literals, and two predicates, is_nan and is_inf, are pure Dusk over IEEE 754 algebra with no foreign call. The module lives at lib/std/math.dusk.

@import std.math

Imported names are flat: after @import std.math you call sin, sqrt, pi, and the rest with no prefix. See stdlib overview for how imports work in general.

Every function here takes and returns float64. There is no float32 overload and no integer variant; widen an integer to float64 before you call.

FunctionDescription
sin(x: float64) -> float64Sine of x, in radians.
cos(x: float64) -> float64Cosine of x, in radians.
tan(x: float64) -> float64Tangent of x, in radians.
asin(x: float64) -> float64Arc sine, result in radians.
acos(x: float64) -> float64Arc cosine, result in radians.
atan(x: float64) -> float64Arc tangent, result in radians.
atan2(y: float64, x: float64) -> float64Arc tangent of y / x, using the signs of both to place the quadrant.
FunctionDescription
exp(x: float64) -> float64e raised to x.
log(x: float64) -> float64Natural logarithm, base e.
log2(x: float64) -> float64Base 2 logarithm.
log10(x: float64) -> float64Base 10 logarithm.
FunctionDescription
sqrt(x: float64) -> float64Square root.
cbrt(x: float64) -> float64Cube root.
FunctionDescription
floor(x: float64) -> float64Largest integer value not greater than x.
ceil(x: float64) -> float64Smallest integer value not less than x.
round(x: float64) -> float64Nearest integer, ties rounded away from zero.
trunc(x: float64) -> float64x with its fractional part dropped, toward zero.
FunctionDescription
fmod(x: float64, y: float64) -> float64Floating-point remainder of x / y.
fabs(x: float64) -> float64Absolute value of x.
hypot(x: float64, y: float64) -> float64sqrt(x*x + y*y), computed without intermediate overflow.
fmin(a: float64, b: float64) -> float64The smaller of a and b.
fmax(a: float64, b: float64) -> float64The larger of a and b.
FunctionDescription
pi() -> float64Archimedes’ constant, to float64 precision.
e() -> float64Euler’s number, to float64 precision.

libm keeps pi and e as C macros rather than linkable symbols, so there is nothing to bind for them across the boundary. Each returns the value as a plain Dusk float64 literal instead. Call them like any other function: pi() and e().

FunctionDescription
is_nan(x: float64) -> boolTrue when x is NaN.
is_inf(x: float64) -> boolTrue when x is positive or negative infinity.

Both predicates are pure Dusk over IEEE 754’s own algebra, no foreign call. NaN is the only float64 value that compares unequal to itself, so is_nan(x) is !(x == x). A finite value only satisfies x + x == x at 0.0, so is_inf(x) is x == x && x + x == x && x != 0.0: the first clause rules out NaN, the last excludes zero, and what remains is exactly the two infinities.

A raw float64’s text format is not pinned across platforms, so a sample checks a result with a bool comparison against a known value rather than printing the float. sqrt(16.0), floor(3.7), and the rest are exact here, so == is safe.

math_check.dusk
@paradigm procedural
@import std.math
func main() -> int32 {
// A raw float64's text form is not pinned across platforms, so we test
// against a known value with a bool comparison instead of printing it.
if sqrt(16.0) == 4.0 {
println("sqrt ok") // sqrt ok
}
if floor(3.7) == 3.0 && ceil(3.2) == 4.0 {
println("round ok") // round ok
}
if fabs(-2.5) == 2.5 {
println("abs ok") // abs ok
}
// is_nan and is_inf are pure Dusk over IEEE 754, no foreign call.
if is_nan(sqrt(-1.0)) {
println("nan caught") // nan caught
}
if is_inf(1.0 / 0.0) {
println("inf caught") // inf caught
}
// pi and e come back as plain Dusk literals.
if pi() > 3.14 && e() > 2.71 {
println("constants ok") // constants ok
}
return 0
}
  • std.rand: the xoshiro256** generator, the other numeric utility added in 1.4.0.
  • Types: the float64 type every function here takes and returns.
  • stdlib overview: the full module list and how imports resolve.