Static
addSafely adds two unsigned 64-bit integers (u64), reverting if an overflow occurs.
The first operand for addition.
The second operand for addition.
The sum of a and b as an unsigned 64-bit integer (u64).
This function is used to safely add two unsigned 64-bit integers without causing an overflow.
if the operation results in a number bigger than u64.MAX_VALUE.
Static
divSafely divides two unsigned 64-bit integers (u64), reverting on division by zero. The result is rounded towards zero.
The dividend.
The divisor.
The quotient of a
divided by b
as an unsigned 64-bit integer (u64).
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.
if the operation results in a division by zero.
Static
mulSafely multiplies two unsigned 64-bit integers (u64), reverting on overflow.
The first operand for multiplication.
The second operand for multiplication.
The product of a and b as an unsigned 64-bit integer (u64).
This function is used to safely mutilply two unsigned 64-bit integers without causing an overflow.
if the operation results in a number bigger than u64.MAX_VALUE.
Static
subSafely subtracts two unsigned 64-bit integers (u64), reverting if an underflow occurs.
The first operand for subtraction (minuend).
The second operand for subtraction (subtrahend).
The difference between a and b as an unsigned 64-bit integer (u64).
This function is used to safely substract two unsigned 64-bit integers without causing an underflow.
if the operation results in a number lower than 0.
Generated using TypeDoc
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.