Math
This library contains common math functions
Functions
add
Calculates the sum of an unsigned integer and a signed integer
function add(uint256 _x, int256 _y) internal pure returns (uint256 _add);
Parameters
Name | Type | Description |
---|---|---|
_x | uint256 | Unsigned integer |
_y | int256 | Signed integer |
Returns
Name | Type | Description |
---|---|---|
_add | uint256 | Unsigned sum of _x and _y |
sub
Calculates the substraction of an unsigned integer and a signed integer
function sub(uint256 _x, int256 _y) internal pure returns (uint256 _sub);
Parameters
Name | Type | Description |
---|---|---|
_x | uint256 | Unsigned integer |
_y | int256 | Signed integer |
Returns
Name | Type | Description |
---|---|---|
_sub | uint256 | Unsigned substraction of _x and _y |
sub
Calculates the substraction of two unsigned integers
function sub(uint256 _x, uint256 _y) internal pure returns (int256 _sub);
Parameters
Name | Type | Description |
---|---|---|
_x | uint256 | Unsigned integer |
_y | uint256 | Unsigned integer |
Returns
Name | Type | Description |
---|---|---|
_sub | int256 | Signed substraction of _x and _y |
mul
Calculates the multiplication of an unsigned integer and a signed integer
function mul(uint256 _x, int256 _y) internal pure returns (int256 _mul);
Parameters
Name | Type | Description |
---|---|---|
_x | uint256 | Unsigned integer |
_y | int256 | Signed integer |
Returns
Name | Type | Description |
---|---|---|
_mul | int256 | Signed multiplication of _x and _y |
rmul
Calculates the multiplication of two unsigned RAY integers
function rmul(uint256 _x, uint256 _y) internal pure returns (uint256 _rmul);
Parameters
Name | Type | Description |
---|---|---|
_x | uint256 | Unsigned RAY integer |
_y | uint256 | Unsigned RAY integer |
Returns
Name | Type | Description |
---|---|---|
_rmul | uint256 | Unsigned multiplication of _x and _y in RAY precision |
rmul
Calculates the multiplication of an unsigned and a signed RAY integers
function rmul(uint256 _x, int256 _y) internal pure returns (int256 _rmul);
Parameters
Name | Type | Description |
---|---|---|
_x | uint256 | Unsigned RAY integer |
_y | int256 | Signed RAY integer |
Returns
Name | Type | Description |
---|---|---|
_rmul | int256 | Signed multiplication of _x and _y in RAY precision |
wmul
Calculates the multiplication of two unsigned WAD integers
function wmul(uint256 _x, uint256 _y) internal pure returns (uint256 _wmul);
Parameters
Name | Type | Description |
---|---|---|
_x | uint256 | Unsigned WAD integer |
_y | uint256 | Unsigned WAD integer |
Returns
Name | Type | Description |
---|---|---|
_wmul | uint256 | Unsigned multiplication of _x and _y in WAD precision |
wmul
Calculates the multiplication of an unsigned and a signed WAD integers
function wmul(uint256 _x, int256 _y) internal pure returns (int256 _wmul);
Parameters
Name | Type | Description |
---|---|---|
_x | uint256 | Unsigned WAD integer |
_y | int256 | Signed WAD integer |
Returns
Name | Type | Description |
---|---|---|
_wmul | int256 | Signed multiplication of _x and _y in WAD precision |
wmul
Calculates the multiplication of two signed WAD integers
function wmul(int256 _x, int256 _y) internal pure returns (int256 _wmul);
Parameters
Name | Type | Description |
---|---|---|
_x | int256 | Signed WAD integer |
_y | int256 | Signed WAD integer |
Returns
Name | Type | Description |
---|---|---|
_wmul | int256 | Signed multiplication of _x and _y in WAD precision |
rdiv
Calculates the division of two unsigned RAY integers
function rdiv(uint256 _x, uint256 _y) internal pure returns (uint256 _rdiv);
Parameters
Name | Type | Description |
---|---|---|
_x | uint256 | Unsigned RAY integer |
_y | uint256 | Unsigned RAY integer |
Returns
Name | Type | Description |
---|---|---|
_rdiv | uint256 | Unsigned division of _x by _y in RAY precision |
rdiv
Calculates the division of two signed RAY integers
function rdiv(int256 _x, int256 _y) internal pure returns (int256 _rdiv);
Parameters
Name | Type | Description |
---|---|---|
_x | int256 | Signed RAY integer |
_y | int256 | Signed RAY integer |
Returns
Name | Type | Description |
---|---|---|
_rdiv | int256 | Signed division of _x by _y in RAY precision |
wdiv
Calculates the division of two unsigned WAD integers
function wdiv(uint256 _x, uint256 _y) internal pure returns (uint256 _wdiv);
Parameters
Name | Type | Description |
---|---|---|
_x | uint256 | Unsigned WAD integer |
_y | uint256 | Unsigned WAD integer |
Returns
Name | Type | Description |
---|---|---|
_wdiv | uint256 | Unsigned division of _x by _y in WAD precision |
rpow
Calculates the power of an unsigned RAY integer to an unsigned integer
function rpow(uint256 _x, uint256 _n) internal pure returns (uint256 _rpow);
Parameters
Name | Type | Description |
---|---|---|
_x | uint256 | Unsigned RAY integer |
_n | uint256 | Unsigned integer exponent |
Returns
Name | Type | Description |
---|---|---|
_rpow | uint256 | Unsigned _x to the power of _n in RAY precision |
max
Calculates the maximum of two unsigned integers
function max(uint256 _x, uint256 _y) internal pure returns (uint256 _max);
Parameters
Name | Type | Description |
---|---|---|
_x | uint256 | Unsigned integer |
_y | uint256 | Unsigned integer |
Returns
Name | Type | Description |
---|---|---|
_max | uint256 | Unsigned maximum of _x and _y |
min
Calculates the minimum of two unsigned integers
function min(uint256 _x, uint256 _y) internal pure returns (uint256 _min);
Parameters
Name | Type | Description |
---|---|---|
_x | uint256 | Unsigned integer |
_y | uint256 | Unsigned integer |
Returns
Name | Type | Description |
---|---|---|
_min | uint256 | Unsigned minimum of _x and _y |
toInt
Casts an unsigned integer to a signed integer
Throws if _x
is too large to fit in an int256
function toInt(uint256 _x) internal pure returns (int256 _int);
Parameters
Name | Type | Description |
---|---|---|
_x | uint256 | Unsigned integer |
Returns
Name | Type | Description |
---|---|---|
_int | int256 | Signed integer |
riemannSum
Calculates the Riemann sum of two signed integers
function riemannSum(int256 _x, int256 _y) internal pure returns (int256 _riemannSum);
Parameters
Name | Type | Description |
---|---|---|
_x | int256 | Signed integer |
_y | int256 | Signed integer |
Returns
Name | Type | Description |
---|---|---|
_riemannSum | int256 | Riemann sum of _x and _y |
absolute
Calculates the absolute value of a signed integer
function absolute(int256 _x) internal pure returns (uint256 _z);
Parameters
Name | Type | Description |
---|---|---|
_x | int256 | Signed integer |
Returns
Name | Type | Description |
---|---|---|
_z | uint256 | Unsigned absolute value of _x |
Errors
IntOverflow
Throws when trying to cast a uint256 to an int256 that overflows
error IntOverflow();