The avg() function in Statistics library performs integer division which truncates decimal values, losing precision critical for financial calculations.
This precision loss becomes critical when exact averages are needed for protocol decisions, price calculations, or token distributions.
https://github.com/Cyfrin/2024-10-swan-dria/blob/main/contracts/libraries/Statistics.sol#L8
In Solidity, division between integers always truncates the decimal part. This means any fractional results are rounded down to the nearest integer.
Basic Case
Could lead to systematic undervaluation in calculations
Accumulated errors in repeated calculations
Particularly problematic in high-precision financial applications
Scale Before Division
Return Both Parts
The choice of solution depends on the specific use case and precision requirements. The scaling solution is more commonly used in DeFi applications where maintaining precision is critical.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.