Calculators that do what you want

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 ?

20 Responses to Calculators that do what you want

  1. VK says:

    I, too, have long looked for such a beast, and I believe I’ve found the ultimate pet calculator: wolfram|alpha.*+mass+of+Sun+*+velocity+of+light2

  2. Nick Cross says:

    Wolfram Alpha looks very nice. These days I use python most of the time, which I find does most of these kind of jobs well, apart from the drawbacks that Andy mentions.

  3. Ross Collins says:

    Andy, if you do a “from __future__ import division” then “/” division works as you would expect and you then use “//” for integer division:

    >>> from __future__ import division
    >>> 1/2
    >>> 1//2

    What’s more, put “from astroconst import *; from __future__ import division” into your “~/.pythonrc” file and these modules will be automatically loaded every time you invoke the Python interactive shell.

    A lot of these issues you mention have been addressed in the new Python 3.0 and back-ported into Python 2.x. For example, the “decimal” data-type:

  4. Tony says:

    Cool stuff, Ross. What’s a good python shell (I like my command lines wrapped in a gui!).

  5. Ross Collins says:

    I don’t do much interactive stuff in Python, so generally just stick to the standard shell.

    There is iPython, which is designed to make scientific use of Python much easier. For example, it’s PyLab module adds quick ‘n’ simple plotting to the shell. You just run “ipython -pylab” instead of “python” when you’ve installed the appropriate package:

  6. Ross Collins says:

    Forgot to mention you need to point the PYTHONSTARTUP environment variable to the “.pythonrc” file too if it’s not already set.

    You can also have your interactive Python command history saved between sessions using the “.pythonrc”, see:

  7. Ross Collins says:

    …and for some more imaginative units for your Wolfram Alpha search results, possible useful for press releases:

  8. Dave says:

    Any of you out there involved in Herschel will soon be getting used to the HIPE interface which provides a GUI front end to Python (well, Jython).

    Despite my earlier worries about the system it doesn’t do too badly especially for this point in the mission.

  9. ipython -p sh gives a system shell and a Python shell all in one. Not perfect, but dead convenient.

  10. Conor says:

    As already mentioned (twice!) wolfram alpha is very good for this sort of thing, but it also permits you to do some truly obscure searches and calculations. Wanted to know the nutritional value of the moon if it were made of cheese? mass cheese

    It even helpfully tells you that you can specify the particular type of cheese you wish to use.

    Slightly more interesting:

    Apparently you can put a price on life.

  11. Simon R says:

    I use the calculator feature of Google often. The big problem (besides the lack of documentation) is that you can’t carry on from one calculation to the next. You can’t even copy an answer back in to the search box. Wolfram Alpha seems to have sorted this out, but you wonder why Google don’t develop their calculator more, to be really useful…

  12. I don’t want to get the reputation as an IDL evangelist but it’s very easy to set up IDL to do this. I have a constants definition file that I call up and I also wrote a cosmology toolkit for comoving distances, Einstein radii etc. It’s command-line driven, remembers variables and previous commands, and seems to do what you’re asking for. I’d be happy to pass my astro code on if you want it. IDL ain’t cheap, but you get what you pay for.

    As Dave mentioned, HIPE (the Herschel jython-based analysis package) will probably do similar things with built-in astro functions – if you can stand having a gazillion Gb download for a beta version.

    There are Firefox calculator add-ons that can store useful astro constants if you’re prepared to write a constants definition file. My favourite is Statusbar Scientific Calculator. This one doesn’t remember past commands though.

  13. andyxl says:

    Steve : if only I had the cash so I could test IDL and see what you mean. Definitely going to try out the Firefox things – didn’t know about these.

    Update : yup, Statusbar Scientific calculator is really good. Graphing calculator add-on is also pretty good. But the one I like the idea of best is the add-on that calculates how much time you have wasted on Facebook.

  14. Chris_C says:

    If you type into a Google search bar
    2.3 * mass of sun * c^2
    it responds
    2.3 * mass of the sun * (the speed of light^2) = 4.11136995 × 10^47 joules


  15. jz says:

    Python rocks – should we pool resources/build a library?

    PS Also not quite what you want, but for completeness (and commission-free) –

  16. Ross Collins says:

    Python 2.7 (released this week) fixes the “x=0.3” issue that you mentioned. See this talk abstract from this year’s EuroPython conference:

    And the Python 2.7 release notes:

    • andyxl says:

      Ross – isn’t it a bit barmy that there are now two python streams, 2.X and 3.X ??????

      • Ross Collins says:

        Just think of Python 3.x as a whole new language.

        C is still developed despite being “superseeded” by C++. 😉

        The problem is that Python 3.x is such a big change to the language (and its performance in some cases) that both regular users and module/library developers can’t be expected to transition their larger code bases to it quickly. It’s almost like transitioning to a new language. Also this gives time to improve Python 3.x’s performance and stability.

        The new features in Python 2.7 are also found in Python 3.1.2.

  17. […] .. so I am going to do it in installments … Just over a year ago I wrote a couple of posts (here and here) about looking for calculators that hit a sweet spot between laborious button-pushing GUI […]

  18. Alexander says:

    Thank you for this review!
    A few month ago I’ve looked for formula calculators in Google. I expected to find some lists, reviews, comparisons like this but I didn’t found them. I was interesting because I decided to implement built-in calculator in my plotting software MagicPlot and I wanted to see what other people have done in calculator user interface. I found a little. In fact I did not found even SpeenCrunch which I have found the week after I released my own calculator (it has simple functionality and is now available built-in MagicPlot and as a standalone Java app also).
    So I decided to start calculators list in Wikipedia: I think this list may be useful. You can add something you know to it. And maybe I have missed some columns in description.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: