Hacker News new | past | comments | ask | show | jobs | submit login
Integrated command line math (danielcorin.github.io)
39 points by danielcorin on July 23, 2013 | hide | past | favorite | 28 comments



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


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.


‘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/


Thanks for sharing this


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


Or:

  echo "$1" | bc -l
No need to rev up python.


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.


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.


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


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


A little syntactic sugar (in zsh):

  function calc {
    print $1 | bc -l
  }
Then:

  calc "(1+2)*3"
  9
zsh actually comes with its own little calculator:

  zcalc "(1+2)*3"
  1> 9
  2> 
Among other things, it features "full command line editing, including the history of previous calculations".


minor nitpick, you don't need "echo"

    $ bc -l <<< 3+2
    5
    $


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


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!


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


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


Just a different way of approaching the problem


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/) with IPython as the shell a very nice way of solving this problem.


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.


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)


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


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

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


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

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


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


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

http://linux.die.net/man/1/xclip


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.


Great post, thanks.


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




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: