Background
In TickMath.sol
of v3-core codebase, getSqrtRatioAtTick(int24 tick)
implements \sqrt{1.0001}^n
with binary fixed point arithmetics in Q128 and down casts to Q96.
With a lookup table of Q128 entries \sqrt{1.0001^-1}^{0b1}
, \sqrt{1.0001^-1}^{0b10}
, ...
, the input tick is split into several terms multiplied together. The negative exponent is chosen to avoid overflow and the reciprocal is taken in the end for positive inputs.
My Question
I have trouble reproducing the exact constants of \sqrt{1/1.0001}^n
defined in the contract. What I've tried is to precompute in IEEE-754 doubles and convert them back to Q128 in python, as in:
>>> int(1/math.sqrt(1.0001) * 2**128)
340265354078545014477014422800776036352
>>> 0xfffcb933bd6fad37aa2d162d1a594001
340265354078544963557816517032075149313
As you can see the result deviates the hexadecimal value 0xfffcb933bd6fad37aa2d162d1a594001
provided in TickMath.sol
for n=1
. My guess is it's due to precision loss and there must be some numerical methods to derive those exact values.
Hope to get some guidances here. Thank you.
getSqrtRatioAtTick
in the Panoptic repository that apparently has accurate value, in the comments: github.com/panoptic-labs/panoptic-v1-core/blob/main/contracts/…