Uint128x128Math
Author: Trader Joe
Helper contract used for power and log calculations
State Variables
LOG_SCALE_OFFSET
uint256 constant LOG_SCALE_OFFSET = 127;
LOG_SCALE
uint256 constant LOG_SCALE = 1 << LOG_SCALE_OFFSET;
LOG_SCALE_SQUARED
uint256 constant LOG_SCALE_SQUARED = LOG_SCALE * LOG_SCALE;
Functions
log2
Calculates the binary logarithm of x.
*Based on the iterative approximation algorithm. https://en.wikipedia.org/wiki/Binary_logarithm#Iterative_approximation Requirements:
x must be greater than zero. Caveats:
The results are not perfectly accurate to the last decimal, due to the lossy precision of the iterative approximation Also because x is converted to an unsigned 129.127-binary fixed-point number during the operation to optimize the multiplication*
function log2(uint256 x) internal pure returns (int256 result);
Parameters
x
uint256
The unsigned 128.128-binary fixed-point number for which to calculate the binary logarithm.
Returns
result
int256
The binary logarithm as a signed 128.128-binary fixed-point number.
pow
Returns the value of x^y. It calculates 1 / x^abs(y)
if x is bigger than 2^128.
At the end of the operations, we invert the result if needed.
function pow(uint256 x, int256 y) internal pure returns (uint256 result);
Parameters
x
uint256
The unsigned 128.128-binary fixed-point number for which to calculate the power
y
int256
A relative number without any decimals, needs to be between ]-2^21; 2^21[
Errors
Uint128x128Math__LogUnderflow
error Uint128x128Math__LogUnderflow();
Uint128x128Math__PowUnderflow
error Uint128x128Math__PowUnderflow(uint256 x, int256 y);
Last updated