I'm trying to do an interface to bc
so it can be used intuitively and without the annoyance of getting "stuck" in it. I haven't got around to test it that much, because I got stuck on another detail, namely how to present the result (which is, I think, a string).
Rounding or truncating does not matter, either one is fine. Take a look below, and you'll understand immediately. I use zsh but an external tool will be just fine as I won't use this in any time or otherwise critical context, it's just a desktop tool.
calc () {
result=`bc <<EOF
scale=3;
$@
EOF`
echo ${result//%0/} # doesn't work; will only remove one zero
# also, if there are only zeroes, should
# remove dot as well - what about .333, etc.?
}
Edit
I'm very impressed by the below solution, especially how the noglob
gets away with the quotes!
But, the use of a dot to force floating point calculation is something I'll never remember (you don't use a normal calculator like that). And it is even a bit risky, especially for calculations when it's not obvious that floating point would yield an altogether different result (most likely the one you wanted).
Also, the calculations below show some un-pretty output (the too long real, and the trailing dot).
Perhaps I should combine this (some of it) with the output formatting of @Gille's answer below? When I get it to work perfectly, I'll post the result here. (Edit: The accepted answer works great. Be sure to read the comments to that answer, as well.)
calc () {
echo $(($*));
}
alias calc='noglob calc'
calc 1./3
0.33333333333333331
calc 7.5 - 2.5
5.
zcalc
(autoload zcalc; zcalc
,info -f zsh --index-search=zcal
)?0
for1/3
. Also, do you know a way to send the termination:q
(orRet
keystroke) from the command line? I'd like to use it in the same way as thebc
example above: e.g.,calc "2*3"
gets you6
and the program terminates.1./3
to force floating point arithmeticscalc() echo $(($*)); alias 'calc=noglob calc'
. Then:calc 2. / 3 * 5