Explore Courses Blog Tutorials Interview Questions
0 votes
in Blockchain by (12.7k points)

I have written this 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;




done() function returns 50000000000000000000

fail() function returns 49999999999999998000

1 Answer

0 votes
by (29.5k points)
edited by

 The issue is 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.

Know the blockchain better through Blockchain Certification by Intellipaat.

Browse Categories