Skip to content
volt
GitHub

std::math

Constants, the C math library (f64 and f32), and integer arithmetic that reports or clamps overflow. With use std::math; the names read std::sqrt(x), std::PI.

Source: std/math.volt

Functions

std::math::sqrt

fn sqrt(x: f64) -> f64

the square root

std::math::cbrt

fn cbrt(x: f64) -> f64

the cube root

std::math::pow

fn pow(x: f64, y: f64) -> f64

x to the power y

std::math::exp

fn exp(x: f64) -> f64

e to the power x

std::math::exp2

fn exp2(x: f64) -> f64

2 to the power x

std::math::log

fn log(x: f64) -> f64

the natural logarithm (base e)

std::math::log2

fn log2(x: f64) -> f64

the base-2 logarithm

std::math::log10

fn log10(x: f64) -> f64

the base-10 logarithm

std::math::sin

fn sin(x: f64) -> f64

the sine of x radians

std::math::cos

fn cos(x: f64) -> f64

the cosine of x radians

std::math::tan

fn tan(x: f64) -> f64

the tangent of x radians

std::math::asin

fn asin(x: f64) -> f64

the arcsine, in radians

std::math::acos

fn acos(x: f64) -> f64

the arccosine, in radians

std::math::atan

fn atan(x: f64) -> f64

the arctangent, in radians

std::math::atan2

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

the angle of the point (x, y) from the x axis, in radians (-pi to pi)

std::math::sinh

fn sinh(x: f64) -> f64

the hyperbolic sine

std::math::cosh

fn cosh(x: f64) -> f64

the hyperbolic cosine

std::math::tanh

fn tanh(x: f64) -> f64

the hyperbolic tangent

std::math::hypot

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

the length of the hypotenuse, sqrt(x*x + y*y) without overflowing

std::math::floor

fn floor(x: f64) -> f64

the largest whole number not above x

std::math::ceil

fn ceil(x: f64) -> f64

the smallest whole number not below x

std::math::round

fn round(x: f64) -> f64

the nearest whole number, halves away from zero

std::math::trunc

fn trunc(x: f64) -> f64

x without its fraction (toward zero)

std::math::fmod

fn fmod(x: f64, y: f64) -> f64

the remainder of x / y, with x's sign

std::math::sqrt

fn sqrt(x: f32) -> f32

the square root

std::math::cbrt

fn cbrt(x: f32) -> f32

the cube root

std::math::pow

fn pow(x: f32, y: f32) -> f32

x to the power y

std::math::exp

fn exp(x: f32) -> f32

e to the power x

std::math::exp2

fn exp2(x: f32) -> f32

2 to the power x

std::math::log

fn log(x: f32) -> f32

the natural logarithm (base e)

std::math::log2

fn log2(x: f32) -> f32

the base-2 logarithm

std::math::log10

fn log10(x: f32) -> f32

the base-10 logarithm

std::math::sin

fn sin(x: f32) -> f32

the sine of x radians

std::math::cos

fn cos(x: f32) -> f32

the cosine of x radians

std::math::tan

fn tan(x: f32) -> f32

the tangent of x radians

std::math::asin

fn asin(x: f32) -> f32

the arcsine, in radians

std::math::acos

fn acos(x: f32) -> f32

the arccosine, in radians

std::math::atan

fn atan(x: f32) -> f32

the arctangent, in radians

std::math::atan2

fn atan2(y: f32, x: f32) -> f32

the angle of the point (x, y) from the x axis, in radians (-pi to pi)

std::math::sinh

fn sinh(x: f32) -> f32

the hyperbolic sine

std::math::cosh

fn cosh(x: f32) -> f32

the hyperbolic cosine

std::math::tanh

fn tanh(x: f32) -> f32

the hyperbolic tangent

std::math::hypot

fn hypot(x: f32, y: f32) -> f32

the length of the hypotenuse, sqrt(x*x + y*y) without overflowing

std::math::floor

fn floor(x: f32) -> f32

the largest whole number not above x

std::math::ceil

fn ceil(x: f32) -> f32

the smallest whole number not below x

std::math::round

fn round(x: f32) -> f32

the nearest whole number, halves away from zero

std::math::trunc

fn trunc(x: f32) -> f32

x without its fraction (toward zero)

std::math::fmod

fn fmod(x: f32, y: f32) -> f32

the remainder of x / y, with x's sign

std::math::is_nan

<T: type> fn is_nan(x: T) -> bool

whether x is NaN (the only value unequal to itself)

std::math::is_finite

<T: type> fn is_finite(x: T) -> bool

whether x is a number that isn't infinite or NaN (true for every integer)

std::math::is_inf

<T: type> fn is_inf(x: T) -> bool

whether x is positive or negative infinity

std::math::abs

<T: type> fn abs(x: T) -> T

x without its sign (for the most negative integer, that overflows)

std::math::abs

fn abs(x: f64) -> f64

x without its sign (-0.0 gives 0.0)

std::math::abs

fn abs(x: f32) -> f32

x without its sign (-0.0 gives 0.0)

std::math::min

<T: type> fn min(a: T, b: T) -> T

the smaller of a and b by < (a when neither is smaller)

std::math::max

<T: type> fn max(a: T, b: T) -> T

the larger of a and b by < (a when neither is larger)

std::math::clamp

<T: type> fn clamp(x: T, lo: T, hi: T) -> T

x, moved into lo..=hi

std::math::checked_add

<T: type> fn checked_add(a: T, b: T) -> T?

a + b, or null when it doesn't fit in T

std::math::checked_sub

<T: type> fn checked_sub(a: T, b: T) -> T?

a - b, or null when it doesn't fit in T

std::math::checked_mul

<T: type> fn checked_mul(a: T, b: T) -> T?

a * b, or null when it doesn't fit in T

std::math::checked_div

<T: type> fn checked_div(a: T, b: T) -> T?

a / b, or null when b is 0 or the quotient doesn't fit (MIN / -1)

std::math::saturating_add

<T: type> fn saturating_add(a: T, b: T) -> T

a + b, or T's largest or smallest value when it doesn't fit

std::math::saturating_sub

<T: type> fn saturating_sub(a: T, b: T) -> T

a - b, or T's largest or smallest value when it doesn't fit

std::math::saturating_mul

<T: type> fn saturating_mul(a: T, b: T) -> T

a * b, or T's largest or smallest value when it doesn't fit

std::math::gcd

<T: type> fn gcd(a: T, b: T) -> T

the greatest common divisor of a and b, never negative (gcd(0, 0) is 0). Like abs, it overflows only when the answer doesn't fit: gcd(MIN, 0) and gcd(MIN, MIN) of a signed type

std::math::lcm

<T: type> fn lcm(a: T, b: T) -> T

the least common multiple of a and b, never negative (0 when either is 0)

Methods on other types

Attached to built-in types, or to every type (T).

<T: type> attach fn max_value(static this: T) -> T

the largest value of integer type T: i32::max_value()

<T: type> attach fn min_value(static this: T) -> T

the smallest value of integer type T: i32::min_value()

Globals

val PI: f64 = 3.141592653589793
val TAU: f64 = 6.283185307179586
val E: f64 = 2.718281828459045
val SQRT2: f64 = 1.4142135623730951
val LN2: f64 = 0.6931471805599453
val LN10: f64 = 2.302585092994046
val EPSILON: f64 = 2.220446049250313e-16
val INF: f64 = over_zero(1.0)
val NAN: f64 = over_zero(0.0)