High, I need to test my arbitrary precision calculator, and bc
seems like a nice yardstick to compare to, however, bc
does truncate the result of each multiplication to what seems to be the maximum scale of the involved operands each.
Is there a quick way to turn this off or to automatically set the scale of each multiplication to the sum of scales of the factors so that it doesn't lose any precision?
If you have a more elegant solution to this involving something other than bc
, I would appreciate your sharing it.
Example:
$ bc <<< '1.5 * 1.5'
2.2
The real answer is 2.25.
Best Answer
You can control the scale that
bc
outputs with thescale=<#>
argument.Using your example:
You can also use the
-l
switch (thanks to @manatwork) which will initialize the scale to 20 instead of the default of 0. For example:You can read more about
scale
in thebc
man page.