Module: math.basic

Source: ./math/basic.reef


Overview

math/basic module

Basic mathematical operations for integers and floating-point numbers. All floating-point functions are implemented in pure Reef using IEEE 754 bit manipulation - no libc/libm dependency.

Algorithms ported from A2 Oberon MathL.Mod (ETH Oberon System)


Functions

fn math_LN2(): float

ln(2) - natural log of 2

fn math_LN10(): float

ln(10) - natural log of 10

fn math_TWO_PI(): float

2*pi

fn math_PI(): float

pi

fn math_PI_2(): float

pi/2

fn math_PI_4(): float

pi/4

fn math_EPS(): float

Epsilon for convergence test (approximately 2.2e-16)

fn math_HUGE(): float

Large positive value (near max float)

fn math_TINY(): float

Small positive value for comparisons

fn float_equal(x: float, y: float): bool

fn abs(x: int): int

Returns the absolute value of x

fn min(a: int, b: int): int

Returns the smaller of a and b

fn max(a: int, b: int): int

Returns the larger of a and b

fn square(x: int): int

Returns x squared (x * x)

fn cube(x: int): int

Returns x cubed (x * x * x)

fn pow_int(base: int, exp: int): int

Returns base raised to the power exp (integer exponentiation) Note: exp must be non-negative

fn clamp(value: int, lo: int, hi: int): int

Clamps value to the range [lo, hi]

fn sign(x: int): int

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

fn gcd(a: int, b: int): int

Returns the greatest common divisor of a and b (Euclidean algorithm)

fn lcm(a: int, b: int): int

Returns the least common multiple of a and b

fn is_even(x: int): bool

Returns true if x is even

fn is_odd(x: int): bool

Returns true if x is odd

fn abs_f(x: float): float

Returns the absolute value of x

fn min_f(a: float, b: float): float

Returns the smaller of a and b

fn max_f(a: float, b: float): float

Returns the larger of a and b

fn trunc_f(x: float): float

Returns x truncated toward zero

fn floor_f(x: float): float

Returns the largest integer <= x (as float)

fn ceil_f(x: float): float

Returns the smallest integer >= x (as float)

fn round_f(x: float): float

Returns x rounded to the nearest integer (as float) Rounds half away from zero

fn exp_f(x: float): float

fn log_f(x: float): float

fn log10_f(x: float): float

fn sqrt_f(x: float): float

fn cbrt_f(x: float): float

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

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

fn pow_f(base: float, exponent: float): float

fn sin_f(x: float): float

Sine function using Taylor series sin(x) = x - x^3/3! + x^5/5! - x^7/7! + ...

fn cos_f(x: float): float

Cosine function using Taylor series cos(x) = 1 - x^2/2! + x^4/4! - x^6/6! + ...

fn tan_f(x: float): float

Tangent function: tan(x) = sin(x) / cos(x)

fn atan_f(x: float): float

Arctangent: atan(x) For |x| <= 1: Taylor series: atan(x) = x - x^3/3 + x^5/5 - x^7/7 + ... For |x| > 1: atan(x) = pi/2 - atan(1/x) (for x > 0) atan(x) = -pi/2 - atan(1/x) (for x < 0)

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

Two-argument arctangent: atan2(y, x) = angle of point (x, y) Returns angle in radians in range (-pi, pi]

fn asin_f(x: float): float

Arcsine: asin(x) asin(x) = atan(x / sqrt(1 - x^2)) Domain: [-1, 1]

fn acos_f(x: float): float

Arccosine: acos(x) acos(x) = pi/2 - asin(x) Domain: [-1, 1]

fn sinh_f(x: float): float

Hyperbolic sine: sinh(x) = (e^x - e^(-x)) / 2

fn cosh_f(x: float): float

Hyperbolic cosine: cosh(x) = (e^x + e^(-x)) / 2

fn tanh_f(x: float): float

Hyperbolic tangent: tanh(x) = sinh(x) / cosh(x) = (e^x - e^(-x)) / (e^x + e^(-x))

fn log2_f(x: float): float


Generated by reefc doc