vmrob
vmrob

Reputation: 3046

Why doesn't bc show the scale for the result of this expression?

This expression, I think, should return 0.000, not 0.

echo "scale = 3 ; 0 / 2" | bc

Using echo "scale = 3 ; scale(0 / 2)" | bc outputs 3, so it's obvious that the scale is applies, but it's not displaying the output properly.

What's going on?

Upvotes: 2

Views: 132

Answers (1)

Jean-François Savard
Jean-François Savard

Reputation: 21004

When dividing 0 by something, the scale won't be considered as it is sure there will never be any decimal values.

You're right, your scale applies, for example try with echo "scale = 3; 1/2" | bc and the output will be .500

Also, if you use a scale only to avoid truncating, you can use the -l options which will allow 20 decimal digits.

This also prove that when dividing 0 by something, the decimal are ignored as when you type :

echo "0/2" | bc - l

the output will be 0

echo "1/2" | bc - l

the output will be .50000000000000000000

I hope I made it a little more clear.

Upvotes: 3

Related Questions