or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

array-operations.mdcollection-types.mderror-types.mdindex.mditerator-helpers.mdmath-utilities.mdobject-methods.mdpromise-utilities.mdreflection-api.mdstring-processing.mdsymbol-management.mdtypedarray-operations.mdweb-apis.md
tile.json

math-utilities.mddocs/

Math Utilities

Extended Math object with modern mathematical functions including hyperbolic functions, logarithms, trigonometry, and precision utilities.

Capabilities

Hyperbolic Functions

Hyperbolic trigonometric functions and their inverses for advanced mathematical calculations.

/**
 * Math static methods for hyperbolic functions
 */
interface Math {
  /**
   * Returns the inverse hyperbolic cosine of x
   * @param x - Number >= 1
   */
  acosh(x: number): number;
  
  /**
   * Returns the inverse hyperbolic sine of x
   * @param x - Any number
   */
  asinh(x: number): number;
  
  /**
   * Returns the inverse hyperbolic tangent of x
   * @param x - Number between -1 and 1
   */
  atanh(x: number): number;
  
  /**
   * Returns the hyperbolic cosine of x
   * @param x - Any number
   */
  cosh(x: number): number;
  
  /**
   * Returns the hyperbolic sine of x
   * @param x - Any number
   */
  sinh(x: number): number;
  
  /**
   * Returns the hyperbolic tangent of x
   * @param x - Any number
   */
  tanh(x: number): number;
}

Usage Examples:

import Math from 'core-js-pure/stable/math';

// Hyperbolic functions
console.log(Math.sinh(1)); // ≈ 1.1752
console.log(Math.cosh(1)); // ≈ 1.5431
console.log(Math.tanh(1)); // ≈ 0.7616

// Inverse hyperbolic functions
console.log(Math.asinh(1.1752)); // ≈ 1
console.log(Math.acosh(1.5431)); // ≈ 1
console.log(Math.atanh(0.7616)); // ≈ 1

// Practical applications
const calculateCatenaryHeight = (a, x) => {
  // Catenary curve: y = a * cosh(x/a)
  return a * Math.cosh(x / a);
};

// Calculate velocity in special relativity
const relativisticVelocity = (u, v, c = 299792458) => {
  // Velocity addition formula using hyperbolic tangent
  const rapidityU = Math.atanh(u / c);
  const rapidityV = Math.atanh(v / c);
  return c * Math.tanh(rapidityU + rapidityV);
};

Logarithmic Functions

Enhanced logarithmic functions for various bases and precision calculations.

interface Math {
  /**
   * Returns the natural logarithm (base e) of 1 + x
   * More accurate for values of x close to 0
   * @param x - Any number > -1
   */
  log1p(x: number): number;
  
  /**
   * Returns the base-2 logarithm of x
   * @param x - Number > 0
   */
  log2(x: number): number;
  
  /**
   * Returns the base-10 logarithm of x
   * @param x - Number > 0
   */
  log10(x: number): number;
}

Usage Examples:

import Math from 'core-js-pure/stable/math';

// Logarithmic functions
console.log(Math.log2(8)); // 3
console.log(Math.log10(1000)); // 3
console.log(Math.log1p(0.5)); // ≈ 0.4055 (more accurate than Math.log(1.5))

// Information theory calculations
const calculateEntropy = (probabilities) => {
  return -probabilities
    .filter(p => p > 0)
    .reduce((entropy, p) => entropy + p * Math.log2(p), 0);
};

const probs = [0.5, 0.25, 0.25];
console.log(calculateEntropy(probs)); // ≈ 1.5 bits

// Decibel calculations
const powerToDecibels = (power, reference = 1) => {
  return 10 * Math.log10(power / reference);
};

const voltageToDecibels = (voltage, reference = 1) => {
  return 20 * Math.log10(voltage / reference);
};

console.log(powerToDecibels(100)); // 20 dB
console.log(voltageToDecibels(10)); // 20 dB

Exponential and Root Functions

Advanced exponential and root calculations with improved precision.

interface Math {
  /**
   * Returns e^x - 1
   * More accurate for values of x close to 0
   * @param x - Any number
   */
  expm1(x: number): number;
  
  /**
   * Returns the cube root of x
   * @param x - Any number
   */
  cbrt(x: number): number;
  
  /**
   * Returns the square root of the sum of squares of arguments
   * Euclidean norm
   * @param values - Numbers to calculate norm of
   */
  hypot(...values: number[]): number;
}

Usage Examples:

import Math from 'core-js-pure/stable/math';

// Root and exponential functions
console.log(Math.cbrt(27)); // 3
console.log(Math.cbrt(-8)); // -2
console.log(Math.expm1(1)); // ≈ 1.7183 (e - 1)

// Euclidean distance calculation
const distance2D = (x1, y1, x2, y2) => {
  return Math.hypot(x2 - x1, y2 - y1);
};

const distance3D = (x1, y1, z1, x2, y2, z2) => {
  return Math.hypot(x2 - x1, y2 - y1, z2 - z1);
};

console.log(distance2D(0, 0, 3, 4)); // 5
console.log(distance3D(0, 0, 0, 1, 1, 1)); // ≈ 1.732

// Vector magnitude
const vectorMagnitude = (...components) => Math.hypot(...components);

const velocity = [3, 4, 5]; // m/s in x, y, z directions
console.log(vectorMagnitude(...velocity)); // ≈ 7.071 m/s

// Financial compound interest with continuous compounding
const continuousCompound = (principal, rate, time) => {
  return principal * Math.expm1(rate * time) + principal;
};

Precision and Bit Functions

Functions for bit manipulation, precision control, and integer operations.

interface Math {
  /**
   * Returns the number of leading zero bits in 32-bit representation
   * @param x - 32-bit integer
   */
  clz32(x: number): number;
  
  /**
   * Returns the result of 32-bit multiplication of a and b
   * @param a - 32-bit integer
   * @param b - 32-bit integer
   */
  imul(a: number, b: number): number;
  
  /**
   * Returns x rounded to single precision float
   * @param x - Any number
   */
  fround(x: number): number;
  
  /**
   * Returns x rounded to half precision float (16-bit)
   * @param x - Any number
   */
  f16round(x: number): number;
  
  /**
   * Returns the sign of x (-1, 0, or 1)
   * @param x - Any number
   */
  sign(x: number): number;
  
  /**
   * Returns the integer part of x (truncates towards zero)
   * @param x - Any number
   */
  trunc(x: number): number;
  
  /**
   * Calculates precise sum of array of numbers
   * @param items - Array of numbers to sum
   */
  sumPrecise(items: number[]): number;
}

Usage Examples:

import Math from 'core-js-pure/stable/math';

// Bit manipulation functions
console.log(Math.clz32(1)); // 31 (31 leading zeros in binary)
console.log(Math.clz32(8)); // 28 (28 leading zeros)

// 32-bit integer multiplication (prevents overflow issues)
console.log(Math.imul(0x7fffffff, 2)); // -2 (32-bit overflow)
console.log(0x7fffffff * 2); // 4294967294 (JavaScript number)

// Precision control
console.log(Math.fround(1.337)); // 1.3370000123977661 (32-bit float precision)
console.log(Math.f16round(1.337)); // 1.3369140625 (16-bit float precision)

// Sign and truncation
console.log(Math.sign(-42)); // -1
console.log(Math.sign(0)); // 0
console.log(Math.sign(42)); // 1

console.log(Math.trunc(42.95)); // 42
console.log(Math.trunc(-42.95)); // -42

// Precise summation (avoids floating point errors)
const numbers = [0.1, 0.2, 0.3];
console.log(numbers.reduce((a, b) => a + b)); // 0.6000000000000001
console.log(Math.sumPrecise(numbers)); // 0.6

// Binary operations utility
const countSetBits = (n) => {
  // Count number of 1s in binary representation
  let count = 0;
  while (n > 0) {
    count += 32 - Math.clz32(n & -n);
    n &= n - 1;
  }
  return count;
};

console.log(countSetBits(7)); // 3 (binary: 111)
console.log(countSetBits(8)); // 1 (binary: 1000)

Common Patterns

Scientific Calculations

Mathematical computations for scientific applications.

import Math from 'core-js-pure/stable/math';

// Physical constants and calculations
const CONSTANTS = {
  SPEED_OF_LIGHT: 299792458, // m/s
  PLANCK_CONSTANT: 6.62607015e-34, // J⋅Hz⁻¹
  BOLTZMANN: 1.380649e-23 // J⋅K⁻¹
};

// Blackbody radiation (Wien's displacement law)
const wienDisplacement = (temperature) => {
  const WIEN_CONSTANT = 2.897771955e-3; // m⋅K
  return WIEN_CONSTANT / temperature;
};

// Relativistic energy
const relativisticEnergy = (mass, velocity) => {
  const c = CONSTANTS.SPEED_OF_LIGHT;
  const gamma = 1 / Math.sqrt(1 - Math.pow(velocity / c, 2));
  return gamma * mass * c * c;
};

// Exponential decay
const exponentialDecay = (initialAmount, decayConstant, time) => {
  return initialAmount * Math.exp(-decayConstant * time);
};

// Half-life calculation
const halfLife = (decayConstant) => {
  return Math.log(2) / decayConstant;
};

Financial Mathematics

Financial calculations with improved precision and mathematical functions.

import Math from 'core-js-pure/stable/math';

// Compound interest with various compounding frequencies
const compoundInterest = (principal, rate, periods, time) => {
  if (periods === Infinity) {
    // Continuous compounding
    return principal * Math.exp(rate * time);
  }
  return principal * Math.pow(1 + rate / periods, periods * time);
};

// Present value calculations
const presentValue = (futureValue, rate, periods) => {
  return futureValue / Math.pow(1 + rate, periods);
};

// Annuity calculations
const annuityPresentValue = (payment, rate, periods) => {
  if (rate === 0) return payment * periods;
  return payment * (1 - Math.pow(1 + rate, -periods)) / rate;
};

// Black-Scholes option pricing (simplified)
const blackScholes = (S, K, T, r, sigma, optionType = 'call') => {
  const d1 = (Math.log(S / K) + (r + 0.5 * sigma * sigma) * T) / 
             (sigma * Math.sqrt(T));
  const d2 = d1 - sigma * Math.sqrt(T);
  
  const normalCDF = (x) => {
    return 0.5 * (1 + Math.sign(x) * Math.sqrt(1 - Math.exp(-2 * x * x / Math.PI)));
  };
  
  if (optionType === 'call') {
    return S * normalCDF(d1) - K * Math.exp(-r * T) * normalCDF(d2);
  } else {
    return K * Math.exp(-r * T) * normalCDF(-d2) - S * normalCDF(-d1);
  }
};

Signal Processing

Mathematical functions for digital signal processing applications.

import Math from 'core-js-pure/stable/math';

// Discrete Fourier Transform (simplified)
const dft = (signal) => {
  const N = signal.length;
  const result = [];
  
  for (let k = 0; k < N; k++) {
    let real = 0, imag = 0;
    
    for (let n = 0; n < N; n++) {
      const angle = -2 * Math.PI * k * n / N;
      real += signal[n] * Math.cos(angle);
      imag += signal[n] * Math.sin(angle);
    }
    
    result.push({
      real,
      imag,
      magnitude: Math.hypot(real, imag),
      phase: Math.atan2(imag, real)
    });
  }
  
  return result;
};

// Window functions for signal processing
const windows = {
  hanning: (n, N) => 0.5 * (1 - Math.cos(2 * Math.PI * n / (N - 1))),
  hamming: (n, N) => 0.54 - 0.46 * Math.cos(2 * Math.PI * n / (N - 1)),
  blackman: (n, N) => {
    const a0 = 0.42659, a1 = 0.49656, a2 = 0.076849;
    return a0 - a1 * Math.cos(2 * Math.PI * n / (N - 1)) + 
           a2 * Math.cos(4 * Math.PI * n / (N - 1));
  }
};

// Apply window function to signal
const applyWindow = (signal, windowType = 'hanning') => {
  const windowFn = windows[windowType];
  const N = signal.length;
  
  return signal.map((sample, n) => sample * windowFn(n, N));
};

// Calculate signal power and energy
const signalPower = (signal) => {
  const sumSquares = Math.sumPrecise(signal.map(x => x * x));
  return sumSquares / signal.length;
};

const signalEnergy = (signal) => {
  return Math.sumPrecise(signal.map(x => x * x));
};

Geometric Calculations

Advanced geometric computations using mathematical functions.

import Math from 'core-js-pure/stable/math';

// Spherical coordinates conversion
const sphericalToCartesian = (r, theta, phi) => {
  return {
    x: r * Math.sin(phi) * Math.cos(theta),
    y: r * Math.sin(phi) * Math.sin(theta),
    z: r * Math.cos(phi)
  };
};

const cartesianToSpherical = (x, y, z) => {
  const r = Math.hypot(x, y, z);
  const theta = Math.atan2(y, x);
  const phi = Math.acos(z / r);
  
  return { r, theta, phi };
};

// Area and volume calculations
const sphereVolume = (radius) => (4 / 3) * Math.PI * Math.pow(radius, 3);
const sphereSurfaceArea = (radius) => 4 * Math.PI * Math.pow(radius, 2);

const ellipsoidVolume = (a, b, c) => (4 / 3) * Math.PI * a * b * c;

// Triangle calculations
const triangleArea = (a, b, c) => {
  // Heron's formula
  const s = (a + b + c) / 2;
  return Math.sqrt(s * (s - a) * (s - b) * (s - c));
};

const triangleAngles = (a, b, c) => {
  // Law of cosines
  const angleA = Math.acos((b * b + c * c - a * a) / (2 * b * c));
  const angleB = Math.acos((a * a + c * c - b * b) / (2 * a * c));
  const angleC = Math.PI - angleA - angleB;
  
  return { A: angleA, B: angleB, C: angleC };
};