Skip to main content

Math

Git Source

Author: Axicon Labs Limited

Contains general math helpers and functions

State Variables

MAX_UINT256

This is equivalent to type(uint256).max — used in assembly blocks as a replacement.

uint256 internal constant MAX_UINT256 = 2 ** 256 - 1;

MAX_UINT128

This is equivalent to type(uint128).max — used in assembly blocks as a replacement.

uint256 internal constant MAX_UINT128 = 2 ** 128 - 1;

Functions

min24

Compute the min of the incoming int24s a and b.

function min24(int24 a, int24 b) internal pure returns (int24);

Parameters

NameTypeDescription
aint24The first number
bint24The second number

Returns

NameTypeDescription
<none>int24The min of a and b: min(a, b), e.g.: min(4, 1) = 1

max24

Compute the max of the incoming int24s a and b.

function max24(int24 a, int24 b) internal pure returns (int24);

Parameters

NameTypeDescription
aint24The first number
bint24The second number

Returns

NameTypeDescription
<none>int24The max of a and b: max(a, b), e.g.: max(4, 1) = 4

min

Compute the min of the incoming a and b.

function min(uint256 a, uint256 b) internal pure returns (uint256);

Parameters

NameTypeDescription
auint256The first number
buint256The second number

Returns

NameTypeDescription
<none>uint256The min of a and b: min(a, b), e.g.: min(4, 1) = 1

min

Compute the min of the incoming a and b.

function min(int256 a, int256 b) internal pure returns (int256);

Parameters

NameTypeDescription
aint256The first number
bint256The second number

Returns

NameTypeDescription
<none>int256The min of a and b: min(a, b), e.g.: min(4, 1) = 1

max

Compute the max of the incoming a and b.

function max(uint256 a, uint256 b) internal pure returns (uint256);

Parameters

NameTypeDescription
auint256The first number
buint256The second number

Returns

NameTypeDescription
<none>uint256The max of a and b: max(a, b), e.g.: max(4, 1) = 4

max

Compute the max of the incoming a and b.

function max(int256 a, int256 b) internal pure returns (int256);

Parameters

NameTypeDescription
aint256The first number
bint256The second number

Returns

NameTypeDescription
<none>int256The max of a and b: max(a, b), e.g.: max(4, 1) = 4

abs

Compute the absolute value of an integer (int256).

Does not support type(int256).min and will revert (type(int256).max = abs(type(int256).min) - 1).

function abs(int256 x) internal pure returns (int256);

Parameters

NameTypeDescription
xint256The incoming signed integer to take the absolute value of

Returns

NameTypeDescription
<none>int256The absolute value of x, e.g. abs(-4) = 4

absUint

Compute the absolute value of an integer (int256).

Supports type(int256).min because the corresponding value can fit in a uint (unlike type(int256).max).

function absUint(int256 x) internal pure returns (uint256);

Parameters

NameTypeDescription
xint256The incoming signed integer to take the absolute value of

Returns

NameTypeDescription
<none>uint256The absolute value of x, e.g. abs(-4) = 4

mostSignificantNibble

Returns the index of the most significant nibble of the 160-bit number, where the least significant nibble is at index 0 and the most significant nibble is at index 39.

function mostSignificantNibble(uint160 x) internal pure returns (uint256 r);

Parameters

NameTypeDescription
xuint160The value for which to compute the most significant nibble

Returns

NameTypeDescription
ruint256The index of the most significant nibble (default: 0)

getApproxTickWithMaxAmount

Computes a tick that will require approximately amount of currency0 to create a tickSpacing-wide position with maxLiquidityPerTick at tickLower = tick in Uniswap.

This function can have a maximum of two ticks of error from one of the ticks with amount(tickA) < amount < amount(tickA + 1 = tickB).

`tickSpacing is assumed to be within the range (0, 32768)

maxLiquidityPerTick for s=tickSpacing should be defined by (2^128 - 1) / ((887272/s) - (-887272/s) + 1)

function getApproxTickWithMaxAmount(uint256 amount, int24 tickSpacing, uint256 maxLiquidityPerTick)
internal
pure
returns (int24);

Parameters

NameTypeDescription
amountuint256The desired amount of currency0 required to fill the returned tick
tickSpacingint24The spacing between initializable ticks in the Uniswap pool
maxLiquidityPerTickuint256The maximum liquidity that can reference any given tick in the Uniswap pool

Returns

NameTypeDescription
<none>int24A tick that will require approximately amount of currency0 to create a tickSpacing-wide position with maxLiquidityPerTick at tickLower = tick

getMaxLiquidityPerTick

Computes the maximum liquidity that is allowed to reference any given tick in a Uniswap V4 pool with tickSpacing.

function getMaxLiquidityPerTick(int24 tickSpacing) internal pure returns (uint128 maxLiquidityPerTick);

Parameters

NameTypeDescription
tickSpacingint24The spacing between initializable ticks in the Uniswap V4 pool

Returns

NameTypeDescription
maxLiquidityPerTickuint128The maximum liquidity that can reference any given tick in the Uniswap V4 pool

getSqrtRatioAtTick

Calculates 1.0001^(tick/2) as an X96 number.

Will revert if abs(tick) > 887272.

function getSqrtRatioAtTick(int24 tick) internal pure returns (uint160);

Parameters

NameTypeDescription
tickint24Value of the tick for which sqrt(1.0001^tick) is calculated

Returns

NameTypeDescription
<none>uint160A Q64.96 number representing the sqrt price at the provided tick

log_Sqrt1p0001

Approximates the log base sqrt(1.0001) of argX128/2^128 with precision bits of precision.

Validated for argX128 in the range [18447437466114719744, 6276865796315986613307619852238232712866172378830163935232).

Validated for precision in the range [0, 20].

function log_Sqrt1p0001(uint256 argX128, uint256 precision) internal pure returns (int256);

Parameters

NameTypeDescription
argX128uint256The Q128.128 fixed-point number to calculate the log of
precisionuint256The bits of precision with which to compute the result, max 64 (err <≈ 2^-precision * log₂(√1.0001)⁻¹)

Returns

NameTypeDescription
<none>int256The log with base sqrt(1.0001) of argX128/2^128

getAmount0ForLiquidity

Calculates the amount of currency0 received for a given LiquidityChunk.

function getAmount0ForLiquidity(LiquidityChunk liquidityChunk) internal pure returns (uint256);

Parameters

NameTypeDescription
liquidityChunkLiquidityChunkA specification for a liquidity chunk in Uniswap containing liquidity, tickLower, and tickUpper

Returns

NameTypeDescription
<none>uint256The amount of currency0 represented by liquidityChunk when currentTick < tickLower

getAmount1ForLiquidity

Calculates the amount of currency1 received for a given LiquidityChunk.

function getAmount1ForLiquidity(LiquidityChunk liquidityChunk) internal pure returns (uint256);

Parameters

NameTypeDescription
liquidityChunkLiquidityChunkA specification for a liquidity chunk in Uniswap containing liquidity, tickLower, and tickUpper

Returns

NameTypeDescription
<none>uint256The amount of currency1 represented by liquidityChunk when currentTick > tickUpper

getAmountsForLiquidity

Calculates the amount of currency0 and currency1 received for a given LiquidityChunk at the provided currentTick.

function getAmountsForLiquidity(int24 currentTick, LiquidityChunk liquidityChunk)
internal
pure
returns (uint256 amount0, uint256 amount1);

Parameters

NameTypeDescription
currentTickint24The tick at which to evaluate liquidityChunk
liquidityChunkLiquidityChunkA specification for a liquidity chunk in Uniswap containing liquidity, tickLower, and tickUpper

Returns

NameTypeDescription
amount0uint256The amount of currency0 represented by liquidityChunk at currentTick
amount1uint256The amount of currency1 represented by liquidityChunk at currentTick

getLiquidityForAmount0

Returns a LiquidityChunk at the provided tick range with liquidity corresponding to amount0.

function getLiquidityForAmount0(int24 tickLower, int24 tickUpper, uint256 amount0)
internal
pure
returns (LiquidityChunk);

Parameters

NameTypeDescription
tickLowerint24The lower tick of the chunk
tickUpperint24The upper tick of the chunk
amount0uint256The amount of currency0

Returns

NameTypeDescription
<none>LiquidityChunkA LiquidityChunk with tickLower, tickUpper, and the calculated amount of liquidity for amount0

getLiquidityForAmount1

Returns a LiquidityChunk at the provided tick range with liquidity corresponding to amount1.

function getLiquidityForAmount1(int24 tickLower, int24 tickUpper, uint256 amount1)
internal
pure
returns (LiquidityChunk);

Parameters

NameTypeDescription
tickLowerint24The lower tick of the chunk
tickUpperint24The upper tick of the chunk
amount1uint256The amount of currency1

Returns

NameTypeDescription
<none>LiquidityChunkA LiquidityChunk with tickLower, tickUpper, and the calculated amount of liquidity for amount1

toUint128

Downcast uint256 to uint128. Revert on overflow or underflow.

function toUint128(uint256 toDowncast) internal pure returns (uint128 downcastedInt);

Parameters

NameTypeDescription
toDowncastuint256The uint256 to be downcasted

Returns

NameTypeDescription
downcastedIntuint128toDowncast downcasted to uint128

toUint128Capped

Downcast uint256 to uint128, but cap at type(uint128).max on overflow.

function toUint128Capped(uint256 toDowncast) internal pure returns (uint128 downcastedInt);

Parameters

NameTypeDescription
toDowncastuint256The uint256 to be downcasted

Returns

NameTypeDescription
downcastedIntuint128toDowncast downcasted to uint128

toInt128

Downcast uint128 to int128.

function toInt128(uint128 toCast) internal pure returns (int128 downcastedInt);

Parameters

NameTypeDescription
toCastuint128The uint256 to be downcasted

Returns

NameTypeDescription
downcastedIntint128toDowncast downcasted to int128

toInt128

Cast an int256 to an int128, revert on overflow or underflow.

function toInt128(int256 toCast) internal pure returns (int128 downcastedInt);

Parameters

NameTypeDescription
toCastint256the int256 to be downcasted

Returns

NameTypeDescription
downcastedIntint128toCast downcasted to int128

toInt256

Cast a uint256 to an int256, revert on overflow.

function toInt256(uint256 toCast) internal pure returns (int256);

Parameters

NameTypeDescription
toCastuint256The uint256 to be downcasted

Returns

NameTypeDescription
<none>int256toCast downcasted to int256

mulDiv

Calculates floor(a×b÷denominator) with full precision. Throws if result overflows a uint256 or denominator == 0.

Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldiv for this and all following mulDiv functions

function mulDiv(uint256 a, uint256 b, uint256 denominator) internal pure returns (uint256 result);

Parameters

NameTypeDescription
auint256The multiplicand
buint256The multiplier
denominatoruint256The divisor

Returns

NameTypeDescription
resultuint256The 256-bit result

mulDivCapped

Calculates min(floor(a×b÷denominator), 2^256-1) with full precision.

function mulDivCapped(uint256 a, uint256 b, uint256 denominator) internal pure returns (uint256 result);

Parameters

NameTypeDescription
auint256The multiplicand
buint256The multiplier
denominatoruint256The divisor

Returns

NameTypeDescription
resultuint256The 256-bit result

mulDivRoundingUp

Calculates ceil(a×b÷denominator) with full precision. Throws if result overflows a uint256 or denominator == 0.

function mulDivRoundingUp(uint256 a, uint256 b, uint256 denominator) internal pure returns (uint256 result);

Parameters

NameTypeDescription
auint256The multiplicand
buint256The multiplier
denominatoruint256The divisor

Returns

NameTypeDescription
resultuint256The 256-bit result

mulDiv64

Calculates floor(a×b÷2^64) with full precision. Throws if result overflows a uint256.

function mulDiv64(uint256 a, uint256 b) internal pure returns (uint256);

Parameters

NameTypeDescription
auint256The multiplicand
buint256The multiplier

Returns

NameTypeDescription
<none>uint256The 256-bit result

mulDiv96

Calculates floor(a×b÷2^96) with full precision. Throws if result overflows a uint256.

function mulDiv96(uint256 a, uint256 b) internal pure returns (uint256);

Parameters

NameTypeDescription
auint256The multiplicand
buint256The multiplier

Returns

NameTypeDescription
<none>uint256The 256-bit result

mulDiv96RoundingUp

Calculates ceil(a×b÷2^96) with full precision. Throws if result overflows a uint256.

function mulDiv96RoundingUp(uint256 a, uint256 b) internal pure returns (uint256 result);

Parameters

NameTypeDescription
auint256The multiplicand
buint256The multiplier

Returns

NameTypeDescription
resultuint256The 256-bit result

mulDiv128

Calculates floor(a×b÷2^128) with full precision. Throws if result overflows a uint256.

function mulDiv128(uint256 a, uint256 b) internal pure returns (uint256);

Parameters

NameTypeDescription
auint256The multiplicand
buint256The multiplier

Returns

NameTypeDescription
<none>uint256The 256-bit result

mulDiv128RoundingUp

Calculates ceil(a×b÷2^128) with full precision. Throws if result overflows a uint256.

function mulDiv128RoundingUp(uint256 a, uint256 b) internal pure returns (uint256 result);

Parameters

NameTypeDescription
auint256The multiplicand
buint256The multiplier

Returns

NameTypeDescription
resultuint256The 256-bit result

mulDiv192

Calculates floor(a×b÷2^192) with full precision. Throws if result overflows a uint256.

function mulDiv192(uint256 a, uint256 b) internal pure returns (uint256);

Parameters

NameTypeDescription
auint256The multiplicand
buint256The multiplier

Returns

NameTypeDescription
<none>uint256The 256-bit result

mulDiv192RoundingUp

Calculates ceil(a×b÷2^192) with full precision.

function mulDiv192RoundingUp(uint256 a, uint256 b) internal pure returns (uint256 result);

Parameters

NameTypeDescription
auint256The multiplicand
buint256The multiplier

Returns

NameTypeDescription
resultuint256The 256-bit result

unsafeDivRoundingUp

Calculates ceil(a÷b), returning 0 if b == 0.

function unsafeDivRoundingUp(uint256 a, uint256 b) internal pure returns (uint256 result);

Parameters

NameTypeDescription
auint256The numerator
buint256The denominator

Returns

NameTypeDescription
resultuint256The 256-bit result

quickSort

QuickSort is a sorting algorithm that employs the Divide and Conquer strategy. It selects a pivot element and arranges the given array around this pivot by correctly positioning it within the sorted array.

function quickSort(int256[] memory arr, int256 left, int256 right) internal pure;

Parameters

NameTypeDescription
arrint256[]The elements that must be sorted
leftint256The starting index
rightint256The ending index

sort

Calls quickSort with default starting index of 0 and ending index of the last element in the array.

function sort(int256[] memory data) internal pure returns (int256[] memory);

Parameters

NameTypeDescription
dataint256[]The elements that must be sorted

Returns

NameTypeDescription
<none>int256[]The sorted array