docs icon indicating copy to clipboard operation
docs copied to clipboard

Maybe incorrect code in doc 02-pool-data.md?

Open jukanntenn opened this issue 3 months ago • 0 comments

function tickToWord(tick: number): number {
  let compressed = Math.floor(tick / tickSpacing)
  // May not necessary?
  if (tick < 0 && tick % tickSpacing !== 0) {
    compressed -= 1
  }
  return compressed >> 8
}

const minWord = tickToWord(-887272)
const maxWord = tickToWord(887272)

Why is compressed -= 1 needed here?

According to Solidity’s official documentation, integer division rounds towards zero:

Since the type of the result of an operation is always the type of one of the operands, division on integers always results in an integer. In Solidity, division rounds towards zero. This means that int256(-5) / int256(2) == int256(-2).

The contract code includes an adjustment to round towards negative infinity:

if (tick < 0 && tick % tickSpacing != 0) compressed--; // round towards negative infinity

However, JavaScript’s Math.floor already rounds towards negative infinity by default. For this reason, I believe the compressed -= 1 adjustment in the above code is unnecessary.

I might be misunderstanding this—please correct me if that’s the case.

jukanntenn avatar Nov 07 '25 07:11 jukanntenn