Performing calculations in unix
Posted by John Hallas on August 31, 2009
Normally if I working on a database and want perform simple calculations I use the Oracle database rather than starting up the windows calculator.
select 6*1024*1024 from dual; 6*1024*1024 ----------- 6291456
However I recently found out a method of using unix itself (I am sure there are many more options other than this one but I find this quite easy)
man bc gives
bc is an interactive processor for a language that resembles C but provides unlimited-precision arithmetic. It takes input from any files given, then reads the standard input.
In usage
/home/oracle $bc 6*1024*1024 6291456 quitArithmetic operators are shown in the normal manner + - * / % ^ and there are lots of functions available such as sine, cosine and log, although I would be likely to use a calculator if I wanted to use these.
danirey said
Thanks for sharing. When using Linux with KDE I prefer to do calculations in the “run command” window (works only with KDE < Version 4)
You can usually open the "run command" dialog with Alt+F2, or in the KDE Menu. If you want to proceed with results 0.2342 Otherwise it’s not recognized as a number.
pgreenwood said
I go to python for quick calculations.
John Hallas said
Thanks Patrick, there are lots of ways of doing calculations using shell, perl, python bit I found bc nice and easy (and there is an alternative dc which looks very simple as well)
lbolla said
I strongly recommend you:
$> bc -l
otherwise 1/2 = 0, instead of 0.5!
L
John Hallas said
Just checked that out. Good call.
Blogroll Report 28/08/2009 – 04/09/2009 « Coskan’s Approach to Oracle said
[...] John Hallas – Performing calculations in unix [...]