dymokomi/luce-std
Source of release 0.7.3
0.7.3 / src / math32 / module.lucb
#==============================================================================================
#
# math32 - Single-precision mathematical functions
#
# DESCRIPTION:
# Single-precision mathematics. `math32` has the floating-point operations of `math`, with
# f32 arguments and results and the same special-value contracts. exp, log, log2,
# log10, pow, sin, cos, sincos, tan, asin, acos, atan, atan2 and hypot have
# algorithms of their own in doubles (the files of this directory), each with an error
# bound: when every value within the bound rounds to one f32, that is the correctly
# rounded result, and the others, rare, take math's f64 result rounded once. That is
# how every elementary function here was defined before, and every f32 input of the
# one-argument functions gives the same bits as then. The rest of the elementary
# functions are math's, rounded once. The exact operations (rounding, sqrt, fma, mod,
# remainder and the decompositions) are the host's single-precision ones.
#
#==============================================================================================
import math
from math_tables import asin_coefficients, atan_taylor, exp_table_high, inverse_ln2_128, inverse_step, ln2_128_high, ln2_128_low, log_inverse, log_point, log_table_high, log_table_low, pi_high, pio2_high, sine_table, step_1, step_2, step_3
extern func c_floor as "floorf"(x: f32) -> f32
extern func c_ceil as "ceilf"(x: f32) -> f32
extern func c_round as "roundf"(x: f32) -> f32
extern func c_trunc as "truncf"(x: f32) -> f32
extern func c_sqrt as "sqrtf"(x: f32) -> f32
extern func c_fmod as "fmodf"(x: f32, y: f32) -> f32
extern func c_fma as "fmaf"(x: f32, y: f32, z: f32) -> f32
extern func c_fabs as "fabsf"(x: f32) -> f32
extern func c_nextafter as "nextafterf"(x: f32, toward: f32) -> f32
extern func c_frexp as "frexpf"(x: f32, out exponent: i32) -> f32
extern func c_scalbn as "scalbnf"(x: f32, exponent: i32) -> f32
extern func c_modf as "modff"(x: f32, out integral: f32) -> f32
extern func c_remainder as "remainderf"(x: f32, y: f32) -> f32
## The ratio of a circle's circumference to its diameter.
pub let pi: f32 = 3.141592653589793
## A full turn in radians, two pi.
pub let tau: f32 = 6.283185307179586
## Euler's number.
pub let e: f32 = 2.718281828459045
## Built from bits so that no constant expression needs to overflow.
pub let infinity: f32 = f32.bits(2139095040)
## A quiet not-a-number; unequal to everything, itself included.
pub let nan: f32 = f32.bits(2143289344)
let sign_mask: u32 = 0x80000000
let magnitude_mask: u32 = 0x7fffffff
let exponent_mask: u32 = 0x7f800000
## 1.5ยท2^52: adding and subtracting it rounds a double below 2^51 to an integer.
let round_shift: f64 = 6755399441055744.0
## |h| for a double, by its bits.
inline func magnitude64(h: f64) -> f64:
return f64.bits(h.bits() & 0x7fffffffffffffff)
## 2^-46: the relative error bound most of the single-precision algorithms test against.
let relative_46: f64 = 1.4210854715202004e-14
## Whether every value within `relative` of h, relatively, rounds to the same f32. Rounding
## is monotonic, so when the two ends do, so does the exact value the bound covers.
inline func rounds_once(h: f64, relative: f64) -> bool:
let bound = magnitude64(h) * relative
return (f32)(h - bound) == (f32)(h + bound)
# mark: Real functions ========================================================================
## Rounding: `floor` toward negative infinity, `ceil` toward positive, `round` to the
## nearest with halves away from zero, `trunc` toward zero.
pub func floor(x: f32) -> f32:
return c_floor(x)
## The smallest integer value not less than `x`.
pub func ceil(x: f32) -> f32:
return c_ceil(x)
## `x` rounded to the nearest integer, halves away from zero.
pub func round(x: f32) -> f32:
return c_round(x)
## `x` with its fractional part removed, toward zero.
pub func trunc(x: f32) -> f32:
return c_trunc(x)
## The square root of `x`.
pub func sqrt(x: f32) -> f32:
return c_sqrt(x)
## The cube root of `x`.
pub func cbrt(x: f32) -> f32:
return (f32)math.cbrt((f64)x)
## `sqrt(x * x + y * y)` without overflow in the squares.
pub func hypot(x: f32, y: f32) -> f32:
return hypot_value(x, y)
## The remainder of `x / y` with the sign of `x`, as C's `fmod`; a zero has the sign of `x`.
pub func mod(x: f32, y: f32) -> f32:
let result = c_fmod(x, y)
# MinGW's fmod loses the dividend's zero sign under directed rounding, as some
# hosts' remainder does: restore it by bits, leaving a nonzero or NaN result.
return copysign(result, x) if (result.bits() & magnitude_mask) == 0 else result
## `x` raised to the power `y`.
pub func pow(x: f32, y: f32) -> f32:
return pow_value(x, y)
## `e` raised to the power `x`.
pub func exp(x: f32) -> f32:
return exp_value(x)
## `2` raised to the power `x`.
pub func exp2(x: f32) -> f32:
return (f32)math.exp2((f64)x)
## The natural logarithm of `x`.
pub func log(x: f32) -> f32:
return log_value(x)
## The base-2 logarithm of `x`.
pub func log2(x: f32) -> f32:
return log2_value(x)
## The base-10 logarithm of `x`.
pub func log10(x: f32) -> f32:
return log10_value(x)
## Natural logarithm of `1 + x`, retaining accuracy when adding `x` to one would
## round it away. At -1 the result is negative infinity; below -1 it is NaN.
pub func log1p(x: f32) -> f32:
return (f32)math.log1p((f64)x)
## `exp(x) - 1` without cancellation near zero. Signed zero is preserved.
pub func expm1(x: f32) -> f32:
return (f32)math.expm1((f64)x)
## Multiply `x * y` and add `z` with one final rounding, rather than rounding the
## product first. Uses the host's floating-point rounding mode.
pub func fma(x: f32, y: f32, z: f32) -> f32:
return c_fma(x, y, z)
## The adjacent representable value toward `toward`; equal arguments return
## `toward`, including its zero sign. A NaN argument produces NaN.
pub func nextafter(x: f32, toward: f32) -> f32:
return c_nextafter(x, toward)
## The adjacent representable value toward positive infinity. Positive infinity
## stays infinite; either zero becomes the smallest positive subnormal.
pub func next_up(x: f32) -> f32:
return c_nextafter(x, infinity)
## The adjacent representable value toward negative infinity. Negative infinity
## stays infinite; either zero becomes the smallest negative subnormal.
pub func next_down(x: f32) -> f32:
return c_nextafter(x, copysign(infinity, -1.0))
## Split `x` into `(fraction, exponent)` such that `x = fraction * 2**exponent`.
## A finite nonzero fraction has magnitude in [0.5, 1). Zero, infinity and NaN
## are returned unchanged with exponent zero, avoiding an unspecified C exponent.
pub func frexp(x: f32) -> (f32, i32):
if x == 0.0 or not is_finite(x):
return (x, 0)
return c_frexp(x)
## Scale by an integer power of two, without forming that power as an intermediate
## float. Overflow/underflow follow the host rounding mode; signed zero survives.
pub func scalbn(x: f32, exponent: i32) -> f32:
return c_scalbn(x, exponent)
## `(fractional, integral)` parts, both with the sign of `x`; the integral part
## truncates toward zero. Infinity has a signed-zero fraction; NaN gives two NaNs.
pub func modf(x: f32) -> (f32, f32):
return c_modf(x)
## `x - n*y` for the nearest integer `n` to `x/y`, with ties choosing even `n`.
## This differs from `mod`, which truncates the quotient. Zero has the sign of `x`;
## a zero divisor or infinite dividend produces NaN.
pub func remainder(x: f32, y: f32) -> f32:
let result = c_remainder(x, y)
# Match the f64 contract even if a host loses the zero sign while rounding.
return copysign(result, x) if (result.bits() & magnitude_mask) == 0 else result
## The sine of `x` radians.
pub func sin(x: f32) -> f32:
return sin_value(x)
## The cosine of `x` radians.
pub func cos(x: f32) -> f32:
return cos_value(x)
## The sine and the cosine of `x` radians, `(sin(x), cos(x))`, from one reduction.
pub func sincos(x: f32) -> (f32, f32):
return sincos_value(x)
## The tangent of `x` radians.
pub func tan(x: f32) -> f32:
return tan_value(x)
## The arcsine of `x`, in radians.
pub func asin(x: f32) -> f32:
return asin_value(x)
## The arccosine of `x`, in radians.
pub func acos(x: f32) -> f32:
return acos_value(x)
## The arctangent of `x`, in radians.
pub func atan(x: f32) -> f32:
return atan_value(x)
## The angle of the point `(x, y)`, in all four quadrants.
pub func atan2(y: f32, x: f32) -> f32:
return atan2_value(y, x)
## The hyperbolic sine of `x`.
pub func sinh(x: f32) -> f32:
return (f32)math.sinh((f64)x)
## The hyperbolic cosine of `x`.
pub func cosh(x: f32) -> f32:
return (f32)math.cosh((f64)x)
## The hyperbolic tangent of `x`.
pub func tanh(x: f32) -> f32:
return (f32)math.tanh((f64)x)
## The error function, as `math.erf`, evaluated in f64 and rounded once to f32: within a
## unit in the last place.
pub func erf(x: f32) -> f32:
return (f32)math.erf((f64)x)
## The complementary error function, `1 - erf(x)` without cancellation, as `math.erfc`,
## evaluated in f64 and rounded once to f32.
pub func erfc(x: f32) -> f32:
return (f32)math.erfc((f64)x)
## True for quiet and signaling NaNs, independent of sign and payload.
pub func is_nan(x: f32) -> bool:
return (x.bits() & magnitude_mask) > exponent_mask
## True for zeros, subnormals and normal finite values. Classification inspects the
## representation and performs no floating-point arithmetic on a signaling NaN.
pub func is_finite(x: f32) -> bool:
return (x.bits() & exponent_mask) != exponent_mask
## True only for positive or negative infinity.
pub func is_infinite(x: f32) -> bool:
return (x.bits() & magnitude_mask) == exponent_mask
## Whether the sign bit is set, including negative zero and signed NaNs.
pub func signbit(x: f32) -> bool:
return (x.bits() & sign_mask) != 0
## Clear the sign bit: negative zero becomes positive zero. NaN payload bits survive.
pub func abs(x: f32) -> f32:
return c_fabs(x)
## The magnitude and payload of `magnitude` with the sign bit of `sign_source`.
pub func copysign(magnitude: f32, sign_source: f32) -> f32:
return f32.bits((magnitude.bits() & magnitude_mask) | (sign_source.bits() & sign_mask))
## `-1.0` or `1.0` for nonzero numbers; zero and NaN are returned unchanged.
pub func sign(x: f32) -> f32:
if is_nan(x):
return x
if x < 0.0:
return -1.0
if x > 0.0:
return 1.0
return x
## Minimum with NaN propagation (left NaN wins if both are NaNs). Negative zero
## wins over positive zero; a selected NaN retains its original bits.
pub func min(a: f32, b: f32) -> f32:
if is_nan(a):
return a
if is_nan(b):
return b
if a == 0.0 and b == 0.0:
return f32.bits(a.bits() | b.bits())
return a if a < b else b
## Maximum with NaN propagation (left NaN wins if both are NaNs). Positive zero
## wins over negative zero; a selected NaN retains its original bits.
pub func max(a: f32, b: f32) -> f32:
if is_nan(a):
return a
if is_nan(b):
return b
if a == 0.0 and b == 0.0:
return f32.bits(a.bits() & b.bits())
return a if a > b else b
## Restrict `x` to inclusive ordered bounds. Reversed or NaN bounds trap. An input
## NaN or an input equal to a bound is returned unchanged, including its zero sign.
pub func clamp(x: f32, low: f32, high: f32) -> f32:
assert(low <= high)
if is_nan(x):
return x
return low if x < low else (high if x > high else x)