“If like me, you do most of your work from the command-line, using 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 calculator to make (what may sometimes be) a single calculation. This article shows how to quickly perform standard calculations on the command line.”
I admire people who can do all that, the last time I did everything in the command line was no a Commodore 64
The GUI can still be productive if you prefer the keyboard. Mac OS + Quicksilver, or when Leopard comes out you can press Cmd+Spc type 1+1 and there’s your answer. Even in windows I used to assign a shortcut to Calc and just bring it up, bash in the question, Ctrl+C and Alt+F4 or Alt+Tab out of it.
I suppose the benefit of calculations on the command line is when you want to pipe the answer somewhere else?
Personally, I like the capabilities of bc very much. The best thing: It’s available by default in the base system, no need to install any package.
The bc manpage is very informative and states in the first sentence: “bc is a language that supports arbitrary precision numbers with interactive execution of statements. There are some similarities in the syntax to the C programming language.” Its versatility and support of functions, blocks, iteration, recursion and many more stuff you know from C or other programming languages. Just have a look on the EXAMPLES section.
“Even in windows I used to assign a shortcut to Calc and just bring it up, bash in the question, Ctrl+C and Alt+F4 or Alt+Tab out of it.”
But can you change the values getting into the calculation? For example, if you entered
125+256+12554+569+9856+5874
and you see 256 is wrong and should be 526, you just go back and change it. Wow, easy, isn’t it? ๐ The command line is very comfortable to use. It also supports the repeat function.
In a GUI, you can connect “xterm -e bc” to a key or key combination to have it in a window. Because xterm supports buffer operations (click the middle mouse button), you can use the mouse to input and output values.
“I suppose the benefit of calculations on the command line is when you want to pipe the answer somewhere else?”
Yes, espeically when you want to do some system statistics that output into logfiles or system mail messages. But bc serves “allday use” purposes as well.
There’s also Microsoft’s unofficial http://www.microsoft.com/windowsxp/downloads/powertoys/xppowertoys…. that has history, limited graphing, and other things.
Beware of the PowerToy Calculator! It seems nice, but every once in a while it gives wrong results. E.g., it can give a value of >30000 for some x in sin(x) which of course should be impossible. (I don’t have it installed currently so I can’t test it, but try something like sin(PI*40).) Also, some variables might change their values unexpectedly (I’ve only seen this with variables whose name contains the letter ‘E’, but this doesn’t happen always so it’s a bit hard to pinpoint.) Anyway, I still think it’s the best program MS ever made even if you can trust it only about as much as you can trust MS.
Whenever I need to do a cuick calculation in Windows I wished I could do it from the command line as I always have at least one shell open and opening Calculator each time feels like such a fuss.
Then install bc (and a host of other GNU utilities) on Windows:
http://unxutils.sourceforge.net/
They’re all just a download and an unzip away.
(Personally, I’m more of a dc guy.)
Edited 2006-11-27 13:53
Why use echo and pipe to bc? I always just type bc<enter> and work from there.
“Why use echo and pipe to bc? “
Maybe it’s to illustrate the capability of reading from stdin, a “piping example”?
“I always just type bc(enter) and work from there.”
Usually I do the same, see example above. You can have bc process files which makes sense if you define functions and / or data. For “small things” the regular interactive mode should work fine.
“Then install bc (and a host of other GNU utilities) on Windows:
http://unxutils.sourceforge.net/
They’re all just a download and an unzip away.”
I have been running bc on Windows at work for several years now and find it very useful. What I like and isn’t mentioned in the article is that you can use variables.
The article mentions the advantage of having a print out of all of your calculations, but many gui calculators do this. One of the best here is Qalculate for Linux, but though simpler the calculator plugin for Firefox is pretty good too.
Python is my calculator both on Windows and on Linux. It does everything bc can do, and a lot more.
You can do bc’s scale thing too.
>>> from decimal import *
>>> getcontext().prec = 100
>>> Decimal(2).sqrt()
1.41421356237309504880168872420969807856967187537694807317667973799073 2478462107038850387534327641573
Enough said.
The problem with using Python though is that you need to type more before you have your answer, unless you keep Python running constantly..
So, anyone in for creating a win32 command line calculator? ๐
I just use:
#!/bin/sh
python -ic ‘from __future__ import division;from math import *’ $*
Put this in e.g. ‘/usr/local/bin/pym’ and use yakuake as a very cool terminal an so you can fire up a calculator with:
F12 (or I configured it on “Scroll Lock”) and enter pym
The advantage: a full scripting language with lists, dictionaries etc. And with rpy you even got the power of R while being able to use cool things like generator statements for complex sum-formulars! I love it.
The problem with using Python though is that you need to type more before you have your answer, unless you keep Python running constantly..
Not necessarily… you could always assign an one lettered alias to ‘python’, or even use keyboard bindings.
I keep Python running constantly, in at least a couple of Terminal windows…
I’ve used Python as a calculator for a few years now, and it’s pretty good at any kind of simple algebraic math. One thing that I’ve found to be very beneficial is that you can set the variables of an equation to the calculated values for those variables, and then just punch in the equation almost how you’d type it elsewhere and get the answer.
For an EE minded networking class I had a small import that sets variables like Mbps, MiB, Kbps, usec, msec, Cvac, Cwire, etc.., so I could usually solve problems very quickly and accurately in the given units without thinking “how many seconds is 40usec?” This might seem trivial but for someone who is not often performing these calculations it really helps to not even bother thinking about them so you can keep your mind on the big picture of what you are trying to solve. (7 +/- 2!)
Another tip that is probably useful for those in undergrad CS courses is that if you find yourself doing something that feels especially ‘algorithmic’, attempt to program it. Not only do these small excersizes keep your practical programming skills relavant, but the process by which you go about describing to a computer how to do something often firms up your own ability to perform it and during your testing you will most likely discover any caveats or gotchas about the task at hand. It’s for this reason (if any) that I suspect programming will become generally understood among most scientists and engineers.
I’ve used Python as a calculator for a few years now, and it’s pretty good at any kind of simple algebraic math. One thing that I’ve found to be very beneficial is that you can set the variables of an equation to the calculated values for those variables, and then just punch in the equation almost how you’d type it elsewhere and get the answer.
You can do the same with bc.
Edited 2006-11-27 19:32
I used to use bc but I got lazy and now
google is my quick calculator
Altough I sometimes use a programming language to calculate a set of conditional calculation steps(etc) mostly because I don’t like maple
KDE’s “Run Command” dialog (usually bound to Alt+F2) can perform simple calculations as well. Try typing 2+2 and hit return!
While bc is useful for simple math, it does not do conversions like google.
“400 kilometers in miles” for instance = 248.548477 miles
“speed of light” = 299792458 m/s
“the answer to life the universe and everything” = 42
Google toolbar has become my new default calculator.
“While bc is useful for simple math, it does not do conversions like google.”
How about this: While bc is useful for simple math, it does not do function plotting like gnuplot. And it does not search the internet. ๐
If you set the proper factors in a file and load it into bc’s memory, you can do conversions as well, e. g. you can have functions like km2mile() or usd2eur(). They’re not built in by default.
“speed of light” = 299792458 m/s[i]
“the answer to life the universe and everything” = 42
Sure these are conversions? The first one is a natural constant, the second one a universal one. ๐
% bc
bc 1.06
life=42
life
42
It knows it. bc knows everything as long as you tell it.
BTW: I usually prefer google in a GUI setting because it definitely is very decent, but bc is better in console / text mode. For each work the best tool.
Google is decent in text-browsers too.
But I prefer the ‘units’ program myslf.
Octave is a little heavy weight, but it makes a fine command-line calculator.
A useful advantage for this purpose over bc is the ability to handle vectors of numbers. To sum the hours in my ASCII tab-delimited timecard file, I typically do:
cut -f2 aug05-hours.txt
copy the column of numbers with the mouse, fire up octave:
H=[
(paste here)
]
sum(H)
And of course it still has all the advanced math and graphing capabilities I could ever want, readline support, etc.
I’m using GNAW myself, which is a cross-platfrom command-line calculator. It’s pretty decent for ordinary use at least. I’m using it in Windows, it has a win32-version.
http://gnaw.sourceforge.net/
Personally, if I’m running without X, that means I’m on laptop batteries on train. In that case, Emacs+Gnus+SLIME+w3m is running, so if I need something calculated I just switch to the REPL buffer and use that instead.
For instance, (sqrt 2) and so on. Gotta love it.
Why use bc/dc when bash has a calculator built in? ๐ It’s mostly C-syntax compatible.
And zsh/ksh even support floating point.
type this in adress bar:
javascript:2+2
or
javascript:Math.sqrt(16)
and press [enter]
“If like me, you do most of your work from the command-line, using vim to edit files, mutt for e-mails, cd/ls/mv/find/etc instead of a file manager”
Why a so long sentence to say a simple thing. This should be:
“If like me, you are a geek”
The physical calculator metaphore really sucks. That’s why I created Calctor, an advanced calculator with a simple interface. It looks like a text editor, and it evaluates your input while you’re typing.
You can download it for free here: http://tommycarlier.blogspot.com/2006/09/calctor-calctor-mobile.htm…
EDIT: you can also download the C# source code here: http://channel9.msdn.com/ShowPost.aspx?PostID=235710
Edited 2006-11-27 18:14
I prefer rpn notation.
dc -e ‘2 3 10 +*p’
Sweet, I can put away my old HP48G now.
I just use my irssi and do /calc (it uses bc).
๐
Hmmmmm bc is a nice calc and powerful as well.
I learned many new things about bc from this article.
Thanks osnews…
I like apcalc. You can use it both directly on the command line (without needing any echo-piping):
$ calc ’10^log(100)’
100
$
and in interactive mode:
$ calc
; define f(x)=2*x
f(x) defined
; f(3)
6
;
For simple calculations, there’s a handy Gnome Deskbar extension. Always there at the top of the screen. Of course no history or anything like that.
If you want quick command line calculations use $[] or $(()).
echo $((2*5/6))
echo $[2+2]
I use apcalc for anything fancier than that and units(1) for conversions.