docs
docs copied to clipboard
Maybe incorrect code in doc 02-pool-data.md?
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.