Blog

Precision Loss Errors in Solidity

Precision loss errors in Solidity are a common issue when dealing with floating-point arithmetic. Precision loss errors occur when arithmetic operations, especially division, result in fractional values that cannot be accurately represented using integer types. This is because Solidity does not support floating-point arithmetic, which can lead to significant issues in financial calculations, token distributions, and other applications requiring high precision. For example, Repeated operations, such as interest calculations or iterative algorithms, can accumulate rounding errors, leading to significant precision loss over time.

“Solidity supports various integer types, but all are limited to whole numbers. When performing operations that result in fractional values, Solidity truncates the decimal portion, leading to precision loss.”

Everyone says: "Solidity rounds down", well, if we are precisely accurate it would be called "Solidity truncates". And the dangerous thing is that such a truncation only occurs if there is a remainder, so it does not necessarily happen everytime.

Consider a scenario where you need to divide two integers but retain the precision of the result:




In the above example, if a = 5 and b = 2, the result will be 2, losing the 0.5 fraction.

These precision loss errors are sometimes exploitable, sometimes not.

Often it is just about a few wei and therefore looping interactions does economically not make sense due to high gas fees. However, if the divisor can be scaled, such in the known "Vault Inflation Attack", then this is a completely different scenario.