Super User is a question and answer site for computer enthusiasts and power users. 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

I'm just starting with Gnu's bc and I'm stuck at the very beginning (very discouraging...). I want to divide two numbers and get a float as result:

bc 1.06.94
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 


The man page says, that division returns a number with the same scale as the initial values. Obviously this is either not true or I'm missing something.

Googling hasn't brought up any new insights (besides that 'BC' can also stand for 'British Columbia').

Do you see my error? Better yet, do you know any good references/tutorials to bc?

share|improve this question
up vote 6 down vote accepted

You can set the scale with


Then division works as expected:


To quote from Wikipedia:

All numbers and variable contents are arbitrary precision numbers whose precision (in decimal places) is determined by the global scale variable.

share|improve this answer
Ah, that is the poodle's core. Can I set the scale globally in some .bcrc file? – Boldewyn Dec 18 '09 at 12:26
Found it: export BC_ENV_ARGS=~/.bcrc – Boldewyn Dec 18 '09 at 12:30
I doubt »Das also war des Pudels Kern [ein fahrender Scholast]« works reliably in other languages :-) At least I don't know how well-known Faust is in other languages :-) – Joey Dec 18 '09 at 19:08
This was the poodle's real core, \\ A travelling scholar, then? The casus is diverting. Project Gutenberg ist dein Freund: – Boldewyn Dec 21 '09 at 17:13
But I must agree, and suggests 'the gist of the matter' ;-) – Boldewyn Dec 21 '09 at 17:14

Use bc -l, which preloads the math library and the default scale is set to 20.

$ bc -l
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
share|improve this answer

In case this helps anyone, I was trying to get scale=2 and quiet mode set by default and I couldn't seem to get it right.

If I set BC_ENV_VARS="-q", I got quiet mode and if I set BC_ENV_VARS="~/.bcrc", I could set scale=2 in this file but I couldn't turn on quiet mode (maybe there is a way but I couldn't find it).

In the end I used export BC_ENV_ARGS=~/.bcrc (with scale=2) and aliased bc to bc -q.

share|improve this answer

To expand on Colin_Daly, use BC_ENV_ARGS="-q $HOME/.bcrc", with scale=INT in .bcrc obviously. The environment variable is basically just stuck right after the bc command.

What's happening is that -q is just a normal command line option, and $HOME/.bcrc is expanded and fed into bc as the first file argument (there can be multiple file arguments). scale=INT is a line of bc, which is a programming language. It's functionally equivalent to running bc and entering scale=INT on the bc prompt. You have to use $HOME and not ~ because ~ is only recognized when it's not quoted (see here), but we need some sort of quoting so the variable is set to both words, not just up to the space.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .