

Integrated command line math - danielcorin
http://danielcorin.github.io/blog/2013/07/23/qc/

======
peter_l_downs
Or, if you're on OS X, you can just use Spotlight. It includes trig functions,
factorials, exponents, etc, and it's only a command-space away. The only
downside is that it doesn't automatically copy the result into your clipboard.

An example: [http://i.imgur.com/vaN7HfS.png](http://i.imgur.com/vaN7HfS.png)

~~~
danielcorin
I'm surprised that pressing enter on the result in Spotlight doesn't copy it
to the clipboard. Amusingly, it opens up the Calculator which I never need
after getting the answer.

------
claudius
‘qalc’ is the command-line version of Qalculate![0] and runs both directly
from the command line as well as interactively with tab-completion of
variables (Planck mass, for example) and arbitrary unit conversion:

    
    
      $ qalc
      > 15 kilogram * (4 m/s)^2 / 2
      
        (15 * kilogram * (((4 * meter) / second)^2)) / 2 = 120((kg*m^2) / s^2)
      
      > convert best
      
        120((kg*m^2) / s^2) = 120 J
      
      > convert gTNT
      
        120 J = approx. 28.680688 mgTNT
    

[0] [http://qalculate.sourceforge.net/](http://qalculate.sourceforge.net/)

~~~
danielcorin
Thanks for sharing this

~~~
claudius
I have the GTK version of it bound to the blue ‘ThinkVantage’ key, it comes in
more than handy multiple times a day :)

------
ubasu
Or:

    
    
      echo "$1" | bc -l
    

No need to rev up python.

~~~
eieio
I thought the same thing until I got to the part where the author used

    
    
      from math import *
    

Which gives you significantly more options.

I love this idea, I have always found math in the command line to be clunky.

~~~
ubasu
That is true, but I have found if I am doing calculations that need something
like the full math library of python, it's not a one-off calculation, and I am
typically on octave anyway.

In any case, since I have emacs open all time, I usually do

    
    
      M-x calc
    

for one-off calculations.

~~~
merlincorey
I just evaluate random lisp in my buffers, sometimes evaluating it to output
directly there. Ah emacs!

~~~
omaranto
I do that too, with textadept & Lua instead of Emacs & elisp.

------
scj
I don't quite understand the argument against bc in this case. 'echo "5 + 5" |
bc' doesn't leave the command line.

~~~
harrytuttle
bc is a little "obtuse". A bc replacement with BCD arithmetic and RPN entry
would make me very happy :)

Actually, perhaps I should write one!

~~~
seryoiupfurds
I think ``dc'' does what you want.

~~~
harrytuttle
Wonderful! Thank you for posting. That's exactly what I wanted :)

------
mfincham
A probable improvement on this would be to leave bash out, and just put a
python hash bang in the script directly.

I'll leave implementing it as an exercise to the reader. Personally I have
found using Guake ([http://guake.org/](http://guake.org/)) with IPython as the
shell a very nice way of solving this problem.

~~~
ante_annum
An even briefer improvement on this would be to remove the whole script and
just alias/function the whole thing.

For example, my .bash_profile contains:

function scrub { ps -ef |grep $1 | grep -v grep | awk '{print $2}' | xargs
kill }

mostly because I was too lazy to install pkill, but I can just do `scrub
tomcat` and it works.

Also, don't alias a script path. Just add the script to the path.

------
phaemon
While we're all about the options...you don't need a separate shell script for
this, you can just do:

    
    
        qc() { python -c "from math import *; print $@" ; }
    

directly (or in your .bashrc)

------
unfamiliar
For anyone who would like this in an Alfred workflow, make a Script Filter
with the following python code (and uncheck all the escape character options):

    
    
      from math import *
      print '<?xml version="1.0"?><items>'
      print '<item uid="qcresult" valid="yes" autocomplete="">'
      print '<title>'+str({query})+'</title>'
      print '</item></items>'

------
cespare
Probably known to most, but your shell can also do simple arithmetic by
itself.

    
    
      $ echo $((3**5))
      243

~~~
kps
ksh and zsh support floating point, and ksh also has the usual math functions.

    
    
      $ echo $((2 * atan2(1, 0)))
      3.14159265358979324

~~~
dgl
In zsh you could use noglob to make this nicer:

    
    
      zmodload zsh/mathfunc
      _calc() { echo $[$*] }
      alias c='noglob _calc'
    

Then:

    
    
      % c 2 * atan(1, 0) 
      3.1415926535897931
    

(Although this works with any command, using an alias with qalc as pointed out
above might be more useful.)

------
tubs
You can use xclip on Linux (presumably other X based systems) to write to the
clipboard.

[http://linux.die.net/man/1/xclip](http://linux.die.net/man/1/xclip)

------
stevejb
If you are using Emacs, I find that having one buffer always open to R, and
another open to shell, serves most of this need.

------
desmondrd
Great post, thanks.

------
waffenklang
a bit off topic: are there any calculators which support c like signed /
unsigned binary representation? 2 -> 01 -2 -> FD ?

