BigMathMinified
the number is divided into two parts: a coefficient and an exponent. This comes at a cost of losing some precision at the end of the number because the exponent simply fills it with zeroes. This precision is oftentimes negligible and can result in significant gas cost reduction due to storage space reduction. Also note, a valid big number is as follows: if the exponent is > 0, then coefficient last bits should be occupied to have max precision.
roundUp is more like a increase 1, which happens everytime for the same number. roundDown simply sets trailing digits after coefficientSize to zero (floor), only once for the same number.
State Variables
ROUND_DOWN
constants to use for roundUp
input param to increase readability
bool internal constant ROUND_DOWN = false;
ROUND_UP
bool internal constant ROUND_UP = true;
Functions
toBigNumber
converts normal
number to BigNumber with exponent
and coefficient
(or precision).
e.g.:
5035703444687813576399599 (normal) = (coefficient32bits, exponent8bits)40bits
5035703444687813576399599 (decimal) => 10000101010010110100000011111011110010100110100000000011100101001101001101011101111 (binary)
=> 10000101010010110100000011111011000000000000000000000000000000000000000000000000000
^-------------------- 51(exponent) -------------- ^
coefficient = 1000,0101,0100,1011,0100,0000,1111,1011 (2236301563)
exponent = 0011,0011 (51)
bigNumber = 1000,0101,0100,1011,0100,0000,1111,1011,0011,0011 (572493200179)
function toBigNumber(uint256 normal, uint256 coefficientSize, uint256 exponentSize, bool roundUp)
internal
pure
returns (uint256 bigNumber);
Parameters
Name | Type | Description |
---|---|---|
normal | uint256 | number which needs to be converted into Big Number |
coefficientSize | uint256 | at max how many bits of precision there should be (64 = uint64 (64 bits precision)) |
exponentSize | uint256 | at max how many bits of exponent there should be (8 = uint8 (8 bits exponent)) |
roundUp | bool | signals if result should be rounded down or up |
Returns
Name | Type | Description |
---|---|---|
bigNumber | uint256 | converted bigNumber (coefficient << exponent) |
fromBigNumber
get normal
number from bigNumber
, exponentSize
and exponentMask
function fromBigNumber(uint256 bigNumber, uint256 exponentSize, uint256 exponentMask)
internal
pure
returns (uint256 normal);
mostSignificantBit
gets the most significant bit lastBit
of a normal
number (length of given number of binary format).
e.g.
5035703444687813576399599 = 10000101010010110100000011111011110010100110100000000011100101001101001101011101111
lastBit = ^--------------------------------- 83 ----------------------------------------^
function mostSignificantBit(uint256 normal) internal pure returns (uint256 lastBit);