Assorted common math functions & utilities for TypeScript/JavaScript applications
—
C standard library math functions providing low-level mathematical operations for compatibility with C and enhanced precision control.
Functions for manipulating the sign and magnitude components of floating-point numbers.
/**
* Returns a value with the magnitude of x and the sign of y
* @param x - Source magnitude
* @param y - Source sign
* @returns Value with magnitude of x and sign of y
*/
function copysign(x: number, y: number): number;Power functions using base 2 operations.
/**
* Returns 2^x
* @param x - Exponent value
* @returns 2 raised to the power of x
*/
function exp2(x: number): number;Functions for positive differences and fused multiply-add operations.
/**
* Returns the positive difference between x and y, i.e. x - y if x > y, otherwise zero
* @param x - First value
* @param y - Second value
* @returns Positive difference or zero
*/
function fdim(x: number, y: number): number;
/**
* Fused multiply-add operation: returns x * y + z
* @param x - First multiplicand
* @param y - Second multiplicand
* @param z - Additive term
* @returns Result of x * y + z
*/
function fma(x: number, y: number, z: number): number;C-style modulo operations with different behavior from JavaScript's default % operator.
/**
* C-style modulo operation. Returns x - y * trunc(x / y)
* Result will always have the sign of x
* @param x - Dividend
* @param y - Divisor
* @returns C-style modulo result
*/
function fmod(x: number, y: number): number;
/**
* IEEE remainder function. Returns x - y * round(x / y)
* @param x - Dividend
* @param y - Divisor
* @returns IEEE remainder
*/
function remainder(x: number, y: number): number;Functions for breaking down and reconstructing floating-point numbers using binary significand and exponent.
/**
* Breaks number x into binary significand and exponent
* Returns [significand, exponent] where x = significand * 2^exponent
* Significand has absolute value in [0.5, 1.0) interval
* @param x - Input number
* @returns Tuple of [significand, exponent]
*/
function frexp(x: number): [number, number];
/**
* Inverse of frexp. Returns x * 2^exp
* @param x - Significand value
* @param exp - Exponent value
* @returns Reconstructed floating-point number
*/
function ldexp(x: number, exp: number): number;Extended division operations returning both quotient and remainder.
/**
* Computes both quotient and remainder of integer division x / y
* @param x - Numerator (converted to integer)
* @param y - Denominator (converted to integer)
* @returns Tuple of [quotient, remainder]
*/
function ldiv(x: number, y: number): [number, number];import { copysign, exp2, fdim, fma, fmod, remainder } from "@thi.ng/math/libc";
import { frexp, ldexp, ldiv } from "@thi.ng/math/libc";
// Sign manipulation
copysign(5, -2); // -5 (magnitude of 5, sign of -2)
copysign(-3, 4); // 3 (magnitude of 3, sign of 4)
// Power of 2 operations
exp2(3); // 8 (2^3)
exp2(0.5); // 1.414... (2^0.5 = sqrt(2))
// Positive difference
fdim(5, 2); // 3 (5 - 2)
fdim(2, 5); // 0 (cannot be negative)
// Fused multiply-add
fma(2, 3, 4); // 10 (2 * 3 + 4)
// C-style vs IEEE modulo
fmod(-7, 3); // -1 (sign of dividend)
remainder(-7, 3); // 2 (IEEE remainder)
// Binary decomposition and reconstruction
const [sig, exp] = frexp(12); // [0.75, 4] since 12 = 0.75 * 2^4
ldexp(0.75, 4); // 12 (reconstructed)
// Integer division with remainder
ldiv(17, 5); // [3, 2] since 17 = 3 * 5 + 2Install with Tessl CLI
npx tessl i tessl/npm-thi-ng--math