# Quick unit-aware calculator

I want to be able to press a keyboard combination, start typing a mathematical expression that includes units and slightly advanced math (not just a four-function calculator), and get a result immediately, in units that I specify, that I can copy and paste.

• Currently I open Firefox and press Ctrl+K, type in the search box, and it usually gives me a result in the drop-down from Google Calculator. It doesn't always, though, so I press "=" at the end, wait for a result, remove the equals, wait for a result, realize it doesn't understand the way I typed a unit, open the result in a new tab, etc. it sucks.
• Wolfram Alpha is smarter, but very much slower, and the output is all images, not text, and I don't have a quick widget for it, if such a thing could even exist.
• GNU units has a ton of units, which is great, and I can define my own units, which is great, but they have to be written in specific, unintuitive ways, it doesn't handle much advanced math, and I'd need to open a terminal, start units, etc. I hate the command line.
• I wasted a lot of time trying to make front-ends for `units` in Deskbar and Launchy, but I'm not a real coder and I don't use either of those anymore.

Any other solutions or enhancements of these?

(Cross-platform is best)

The ideal unit-aware calculator would be able to handle all of these:

• `7 nV/sqrt(Hz) ⋅ sqrt(20000 Hz - 20 Hz) → μV` = 0.99 μV
• `sqrt(4*k*25 °C * 1 kΩ * (20 kHz - 20 Hz))` = 0.57357 μV
• `3.5 billion kilowatt-hours per year` = 399.5 MW
• `60 billion kWh per year` = 6.849 GW
• `1/sqrt(2 mH * 2 nF)` = 500 kHz
• `1/(2*pi*10 kΩ * 22 µF)` = 0.7234 Hz
• `1/(2·π·100 Hz·30pF) → ohm` = 53.05 MΩ
• `−10 dBV + 4 dB + 10 dB − 12 dB → dBu` = -5.782 dBu
• `94 dBSPL → Pa` = 1.00237 Pa
• `54 inches + 2 feet → cm` = 198.1 cm
• `12 V ÷ 141.5 µA` = 84.8 kΩ
• `16 bit * 44.1 kHz * 2 → kbit/s` = 1411 kbit/s
• `1 hundred V / 5 ohms` = 20 A
• `furlong per fortnight → cm/minute` = 0.9979
• `attoparsec/microfortnight → in/second` = 1.004
• `1 ft / speed of sound` = 0.89576 ms
• What is the level of complexity of the problems you are trying to solve? Apr 26 '10 at 0:33
• What did you try to script that a combination of `bc -l` and `units` couldn't do? Apr 26 '10 at 0:38
• Can you give an example of using `bc -l` and `units` together? I didn't know that was possible. Apr 26 '10 at 15:04
• @honk: sounds like something you should post in an answer (y'know, with an example). @endolith: use @ for comment notifications so the user gets notified when you reply to them. Apr 26 '10 at 15:29
• @endolith: Actually my comment was too fast, `bc` doesn't gain you anything if you do not plan to define your own functions . The trigonometric, exponential and logarithmic function are all already defined in `units` (only Bessel functions are missing). Apr 26 '10 at 19:48

I'm very impressed with Qalculate!. • +1 So am I, although my user interface (the Qalculate plasmoid for KDE) doesn't offer a way to express a result in specified units. I work around it by dividing my result by the units I want. I forget whether the full interface, if it even still exists, can do that. (Just so it's clear: even with the UI limitations, it's still my favorite calculator for anything I don't need Mathematica for) Apr 26 '10 at 2:26
• I don't see standard temperature, speed of sound, and I don't understand how it handles dB, but I am also very impressed. I wish the answers appeared after a short delay without pressing Enter, too. But these are all things I can complain about. :) Apr 27 '10 at 23:05
• @endolith: You can go into Edit>Manage Variables>Physical Constants and Edit>Manage Units and add your own or edit existing entries. I've never seen a calculator complete a calculation after a timeout. One thing I would like to see in this program is history navigation using arrow keys or scroll-and-click with the mouse instead of having to copy-paste from the history. Apr 28 '10 at 4:28
• Google Calculator in the Firefox search bar completes as I type, though I have to prod it sometimes by typing an equals sign. The thing I wrote for Launchy also displays the result as you type, though it was slow because of the way it accesses Units. Apr 30 '10 at 18:54
• @DavidZ: (meanwhile) Qualculate offers the "to" command to convert units: "2m to cm" will express the result in cm. Note that (lib)qalculate comes with qalc, a command-line version that uses readline and can recall history with the arrow keys or Ctrl-R (and other neat CLI tricks provided by readline, like Tab completion, copy/paste, undo, etc., also found in e.g. Bash). Apr 6 '17 at 17:24

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
s_unit  = sys.argv
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
``````
• Also see launchpad.net/python-quantities Apr 27 '10 at 11:56
• @endolith: It all really only depends on what kind of math you want. The basics are in `units`, but obviously it doesn't have symbolic algebra or higher math. Apr 27 '10 at 13:41

I mostly share your ideas about an ideal calculator and the disadvantages of using GNU Units and Wolfram and rolled my own online calculator which I put online at PhyCalc.com. It does its own unit algebra for SI units and uses GNU Units for conversion to other units, or as a fallback when the expression itself contains non-SI units.

Its aims to interpret Unicode input where it makes sense. And tries to be smart about guessing the exact quantity and corresponding unit. It automatically inserts SI-prefixes. (much like my trusty old Casio pocket calculator does in "ENG" mode)

sqrt(4*k*25 °C * 1 kΩ * (20 kHz - 20 Hz)) = 0.57357 μV

Its not exactly accurate to automatically assume °C to be an absolute temperature. Also k is not exactly an unambiguous symbol for the Boltzmann constant. However this should give you the required answer:

``````sqrt(4*1.38064852e-23J/K*(273.15K+25 °C) * 1 kΩ * (20 kHz - 20 Hz)) in μV
``````

These fail in GNU Units:

3.5 billion kilowatt-hours per year

1 ft / speed of sound

GNU Units alternatives could be:

``````3.5 billion kWh per year
1 ft / mach in ms
``````

1/(2·π·100 Hz·30pF) → ohm

You can leave out the "→ ohm" part. That is what I meant with "being smart about guessing the quantity" :-).

−10 dBV + 4 dB + 10 dB − 12 dB → dBu

GNU Units has some dB conversion, but it is defined as 10^(x/10) which is a scalar that it will not add to a voltage, where you assume that the calculator should recognize that it should be scaling Voltage instead of Power and should apply scaling of 10^(x/20) instead. That would be very cool, but is a bridge too far for gnu units I'm afraid. This will at least give you the correct answer using GNU Units:

`````` 1V*dB(-10)^0.5*dB(4)^0.5*dB(10)^0.5*dB(-12)^0.5 → dBu
``````

But I can see why you may find that un-intuitive.

Otherwise I think PhyCalc.com may come pretty close to your ideal calculator.

To have fast access to it, I map my dedicated calculator keyboard key to it by adding to the following string (REG_SZ) name/value pair in the Windows registry:

``````[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\AppKey\18]
"ShellExecute"="http://phycalc.com"
``````

Wolfram Alpha is capable of crunching math and doesn't require you to download or install anything.

Check here for how to use its math functions.