math
math: common mathematical functions and floating point support
The math module provides implementations of common math functionality and support for dealing with floating point numbers.
Submodules
- checked:
overflow-checked integer arithmetic
- complex:
complex (imaginary) floating-point number support
- random:
pseudorandom number generator
Index
Types
type floatinfo;
Constants
def E: f64 = 2.718281828459045;
def F32_EPS: f32 = 1.1920928955078125e-7;
def F32_EXPONENT_BIAS = 127;
def F32_EXPONENT_BITS = 8;
def F32_EXPONENT_MASK: u32 = (1 << F32_EXPONENT_BITS) - 1;
def F32_MANTISSA_BITS = 23;
def F32_MANTISSA_MASK: u32 = (1 << F32_MANTISSA_BITS) - 1;
def F32_MAX_NORMAL: f32 = 3.4028234e38;
def F32_MIN_NORMAL: f32 = 1.1754944e-38;
def F32_MIN_SUBNORMAL: f32 = 1.0e-45;
def F64_EPS: f64 = 2.2204e-16;
def F64_EXPONENT_BIAS = 1023;
def F64_EXPONENT_BITS = 11;
def F64_EXPONENT_MASK: u64 = (1 << F64_EXPONENT_BITS) - 1;
def F64_MANTISSA_BITS = 52;
def F64_MANTISSA_MASK: u64 = (1 << F64_MANTISSA_BITS) - 1;
def F64_MAX_NORMAL: f64 = 1.7976931348623157e308;
def F64_MIN_NORMAL: f64 = 2.2250738585072014e-308;
def F64_MIN_SUBNORMAL: f64 = 5.0e-324;
def INF = 1.0 / 0.0;
def LN2_HI: f64 = 0.6931471803691238;
def LN2_LO: f64 = 1.9082149292705877e-10;
def LN_10: f64 = 2.302585092994046;
def LN_2: f64 = 0.6931471805599453;
def LOG10_E: f64 = 1f64 / LN_10;
def LOG2_E: f64 = 1f64 / LN_2;
def NAN = 0.0 / 0.0;
def PHI: f64 = 1.618033988749895;
def PI: f64 = 3.141592653589793;
def SQRT_2: f64 = 1.4142135623730951;
def SQRT_E: f64 = 1.6487212707001282;
def SQRT_PHI: f64 = 1.272019649514069;
def SQRT_PI: f64 = 1.772453850905516;
def STANDARD_TOL = 1.0e-14;
def TAU: f64 = 6.283185307179586;
Globals
const f32info: floatinfo;
const f64info: floatinfo;
Functions
fn absf32(n: f32) f32;
fn absf64(n: f64) f64;
fn absi(n: int) uint;
fn absi16(n: i16) u16;
fn absi32(n: i32) u32;
fn absi64(n: i64) u64;
fn absi8(n: i8) u8;
fn acosf64(x: f64) f64;
fn acoshf64(x: f64) f64;
fn asinf64(x: f64) f64;
fn asinhf64(x: f64) f64;
fn atan2f64(y: f64, x: f64) f64;
fn atanf64(x: f64) f64;
fn atanhf64(x: f64) f64;
fn bit_size(x: u64) u8;
fn ceilf64(x: f64) f64;
fn copysignf32(x: f32, y: f32) f32;
fn copysignf64(x: f64, y: f64) f64;
fn cosf64(x: f64) f64;
fn coshf64(x: f64) f64;
fn divu(hi: uint, lo: uint, y: uint) (uint, uint);
fn divu32(hi: u32, lo: u32, y: u32) (u32, u32);
fn divu64(hi: u64, lo: u64, y: u64) (u64, u64);
fn exp2f64(x: f64) f64;
fn expf64(x: f64) f64;
fn expmultif64(hi: f64, lo: f64, k: i64) f64;
fn f32bits(n: f32) u32;
fn f32frombits(n: u32) f32;
fn f64bits(n: f64) u64;
fn f64frombits(n: u64) f64;
fn floorf64(x: f64) f64;
fn frexpf32(n: f32) (f32, i64);
fn frexpf64(n: f64) (f64, i64);
fn gcd(a: u64, b: u64) u64;
fn hypotf64(a: f64, b: f64) f64;
fn isclosef32(x: f32, y: f32, tol: f32 = STANDARD_TOL) bool;
fn isclosef64(x: f64, y: f64, tol: f64 = STANDARD_TOL) bool;
fn isinf(n: f64) bool;
fn isnan(n: f64) bool;
fn isnegativef32(x: f32) bool;
fn isnegativef64(x: f64) bool;
fn isnormalf32(n: f32) bool;
fn isnormalf64(n: f64) bool;
fn ispositivef32(x: f32) bool;
fn ispositivef64(x: f64) bool;
fn issubnormalf32(n: f32) bool;
fn issubnormalf64(n: f64) bool;
fn ldexpf32(mantissa: f32, exp: i64) f32;
fn ldexpf64(mantissa: f64, exp: i64) f64;
fn leading_zeros_u(x: uint) u8;
fn leading_zeros_u16(x: u16) u8;
fn leading_zeros_u32(x: u32) u8;
fn leading_zeros_u64(x: u64) u8;
fn leading_zeros_u8(x: u8) u8;
fn log10f64(x: f64) f64;
fn log1pf64(x: f64) f64;
fn log2f64(x: f64) f64;
fn logf64(x: f64) f64;
fn modf64(x: f64, y: f64) f64;
fn modfracf32(n: f32) (f32, f32);
fn modfracf64(n: f64) (f64, f64);
fn mulu(x: uint, y: uint) (uint, uint);
fn mulu32(x: u32, y: u32) (u32, u32);
fn mulu64(x: u64, y: u64) (u64, u64);
fn nextafterf32(x: f32, y: f32) f32;
fn nextafterf64(x: f64, y: f64) f64;
fn normalizef32(n: f32) (f32, i64);
fn normalizef64(n: f64) (f64, i64);
fn popcount(x: u64) u8;
fn powf64(x: f64, p: f64) f64;
fn powi64(x: i64, n: uint) i64;
fn remu(hi: uint, lo: uint, y: uint) uint;
fn remu32(hi: u32, lo: u32, y: u32) u32;
fn remu64(hi: u64, lo: u64, y: u64) u64;
fn roundf64(x: f64) f64;
fn signf32(x: f32) i64;
fn signf64(x: f64) i64;
fn signi(n: int) i64;
fn signi16(n: i16) i16;
fn signi32(n: i32) i32;
fn signi64(n: i64) i64;
fn signi8(n: i8) i8;
fn sinf64(x: f64) f64;
fn sinhf64(x: f64) f64;
fn sqrtf64(x: f64) f64;
fn tanf64(x: f64) f64;
fn tanhf64(x: f64) f64;
fn trailing_zeros_u(x: uint) u8;
fn trailing_zeros_u16(x: u16) u8;
fn trailing_zeros_u32(x: u32) u8;
fn trailing_zeros_u64(x: u64) u8;
fn trailing_zeros_u8(x: u8) u8;
fn truncf64(x: f64) f64;
Types
type floatinfo
type floatinfo = struct {
mantbits: u64,
expbits: u64,
expbias: int,
mantmask: u64,
expmask: u64,
};
Contains information about the structure of a specific floating point number type.
Constants
def E
def E: f64 = 2.718281828459045;
e - https://oeis.org/A001113
def F32_EPS
def F32_EPS: f32 = 1.1920928955078125e-7;
The difference between 1 and the smallest f32 representable value that is greater than 1.
def F32_EXPONENT_BIAS
def F32_EXPONENT_BIAS = 127;
The bias of the exponent of the binary representation of f32. Subtract this from the exponent in the binary representation to get the actual exponent.
def F32_EXPONENT_BITS
def F32_EXPONENT_BITS = 8;
The number of bits in the exponent of the binary representation of f32.
def F32_EXPONENT_MASK
def F32_EXPONENT_MASK: u32 = (1 << F32_EXPONENT_BITS) - 1;
Mask with each bit of an f32's exponent set.
def F32_MANTISSA_BITS
def F32_MANTISSA_BITS = 23;
The number of bits in the significand of the binary representation of f32.
def F32_MANTISSA_MASK
def F32_MANTISSA_MASK: u32 = (1 << F32_MANTISSA_BITS) - 1;
Mask with each bit of an f32's mantissa set.
def F32_MAX_NORMAL
def F32_MAX_NORMAL: f32 = 3.4028234e38;
The largest representable f32 value which is less than Infinity.
def F32_MIN_NORMAL
def F32_MIN_NORMAL: f32 = 1.1754944e-38;
The smallest representable normal f32 value.
def F32_MIN_SUBNORMAL
def F32_MIN_SUBNORMAL: f32 = 1.0e-45;
The smallest (subnormal) f32 value greater than zero.
def F64_EPS
def F64_EPS: f64 = 2.2204e-16;
The difference between 1 and the smallest f64 representable value that is greater than 1.
def F64_EXPONENT_BIAS
def F64_EXPONENT_BIAS = 1023;
The bias of the exponent of the binary representation of f64. Subtract this from the exponent in the binary representation to get the actual exponent.
def F64_EXPONENT_BITS
def F64_EXPONENT_BITS = 11;
The number of bits in the exponent of the binary representation of f64.
def F64_EXPONENT_MASK
def F64_EXPONENT_MASK: u64 = (1 << F64_EXPONENT_BITS) - 1;
Mask with each bit of an f64's exponent set.
def F64_MANTISSA_BITS
def F64_MANTISSA_BITS = 52;
The number of bits in the significand of the binary representation of f64.
def F64_MANTISSA_MASK
def F64_MANTISSA_MASK: u64 = (1 << F64_MANTISSA_BITS) - 1;
Mask with each bit of an f64's mantissa set.
def F64_MAX_NORMAL
def F64_MAX_NORMAL: f64 = 1.7976931348623157e308;
The largest representable f64 value which is less than Infinity.
def F64_MIN_NORMAL
def F64_MIN_NORMAL: f64 = 2.2250738585072014e-308;
The smallest representable normal f64 value.
def F64_MIN_SUBNORMAL
def F64_MIN_SUBNORMAL: f64 = 5.0e-324;
The smallest (subnormal) f64 value greater than zero.
def INF
def INF = 1.0 / 0.0;
The floating point value representing positive infinity. Use -INF for negative infinity.
def LN2_HI
def LN2_HI: f64 = 0.6931471803691238;
ln(2) - https://oeis.org/A002162
def LN2_LO
def LN2_LO: f64 = 1.9082149292705877e-10;
ln(2) - https://oeis.org/A002162
def LN_10
def LN_10: f64 = 2.302585092994046;
ln(10) - https://oeis.org/A002392
def LN_2
def LN_2: f64 = 0.6931471805599453;
ln(2) - https://oeis.org/A002162
def LOG10_E
def LOG10_E: f64 = 1f64 / LN_10;
log_{10}(e)
def LOG2_E
def LOG2_E: f64 = 1f64 / LN_2;
log_{2}(e)
def NAN
def NAN = 0.0 / 0.0;
The floating point value representing Not a Number, i.e. an undefined or unrepresentable value. You cannot test if a number is NaN by comparing to this value; see isnan instead.
def PHI
def PHI: f64 = 1.618033988749895;
phi - https://oeis.org/A001622
def PI
def PI: f64 = 3.141592653589793;
pi - https://oeis.org/A000796
def SQRT_2
def SQRT_2: f64 = 1.4142135623730951;
sqrt(2) - https://oeis.org/A002193
def SQRT_E
def SQRT_E: f64 = 1.6487212707001282;
sqrt(e) - https://oeis.org/A019774
def SQRT_PHI
def SQRT_PHI: f64 = 1.272019649514069;
sqrt(phi) - https://oeis.org/A139339
def SQRT_PI
def SQRT_PI: f64 = 1.772453850905516;
sqrt(pi) - https://oeis.org/A002161
def STANDARD_TOL
def STANDARD_TOL = 1.0e-14;
The standard tolerance used by isclosef32 and isclosef64, which is just an arbitrary way to measure whether two floating-point numbers are "sufficiently" close to each other.
def TAU
def TAU: f64 = 6.283185307179586;
tau - https://oeis.org/A019692
Globals
let f32info
const f32info: floatinfo;
A floatinfo structure defining the structure of the f32 type.
let f64info
const f64info: floatinfo;
A floatinfo structure defining the structure of the f64 type.
Functions
fn absf32
fn absf32(n: f32) f32;
Returns the absolute value of f32 n.
fn absf64
fn absf64(n: f64) f64;
Returns the absolute value of f64 n.
fn absi
fn absi(n: int) uint;
Returns the absolute value of signed integer n.
fn absi16
fn absi16(n: i16) u16;
Returns the absolute value of signed integer n.
fn absi32
fn absi32(n: i32) u32;
Returns the absolute value of signed integer n.
fn absi64
fn absi64(n: i64) u64;
Returns the absolute value of signed integer n.
fn absi8
fn absi8(n: i8) u8;
Returns the absolute value of signed integer n.
fn acosf64
fn acosf64(x: f64) f64;
Returns the arccosine, in radians, of x.
fn acoshf64
fn acoshf64(x: f64) f64;
Returns the inverse hyperbolic cosine of x.
fn asinf64
fn asinf64(x: f64) f64;
Returns the arcsine, in radians, of x.
fn asinhf64
fn asinhf64(x: f64) f64;
Returns the inverse hyperbolic sine of x.
fn atan2f64
fn atan2f64(y: f64, x: f64) f64;
Returns the arctangent, in radians, of y / x.
fn atanf64
fn atanf64(x: f64) f64;
Returns the arctangent, in radians, of x.
fn atanhf64
fn atanhf64(x: f64) f64;
Returns the inverse hyperbolic tangent of x.
fn bit_size
fn bit_size(x: u64) u8;
Returns the minimum number of bits required to represent x.
fn ceilf64
fn ceilf64(x: f64) f64;
Returns the least integer value greater than or equal to x.
fn copysignf32
fn copysignf32(x: f32, y: f32) f32;
Returns x, but with the sign of y.
fn copysignf64
fn copysignf64(x: f64, y: f64) f64;
Returns x, but with the sign of y.
fn cosf64
fn cosf64(x: f64) f64;
Returns the cosine of x, in radians.
fn coshf64
fn coshf64(x: f64) f64;
Returns the hyperbolic cosine of x.
fn divu
fn divu(hi: uint, lo: uint, y: uint) (uint, uint);
Returns the quotient and remainder of (hi, lo) divided by y: quo = (hi, lo) / y, rem = (hi, lo) % y with the dividend bits' upper half in parameter hi and the lower half in parameter lo. Aborts if y == 0 (division by zero) or y <= hi (quotient overflow).
fn divu32
fn divu32(hi: u32, lo: u32, y: u32) (u32, u32);
Returns the quotient and remainder of (hi, lo) divided by y: quo = (hi, lo) / y, rem = (hi, lo) % y with the dividend bits' upper half in parameter hi and the lower half in parameter lo. Aborts if y == 0 (division by zero) or y <= hi (quotient overflow).
fn divu64
fn divu64(hi: u64, lo: u64, y: u64) (u64, u64);
Returns the quotient and remainder of (hi, lo) divided by y: quo = (hi, lo) / y, rem = (hi, lo) % y with the dividend bits' upper half in parameter hi and the lower half in parameter lo. Aborts if y == 0 (division by zero) or y <= hi (quotient overflow).
fn exp2f64
fn exp2f64(x: f64) f64;
Returns 2^x.
fn expf64
fn expf64(x: f64) f64;
Returns e^x.
fn expmultif64
fn expmultif64(hi: f64, lo: f64, k: i64) f64;
Returns e^r * 2^k where r = hi - lo and |r| <= (ln(2) / 2).
fn f32bits
fn f32bits(n: f32) u32;
Returns the binary representation of the given f32.
fn f32frombits
fn f32frombits(n: u32) f32;
Returns f32 with the given binary representation.
fn f64bits
fn f64bits(n: f64) u64;
Returns the binary representation of the given f64.
fn f64frombits
fn f64frombits(n: u64) f64;
Returns f64 with the given binary representation.
fn floorf64
fn floorf64(x: f64) f64;
Returns the greatest integer value less than or equal to x.
fn frexpf32
fn frexpf32(n: f32) (f32, i64);
Breaks a f32 down into its mantissa and exponent. The mantissa will be between 0.5 and 1.
fn frexpf64
fn frexpf64(n: f64) (f64, i64);
Breaks a f64 down into its mantissa and exponent. The mantissa will be between 0.5 and 1.
fn gcd
fn gcd(a: u64, b: u64) u64;
Returns the greatest common divisor of a and b.
fn hypotf64
fn hypotf64(a: f64, b: f64) f64;
Returns the square root of a*a + b*b, taking care to avoid unnecessary overflow and underflow.
fn isclosef32
fn isclosef32(x: f32, y: f32, tol: f32 = STANDARD_TOL) bool;
Returns whether x and y are within tol of each other.
fn isclosef64
fn isclosef64(x: f64, y: f64, tol: f64 = STANDARD_TOL) bool;
Returns whether x and y are within tol of each other.
fn isinf
fn isinf(n: f64) bool;
Returns true if the given floating-point number is infinite.
fn isnan
fn isnan(n: f64) bool;
Returns true if the given floating-point number is NaN.
fn isnegativef32
fn isnegativef32(x: f32) bool;
Returns whether or not x is negative.
fn isnegativef64
fn isnegativef64(x: f64) bool;
Returns whether or not x is negative.
fn isnormalf32
fn isnormalf32(n: f32) bool;
Returns true if the given f32 is normal.
fn isnormalf64
fn isnormalf64(n: f64) bool;
Returns true if the given f64 is normal.
fn ispositivef32
fn ispositivef32(x: f32) bool;
Returns whether or not x is positive.
fn ispositivef64
fn ispositivef64(x: f64) bool;
Returns whether or not x is positive.
fn issubnormalf32
fn issubnormalf32(n: f32) bool;
Returns true if the given f32 is subnormal.
fn issubnormalf64
fn issubnormalf64(n: f64) bool;
Returns true if the given f64 is subnormal.
fn ldexpf32
fn ldexpf32(mantissa: f32, exp: i64) f32;
Creates an f32 from a mantissa and an exponent.
fn ldexpf64
fn ldexpf64(mantissa: f64, exp: i64) f64;
Creates an f64 from a mantissa and an exponent.
fn leading_zeros_u
fn leading_zeros_u(x: uint) u8;
Returns the number of leading zero bits in x The result is size(uint) * 8 for x == 0.
fn leading_zeros_u16
fn leading_zeros_u16(x: u16) u8;
Returns the number of leading zero bits in x The result is 16 for x == 0.
fn leading_zeros_u32
fn leading_zeros_u32(x: u32) u8;
Returns the number of leading zero bits in x The result is 32 for x == 0.
fn leading_zeros_u64
fn leading_zeros_u64(x: u64) u8;
Returns the number of leading zero bits in x The result is 64 for x == 0.
fn leading_zeros_u8
fn leading_zeros_u8(x: u8) u8;
Returns the number of leading zero bits in x The result is 8 for x == 0.
fn log10f64
fn log10f64(x: f64) f64;
Returns the decimal logarithm of x.
fn log1pf64
fn log1pf64(x: f64) f64;
Returns the natural logarithm of 1 plus its argument x. It is more accurate than log(1 + x) when x is near zero.
fn log2f64
fn log2f64(x: f64) f64;
Returns the binary logarithm of x.
fn logf64
fn logf64(x: f64) f64;
Returns the natural logarithm of x.
fn modf64
fn modf64(x: f64, y: f64) f64;
Returns the floating-point remainder of x / y. The magnitude of the result is less than y and its sign agrees with that of x.
fn modfracf32
fn modfracf32(n: f32) (f32, f32);
Returns the integer and fractional parts of an f32.
fn modfracf64
fn modfracf64(n: f64) (f64, f64);
Returns the integer and fractional parts of an f64.
fn mulu
fn mulu(x: uint, y: uint) (uint, uint);
Returns the product of x and y: (hi, lo) = x * y with the product bits' upper half returned in hi and the lower half returned in lo.
fn mulu32
fn mulu32(x: u32, y: u32) (u32, u32);
Returns the 64-bit product of x and y: (hi, lo) = x * y with the product bits' upper half returned in hi and the lower half returned in lo.
fn mulu64
fn mulu64(x: u64, y: u64) (u64, u64);
Returns the 128-bit product of x and y: (hi, lo) = x * y with the product bits' upper half returned in hi and the lower half returned in lo.
fn nextafterf32
fn nextafterf32(x: f32, y: f32) f32;
Returns the f32 that is closest to 'x' in direction of 'y'. Returns NaN if either parameter is NaN. Returns 'x' if both parameters are same.
fn nextafterf64
fn nextafterf64(x: f64, y: f64) f64;
Returns the f64 that is closest to 'x' in direction of 'y' Returns NaN if either parameter is NaN. Returns 'x' if both parameters are same.
fn normalizef32
fn normalizef32(n: f32) (f32, i64);
Takes a potentially subnormal f32 n and returns a normal f32 normal_float and an exponent exp such that n == normal_float * 2^{exp}.
fn normalizef64
fn normalizef64(n: f64) (f64, i64);
Takes a potentially subnormal f64 n and returns a normal f64 normal_float and an exponent exp such that n == normal_float * 2^{exp}.
fn popcount
fn popcount(x: u64) u8;
Returns the number of bits set (the population count) of x.
fn powf64
fn powf64(x: f64, p: f64) f64;
Returns x^p.
fn powi64
fn powi64(x: i64, n: uint) i64;
Return x to the power of n (a positive integer)
fn remu
fn remu(hi: uint, lo: uint, y: uint) uint;
Returns the remainder of (hi, lo) divided by y. Aborts if y == 0 (division by zero) but, unlike divu, it doesn't abort on a quotient overflow.
fn remu32
fn remu32(hi: u32, lo: u32, y: u32) u32;
Returns the remainder of (hi, lo) divided by y. Aborts if y == 0 (division by zero) but, unlike divu32, it doesn't abort on a quotient overflow.
fn remu64
fn remu64(hi: u64, lo: u64, y: u64) u64;
Returns the remainder of (hi, lo) divided by y. Aborts if y == 0 (division by zero) but, unlike divu64, it doesn't abort on a quotient overflow.
fn roundf64
fn roundf64(x: f64) f64;
Returns the nearest integer, rounding half away from zero.
fn signf32
fn signf32(x: f32) i64;
Returns 1 if x is positive and -1 if x is negative. Note that zero is also signed.
fn signf64
fn signf64(x: f64) i64;
Returns 1 if x is positive and -1 if x is negative. Note that zero is also signed.
fn signi
fn signi(n: int) i64;
Return 1 if n is positive, -1 if it's negative and 0 if it's 0.
fn signi16
fn signi16(n: i16) i16;
Return 1 if n is positive, -1 if it's negative and 0 if it's 0.
fn signi32
fn signi32(n: i32) i32;
Return 1 if n is positive, -1 if it's negative and 0 if it's 0.
fn signi64
fn signi64(n: i64) i64;
Return 1 if n is positive, -1 if it's negative and 0 if it's 0.
fn signi8
fn signi8(n: i8) i8;
Return 1 if n is positive, -1 if it's negative and 0 if it's 0.
fn sinf64
fn sinf64(x: f64) f64;
Returns the sine of x, in radians.
fn sinhf64
fn sinhf64(x: f64) f64;
Returns the hyperbolic sine of x.
fn sqrtf64
fn sqrtf64(x: f64) f64;
Returns the non-negative square root of x.
fn tanf64
fn tanf64(x: f64) f64;
Returns the tangent of x, in radians.
fn tanhf64
fn tanhf64(x: f64) f64;
Returns the hyperbolic tangent of x.
fn trailing_zeros_u
fn trailing_zeros_u(x: uint) u8;
Returns the number of trailing zero bits in x The result is size(uint) * 8 for x == 0.
fn trailing_zeros_u16
fn trailing_zeros_u16(x: u16) u8;
Returns the number of trailing zero bits in x The result is 16 for x == 0.
fn trailing_zeros_u32
fn trailing_zeros_u32(x: u32) u8;
Returns the number of trailing zero bits in x The result is 32 for x == 0.
fn trailing_zeros_u64
fn trailing_zeros_u64(x: u64) u8;
Returns the number of trailing zero bits in x The result is 64 for x == 0.
fn trailing_zeros_u8
fn trailing_zeros_u8(x: u8) u8;
Returns the number of trailing zero bits in x The result is 8 for x == 0.
fn truncf64
fn truncf64(x: f64) f64;
Returns the integer value of x.