BigMathVault
this is an optimized version mainly created by taking Fluid vault's codebase into consideration so it's use is limited for other cases.
MAX_MASK_DEBT_FACTOR
uint256 MAX_MASK_DEBT_FACTOR
PRECISION
uint256 PRECISION
TWO_POWER_64
uint256 TWO_POWER_64
TWO_POWER_69_MINUS_1
uint256 TWO_POWER_69_MINUS_1
mulDivNormal
function mulDivNormal(uint256 normal, uint256 bigNumber1, uint256 bigNumber2) internal pure returns (uint256)
multiplies a normal
number with a bigNumber1
and then divides by bigNumber2
.
For vault's use case MUST always: - bigNumbers have exponent size 15 bits - bigNumbers have coefficient size 35 bits and have 35th bit always 1 (when exponent > 0 BigMath numbers have max precision)
so coefficients must always be in range 17179869184 <= coefficient <= 34359738367. - bigNumber1 (debt factor) always have exponent >= 1 & <= 16384 - bigNumber2 (connection factor) always have exponent >= 1 & <= 32767 (15 bits) - bigNumber2 always >= bigNumber1 (connection factor can never be < base branch debt factor) - as a result of previous points, numbers must never be 0 - normal is positionRawDebt and is always within 10000 and type(int128).max
Return Values
Name | Type | Description |
---|---|---|
0 | uint256 | normal * bigNumber1 / bigNumber2 |
mulDivBigNumber
function mulDivBigNumber(uint256 bigNumber, uint256 number1) internal pure returns (uint256 result)
multiplies a bigNumber
with normal number1
and then divides by TWO_POWER_64
.
For vault's use case (calculating new branch debt factor after liquidation): - number1 is debtFactor, intialized as TWO_POWER_64 and reduced from there, hence it's always <= TWO_POWER_64 and always > 0. - bigNumber is branch debt factor, which starts as ((X35 << 15) | (1 << 14)) and reduces from there. - bigNumber must have have exponent size 15 bits and be >= 1 & <= 16384 - bigNumber must have coefficient size 35 bits and have 35th bit always 1 (when exponent > 0 BigMath numbers have max precision)
so coefficients must always be in range 17179869184 <= coefficient <= 34359738367.
Parameters
| Name | Type | Description | | --------- | ------- | -------------- | --------- | | bigNumber | uint256 | Coefficient | Exponent. | | number1 | uint256 | normal number. |
Return Values
Name | Type | Description |
---|---|---|
result | uint256 | bigNumber * number1 / TWO_POWER_64. |
mulBigNumber
function mulBigNumber(uint256 bigNumber1, uint256 bigNumber2) internal pure returns (uint256)
multiplies a bigNumber1
with another bigNumber2
.
For vault's use case (calculating connection factor of merged branches userTickDebtFactor * connectionDebtFactor *... connectionDebtFactor): - bigNumbers must have have exponent size 15 bits and be >= 1 & <= 32767 - bigNumber must have coefficient size 35 bits and have 35th bit always 1 (when exponent > 0 BigMath numbers have max precision)
so coefficients must always be in range 17179869184 <= coefficient <= 34359738367.
sum of exponents from bigNumber1
bigNumber2
should be > 16384.
e.g. res = bigNumber1 * bigNumber2 = (coe1, exp1) * (coe2, exp2) >> decimal
= (coe1*coe2>>overflow, exp1+exp2+overflow-decimal)
Parameters
Name | Type | Description |
---|---|---|
bigNumber1 | uint256 | BigNumber format with coefficient and exponent. |
bigNumber2 | uint256 | BigNumber format with coefficient and exponent. |
Return Values
Name | Type | Description |
---|---|---|
0 | uint256 | BigNumber format with coefficient and exponent |
divBigNumber
function divBigNumber(uint256 bigNumber1, uint256 bigNumber2) internal pure returns (uint256)
divides a bigNumber1
by bigNumber2
.
For vault's use case (calculating connectionFactor = baseBranchDebtFactor / currentBranchDebtFactor) bigNumbers MUST always: - have exponent size 15 bits and be >= 1 & <= 16384 - have coefficient size 35 bits and have 35th bit always 1 (when exponent > 0 BigMath numbers have max precision)
so coefficients must always be in range 17179869184 <= coefficient <= 34359738367. - as a result of previous points, numbers must never be 0
e.g. res = bigNumber1 / bigNumber2 = (coe1, exp1) / (coe2, exp2) << decimal
= ((coe1<<precision*)/coe2, exp1+decimal-exp2-precision*)_
Parameters
Name | Type | Description |
---|---|---|
bigNumber1 | uint256 | BigNumber format with coefficient and exponent |
bigNumber2 | uint256 | BigNumber format with coefficient and exponent |
Return Values
Name | Type | Description |
---|---|---|
0 | uint256 | BigNumber format with coefficient and exponent Returned connection factor can only ever be >= baseBranchDebtFactor (c = x*100/y with both x,y > 0 & x,y <= 100: c can only ever be >= x) |