Variations of integer divisions

In Solidity:

➜ int256(5) / int256(-2)
Type: int256
├ Hex: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
├ Hex (full word): 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe
└ Decimal: -2
➜ 

In Python:

>>> 5 // -2
-3

In Haskell:

λ 5 `div` (-2)
-3
λ 5 `quot` (-2)
-2

One variant (solidity’s /, Haskell’s quot) is truncating towards 0, the other is truncating towards negative infinity (python’s /``, Haskell's div`).

Does it matter? In the wild, there was a case in which Uniswap v3 used a version that is close to // in Python or div in Haskell, and it made a difference in application behaviour. See: Negative Tick Rounding raises price of asset · Issue #65 · code-423n4/2024-05-predy-findings · GitHub

Additionally, for numbers that are not integer-like, e.g., fixed-point decimal numbers that Solidity doesn’t fully support, the definition becomes even more critical. That was the reason Python has a separate operator // instead of /.

Question: should solidity provide an additional primitive for “quotient” integer operation?

1 Like