Calculators that do what you want

June 7, 2009

I have been on a hunt for the sort of calculator a scientist really wants, and thought I would share some results with youse all. But first let me explain :

I often feel the need for a quick calculation whose complexity is somewhere  between a simple sum and a full blown programme. The sort of thing I have in mind is “how much rest mass energy is in that star ?”, or “what would be the black body luminosity from that object?”. You don’t want to write and compile a C program with loops and so on; but neither do you want to navigate lots of menus and laborious button clickings. You just want to jump in and type 2.3 * Msun * c^2 and get the answer. Or for the second example, something like R=3.28e6 and then T=11000 and then 4*pi*R^2 * BB(R,T); and then T=12000 and repeat. You get the idea.

Unix purists would now explain that you have some of this functionality pre-supplied with bc , and in fact you can do calculations with awk, but these are pretty clunky. These days I do this sort of thing with Python in interactive mode. After firing up Python I load up a short module containing some constants – i.e. I put this file in my PYTHONPATH and type  from astroconst import *.  Then indeed I can just type 2.3 * Msun * c**2. I keep meaning to add some simple functions to address the second type of example, but haven’t gotten round to it. Then I began to wonder the other day whether there are tools out there somewhere that have already done this, so I did a bit of a web crawl. There are gazillions of online and downloadable things that emulate traditional button calculators, some of which have good maths and science-y buttons, but this is not what I was after. There are also web based things for specific purposes – like Ned Wright’s excellent cosmocalc – but again not what I was looking for.

I turned up several very interesting and impressive tools. Give ’em a go.

Plain Calc. Mac only. Plain is the word. When you launch it, there is nothing but an empty white window. Beautiful ! But you can type expressions, create variables and use them, and define one-line functions. The history of what you have done scrolls back up, just like a terminal window. You can save your current worksheet and reload it later; so this way you can save your favourite astro constants etc, and get them back tomorrow. However, what you save and reload is literally the worksheet you were in the middle of, complete with the whole history, whereas what you really want is to simply save the variable and function definitions. So… this app is really fantastic and almost exactly what you want, but … maybe just too plain for some; Mac only; and creating your saved “astro-world” is a bit clunky.

PEMDAS Another Mac App. Similar to Plain Calc but with just a little more GUI-ness, so you can for example see your saved variables at a glance. On the negative side, it doesn’t have user-defined functions. Its at an early stage (0.2.4) so a lot of things could improve… Once its finished its going to cost money – but not much.

Google calculator Now why didn’t I know this before ? In any Google search bar, try typing 3.42*sqrt(13)/(4.2*sin(16)). Even weirder, try G/h. The beast obviously knows some physical constants as well as maths functions. But the “more about calc” link above doesn’t tell you what constants are available….

SpeedCrunch This is available for Windows, Mac, and Linux. Like PEMDAS it combines a plain typing window with GUI elements, but you can switch these on and off. (e.g. you can have it with or without a keypad). It is very nicely designed and easy to use. You can define variables and display their current state. It has an extensive set of built-in constants, but you apply these by clicking on them. They don’t have variable-like names that you can type (e.g. “Msun”).  SpeedCrunch has a very extensive built in function list, but doesn’t have user defined functions, although it seems this is on the to-do list. Like Plain Calc, you can save your session, and when re-loaded you get the whole history. So… this a fantastic app, and is clearly going to get even better, but … its a shame it doesn’t have user-defined functions, and the lack of names for the constants is a niggle.

Python As discussed above, running Python in interpreter mode does pretty much what you want. It comes pre-supplied with Mac and most Linux distros, and is pretty easy to install on Windows.  It has a wide set of functions, you can create variables as you wish, and you can define functions. You can write your own modules with a library of constants or simple functions which you can then use. So thats pretty much everything..  But … there are some useability niggles. (i) Integer division gives the “floor”, i.e. 7/2 gives 3, and 7/9 gives 0. If you really want 7.0/2.0 = 3.5 you have to explicitly put the decimal points in. (ii) Results automatically come with seventeen figs, until a number is too big. So 1.3e7*2.4e9 gives 31200000000000000.0 whereas 1.3e8*2.4e9 gives 3.12e17. If you are writing a module, you can format output however you like, with all that %f stuff etc. But in interpreter mode you are stuck with the default. There doesn’t seem to be a command for “switch to 3 sig figs” or “output everything in exponential notation”. (iii) Python stores reals as integer fractions. This produces some peculiar results. If you define “x=0.3” and then type “x” the interpreter returns 0.29999999999999999. This is all perfectly logical, but like the 7/2 business, the result is that the Python interpreter does not behave like a human would like his/her calculator to behave.

Calc. This is a Linux thing, but is also installable on Mac with Macports or Fink. I haven’t had time to look at this properly yet, but it seems to be a mini-programming language with a syntax pretty much like C and the ability to save and load scripts as well as variables. So it may well be the bees knees, but I couldn’t see an obvious advantage over Python, or a nice tool like Speedcrunch. But may I was just running out of steam.

So, lots of really good things but nothing quite right. Can any Python pundits out there tell me how to fix those interpreter mode niggles ?