Skip to content
volt
GitHub

std::math_portable

:portable: std::math's functions in Volt, the same bits on every machine and no C math library needed. Programs with no OS (voltc --target) get them as std::math's; elsewhere std::math is the system's libm, and these are here by name. sqrt, floor, ceil, round, trunc and fmod are exact; the rest are within an ulp of the true value. Each f32 function computes in f64 and rounds once. Ported from musl's src/math (MIT license, Copyright 2005-2020 Rich Felker, et al.: https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT), partly by way of Zig's ports of it. Most of it is FreeBSD's msun and Sun's fdlibm, under this notice: Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. Developed at SunPro, a Sun Microsystems, Inc. business. Permission to use, copy, modify, and distribute this software is freely granted, provided that this notice is preserved. exp and pow, and their tables, are Arm's optimized routines (Copyright (c) 2018, Arm Limited, MIT license). sqrt is a digit-by-digit square root written for Volt.

Source: std/math_portable.volt

Functions

std::math::portable::trunc

fn trunc(x: f64) -> f64

x without its fraction (toward zero)

std::math::portable::floor

fn floor(x: f64) -> f64

the largest whole number not above x

std::math::portable::ceil

fn ceil(x: f64) -> f64

the smallest whole number not below x

std::math::portable::round

fn round(x: f64) -> f64

the nearest whole number, halves away from zero

std::math::portable::fmod

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

the remainder of x / y, with x's sign: exact (musl's fmod)

std::math::portable::sqrt

fn sqrt(x: f64) -> f64

the square root, correctly rounded: digit by digit on the significand, two bits a step

std::math::portable::cbrt

fn cbrt(x: f64) -> f64

the cube root (musl's cbrt)

std::math::portable::hypot

fn hypot(x0: f64, y0: f64) -> f64

the length of the hypotenuse, sqrt(x*x + y*y) without overflowing (musl's hypot: the squares split into exact halves)

std::math::portable::exp

fn exp(x: f64) -> f64

e^x

std::math::portable::exp2

fn exp2(x: f64) -> f64

2^x (musl's older exp2: a 256-entry table and a polynomial)

std::math::portable::expm1

fn expm1(x0: f64) -> f64

e^x - 1, exact near 0 (musl's expm1)

std::math::portable::log

fn log(x: f64) -> f64

the natural logarithm (fdlibm's)

std::math::portable::log2

fn log2(x: f64) -> f64

the base-2 logarithm (fdlibm's)

std::math::portable::log10

fn log10(x: f64) -> f64

the base-10 logarithm (fdlibm's)

std::math::portable::pow

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

x to the power y (musl's pow, from Arm's optimized routines: within 0.52 ulp)

std::math::portable::sin

fn sin(x: f64) -> f64

the sine, x in radians

std::math::portable::cos

fn cos(x: f64) -> f64

the cosine, x in radians

std::math::portable::tan

fn tan(x: f64) -> f64

the tangent, x in radians

std::math::portable::asin

fn asin(x: f64) -> f64

the angle whose sine is x, in radians (-pi/2 to pi/2)

std::math::portable::acos

fn acos(x: f64) -> f64

the angle whose cosine is x, in radians (0 to pi)

std::math::portable::atan

fn atan(x0: f64) -> f64

the angle whose tangent is x, in radians (-pi/2 to pi/2)

std::math::portable::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::portable::sinh

fn sinh(x: f64) -> f64

the hyperbolic sine

std::math::portable::cosh

fn cosh(x: f64) -> f64

the hyperbolic cosine

std::math::portable::tanh

fn tanh(x: f64) -> f64

the hyperbolic tangent

std::math::portable::sqrt

fn sqrt(x: f32) -> f32

the square root

std::math::portable::cbrt

fn cbrt(x: f32) -> f32

the cube root

std::math::portable::pow

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

x to the power y

std::math::portable::exp

fn exp(x: f32) -> f32

e to the power x

std::math::portable::exp2

fn exp2(x: f32) -> f32

2 to the power x

std::math::portable::expm1

fn expm1(x: f32) -> f32

e to the power x, minus 1

std::math::portable::log

fn log(x: f32) -> f32

the natural logarithm (base e)

std::math::portable::log2

fn log2(x: f32) -> f32

the base-2 logarithm

std::math::portable::log10

fn log10(x: f32) -> f32

the base-10 logarithm

std::math::portable::sin

fn sin(x: f32) -> f32

the sine of x radians

std::math::portable::cos

fn cos(x: f32) -> f32

the cosine of x radians

std::math::portable::tan

fn tan(x: f32) -> f32

the tangent of x radians

std::math::portable::asin

fn asin(x: f32) -> f32

the arcsine, in radians

std::math::portable::acos

fn acos(x: f32) -> f32

the arccosine, in radians

std::math::portable::atan

fn atan(x: f32) -> f32

the arctangent, in radians

std::math::portable::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::portable::sinh

fn sinh(x: f32) -> f32

the hyperbolic sine

std::math::portable::cosh

fn cosh(x: f32) -> f32

the hyperbolic cosine

std::math::portable::tanh

fn tanh(x: f32) -> f32

the hyperbolic tangent

std::math::portable::hypot

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

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

std::math::portable::floor

fn floor(x: f32) -> f32

the largest whole number not above x

std::math::portable::ceil

fn ceil(x: f32) -> f32

the smallest whole number not below x

std::math::portable::round

fn round(x: f32) -> f32

the nearest whole number, halves away from zero

std::math::portable::trunc

fn trunc(x: f32) -> f32

x without its fraction (toward zero)

std::math::portable::fmod

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

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