Class Math

Summary

Additional static members for the native Math class.

Description

Does not extend the native Math class.

Constructors

Methods

  • Average two numbers, with a weight favoring the 2nd number.

    Parameters

    • x: number

      1st finite number

    • y: number

      2nd finite number

    • w: number = 0.5

      weight of 2nd number; between 0–1

    Returns number

    exactly interpolateArithmetic(x, y, w)

    Deprecated

    This method is an alias of xjs_Math.interpolateArithmetic.

  • Return the argument, clamped between two bounds.

    This method returns the argument unchanged iff it is loosely between min and max; it returns min iff the argument is strictly less than min; and max iff the argument is strictly greater than max. If min === max then this method returns that value. If min > max then this method switches the bounds.

    Parameters

    • min: number

      the lower bound

    • val: number

      the value to clamp between the bounds

    • max: number

      the upper bound

    Returns number

    Math.min(Math.max(min, x), max)

  • xjs_Math.clamp, but for bigint types.

    Parameters

    • min: bigint

      the lower bound

    • val: bigint

      the value to clamp between the bounds

    • max: bigint

      the upper bound

    Returns bigint

    the clamped value

  • Linearlly interpolate between, or extrapolate from, two numbers.

    If the argument p is within the interval [0, 1], the result is an interpolation within the interval [a, b], such that p == 0 produces a and p == 1 produces b.

    For example, interpolateArithmetic(10, 20, 0.7) will return 17, while interpolateArithmetic(20, 10, 0.7) will return 13 (the same result as interpolateArithmetic(10, 20, 1 - 0.7)).

    If p is outside [0, 1], the result is an extrapolation outside the range of [a, b]. For example, interpolateArithmetic(10, 20, 1.3) will return 23, and interpolateArithmetic(10, 20, -0.3) will return 7.

    p defaults to 0.5, thus yielding an even average, that is, the arithmetic mean, of the two numbers.

    Parameters

    • a: number

      1st number

    • b: number

      2nd number

    • p: number = 0.5

      the interpolation/extrapolation parameter

    Returns number

    exactly a * (1 - p) + (b * p)

    See

    https://www.desmos.com/calculator/tfuwejqtav

    Throws

    if a, b, or p is not a finite number

    Throws

    if an argument is NaN

  • Exponentially interpolate between, or extrapolate from, two numbers.

    If the argument p is within the interval [0, 1], the result is an interpolation within the interval [a, b], such that p == 0 produces a and p == 1 produces b.

    For example, interpolateGeometric(10, 20, 0.7) will return ~16.245, while interpolateGeometric(20, 10, 0.7) will return ~12.311 (the same result as interpolateGeometric(10, 20, 1 - 0.7)).

    If p is outside [0, 1], the result is an extrapolation outside the range of [a, b]. For example, interpolateGeometric(10, 20, 1.3) will return ~24.623, and interpolateGeometric(10, 20, -0.3) will return ~8.123.

    p defaults to 0.5, thus yielding an even average, that is, the geometric mean, of the two numbers.

    Parameters

    • a: number

      1st number

    • b: number

      2nd number

    • p: number = 0.5

      the interpolation/extrapolation parameter

    Returns number

    exactly a ** (1 - p) * b ** p

    See

    https://www.desmos.com/calculator/tfuwejqtav

    Throws

    if a, b, or p is not a finite number

    Throws

    if an argument is NaN

  • Rationally interpolate between, or extrapolate from, two numbers.

    If the argument p is within the interval [0, 1], the result is an interpolation within the interval [a, b], such that p == 0 produces a and p == 1 produces b.

    For example, interpolateHarmonic(10, 20, 0.7) will return ~15.385, while interpolateHarmonic(20, 10, 0.7) will return ~11.765 (the same result as interpolateHarmonic(10, 20, 1 - 0.7)).

    If p is outside [0, 1], the result is an extrapolation outside the range of [a, b]. For example, interpolateHarmonic(10, 20, 1.3) will return ~28.571, and interpolateHarmonic(10, 20, -0.3) will return ~8.696.

    p defaults to 0.5, thus yielding an even average, that is, the harmonic mean, of the two numbers.

    Parameters

    • a: number

      1st number

    • b: number

      2nd number

    • p: number = 0.5

      the interpolation/extrapolation parameter

    Returns number

    exactly 1 / interpolateArithmetic(1/a, 1/b, p)

    See

    https://www.desmos.com/calculator/tfuwejqtav

    Throws

    if a, b, or p is not a finite number

    Throws

    if an argument is NaN

  • Tests whether a given value is within a certain range, inclusive.

    Parameters

    • val: number

      the value to test

    • min: number

      the minimum

    • max: number

      the maximum

    Returns boolean

    is val equal to or between min and max?

  • Math.max, but for bigint types.

    Parameters

    • Rest ...ints: bigint[]

      the bigint arguments

    Returns bigint

    the maximum argument

    Throws

    if no arguments are supplied

  • Return the arithmetic mean of a set of numbers.

    meanArithmetic(a,b)   == (a + b)     / 2
    meanArithmetic(a,b,c) == (a + b + c) / 3

    Parameters

    • Rest ...nums: number[]

      finite numbers to average

    Returns number

    the arithmetic mean of the given numbers

    Throws

    if one of the numbers is not finite

    Throws

    if one of the numbers is NaN

  • Return the geomeric mean of a set of numbers.

    meanGeometric(a,b)   == (a * b)     ** (1/2)
    meanGeometric(a,b,c) == (a * b * c) ** (1/3)

    Parameters

    • Rest ...nums: number[]

      finite numbers to average

    Returns number

    the geometric mean of the given numbers

    Throws

    if one of the numbers is not finite

    Throws

    if one of the numbers is NaN

  • Return the harmonic mean of a set of numbers.

    meanHarmonic(a,b)   == 1 / ((1/a + 1/b)       / 2)
    meanHarmonic(a,b,c) == 1 / ((1/a + 1/b + 1/c) / 3)

    Parameters

    • Rest ...nums: number[]

      finite numbers to average

    Returns number

    the harmonic mean of the given numbers

    Throws

    if one of the numbers is not finite

    Throws

    if one of the numbers is NaN

  • Math.min, but for bigint types.

    Parameters

    • Rest ...ints: bigint[]

      the bigint arguments

    Returns bigint

    the minimum argument

    Throws

    if no arguments are supplied

  • Return the remainder of Euclidean division of x by n.

    This method returns x % n when x is positive, but returns a positive result when x is negative. The divisor n must be positive.

    Parameters

    • x: number

      the dividend

    • n: number | bigint

      the divisor, a positive integer

    Returns number

    exactly ((x % n) + n) % n

    Throws

    if n is not a positive integer

  • Return the nth tetration of x.

    Tetration is considered the next hyperoperation after exponentiation (which follows multiplication, following addition). For example, tetrate(5, 3) returns the result of 5 ** 5 ** 5: repeated exponentiation. (Note that with ambiguous grouping, a ** b ** c is equal to a ** (b ** c).) If there were a native JavaScript operator for tetration, it might be a triple-asterisk: 5 *** 3.

    Currently, there is only support for non-negative integer hyperexponents. Negative numbers and non-integers are not yet allowed.

    tetrate(5, 3) // returns 5 ** 5 ** 5 // equal to 5 ** (5 ** 5)
    tetrate(5, 1) // returns 5
    tetrate(5, 0) // returns 1

    Parameters

    • x: number

      the root, any number

    • n: number

      the hyper-exponent to which the root is raised, a non-negative integer

    Returns number

    informally, x *** n

    Throws

    if n is not a non-negative integer