Math

Git Source

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

NameTypeDescription
_xuint256Unsigned integer
_yint256Signed integer

Returns

NameTypeDescription
_adduint256Unsigned 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

NameTypeDescription
_xuint256Unsigned integer
_yint256Signed integer

Returns

NameTypeDescription
_subuint256Unsigned 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

NameTypeDescription
_xuint256Unsigned integer
_yuint256Unsigned integer

Returns

NameTypeDescription
_subint256Signed 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

NameTypeDescription
_xuint256Unsigned integer
_yint256Signed integer

Returns

NameTypeDescription
_mulint256Signed 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

NameTypeDescription
_xuint256Unsigned RAY integer
_yuint256Unsigned RAY integer

Returns

NameTypeDescription
_rmuluint256Unsigned 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

NameTypeDescription
_xuint256Unsigned RAY integer
_yint256Signed RAY integer

Returns

NameTypeDescription
_rmulint256Signed 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

NameTypeDescription
_xuint256Unsigned WAD integer
_yuint256Unsigned WAD integer

Returns

NameTypeDescription
_wmuluint256Unsigned 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

NameTypeDescription
_xuint256Unsigned WAD integer
_yint256Signed WAD integer

Returns

NameTypeDescription
_wmulint256Signed 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

NameTypeDescription
_xint256Signed WAD integer
_yint256Signed WAD integer

Returns

NameTypeDescription
_wmulint256Signed 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

NameTypeDescription
_xuint256Unsigned RAY integer
_yuint256Unsigned RAY integer

Returns

NameTypeDescription
_rdivuint256Unsigned 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

NameTypeDescription
_xint256Signed RAY integer
_yint256Signed RAY integer

Returns

NameTypeDescription
_rdivint256Signed 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

NameTypeDescription
_xuint256Unsigned WAD integer
_yuint256Unsigned WAD integer

Returns

NameTypeDescription
_wdivuint256Unsigned 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

NameTypeDescription
_xuint256Unsigned RAY integer
_nuint256Unsigned integer exponent

Returns

NameTypeDescription
_rpowuint256Unsigned _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

NameTypeDescription
_xuint256Unsigned integer
_yuint256Unsigned integer

Returns

NameTypeDescription
_maxuint256Unsigned 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

NameTypeDescription
_xuint256Unsigned integer
_yuint256Unsigned integer

Returns

NameTypeDescription
_minuint256Unsigned 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

NameTypeDescription
_xuint256Unsigned integer

Returns

NameTypeDescription
_intint256Signed integer

riemannSum

Calculates the Riemann sum of two signed integers

function riemannSum(int256 _x, int256 _y) internal pure returns (int256 _riemannSum);

Parameters

NameTypeDescription
_xint256Signed integer
_yint256Signed integer

Returns

NameTypeDescription
_riemannSumint256Riemann sum of _x and _y

absolute

Calculates the absolute value of a signed integer

function absolute(int256 _x) internal pure returns (uint256 _z);

Parameters

NameTypeDescription
_xint256Signed integer

Returns

NameTypeDescription
_zuint256Unsigned absolute value of _x

Errors

IntOverflow

Throws when trying to cast a uint256 to an int256 that overflows

error IntOverflow();