Module std::math

Mathematical operations.

Generic numeric functions (absolute value, min, max), integer helpers (clamp, sign), floating-point functions (sqrt, pow, floor, ceil, round), and transcendentals (exp, log, sin, cos) plus common constants.

Generic numeric and floating-point functions are lowered to LLVM math ops by the codegen backend. Integer-only helpers and constants are implemented directly in Hew. The Rust crate in src/lib.rs remains only as a workspace build placeholder.

Examples

import std.math;

fn main() {
    println(math.abs(-5));         // 5
    println(math.clamp(10, 0, 5)); // 5
    println(math.sqrt(4.0));       // 2
    println(math.pi());            // 3.14159
}

Contents

Functions

Function pi

pub fn pi() -> f64

The ratio of a circle's circumference to its diameter.

Function e

pub fn e() -> f64

Euler's number, the base of natural logarithms.

Function abs

pub fn abs(x: T) -> T

Return the absolute value of a numeric value.

Function min

pub fn min(a: T, b: T) -> T

Return the smaller of two numeric values.

Function max

pub fn max(a: T, b: T) -> T

Return the larger of two numeric values.

Function clamp

pub fn clamp(x: i64, lo: i64, hi: i64) -> i64

Clamp x to the range [lo, hi].

Function sign

pub fn sign(x: i64) -> i64

Return the sign of an integer: -1, 0, or 1.

Function sqrt

pub fn sqrt(x: f64) -> f64

Return the square root of a float.

Function pow

pub fn pow(base: f64, exp: f64) -> f64

Raise base to the power exp.

Function floor

pub fn floor(x: f64) -> f64

Return the largest integer value less than or equal to x.

Function ceil

pub fn ceil(x: f64) -> f64

Return the smallest integer value greater than or equal to x.

Function round

pub fn round(x: f64) -> f64

Round to the nearest integer, with ties rounding away from zero.

Function exp

pub fn exp(x: f64) -> f64

Return e raised to the power x.

Function log

pub fn log(x: f64) -> f64

Return the natural logarithm of x.

Function sin

pub fn sin(x: f64) -> f64

Return the sine of x (radians).

Function cos

pub fn cos(x: f64) -> f64

Return the cosine of x (radians).

Function tan

pub fn tan(x: f64) -> f64

Return the tangent of x (radians).

Function asin

pub fn asin(x: f64) -> f64

Return the arcsine of x, in radians, in the range [-pi/2, pi/2].

Function acos

pub fn acos(x: f64) -> f64

Return the arccosine of x, in radians, in the range [0, pi].

Function atan

pub fn atan(x: f64) -> f64

Return the arctangent of x, in radians, in the range [-pi/2, pi/2].

Function atan2

pub fn atan2(y: f64, x: f64) -> f64

Return the four-quadrant arctangent of y / x, in radians, using the signs of both arguments to select the correct quadrant.

Function sinh

pub fn sinh(x: f64) -> f64

Return the hyperbolic sine of x.

Function cosh

pub fn cosh(x: f64) -> f64

Return the hyperbolic cosine of x.

Function tanh

pub fn tanh(x: f64) -> f64

Return the hyperbolic tangent of x.

Function exp2

pub fn exp2(x: f64) -> f64

Return 2 raised to the power x.

Function log2

pub fn log2(x: f64) -> f64

Return the base-2 logarithm of x.

Function log10

pub fn log10(x: f64) -> f64

Return the base-10 logarithm of x.

Function log1p

pub fn log1p(x: f64) -> f64

Return the natural logarithm of 1 + x, accurate for x near zero.

Function expm1

pub fn expm1(x: f64) -> f64

Return e^x - 1, accurate for x near zero.

Function cbrt

pub fn cbrt(x: f64) -> f64

Return the cube root of x.

Function hypot

pub fn hypot(x: f64, y: f64) -> f64

Return sqrt(x*x + y*y), computed without spurious overflow or underflow.

Function fma

pub fn fma(a: f64, b: f64, c: f64) -> f64

Return a * b + c, computed as one correctly-rounded ternary operation (fused multiply-add).

Function trunc

pub fn trunc(x: f64) -> f64

Return x truncated toward zero to the nearest integer value.

Function copysign

pub fn copysign(x: f64, y: f64) -> f64

Return a value with the magnitude of x and the sign of y.

Function powi

pub fn powi(x: f64, n: i32) -> f64

Return x raised to the integer power n.

n is i32: LLVM's powi intrinsic is parameterized on a fixed integer width and does not generalize across widths the way pow's float exponent does.

Function from_bits

pub fn from_bits(bits: u64) -> f64

Reinterpret the bits of bits as an f64 (the inverse of x.to_bits()).

Spelled as a module function rather than the static call f64.from_bits: the checker does not admit a bare builtin type name as a call receiver today.