Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

@jukanntenn

Description

@jukanntenn
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions