linuxbasharithmetic-expressionsexpr

floating point operations in Linux using expr utility


Not sure, but I'm unable to get this.

38/1024 = 0.0371

When i'm performing: echo "scale=2; 15 / 0.0371" |bc, it gives me result:

404.31

But, when i', performing : echo 'scale=2; 15/(38/1024)' |bc , the output is:

500.00

Why there's difference in results.

I need the output to be 404.31 from the second command.

Thanks.


Solution

  • This is because your floating point precision is different in second case.

    In first case you are using 0.0371 for division whereas in second case you are using 38/1024 with precision for 2 decimal points which gives 0.03.

    So essentially your actual command in second case is

    echo "scale=2; 15 / 0.03" |bc
    

    hence different output.

    If you want the same output as first use scale as 4 like

    echo "scale=4; 15/(38/1024)" |bc