Interpreters as calculators


Note: This post contains errors. I’m quite sorry, but see my correction post on the difference between floating-point and integer math for details.

Sometimes when you’re sitting at a computer you need to hammer out a quick calculation. Despite not being a huge math fan, I find this to be true more often than I’d like.

The problem is, I find calculators to be fairly limited and annoying machines to work with. If you make a mistake, you have to go back and do all of those steps over again. As your math gets more complicated, this tends to get more and more aggrevating.

What does this have to do with scripting? Well, if you have a Perl, Python or Ruby interpreter on hand, you can hammer out calculations quickly using just the command line.

With Perl, just fire up a command line and type your calculations (prefaced by the word “print”) in single-quotes after “perl -e”:

kschwen$ perl -e 'print sqrt((5 + (2 * 80))/2) . "\n"'

I think it’s pretty nifty because you can see your whole equation mapped out. The ‘. “\n”‘ at the end forces the answer to print on its own line, otherwise the number would run into the beginning of your command prompt.

Perl is widely known for its command-line scripting abilities. Savvy system administrators know that executing a “perl -e” can save a ton of time when there’s work to be done. I’ve actually found a great resource on perl one-liners if you’re interested.

In Python, the easiest way is to just type “python” at the prompt, and do your calculations in the interactive interpreter:

kschwen$ python
>>> from math import sqrt
>>> sqrt((5 + (2 * 80))/2)

I like doing it this way better, as you don’t have to add print statements or force a newline at the end. Also, since you’re in the interpreter, you can import new modules from the math library to do different things as you please.

I know I haven’t really talked about Ruby, but that’s because I don’t use it. However, Ruby is interesting because you can accomplish this two ways:

kschwen$ ruby -e 'print 5 *5; print "\n";'


kschwen$ irb
>> 5 * 5
=> 25

From what I know, this is because Ruby has two interpreters. The interactive one, “irb”, is like Python’s interpreter — you can play with the language, executing different statements in your session. The actual “ruby” program interprets your script files, but can also be given the -e switch like perl’s interpreter, allowing you to execute one-liners on the command prompt.

If that’s inaccurate, please feel free to correct me, as I am not a ruby person.

In any case, doing math in a scripting language’s interepreter is an interesting and simple way to both play with a language and get some serious math done.


One Response to “Interpreters as calculators”

  1. 1 A correction: Interpters as calculators « Everyday Scripting

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: