Uint256x256Math
Author: Trader Joe
Helper contract used for full precision calculations
Functions
mulDivRoundDown
Calculates floor(x*y/denominator) with full precision The result will be rounded down
*Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv Requirements:
The denominator cannot be zero
The result must fit within uint256 Caveats:
This function does not work with fixed-point numbers*
Parameters
x
uint256
The multiplicand as an uint256
y
uint256
The multiplier as an uint256
denominator
uint256
The divisor as an uint256
Returns
result
uint256
The result as an uint256
mulDivRoundUp
Calculates ceil(x*y/denominator) with full precision The result will be rounded up
*Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv Requirements:
The denominator cannot be zero
The result must fit within uint256 Caveats:
This function does not work with fixed-point numbers*
Parameters
x
uint256
The multiplicand as an uint256
y
uint256
The multiplier as an uint256
denominator
uint256
The divisor as an uint256
Returns
result
uint256
The result as an uint256
mulShiftRoundDown
Calculates floor(x * y / 2**offset) with full precision The result will be rounded down
*Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv Requirements:
The offset needs to be strictly lower than 256
The result must fit within uint256 Caveats:
This function does not work with fixed-point numbers*
Parameters
x
uint256
The multiplicand as an uint256
y
uint256
The multiplier as an uint256
offset
uint8
The offset as an uint256, can't be greater than 256
Returns
result
uint256
The result as an uint256
mulShiftRoundUp
Calculates floor(x * y / 2**offset) with full precision The result will be rounded down
*Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv Requirements:
The offset needs to be strictly lower than 256
The result must fit within uint256 Caveats:
This function does not work with fixed-point numbers*
Parameters
x
uint256
The multiplicand as an uint256
y
uint256
The multiplier as an uint256
offset
uint8
The offset as an uint256, can't be greater than 256
Returns
result
uint256
The result as an uint256
shiftDivRoundDown
Calculates floor(x << offset / y) with full precision The result will be rounded down
*Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv Requirements:
The offset needs to be strictly lower than 256
The result must fit within uint256 Caveats:
This function does not work with fixed-point numbers*
Parameters
x
uint256
The multiplicand as an uint256
offset
uint8
The number of bit to shift x as an uint256
denominator
uint256
The divisor as an uint256
Returns
result
uint256
The result as an uint256
shiftDivRoundUp
Calculates ceil(x << offset / y) with full precision The result will be rounded up
*Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv Requirements:
The offset needs to be strictly lower than 256
The result must fit within uint256 Caveats:
This function does not work with fixed-point numbers*
Parameters
x
uint256
The multiplicand as an uint256
offset
uint8
The number of bit to shift x as an uint256
denominator
uint256
The divisor as an uint256
Returns
result
uint256
The result as an uint256
_getMulProds
Helper function to return the result of x * y
as 2 uint256
Parameters
x
uint256
The multiplicand as an uint256
y
uint256
The multiplier as an uint256
Returns
prod0
uint256
The least significant 256 bits of the product
prod1
uint256
The most significant 256 bits of the product
_getEndOfDivRoundDown
Helper function to return the result of x * y / denominator
with full precision
Parameters
x
uint256
The multiplicand as an uint256
y
uint256
The multiplier as an uint256
denominator
uint256
The divisor as an uint256
prod0
uint256
The least significant 256 bits of the product
prod1
uint256
The most significant 256 bits of the product
Returns
result
uint256
The result as an uint256
sqrt
Calculates the square root of x
Credit to OpenZeppelin's Math library under MIT license
Errors
Uint256x256Math__MulShiftOverflow
Uint256x256Math__MulDivOverflow
Last updated