Function sum

  • Computes the sum of a number array.

    Parameters

    • array: number[]

      The array of numbers.

    Returns number

    The sum of all numbers in the array.

    Throws

    If the array length is 0.

    Example

    sum([1, 2, 3]);  // Returns 6
    
  • Computes the sum of a bigint array.

    Parameters

    • array: bigint[]

      The array of bigints.

    Returns bigint

    The sum of all bigints in the array.

    Throws

    If the array length is 0.

    Example

    sum([1n, 2n, 3n]);  // Returns 6n
    
  • Computes the sum of an array of type T using the provided numerical implementation.

    Type Parameters

    • T

      The type of elements in the array.

    Parameters

    • array: T[]

      The array of elements.

    • Optional numerical: Numerical<T>

      (Optional) An instance of the Numerical interface for type T. If not provided, a default numerical implementation will be used based on the type of the array elements. If the array is neither number[] nor bigint[], a numerical instance must be provided.

    Returns T

    The sum of all elements in the array.

    Throws

    If the array length is 0.

    Throws

    If the array type is not number[] or bigint[] and no numerical implementation is provided.

    Example

    sum([1, 2, 3]);                            // Returns 6
    sum([1n, 2n, 3n]); // Returns 6n
    sum([1, 2, 3], new NumericalNumber()); // Returns 6 using NumericalNumber implementation
    sum([1n, 2n, 3n], new NumericalBigInt()); // Returns 6n using NumericalBigInt implementation

Generated using TypeDoc