Reputation: 15548
I have written this below simple solidity code to calculate some numbers. But, ethereum blockchain gives different outputs on each function. Is there anything wrong on my code or it's ethereum blockchain issue?
My solidity code:
pragma solidity ^0.4.24;
contract Calculate {
uint balance = 50000000000000000000;
function done() public pure returns (uint) {
return (50000000000000000000 / 3000) * 3000;
}
function fail() public view returns (uint) {
return (balance / 3000) * 3000;
}
}
Output:
done() function returns 50000000000000000000
fail() function returns 49999999999999998000
Please checkout live example here: https://ropsten.etherscan.io/address/0xf45a2a66be9835fdc9e1065875808616cb8e752e#readContract
Upvotes: 2
Views: 219
Reputation: 60143
I believe the issue is that in the done
case, the compiler doesn't actually emit code to do the calculation. The calculation is instead done at compile time, and a single constant is put in its place.
The compiler, unlike the EVM at runtime, does support decimal math. E.g. you can write 0.5 ether
despite the EVM having no representation for the number 0.5.
Upvotes: 7