LFJ Developer Docs
  • Liquidity Book
  • Introduction
  • LB V2.2 Key Changes
  • Guides
    • Swap Tokens
    • Add/Remove Liquidity
    • Tracking Volume
    • Tracking Pool Balances
    • Finding The Best Quote
    • Byte32 Decoding
    • Price From Bin Id
    • Bin Id From Price
    • Finding Liquidity Depth
    • User Balances
  • Concepts
    • Concentrated Liquidity
    • Bin Math
    • Bin Liquidity
    • Swaps
    • Fees
    • Oracle
  • Contracts
    • Interfaces
      • ILBLegacyFactory
      • ILBLegacyToken
      • ILBLegacyPair
      • ILBLegacyRouter
      • ILBFlashLoanCallback
      • IPendingOwnable
      • IJoeFactory
      • IJoePair
      • IJoeRouter01
      • IJoeRouter02
      • IWNATIVE
      • ILBFactory
      • ILBHooks
      • ILBPair
      • ILBRouter
      • ILBToken
    • Libraries
      • Math
        • BitMath
        • Encoded
        • LiquidityConfigurations
        • PackedUint128Math
        • SafeCast
        • SampleMath
        • TreeMath
        • Uint128x128Math
        • Uint256x256Math
      • BinHelper
      • Clone
      • Constants
      • FeeHelper
      • Hooks
      • ImmutableClone
      • JoeLibrary
      • OracleHelper
      • PairParameterHelper
      • PriceHelper
      • ReentrancyGuardUpgradeable
      • TokenHelper
    • LBBaseHooks
    • LBFactory
    • LBPair
    • LBQuoter
    • LBRouter
    • LBToken
  • Deployment Addresses
    • Avalanche C-Chain
    • Fuji Testnet
    • Arbitrum One
    • Binance Smart Chain
    • Binance Smart Chain Testnet
    • Ethereum Mainnet
    • Monad Testnet
  • SDK
    • Introduction
    • Making a Trade
    • Adding Liquidity
    • Removing Liquidity
  • Audits
  • AMM
    • Joe V1 Contracts
    • Joe V1 Audits
  • LFJ DEX API
    • Dex Analytics
    • Pools
    • Rewards
    • User
    • User Lifetime Stats
    • Vaults
    • Models
  • LFJ Aggregator API
    • Default
    • Models
Powered by GitBook
On this page
  • State Variables
  • LOG_SCALE_OFFSET
  • LOG_SCALE
  • LOG_SCALE_SQUARED
  • Functions
  • log2
  • pow
  • Errors
  • Uint128x128Math__LogUnderflow
  • Uint128x128Math__PowUnderflow
  1. Contracts
  2. Libraries
  3. Math

Uint128x128Math

PreviousTreeMathNextUint256x256Math

Last updated 8 days ago

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

Name
Type
Description

x

uint256

The unsigned 128.128-binary fixed-point number for which to calculate the binary logarithm.

Returns

Name
Type
Description

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

Name
Type
Description

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);
Git Source