Quick unit-aware calculator

I'm very impressed with Qalculate!.

Qalculate! screenshot


insect has both web- and terminal-based versions. It does support parsing, handling and conversion of physical units, for example:

>>> 2min + 30s

   = 2.5min

>>> 40000km / speedOfLight -> ms

   = 133.426ms

>>> 6Mbit/s * 1.5h -> GB

   = 4.05GB

>>> 2J·s + 3W

  Unification error:

    Cannot unify unit W (base units: kg·m²·s⁻³)
            with unit J·s (base units: kg·m²·s⁻¹)

For a easily scriptable solution you might want to have a look a the sympy python module.

$ cat conv.py

#!/usr/bin/env python
import sys
from sympy.physics.units import *
from sympy.printing.pretty.pretty import pprint
from sympy.abc import x, y, z
from sympy import *

# adding extra units is easy
parsec = 3.26163626*ly

if __name__ == '__main__':
  s_input = sys.argv[1]
  s_unit  = sys.argv[2]
  input = eval(s_input)  # input string
  unit  = eval(s_unit)   # output unit

  print 'Converting:'
  pprint(input)
  print
  print str((input/unit).evalf()) +' '+ s_unit

which gives e.g. for some moderately ugly expression converted to mm:

$./conv.py 'tanh(3*m/(2*m))*sinh(60*deg)*1*parsec' 'mm'


Converting:
                           /π\
3.08574615554565e+16*m*sinh|--|*tanh(3/2)
                           \3 /

3.48955431541888e+19 mm

Of course this is really studpid code that does no checking thet unit compatibility of the input and output, so you might end up extra units in the result.

$./conv.py 'c' 'parsec'

Converting:
299792458*m
-----------
     s

9.71539598165644e-9/s parsec