Module: math.floats

Source: ./math/floats.reef


Overview

math/floats - Extended floating-point mathematical functions

Provides additional floating-point operations including:

  • Mathematical constants (pi, e)
  • Inverse trigonometric functions
  • Additional utility functions
  • Special value checking (NaN, infinity)

Note: Basic float functions (sqrt, sin, cos, etc.) are in math/basic

FREESTANDING (2026-09-06). This module no longer depends on libc/libm.

It used to wrap libc for eleven functions -- trunc_f, cbrt_f, log2_f, asin_f, acos_f, atan_f, atan2_f, sinh_f, cosh_f, tanh_f, hypot_f -- and those eleven were its ONLY use of libc. They were also exactly the eleven names it duplicated from math.basic, which implements all of them in pure Reef (IEEE-754 bit manipulation, ported from A2 Oberon MathL.Mod). So the module carried two bodies per name, with no caller able to tell which it would get, and the whole libm dependency existed to serve the duplicates.

They now forward to math.basic: one implementation per function, no libm, and the same public API. Divergence was MEASURED against libc across [-10, 10] before this change was made, and every one of the eleven agrees to <= 2.8e-14, with nine of them at <= 1e-15:

trunc_f 0 | cbrt_f, tanh_f 2.2e-16 | log2_f 3.3e-16 | hypot_f 8.9e-16 atan_f, atan2_f, asin_f 2.2e-16 | acos_f 4.4e-16 | sinh_f, cosh_f 2.8e-14

That was NOT true before reef-lang#56: atan_f/atan2_f were 5.0e-4 off and asin_f/acos_f 1.4e-7, so forwarding would have silently degraded accuracy in graphics (the one package that imports both modules). Fix the pure implementation first, measure, then converge -- in that order.


Functions

fn pi(): float

Returns the mathematical constant pi (3.14159...)

fn e(): float

Returns Euler's number e (2.71828...)

fn tau(): float

Returns tau (2 * pi = 6.28318...)

fn trunc_f(x: float): float

Returns x truncated toward zero (removes fractional part)

fn cbrt_f(x: float): float

Returns the cube root of x

fn log2_f(x: float): float

Returns the base-2 logarithm of x

fn asin_f(x: float): float

Returns the arc sine of x (result in radians) Input must be in range [-1, 1]

fn acos_f(x: float): float

Returns the arc cosine of x (result in radians) Input must be in range [-1, 1]

fn atan_f(x: float): float

Returns the arc tangent of x (result in radians)

fn atan2_f(y: float, x: float): float

Returns the arc tangent of y/x using signs to determine quadrant Result in radians, range [-pi, pi]

fn sinh_f(x: float): float

Returns the hyperbolic sine of x

fn cosh_f(x: float): float

Returns the hyperbolic cosine of x

fn tanh_f(x: float): float

Returns the hyperbolic tangent of x

fn hypot_f(x: float, y: float): float

Returns the hypotenuse: sqrt(xx + yy) More accurate than computing directly for large values

fn degrees(rad: float): float

Converts radians to degrees

fn radians(deg: float): float

Converts degrees to radians

fn clamp_f(x: float, lo: float, hi: float): float

Clamps x to the range [lo, hi]

fn sign_f(x: float): int

Returns -1 if x < 0, 0 if x == 0, 1 if x > 0

fn lerp_f(a: float, b: float, t: float): float

Linear interpolation between a and b Returns a when t=0, b when t=1

fn is_nan(x: float): bool

Returns true if x is NaN (Not a Number)

fn is_infinite(x: float): bool

Returns true if x is positive or negative infinity

fn is_finite(x: float): bool

Returns true if x is a normal finite number (not NaN or infinity)

fn to_int(x: float): int

Converts float to int by truncating toward zero

fn to_int_floor(x: float): int

Converts float to int by rounding toward negative infinity

fn to_int_ceil(x: float): int

Converts float to int by rounding toward positive infinity

fn to_int_round(x: float): int

Converts float to int by rounding to nearest integer


Generated by reefc doc