Skip to main content

Rounding Errors

What it detects​

This detector identifies potential loss of precision due to Solidity's use of integer arithmetic, particularly in division and modulo operations. Because Solidity does not support floating-point numbers, any division involving integers will truncate the result toward zero, silently discarding any remainder.

Rounding errors can lead to subtle and compounding issues in contracts that involve:

  • Percentage-based calculations
  • Pro-rata distribution of funds
  • Interest, reward, or staking logic
  • Token swaps and automated market makers (AMMs)

These errors often go unnoticed during testing but can significantly affect fairness, financial accuracy, or protocol stability over time.

Typical symptoms​

  • Calculations yield smaller (rounded-down) results than expected
  • Division returns zero when small values are divided by larger ones
  • Percentage math results in zero for low inputs (e.g., amount * 5 / 100)
  • Accumulated rounding leads to leftover tokens, fees, or misallocated funds
  • Users receive slightly less than their fair share in distributions

Solidity snippet (v0.8.25)​

pragma solidity ^0.8.25;

contract RoundingDemo {
function divide(uint256 a, uint256 b) external pure returns (uint256) {
// Precision loss if 'a' is not an exact multiple of 'b'
return a / b;
}

function percent(uint256 amount) external pure returns (uint256) {
// Example: returns 0 if amount < 20
return amount * 5 / 100;
}
}

Mitigation Strategies​

  • Perform multiplication before division (e.g., amount * 100 / total)
  • Use fixed-point math libraries (e.g., OpenZeppelin’s SafeMath or ABDKMath64x64)
  • Round up using (a + b - 1) / b where appropriate
  • Track remainders or leftover values and redistribute them later

Why it matters on EVM​

On the EVM, rounding is not just a precision issue, it directly affects value distribution, token economics, and fairness. Since the smallest unit (wei or token decimal) is meaningful in DeFi, losing even a few units repeatedly across thousands of transactions can cause:

  • Protocol fund imbalances
  • Inconsistent user experiences
  • Unfair outcomes in financial logic
  • Exploits involving leftover tokens or fees (e.g., dust sniping)
  • Mispriced swaps and errors in AMM pools

Rounding errors are a fundamental risk in financial smart contracts. Proactively detecting and compensating for them ensures robust, fair, and predictable protocol behavior.