Precision Loss

Cumulative inaccuracy caused by integer arithmetic and rounding in smart contract calculations.

Precision Loss refers to the cumulative degradation of numerical accuracy that occurs when smart contracts perform mathematical operations using integer arithmetic. Since the EVM does not support floating-point numbers, all calculations use fixed-point integer math where division operations truncate remainders. While individual precision losses are often negligible, they can compound over repeated operations — particularly in moving average calculations, interest accruals, and AMM invariant computations — creating exploitable vulnerabilities.

Why precision loss occurs

Solidity uses integer division, which always rounds toward zero:

1// Expected: 10 / 3 = 3.333...
2// Actual: 10 / 3 = 3 (remainder discarded)
3uint256 result = 10 / 3; // Returns 3, losing 0.333...

For a single operation, losing 0.333 wei is inconsequential. But in a loop calculating a moving average over many periods, or in compound interest calculations executed every block, these small errors accumulate.

Impact on moving averages

Moving average calculations are particularly susceptible:

1// SMA of [100, 101, 102] with 3 periods
2// Expected: (100 + 101 + 102) / 3 = 101.0
3// Actual: 303 / 3 = 101 ✓ (exact in this case)
4
5// SMA of [100, 101, 103] with 3 periods
6// Expected: (100 + 101 + 103) / 3 = 101.333...
7// Actual: 304 / 3 = 101 (lost 0.333...)

Over thousands of updates, this accumulated precision loss can drift the moving average away from its true value, potentially causing incorrect interest rate adjustments, faulty oracle prices, or invalid liquidation triggers.

Mitigation strategies

  • Scale up before dividing: Multiply by a precision factor (e.g., 1e18) before division to preserve more significant digits.
  • Round in the protocol's favor: Use mulDown for amounts going to users, mulUp for amounts coming from users.
  • Enforce minimum values: Prevent calculations at microscopic scales where rounding errors dominate.
  • Use higher-precision libraries: Libraries like PRBMath or ABDKMath provide 59.18 or 64.64 fixed-point arithmetic with better precision guarantees.

Difference from rounding error

Precision loss and rounding error are closely related but distinct. Rounding error is the inaccuracy in a single operation; precision loss is the cumulative effect of many rounding errors compounding over time. An attacker might exploit precision loss by deliberately triggering thousands of operations where each rounding error favors them.

Understanding precision loss is essential for auditing any protocol that performs iterative calculations, especially those involving moving averages, compound interest, or token price computations.

Need expert guidance on Precision Loss?

Our team at Zealynx has deep expertise in blockchain security and DeFi protocols. Whether you need an audit or consultation, we're here to help.

Get a Quote

oog
zealynx

Subscribe to Our Newsletter

Stay updated with our latest security insights and blog posts

© 2024 Zealynx