vera:math
Mathematical functions for f64 floating-point arithmetic.
import "vera:math"
Overview
Provides constants, utility functions, and transcendental functions for f64 floating-point arithmetic. All functions operate on IEEE 754 double-precision values. Trigonometric functions use Cody-Waite range reduction and minimax polynomial approximations. Exponential and logarithmic functions use Horner-form polynomial evaluation with FMA instructions.
Dependencies: None (pure VERA)
Functions
Constants
math_pi
function math_pi() -> f64
Returns the constant pi (3.14159265358979323846).
math_e
function math_e() -> f64
Returns Euler's number e (2.71828182845904523536).
math_ln2
function math_ln2() -> f64
Returns the natural logarithm of 2 (0.69314718055994530942).
math_ln10
function math_ln10() -> f64
Returns the natural logarithm of 10 (2.30258509299404568402).
Utility
abs_f64
function abs_f64(x: f64) -> f64
Absolute value of a float.
Parameters:
x(f64) — input value
Returns: |x|
floor_f64
function floor_f64(x: f64) -> f64
Round toward negative infinity. Returns NaN unchanged. Values with magnitude >= 2^53 are returned unchanged (already integers at f64 precision).
Parameters:
x(f64) — input value
Returns: the largest integer value not greater than x
ceil_f64
function ceil_f64(x: f64) -> f64
Round toward positive infinity. Implemented as -floor(-x).
Parameters:
x(f64) — input value
Returns: the smallest integer value not less than x
round_f64
function round_f64(x: f64) -> f64
Round to nearest integer (half rounds up). Implemented as floor(x + 0.5).
Parameters:
x(f64) — input value
Returns: the nearest integer to x
min_f64
function min_f64(a: f64, b: f64) -> f64
Minimum of two floats.
Parameters:
a(f64) — first valueb(f64) — second value
Returns: the smaller of a and b
max_f64
function max_f64(a: f64, b: f64) -> f64
Maximum of two floats.
Parameters:
a(f64) — first valueb(f64) — second value
Returns: the larger of a and b
clamp_f64
function clamp_f64(x: f64, lo: f64, hi: f64) -> f64
Clamp x to the range [lo, hi]. Equivalent to min(max(x, lo), hi).
Parameters:
x(f64) — value to clamplo(f64) — lower boundhi(f64) — upper bound
Returns: x clamped to [lo, hi]
sqrt_f64
function sqrt_f64(x: f64) -> f64
Square root. Wraps the ISA's square_root.f64 instruction.
Parameters:
x(f64) — input value (must be >= 0)
Returns: the square root of x
degrees_to_radians
function degrees_to_radians(deg: f64) -> f64
Convert degrees to radians.
Parameters:
deg(f64) — angle in degrees
Returns: angle in radians
radians_to_degrees
function radians_to_degrees(rad: f64) -> f64
Convert radians to degrees.
Parameters:
rad(f64) — angle in radians
Returns: angle in degrees
Trigonometric
sin_f64
function sin_f64(x: f64) -> f64
Sine of x (radians). Uses Cody-Waite range reduction to [-pi/4, pi/4] and minimax polynomial kernels. Returns NaN unchanged.
Parameters:
x(f64) — angle in radians
Returns: sin(x)
cos_f64
function cos_f64(x: f64) -> f64
Cosine of x (radians). Uses the same range reduction as sin_f64. Returns NaN unchanged.
Parameters:
x(f64) — angle in radians
Returns: cos(x)
tan_f64
function tan_f64(x: f64) -> f64
Tangent of x (radians). Computed as sin(x) / cos(x).
Parameters:
x(f64) — angle in radians
Returns: tan(x)
Exponential / Logarithmic
exp_f64
function exp_f64(x: f64) -> f64
Exponential function e^x. Uses range reduction via x = k * ln2 + r and a minimax polynomial for exp(r). Handles overflow (x > ~709.78 returns +infinity) and underflow (x < ~-745.13 returns 0). Returns NaN unchanged.
Parameters:
x(f64) — exponent
Returns: e^x
log_f64
function log_f64(x: f64) -> f64
Natural logarithm of x. Decomposes x into mantissa and exponent, then uses a Horner-form polynomial on s = (m-1)/(m+1). Returns NaN for negative inputs, -infinity for zero, and NaN unchanged.
Parameters:
x(f64) — input value (must be > 0 for a meaningful result)
Returns: ln(x)
log2_f64
function log2_f64(x: f64) -> f64
Base-2 logarithm of x. Computed as log(x) * (1/ln2).
Parameters:
x(f64) — input value (must be > 0 for a meaningful result)
Returns: log2(x)
log10_f64
function log10_f64(x: f64) -> f64
Base-10 logarithm of x. Computed as log(x) * (1/ln10).
Parameters:
x(f64) — input value (must be > 0 for a meaningful result)
Returns: log10(x)
Power
pow_f64
function pow_f64(base: f64, exponent: f64) -> f64
Raise base to the power of exponent. Computed as exp(exponent * log(base)) for positive bases. Handles special cases:
- Any base raised to 0 returns 1.0 (including NaN^0).
- 1.0 raised to any exponent returns 1.0.
- 0.0 raised to a positive exponent returns 0.0; to a negative exponent returns +infinity.
- Negative bases with integer exponents use sign parity; non-integer exponents return NaN.
Parameters:
base(f64) — the baseexponent(f64) — the exponent
Returns: base^exponent