# SafeMath
This is an utility class with a set of static methods to perform mathematical operations without overflow.
# add
static add(x: u64, y: u64): u64
Returns the result of adding one u64 number to another
Throws if:
- If the result overflows the u64 type
Example:
import { SafeMath } from 'proton-tsc' const a = SafeMath.add(<u64>4398046836870, <u64>4398046836871); // Result: 8796093673741 const b = SafeMath.add(<u64>18446744073709551615, <u64>1); // Error: SafeMath Add Overflow
# sub
static sub(x: u64, y: u64): u64
Returns the result of substracting
y
fromx
Throws if:
- If the result is less than zero
Example:
import { SafeMath } from 'proton-tsc' const a = SafeMath.sub(<u64>4398046836871, <u64>4398046836870); // Result: 1 const b = SafeMath.add(<u64>4398046836870, <u64>4398046836871); // Error: SafeMath Sub Overflow
# mul
static mul(x: u64, y: u64): u128
Returns the result of multiplication
x
byy
Throws if:
- If the division of the result to
y
is not equal tox
Example:
import { SafeMath } from 'proton-tsc' const a = SafeMath.mul(<u64>18446744073709551615, <u64>18446744073709551615); // result is 40282366920938463463374607431768211455
- If the division of the result to
# div
static div(x: u64, y: u64): u64
Returns the result of division
x
toy
Throws if:
- If
y
is equal to zero
Example:
import { SafeMath } from 'proton-tsc' const a = SafeMath.div(<u64>4398046836870, <u64>4); // result: 1099511709217 const a = SafeMath.div(<u64>4398046836870, <u64>0); // error: SafeMath Div Overflow
- If
← Transaction ID Name →