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

A simple command-line calculator UNIX
tc is yet-another-calculator. This time written in tcl, and it's very easy to personalize. It has a few symbols showing the ways you can customize it. With this script (I put it in ~/bin/tc; chmod +x and rehash), you can write arithmetic expressions in a natural way, avoiding the use of the * and () signs that are preinterpreted by the shells. It is also very easy to add symbols that are replaced before the expression is evaluated. I use it also as a euroconverter, showing how easy it is to expand its behaviour. For example:
  % tc sin 3 eu
sin(3) * 166.386 = 23.480393661
% tc 2 2
2 * 2 = 4
% tc 2 x 2
2 * 2 = 4
Read the rest of the article for the script...

  #!/usr/bin/tclsh
# define your own symbols here
set eu 166.386
set pts (1.0/166.386)
set l $argv
# add your substitutions here
regsub -all x "$l" * l
regsub -all eu "$l" $eu l
regsub -all pts "$l" $pts l
regsub -all {([\d\.\)]) +([\(\.\d])} $l {\1 * \2} l
regsub -all {(\w) +([\(\.\d]+)} $l {\1(\2)} l

puts "$l = [eval expr $l]"
[Editor's note: I've tested this, and it works just as described.]
    •    
  • Currently 3.00 / 5
  You rated: 3 / 5 (3 votes cast)
 
[7,030 views]  

A simple command-line calculator | 5 comments | Create New Account
Click here to return to the 'A simple command-line calculator' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
HP 48GX emulator
Authored by: twalkabout on Feb 24, '02 09:48:04PM

Although not entirely related to a command line calculator -- I think the best calculator is the Hewlett-Packard 48GX -- which I used to use all the time for my undergrad degree. Although the RPN takes a bit of time to get used to (about a week or so) -- after you do get used it -- then you'll realize how much easier it is. RPN basically works as follows: Instead of entering "1 + 1 =" you would enter "1 ENTER 1 ENTER +" Which seems completely stupid, until you start to use it for a while, and then it suddenly makes so much sense you won't be able to go back to a normal calculator. Especially for complicated calculations.

There is an amazing emulator for OS X at http://www.markus-fritze.de/x48/


--tom--



[ Reply to This | # ]
HP 48GX emulator
Authored by: babbage on Feb 25, '02 08:27:17PM
It's called 'postfix' math, and the HP calculator is a common teaching tool in introductory programming classes, because [a] it's a great way to reduce ambiguity in your notation, and [b] it's a great way to teach about stack based data structures. Consider:
  • "normal" infix math:
    3 * 4 / 5 = ???
    parentheses are needed to solve the problem, because there is more than one valid way to parse the equation.
  • postfix math:
    3 4 * 5 / = 12 5 / = 60
    or
    3 4 5 / * = 3 4/5 * = 2.4 [or 2 2/5]
    no ambiguity, and solving the problem is a simple series of repitions

The thing to notice is that you keep adding values to your 'stack' until you hit an operator, at which point you pop the top two items off the stack and apply the operator to those two items, placing the resulting value back on the stack, and you repeat this process until the stack is reduced to a single value. figuring out the strategy used by HP's calculators is a great way to learn about stack structures, and knowing how to work with stacks makes all kinds of programming problems easier (stacks describe the most recent web pages visited, the order you're allowed to use tags in valid HTML/XML documents, etc).

I hated that class but I'm glad I understand the material... ;)

[ Reply to This | # ]

two other calculators
Authored by: Anonymous on Feb 25, '02 10:29:18PM

There are two other calculators that come with OS X. bc is an infix calculator, and dc is a postfix calculator.

bc - looks "normal"

dc - works like an HP calculator



[ Reply to This | # ]
two other calculators, but...
Authored by: juanfc on Feb 27, '02 06:27:52AM
Thanks a lot for the notes about bc and dc. These are very serious calculators... I think I met with them some time ago. I am surprised specially with the ability of managing arbitrary long precition arithmetic.
  echo "scale=500; 4*a(1)" | bc -l  
shows to you 3.14.... with 500 digits!! (although a bit slow!). But my original idea stands, since I want only calcs like
 tc 2 2 
or
 % tc 2+sin 3 
that gives directly
 2+sin(3) = 2.14112000806  


[ Reply to This | # ]
two other calculators
Authored by: danmccomb on Mar 05, '02 11:38:13PM

An easy way to do quick math at the command line is simply type "echo 2+3 | bc" replacing 2+3 with your own math expression.

That way you can feed the string into bc and you'll get "4" back as standard output.



[ Reply to This | # ]