Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A more functional command-line calculator UNIX
I have read all the hints about command line calculators, and I've tried just about all of them, including:
  1. Python
  2. awk
  3. bc
Unfortunately, none of these options had built-in functions like sin() or cos(), or constants like pi. For Python, in order to include the math library you have to type from math import *. But then to get it to do the things you want (like adjusting precision), you have to be familiar with Python and/or you have to read documentation on the Python math library. The problem with bc is that when you type bc -l you only get six built-in functions, such as s(), c(), a(), which are sine, cosine, and arctangent respectively. You don't get any built-in constants.

There's a better way, but it requires a small download of a program called wcalc. If you have Fink installed, you can just type fink install wcalc (or get the pre-compiled version via sudo apt-get install wcalc. After you've installed wcalc, on the command-line in Terminal, type wcalc and you're set to go. The program provides you with a prompt and tells you "Enter an expression to evaluate, q to quit, or ? for help." It's pretty self-explanatory.

[robg adds: The Fink version of wcalc is 1.7. However, on the home page I linked above, you can download version 2.2.2 for Mac OS X, which also includes a GUI version.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[9,789 views]  

A more functional command-line calculator | 16 comments | Create New Account
Click here to return to the 'A more functional command-line calculator' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A more functional command-line calculator
Authored by: tammojan on Jul 09, '07 07:54:41AM
Actually, the unstable tree of fink already includes wcalc 2.2.2. All you have to do is enable the unstable tree, see http://finkproject.org/faq/usage-fink.php. (Insert warning about using unstable software here...)

[ Reply to This | # ]
A more functional command-line calculator
Authored by: boredzo on Jul 09, '07 08:51:06AM

Here's another one: calc, by Landon Curt Noll.

Wcalc looks interesting, though. I'll have to try it out.



[ Reply to This | # ]
even better and simpler way
Authored by: SOX on Jul 09, '07 09:34:25AM
Perl has a solution ready made for you and it's built in and does what you want. namely at the command line type:
perl -ne 'print eval($_)."\n"'
Now in case that's too much trouble, just create an alias like this.
alias calc perl\ \-ne\ \'print\ eval\(\$_\)\.\"\\n\"\'
This will give you a perl interpreter at the command line that executes any commands you type in. THis saves you the hassle of having to type "print" before your entries or worrying about parentheses from confusing the print statement. All the usual math function like sine and cosine still function. want fixed decimal places well then here you go:
alias calc2 perl\ \-ne\ \'printf\ \"\%.2f\\n\"\,eval\(\$_\)\'
This will give you 2 decimal places.

[ Reply to This | # ]
improved version:
Authored by: SOX on Jul 09, '07 10:40:23AM
Here's an improved version
alias calc perl\ \-ne\'\$bb\=\$x\;\$z\[\+\+\$i\]\=\$x\=eval\(\$_\)\;print\"\[\$i\]\\t\$x\\n\"\;\$y\=\$bb\'
This defines three variables $x, $y, and @z. results of the previous two commands, plus a stack of all the previous results.

>calc
27+18
[1]     45
3.3+7
[2]     10.3
$y             # the second to last result value
[3]     45
17+14
[4]     31
$x+9          # use the last result value
[5]     40
$z[2]-1       # use the very second result value
[6]     9.3
"@z"           # see the whole history
[7]      45 10.3 45 31 40 9.3


[ Reply to This | # ]
even better and simpler way
Authored by: rjetton on Jul 11, '07 05:37:46PM
You should look into the purpose behind taint checking before proceeding. Some of this eval() stuff can be dangerous! Imagine... blindly executing anything that a user types as input to a perl script! And if you don't get it, think about some of perl's builtin functions that don't do math. Like unlink(), maybe?

[ Reply to This | # ]
A more functional command-line calculator
Authored by: lar3ry on Jul 09, '07 10:00:39AM
BC has support for cosine, sine, etc. Just use the "-l" option to the BC command. RTFM(an)P(age)...


lar3ry@beth> lar3ry $ bc -l
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
c(0) /* cosine of 0 radians */
1.00000000000000000000
s(0) /* sine of 0 radians */
0
a(1) /* artangent of 1 (pi/4) */
.78539816339744830961
scale=100; 4 * a(1) /* pi to 100 decimal places */
3.141592653589793238462643383279502884197169399375105820974944592307\
8164062862089986280348253421170676
lar3ry@beth> lar3ry $


[ Reply to This | # ]
Um...
Authored by: SeanAhern on Jul 09, '07 10:51:51AM

Um...he mentioned in the posting that he knows about "bc -l". His problem was that it didn't have constants built in; you'd have to do formulas to get them.



[ Reply to This | # ]
There is also the good 'ole calc
Authored by: mayo2ca on Jul 09, '07 11:40:28AM

calc (stands for c calculator) the good old unix calc. You still have to install it as it doesn't come bundled, but I've used that thing for years and it never disappointed me. Not sure about fink, but it is in macports



[ Reply to This | # ]
A more functional command-line calculator
Authored by: geminie on Jul 09, '07 02:32:41PM
This perl script, from Alexey Vikhlinin, is my favorite.

[ Reply to This | # ]
A more functional command-line calculator
Authored by: mkomitee on Jul 09, '07 08:25:15PM
If you're considering something like commandline python, then you may want to give ruby a shot, which allows you to have a config file automatically import (include in ruby-ese) modules. ie, if i set my ~/.irbrc to include the following line:
include Math
I can do the following:

avalon:~ mkomitee$ irb
irb(main):001:0> (4.0*10+9*(100+1)/2.0)/cos(1)
=> 915.228372393218
irb(main):002:0> foo = cos(1)
=> 0.54030230586814
irb(main):003:0> 10 * foo
=> 5.4030230586814
irb(main):004:0> quit
avalon:~ mkomitee$ 
-- not that this is the easiest solution, but it's pretty powerful, and with the config file you don't have to include Math manually everytime you want. If you learn a little ruby, it goes a long way and you can do stuff like this in your config file:

require 'rubygems'
include Math
require "math/statistics"
class Array
  include Math::Statistics
end
and then do stuff like this:

avalon:~ mkomitee$ irb
irb(main):001:0> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].avg
=> 5.5
irb(main):002:0> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].std
=> 2.87228132326901
irb(main):003:0> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].var
=> 8.25
irb(main):004:0> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].min
=> 1
irb(main):005:0> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].max
=> 10
irb(main):006:0>quit
and with things like this, it'll become pretty easy for you to extend your own calculator.

[ Reply to This | # ]
A more functional command-line calculator
Authored by: Doc Drang on Jul 09, '07 10:37:23PM

First, from the Python tutorial:

When you use Python interactively, it is frequently handy to have some standard commands executed every time the interpreter is started. You can do this by setting an environment variable named PYTHONSTARTUP to the name of a file containing your start-up commands.

So you can put from math import * in a Python startup file, set the PYTHONSTARTUP environment variable, and the math commands will be available every time you start an interactive session.

Second, depending on your math needs, you may want to look into Octave, a very powerful numerical package.

Third, thanks for the tip on wcalc. I don't like the GUI version, but the command-line tool seems nice and certainly starts up very quickly. I'm a little concerned about the number of predefined variables it has--I foresee conflicts with the variable names I may want to use--but I'll give it a try.

---
Doc Drang
http://www.leancrew.com/all-this

[ Reply to This | # ]

Google search as calculator
Authored by: chp on Jul 10, '07 12:02:17AM
Google search will recognise and compute a lot of calculations, including trigonometric functions and unit and currency conversions, see:

http://www.google.com/intl/en/help/features.html#calculator

And if you use the Google toolbar in your browser, the answer appears as a tooltip as soon as you type, you don't even need to load a web page.

Sure, this requires a web connection, and Google might snoop on everything you type, but in terms of convenience this is the best I have found so far.

NB: I am not affiliated with Google, just a random user.

[ Reply to This | # ]
AppleScript shell is another possibility
Authored by: hayne on Jul 10, '07 12:11:52AM
Another possibility for a command-line calculator is to use my "AppleScript shell"

This provides basic calculator functionality since that is built into AppleScript. Installing Satimage's scripting addition provides all the usual trignometric functions (and much more). And you can use AppleScript syntax to define custom functionality if you desire.

Sample usage:


% ash
Welcome to ash (AppleScript Shell) version 0.62
Type: -help for help, type: -exit to exit
Sourcing "/Users/fred/.ashrc"

ash> 35 / 14.7
35 / 14.7
2.380952380952

ash> 4 + 3 * 5
4 + 3 * 5
19

ash> sin(pi/4)
sin(pi/4)
0.707106781187

ash> set x to 35 / 14.7
set x to 35 / 14.7
2.380952380952

ash> x
x
2.380952380952

ash> x * atan(1)
x * atan(1)
1.869995627136                                           


[ Reply to This | # ]
A simple shell alias for using python the way you want
Authored by: jochen Küpper on Jul 10, '07 03:10:07AM
I've used this (tc) shell-script and its bash equivalent for a long time now, works like a charm

# line-calculator
alias calc 'python -c "from __future__ import division;' \
    'from math import *;' \
    'print \!* " '


[ Reply to This | # ]
A more functional command-line calculator
Authored by: lsequeir on Jul 10, '07 06:00:05AM

I occasionally use bc, but I would also suggest another alternative: using CalcService, you can type your expressions anywhere you can input text (at least in Cocoa applications).

Often I need a quick calculation as I'm typing in, say, TeXShop. No problem, just type it right in, select it and calculate (you can choose Calculate and Append, Calculate and Replace, Calculate and Show). With a keyboard shortcut, I don't even take my hands away from the keyboard.

Luis Sequeira

---
Luís



[ Reply to This | # ]
A more functional command-line calculator
Authored by: CoolerQ on Jul 23, '07 12:00:03AM
My favorite calculator is "units"; it's included with OS X, but you'll want the fink version as the native one is very very old. Some example usage:

maclap01:~ quentins$ units
1948 units, 71 prefixes, 28 functions

You have: cos(0)
You want: 
        Definition: 1
You have: cos(pi)
You want: 
        Definition: -1
You have: 15 miles
You want: meters
        * 24140.16
        / 4.1424746e-05
At the "You want" prompt, it wants you to type in the units to convert to. If you're doing purely numerical calculations, just hit enter. Note that it prints two numbers when doing unit conversion. The number following the * is the answer you want. The number following the / is what the answer would be if you reversed the units; this is in case you make a mistake.

--Quentin



[ Reply to This | # ]