Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


$ bc <<< '1.5 * 1.5'

The real answer is 2.25.

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

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

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

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

Using your example:

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

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'

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

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

share|improve this answer
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
1.5 * 1.5

Search bc's man for scale variable

share|improve this answer

I've created this bash function which won't display trailing zeroes

calc () { MAX_PRECISION=20; bc <<< "scale=$MAX_PRECISION; scale = scale($@); $@"; }

Example: calc 1.25^3 = 1.953125

share|improve this answer

Your Answer


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.