People are talking about manually setting the "scale" variable in bc,
and using small values like 3 for it.

Please note that this the scale variable is NOT just used for rounding of
final results for display.  It's used for internal precision of storage
of ALL intermediate results.  Using a small scale can and will distort
your results.

I learned this the hard way.

Need an example?  OK.  Let's calculate 1.4 to the power of 50.5.

$ bc -l
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
1.4^50.5
Runtime warning (func=(main), adr=12): non-zero scale in exponent
20248916.23976437135118764865

As the warning indicates, you can't actually use a non-integer exponent
with the ^ operator in bc -l.  But you can rewrite it with a logarithm
and an exponent.  So let's do that.

e(50.5*l(1.4))
23958840.79914949898175198920

OK, that's too many digits, so now let's do the same thing with scale=3:

scale=3
e(50.5*l(1.4))
23394230.731

The result is substantially different.  This is because the scale is
used internally, NOT just on the final result.

If you use bc, always just use "bc -l" which sets a scale of 20, and
leave it set that way.  If you need a prettier result, take the result
from bc -l and use printf to round it.  Or use a programming language
that has floating point arithmetic support.

Reply via email to