6 ms·
Integrated command line math
- ubasu 13y agoOr: echo "$1" | bc -l No need to rev up python.
- gnosis 13y agoA 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".
- eieio 13y agoI 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 13y agoThat 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 13y agoI just evaluate random lisp in my buffers, sometimes evaluating it to output directly there. Ah emacs!
- omaranto 13y agoI do that too, with textadept & Lua instead of Emacs & elisp.
- _ZeD_ 13y agominor nitpick, you don't need "echo" $ bc -l <<< 3+2 5 $
- scj 13y agoI don't quite understand the argument against bc in this case. 'echo "5 + 5" | bc' doesn't leave the command line.
- danielcorin 13y agoJust a different way of approaching the problem
- harrytuttle 13y agobc 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 13y agoI think ``dc'' does what you want.
- harrytuttle 13y agoWonderful! Thank you for posting. That's exactly what I wanted :)
- mfincham 13y agoA 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 13y agoAn 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.
- cespare 13y agoProbably known to most, but your shell can also do simple arithmetic by itself. $ echo $((3**5)) 243
- kps 13y agoksh and zsh support floating point, and ksh also has the usual math functions. $ echo $((2 * atan2(1, 0))) 3.14159265358979324
- dgl 13y agoIn 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.)
- unfamiliar 13y agoFor 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>'
- desmondrd 13y agoGreat post, thanks.
- claudius 13y ago‘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 13y agoThanks for sharing this
- claudius 13y agoI have the GTK version of it bound to the blue ‘ThinkVantage’ key, it comes in more than handy multiple times a day :)
- waffenklang 13y agoa bit off topic: are there any calculators which support c like signed / unsigned binary representation? 2 -> 01 -2 -> FD ?
- peter_l_downs 13y agoOr, 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 13y agoI'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.
- phaemon 13y agoWhile 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)
- stevejb 13y agoIf you are using Emacs, I find that having one buffer always open to R, and another open to shell, serves most of this need.
- tubs 13y agoYou 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