1
  function div(uint256 a, uint256 b) internal pure returns (uint256) {
    uint256 c = a / b;
    return c;
  }

When I call the function above as library in my solidity code 107752139522000.div(1099511627776) I receive 97, when the actual result is 98

Here is an example of the same problem in eth fiddle (deploy and call the function div): https://ethfiddle.com/EL2MbHI8h_

3
  • 1
    The actual result is 97.99999999995634...
    – Ismael
    Commented Oct 1, 2019 at 22:30
  • how did you determine that is the actual result ? Commented Oct 1, 2019 at 22:30
  • I used python, but any calculator that support large integers should do fine.
    – Ismael
    Commented Oct 1, 2019 at 22:31

1 Answer 1

3

This is expected. Solidity does integer math. This means results are truncated (not rounded). 107752139522000/1099511627776 is a little less than 98, so the result is 97.

2
  • so would simply adding +1 compensate for this, what are the limitations of this approach ? Commented Oct 1, 2019 at 22:33
  • 1
    What exactly do you want to happen? It's hard to make suggestions if we don't know what you're trying to do.
    – user19510
    Commented Oct 2, 2019 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.