Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
Closely related to Float operation with bc?. –  manatwork Jun 20 '13 at 8:37

2 Answers 2

up vote 2 down vote accepted

You can control the scale that bc outputs with the scale=<#> argument.

$ echo "scale=10; 5.1234 * 5.5678" | bc
28.52606652

$ echo "scale=5; 5.1234 * 5.5678" | bc
28.52606

Using your example:

$ bc <<< 'scale=2; 1.5 * 1.5'
2.25

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:

$ bc -l <<< '1.5 * 1.5'
2.25

$ bc -l <<< '1.52 * 1.52'
2.3104

You can read more about scale in the bc man page.

share|improve this answer
2  
Or shorter: bc -l. “ -l (The letter ell.) Define the math functions and initialize scale to 20, instead of the default zero” – bc specification –  manatwork Jun 20 '13 at 8:33
    
@manatwork - thanks, didn't notice that switch. –  slm Jun 20 '13 at 8:37
    
Thanks. I guess I could set the scale to 2147483647 (which happens to be equal to INT_MAX), which bc tells me is the maximum scale value I can set (I'm am goint that far in my tests), but I'll guess I'll only test it with integers and give bc the performance benefits that come from using dynamic-length numbers. –  PSkocik Jun 20 '13 at 11:10

Try this :

$ bc
scale=10
1.5 * 1.5
2.25

Search bc's man for scale variable

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.