Clamp price before calculating inventory
Let s be the square root of current price clamped between the lower and upper bounds. Let a and b be the square roots of those lower and upper bounds. Token A equals L × (b − s) ÷ (s × b). Token B equals L × (s − a). Below the range, the position is entirely A; above the range, it is entirely B. Require positive prices and upper greater than lower.
Inspect an in-range worked example
Consider hypothetical liquidity L = 100, a lower price of 1, an upper price of 4, and current price of 2.25 B per A. The square roots are 1, 2, and 1.5. Token A is 100 × 0.5 ÷ 3, or approximately 16.6667. Token B is 100 × 0.5, or 50. Valuing A at 2.25 B gives total inventory value of 87.5 B before fees.
Use normalized units and a consistent liquidity scale
The liquidity input is an abstract quantity calibrated to the human-price formulas. Raw contract liquidity and square-root prices use different numeric scales and may require token-decimal conversions before comparison. The formulas describe inventory only; separately accrued fees are excluded. Scaling L by two doubles both token balances without changing their proportions. At either boundary one token balance becomes zero, but the position still has value in the remaining token.