Linked by Thom Holwerda on Mon 27th Nov 2006 10:14 UTC, submitted by Rob Newcater
OSNews, Generic OSes "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."
Order by: Score:
RE
by Kroc on Mon 27th Nov 2006 11:11 UTC
Kroc
Member since:
2005-11-10

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?

Reply Score: 2

RE
by Doc Pain on Mon 27th Nov 2006 11:57 UTC in reply to "RE"
Doc Pain Member since:
2006-10-08

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.

Reply Score: 2

RE
by lpetrazickis on Mon 27th Nov 2006 17:55 UTC in reply to "RE"
lpetrazickis Member since:
2005-07-06

There's also Microsoft's unofficial http://www.microsoft.com/windowsxp/downloads/powertoys/xppowertoys.... that has history, limited graphing, and other things.

Reply Score: 3

RE
by msundman on Tue 28th Nov 2006 01:28 UTC in reply to "RE"
msundman Member since:
2005-07-06

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.

Reply Score: 1

Neat.
by JamesTRexx on Mon 27th Nov 2006 11:20 UTC
JamesTRexx
Member since:
2005-11-06

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.

Reply Score: 1

RE: Neat.
by eMagius on Mon 27th Nov 2006 13:45 UTC in reply to "Neat."
eMagius Member since:
2005-07-06

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

Reply Score: 1

RE[2]: Neat.
by diginux on Mon 27th Nov 2006 13:51 UTC in reply to "RE: Neat."
diginux Member since:
2005-07-06

Why use echo and pipe to bc? I always just type bc<enter> and work from there.

Reply Score: 1

RE[3]: Neat.
by Doc Pain on Mon 27th Nov 2006 14:13 UTC in reply to "RE[2]: Neat."
Doc Pain Member since:
2006-10-08

"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.

Reply Score: 1

RE[3]: Neat.
by chemical_scum on Mon 27th Nov 2006 15:29 UTC in reply to "RE: Neat."
chemical_scum Member since:
2005-11-02

"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.

Reply Score: 1

Python
by saxiyn on Mon 27th Nov 2006 11:39 UTC
saxiyn
Member since:
2005-07-08

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.

Reply Score: 4

RE: Python
by JamesTRexx on Mon 27th Nov 2006 13:41 UTC in reply to "Python"
JamesTRexx Member since:
2005-11-06

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? :-)

Reply Score: 2

RE[2]: Python
by panzi on Mon 27th Nov 2006 15:40 UTC in reply to "RE: Python"
panzi Member since:
2006-01-22

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. ;)

Reply Score: 1

RE[2]: Python
by magick on Mon 27th Nov 2006 15:45 UTC in reply to "RE: Python"
magick Member since:
2005-08-29

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.

Reply Score: 1

RE[2]: Python
by rayiner on Tue 28th Nov 2006 15:46 UTC in reply to "RE: Python"
rayiner Member since:
2005-07-06

I keep Python running constantly, in at least a couple of Terminal windows...

Reply Score: 1

RE: Python
by jonas on Mon 27th Nov 2006 18:36 UTC in reply to "Python"
jonas Member since:
2005-07-08

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.

Reply Score: 1

RE[2]: Python
by chemical_scum on Mon 27th Nov 2006 19:31 UTC in reply to "RE: Python"
chemical_scum Member since:
2005-11-02

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

Reply Score: 1

heh
by czubin on Mon 27th Nov 2006 12:02 UTC
czubin
Member since:
2005-12-31

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

Reply Score: 3

eikehein
Member since:
2005-10-19

KDE's "Run Command" dialog (usually bound to Alt+F2) can perform simple calculations as well. Try typing 2+2 and hit return! ;)

Reply Score: 5

Google is pretty decent
by Priest on Mon 27th Nov 2006 12:50 UTC
Priest
Member since:
2006-05-12

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.

Reply Score: 1

RE: Google is pretty decent
by Doc Pain on Mon 27th Nov 2006 13:08 UTC in reply to "Google is pretty decent"
Doc Pain Member since:
2006-10-08

"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.

Reply Score: 3

RE[2]: Google is pretty decent
by grfgguvf on Mon 27th Nov 2006 15:59 UTC in reply to "RE: Google is pretty decent"
grfgguvf Member since:
2006-09-25

Google is decent in text-browsers too.

But I prefer the 'units' program myslf.

Reply Score: 1

Octave
by braddock on Mon 27th Nov 2006 13:24 UTC
braddock
Member since:
2005-07-08

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.

Reply Score: 2

Command-line Calculator
by Datafriken on Mon 27th Nov 2006 14:10 UTC
Datafriken
Member since:
2006-11-27

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/

Reply Score: 2

Interesting
by zsitvaij on Mon 27th Nov 2006 15:23 UTC
zsitvaij
Member since:
2006-06-14

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. ;)

Reply Score: 2

Command line calculator, built in!
by grfgguvf on Mon 27th Nov 2006 15:57 UTC
grfgguvf
Member since:
2006-09-25

Why use bc/dc when bash has a calculator built in? ;-) It's mostly C-syntax compatible.

And zsh/ksh even support floating point.

Reply Score: 2

web browser is enough
by marcell on Mon 27th Nov 2006 16:57 UTC
marcell
Member since:
2005-07-11

type this in adress bar:

javascript:2+2

or

javascript:Math.sqrt(16)

and press [enter] ;)

Reply Score: 4

lol
by Duffman on Mon 27th Nov 2006 17:06 UTC
Duffman
Member since:
2005-11-23

"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"

Reply Score: 2

TommyCarlier
Member since:
2006-08-02

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

Reply Score: 1

why bc when you can dc
by rajj on Mon 27th Nov 2006 18:16 UTC
rajj
Member since:
2005-07-06

I prefer rpn notation.

dc -e '2 3 10 +*p'

Reply Score: 2

RE: why bc when you can dc
by MattPie on Mon 27th Nov 2006 18:36 UTC in reply to "why bc when you can dc"
MattPie Member since:
2006-04-18

Sweet, I can put away my old HP48G now. ;)

Reply Score: 1

Irssi?
by gothic on Mon 27th Nov 2006 19:12 UTC
gothic
Member since:
2005-07-06

I just use my irssi and do /calc (it uses bc).

:-)

Reply Score: 1

Nice Calc
by microFawad on Mon 27th Nov 2006 19:55 UTC
microFawad
Member since:
2005-12-09

Hmmmmm bc is a nice calc and powerful as well.
I learned many new things about bc from this article.
Thanks osnews...

Reply Score: 1

apcalc
by msundman on Tue 28th Nov 2006 01:18 UTC
msundman
Member since:
2005-07-06

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
;

Reply Score: 1

gnome deskbar
by zerohalo on Tue 28th Nov 2006 02:00 UTC
zerohalo
Member since:
2005-07-26

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.

Reply Score: 1

bc is too much
by sorpigal on Tue 28th Nov 2006 13:46 UTC
sorpigal
Member since:
2005-11-02

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.

Reply Score: 1