0

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.

User123
  • 1,498
  • 2
  • 12
  • 26
  • 3
    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``` – Amit Bhardwaj Oct 15 '18 at 07:57
  • Thanks, didn't realized the concept of `scale` here..Many Thanks :-) – User123 Oct 15 '18 at 08:01

1 Answers1

2

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
Amit Bhardwaj
  • 333
  • 1
  • 8
  • You don't get the same output, you must use echo "scale=4;a=15/(38/1024);scale=2;a/1"|bc or with dc echo '4k15 38 1024/2k/p'|dc -f - – ctac_ Oct 15 '18 at 10:10