Class SafeMath

This class provides utility functions for basic arithmetic operations on unsigned real positive integers. These functions perform overflow and underflow checks to prevent unwanted behavior when dealing with unsigned 64-bit integers (u64). The SafeMath class should be used when working with arithmetic operations that require increased safety and precision.

Remarks

The SafeMath class is designed to be a drop-in replacement for standard arithmetic operations on unsigned 64-bit integers. By using the methods provided by this class, developers can avoid potential overflow and underflow issues and ensure that their code behaves correctly even in edge cases.

Hierarchy

  • SafeMath

Constructors

Methods

Constructors

Methods

  • Safely adds two unsigned 64-bit integers (u64), reverting if an overflow occurs.

    Parameters

    • a: number

      The first operand for addition.

    • b: number

      The second operand for addition.

    Returns number

    The sum of a and b as an unsigned 64-bit integer (u64).

    Remarks

    This function is used to safely add two unsigned 64-bit integers without causing an overflow.

    Throws

    if the operation results in a number bigger than u64.MAX_VALUE.

  • Safely divides two unsigned 64-bit integers (u64), reverting on division by zero. The result is rounded towards zero.

    Parameters

    • a: number

      The dividend.

    • b: number

      The divisor.

    Returns number

    The quotient of a divided by b as an unsigned 64-bit integer (u64).

    Remarks

    This function is used to safely divide two unsigned 64-bit integers without causing a division by zero error. However, due to the rounding towards zero, there might be a loss of precision in the result, especially when the dividend is not perfectly divisible by the divisor. For example, when dividing 4 by 3, the result will be 1, and the remainder (1) will be lost.

    Throws

    if the operation results in a division by zero.

  • Safely multiplies two unsigned 64-bit integers (u64), reverting on overflow.

    Parameters

    • a: number

      The first operand for multiplication.

    • b: number

      The second operand for multiplication.

    Returns number

    The product of a and b as an unsigned 64-bit integer (u64).

    Remarks

    This function is used to safely mutilply two unsigned 64-bit integers without causing an overflow.

    Throws

    if the operation results in a number bigger than u64.MAX_VALUE.

  • Safely subtracts two unsigned 64-bit integers (u64), reverting if an underflow occurs.

    Parameters

    • a: number

      The first operand for subtraction (minuend).

    • b: number

      The second operand for subtraction (subtrahend).

    Returns number

    The difference between a and b as an unsigned 64-bit integer (u64).

    Remarks

    This function is used to safely substract two unsigned 64-bit integers without causing an underflow.

    Throws

    if the operation results in a number lower than 0.

Generated using TypeDoc