Solidity Design Patterns: Multiply before Dividing

Do your multiplication before division!

console.log((30 * 100 * 13) / 13)
< 3000
console.log((30 / 13) * 100 * 13)
< 2999.9999999999995

Not always sufficient!

uint256 numerator = 30 * 100 * 13;
uint256 denominator = 13;

Markus Waas — soliditydeveloper.com

Written by

Lead smart contract developer at Starbase and Juriproject. Passionate about the revolution in finances. And sport. And healthy food.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade