Basically T ech
ncguisonsiatom
b culalcn
m
eianld- skniL
If like me, you do most of your work from the command-line, using Home Page
Blog Front Page
vim to edit files, mutt for e-mails, cd/ls/mv/find/etc instead of a file
manager, then you may get annoyed by having to fire up a GUI sl iaD tetcatn
C
o
calculator to make (what may sometimes be) a single calculation. rob@basicallytech.com
converted by W eb2PDFConvert.com
a quick challenge for your PC (GNU bc only) Free Publishing Guide
Do you have a manuscript that is
ready to publish? Get started now!
Most of these examples follow a simple formula.
addition ybdew
oreP
$ echo '57+43' | bc
100
subtraction
$ echo '57-43' | bc
14
multiplication
$ echo '57*43' | bc
2451
scale
The scale variable determines the number of digits which follow the
decimal point in your result. By default, the value of the scale
variable is zero. (Unless you use the -l option in which case it
defaults to 20 decimal places. More about -l later.) This can be set
by declaring scale before your calculation, as in the following
division example:
division
$ echo 'scale=25;57/43' | bc
1.3255813953488372093023255
square root
$ echo 'scale=30;sqrt(2)' | bc
1.414213562373095048801688724209
power
$ echo '6^6' | bc
46656
parentheses
If you have read Robert Heinlein's The Number of the Beast, you
may recall that the number of parallel universes in the story equals
(six to the power of six) to the power of six. If you should try to
calculate that like this:
$ echo '6^6^6' | bc
You will get a screen full of numbers (some 37374 digits), not the
10314424798490535546171949056
converted by W eb2PDFConvert.com
exp too big
empty stack
save:args
That's because you typed the wrong question. You need to type:
$ echo '(6^6)^6' | bc
$ echo '7+6*5' | bc
$ echo '6*5+7' | bc
They all give the same answer, 37, but I would have typed the first
calculation, unless of course, I meant:
$ echo '(7+6)*5' | bc
which is 65.
obase and ibase are special variables which define output and input
base.
FF
1100
converted by W eb2PDFConvert.com
Here we're converting the binary number 10 to a base 10 (decimal)
number.
$ echo 'ibase=2;obase=A;10' | bc
Note that the obase is "A" and not "10". Sorry, you've got to learn
some hex. The reason for this is you've set the ibase to "2", so if
you now had tried to use "10" as the value for the obase, it would
stay as "2", because "10" in base 2 is "2". So you need to use hex
to "break out" of binary mode.
Well, that was just to explain the joke; now something a bit more
challenging:
$ echo 'ibase=2;obase=A;10000001' | bc
129
$ echo 'ibase=16;obase=A;FF' | bc
255
Again, note the use of "A" to denote base 10. That is because "10"
in hex (base 16 - the ibase value) is 16.
If you're running GNU bc, you should get the following notice:
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'.
If you wish to get straight to the uninviting blank prompt, use the
-q option, which runs bc in quiet mode, preventing the normal GNU
bc welcome from being printed:
$ bc -q
Using the basics we've been through from the examples above,
enter a calculation:
scale=5
57/43
1.32558
You can use shell variables with bc, which is very useful in shell
scripts:
$ FIVE=5 ; echo "$FIVE^2" | bc
25
converted by W eb2PDFConvert.com
Using bc with files allows complex calculations to be repeated, again
and again, a bit like using a spreadsheet to run the same
calculations on changing figures ... but faster.
scale=2
/* C-style comments
are allowed, as are spaces */
Temperature in Fahrenheit: 61
Equivalent Temperature in Celsius is: 16.11
Note that this example has only been tested with GNU bc. Other
(proprietary) versions of bc may have more stringent syntax
requirements. Some bcs don't allow the use of print or read, for
example, so you have to edit your file before each calculation. Not
very useful.
If you wish to test the comparative speed of your PC, try this
challenge: use bc to calculate Pi to 5000 decimal places. The idea for
this challenge came from a great article at Geekronomicon.
If you really want to tie up your machine for an hour (or more), you
could try the "Pi to 25000 decimal places" challenge from the
aforementioned Geekronomicon.
3.141592653589793238462643383279502884197169399375105820974944592307\
...
...
...
73774418426312986080998886874132604720
real 0m44.164s
user 0m44.099s
sys 0m0.008s
converted by W eb2PDFConvert.com
44.099 seconds! Not bad. I imagine that some Gentoo folks may
be interested to see what difference their compile-time
optimisations make to the speed of bc. FWIW, my distro of choice
is Arch Linux.
useful links
GNU bc manual
kab
ckarscT
Trackback specific URI for this entry
PingBack
Weblog: www.freeminded.org
Tracked: Apr 12, 16:37
Commsent
Display comments as (Linear | Threaded)
flamebaitdetector said,
Monday, November 27. 2006 at 11:43 (Reply)
converted by W eb2PDFConvert.com
mention something about an OS, even as a joke, you're going to
start a flame war.
Rob said,
Monday, November 27. 2006 at 12:02 (Link) (Reply)
Tom said,
Monday, November 27. 2006 at 11:57 (Reply)
real 0m41.555s
user 0m41.275s
sys 0m0.152s
benoit said,
Monday, November 27. 2006 at 12:18 (Link) (Reply)
Hi folks,
bash# find /path -type f -iname '*.txt' -ls | gawk '{print $7 }' |
.... | bc
Benoît
Rob said,
Monday, November 27. 2006 at 12:30 (Link) (Reply)
Hi Benoît,
Regards,
Rob
converted by W eb2PDFConvert.com
$ find /path -type f -iname '*.txt' -ls | gawk
'{sum+=$7}END{print sum}'
Axel said,
Monday, November 27. 2006 at 12:31 (Reply)
How have I missed this??
DES said,
Monday, November 27. 2006 at 14:41 (Link) (Reply)
Rob said,
Monday, November 27. 2006 at 15:05 (Link) (Reply)
Hi Des,
Regards,
Rob
LeLutin said,
Tuesday, November 28. 2006 at 03:53 (Reply)
4 atan( 1 ) is in fact a function using an approximation based
on infinite Taylor series. it is obtained by substituting x=1 in
the infinite series obtained from atan( x )
reference: http://www.ams.org/featurecolumn/archive/pi-
calc.html
Henno said,
Monday, November 27. 2006 at 20:48 (Reply)
converted by W eb2PDFConvert.com
Chris said,
Monday, November 27. 2006 at 18:22 (Reply)
gjohnson said,
Monday, November 27. 2006 at 18:46 (Reply)
Am I missing something or is there a bug?
01010101 bin is 55 hex.
However when I type echo 'ibase=2;obase=F;01010101' | bc the
answer comes back 5A hex.
What is going on? I tried it on Debian Linux, NetBSD and
OpenBSD. Must be operator error, should obase be set to
something else?
nugunga said,
Monday, November 27. 2006 at 19:43 (Link) (Reply)
echo 'ibase=2;obase=F;01010101' | bc
you're using decimal 15 (hex F) as output base.
0x55 = 5*16+5 = 85
(dec) = 5*15+10 = 5A (base 15)
woody said,
Wednesday, November 29. 2006 at 01:34 (Reply)
no tricky at all
Hex said,
Monday, November 27. 2006 at 18:55 (Link) (Reply)
real 4m10.032s
user 2m38.012s
sys 0m1.403s
converted by W eb2PDFConvert.com
Volt said,
Monday, November 27. 2006 at 21:22 (Reply)
Wow. What's up with that? I didn't want to fill the comments
with "benchmark" numbers, but that was surprising.
real 1m36.602s
user 1m19.415s
sys 0m1.057s
The G4s also seem to be a little faster than the G5s for some
reason.
Brad said,
Monday, November 27. 2006 at 18:58 (Reply)
Dual 2 GHz PowerPC G5
OS/X version 10.4.8
real 1m29.076s
user 1m27.758s
sys 0m0.570s
nugunga said,
Monday, November 27. 2006 at 19:21 (Link) (Reply)
Chris said,
Monday, November 27. 2006 at 19:45 (Reply)
echo 'ibase=2;obase=10000;01010101' | bc
55
echo 'obase=16;ibase=2;01010101' | bc
55
Lothar said,
Monday, November 27. 2006 at 21:15 (Link) (Reply)
model name : AMD Athlon(tm)
cpu MHz : 2000.082
real 0m58.122s
user 0m57.960s
sys 0m0.068s
converted by W eb2PDFConvert.com
Not bad for a $50 cpu
pMax said,
Monday, November 27. 2006 at 21:46 (Reply)
Intel(R) Pentium(R) III Mobile CPU 700MHz
real 2m47.996s
user 2m37.566s
sys 0m0.084s
Steve said,
Tuesday, November 28. 2006 at 01:13 (Reply)
real 1m5.593s
user 1m4.892s
sys 0m0.008s
Ravi said,
Tuesday, November 28. 2006 at 03:14 (Reply)
Macbook Pro (Core Duo with 2 gigs of RAM running OS X 10.4)
real 0m55.205s
user 0m55.013s
sys 0m0.103s
PiX said,
Tuesday, November 28. 2006 at 09:04 (Link) (Reply)
real 0m35.305s
user 0m35.178s
sys 0m0.108s
Of course, it only used one CPU so I got on with some work while
converted by W eb2PDFConvert.com
it was running.
Linulin said,
Tuesday, November 28. 2006 at 10:46 (Reply)
gdb, (yes, the debugger), could be also useful for simple
interactive calculations:
(gdb) p 3.0/8
$1 = 0.375
(gdb) p $1*(5-9)
$2 = -1.5
(gdb) p/x 255
$3 = 0xff
(gdb) p/c 35
$4 = 35 '#'
(gdb) help p
(gdb) help x
Alessandro said,
Tuesday, November 28. 2006 at 12:14 (Reply)
I want to share this marvel (not mine, but forgot where I took
it): in my opinion it surpasses any other console calculator, giving
you all the power of perl too - provided perl is installed, as it
should on any average system:
and use calc from the command line (exit with Ctrl-C)
Alessandro
BasketCase said,
Tuesday, November 28. 2006 at 16:25 (Link) (Reply)
It is good to know bc since bc is available on most UNIX systems.
This is the same reason it is good to know vi.
However, if you find yourself using it often you should try calc
(http://www.isthe.com/chongo/tech/comp/calc/) instead as it is a
bit nicer.
If you like the RPN style calculators (like HP) then you should try
Orpie (http://www.eecs.umich.edu/~pelzlpj/orpie/)
pepe said,
Wednesday, November 29. 2006 at 00:39 (Reply)
in most cases, shell-features will do. Don't know if it's bash-
specific, but try:
echo $((4+5))
converted by W eb2PDFConvert.com
sometimes:
i=4
for foo in bar; do something with foo; let i+=2; done
cu,
pepe
adren said,
Wednesday, November 29. 2006 at 06:42 (Reply)
Useless use of cat (only one arg) spotted
http://sial.org/howto/shell/useless-cat/
so instead of
cat /proc/cpuinfo | egrep "model name|MHz"
one might just use
egrep "model name|MHz" < /proc/cpuinfo
Anonymous said,
Wednesday, November 29. 2006 at 11:27 (Reply)
> Useless use of cat [] spotted
BTW:
model name : AMD Opteron(tm) Processor 242
cpu MHz : 1589.818
model name : AMD Opteron(tm) Processor 242
cpu MHz : 1589.818
real 1m32.591s
user 1m22.830s
sys 0m0.110s
converted by W eb2PDFConvert.com
Danny C. said,
Wednesday, November 29. 2006 at 12:00 (Reply)
xinuflux said,
Thursday, November 30. 2006 at 01:56 (Reply)
one item missing from some of the bc alternatives mentioned in
the comments is bc's wonderful ability to use history via readline
like bash does (if compiled properly)
pavlinux said,
Friday, December 1. 2006 at 20:08 (Link) (Reply)
....
In Maple 10, less than one second.
Zed said,
Sunday, December 3. 2006 at 07:37 (Reply)
real 0m31.109s
user 0m31.098s
sys 0m0.008s
from work server with minimal load (HP DL380g5, 2x dual core,
8GB ram, 4 x 147gb sas in RAID5)
Rich said,
Wednesday, December 6. 2006 at 20:05 (Reply)
Running cygwin on windows xp and a 2.6GHZ hyperthreading
P4...
real 1m27.282s
user 1m26.795s
sys 0m0.046s
Pat said,
Sunday, December 10. 2006 at 08:21 (Reply)
You don't actually need the quotes in
Using
converted by W eb2PDFConvert.com
works just as well (as you actually want the shell to substitute 5
for $FIVE). On some shells you may need to escape/quote the ^
thou...
ceugenio said,
Sunday, December 10. 2006 at 11:57 (Link) (Reply)
Holonic said,
Wednesday, December 13. 2006 at 13:25 (Reply)
cat /proc/cpuinfo | egrep "model name|MHz"
model name : Intel(R) Core(TM)2 CPU 6300 @ 1.86GHz
cpu MHz : 600.000
model name : Intel(R) Core(TM)2 CPU 6300 @ 1.86GHz
cpu MHz : 600.000
real 0m30.290s
user 0m30.235s
sys 0m0.040s
ishit said,
Thursday, December 14. 2006 at 16:40 (Reply)
FYI.
Mario said,
Saturday, November 8. 2008 at 17:47 (Reply)
That's a really poor measure of performance. On my 8 core (2
physical Xeon 2.8 GHz processors each with 4 cores) the CPU
usage was only 13% and it took 36 seconds.
Oscar_the_Grouch said,
Monday, March 30. 2009 at 22:49 (Reply)
That's because it's not multi-threaded, and is using but a
single processor.
converted by W eb2PDFConvert.com
chillin'.
manolo said,
Friday, December 19. 2008 at 11:03 (Reply)
Hello,
Oscar_the_Grouch said,
Monday, March 30. 2009 at 23:20 (Reply)
Trig Identities:
(Google FTW)
I have tried "..." > pi.txt but it just has an empty file.
Suggestions ?
netwoR said,
Sunday, April 26. 2009 at 10:26 (Reply)
"..." 1> pi.txt
Michael said,
Saturday, July 25. 2009 at 21:11 (Reply)
$ export BC_LINE_LENGTH=105
$ echo "scale=100;last=10;8/(1+(1/3))"|bc
converted by W eb2PDFConvert.com
6.0000000...(etc)...0000001
Michael said,
Saturday, July 25. 2009 at 22:52 (Reply)
Thanks for sharing.
(
by the way
Just looked at the source main.c and found the statement:
env_value = getenv ("BC_LINE_LENGTH");
if (env_value != NULL)
{
line_size = atoi (env_value);
if (line_size < 2)
line_size = 70;
}
else
line_size = 70;
always nice to have a change to peek at the source
)
kirk said,
Thursday, September 17. 2009 at 09:53 (Link) (Reply)
help me i cant evan do a simple 3+3
NOTE that both the $ and > are system prompts. Note also
that the outer quote marks are backticks ie ` while the inner
quotes are single quotes ie '
Keith said,
Tuesday, May 4. 2010 at 19:57 (Link) (Reply)
I use Midnight Commander as a file manager, personally. It's very
nice and is faster and more powerful than X file managers. I could
never figure out mutt, personally.
lex said,
Wednesday, September 29. 2010 at 13:54 (Link) (Reply)
Distro: Ubuntu Lucyd Lynx
CPUs: 4 x Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz
converted by W eb2PDFConvert.com
cpu MHz : 1600.000
real 0m36.001s
user 0m35.940s
sys 0m0.050s
Blackmail said,
Sunday, October 3. 2010 at 22:54 (Reply)
Well some info, tell me what you think:
real 0m44.267s
user 0m43.787s
sys 0m0.008s
and the processor is:
Michael said,
Thursday, October 14. 2010 at 09:57 (Reply)
Alas, bc doesn't understand lower case hex digits, which as I'm
copy and pasting my numbers from disassembly listings and
symbol tables makes it useless to me
Lordy said,
Friday, November 26. 2010 at 05:34 (Reply)
running virtual box on window7
real 0m40.616s
user 0m40.363s
sys 0m0.032s
Steve said,
Wednesday, July 13. 2011 at 20:04 (Reply)
any idea why this happens?
echo "ibase=16;obase=F;104D7" | bc
14BBA
echo "ibase=F;obase=F;104D7" | bc
104D7
Rob said,
Wednesday, July 13. 2011 at 21:33 (Reply)
converted by W eb2PDFConvert.com
According to the man page, valid ibase values are 2 through to
16. In Hex, 16 is 10, so having set ibase to F, you need to set
obase to 10 if you want hex output, e.g.
$ echo 'ibase=16;obase=10;FF*FF' | bc
FE01
$ echo 'ibase=16;obase=A;FF*FF' | bc
65025
russ said,
Sunday, August 7. 2011 at 02:02 (Reply)
real 0m19.653s
user 0m19.468s
sys 0m0.046s
Gauri said,
Friday, April 13. 2012 at 11:32 (Reply)
I want to perform the calculation:
22.20 - 22.10
00.10
priya said,
Monday, September 10. 2012 at 15:31 (Reply)
sean said,
Saturday, December 29. 2012 at 19:14 (Reply)
AC
domment
Name
Homepage
converted by W eb2PDFConvert.com
Enclosing asterisks marks text as bold (*word*), underscore are
made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
Remember Information?
Subscribe to this entry
converted by W eb2PDFConvert.com