Quick unit-aware calculator

6

3

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

endolith

Posted 2010-04-26T00:22:46.557

Reputation: 6 626

1What is the level of complexity of the problems you are trying to solve? – spowers – 2010-04-26T00:33:12.917

What did you try to script that a combination of bc -l and units couldn't do? – Benjamin Bannier – 2010-04-26T00:38:50.250

1Can you give an example of using bc -l and units together? I didn't know that was possible. – endolith – 2010-04-26T15:04:54.077

@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.

– quack quixote – 2010-04-26T15:29:15.527

@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). – Benjamin Bannier – 2010-04-26T19:48:42.317

@quack quixote: There should be a reply button for that. – endolith – 2010-04-27T23:12:20.687

@endo: suggest it on meta if you like... :)

– quack quixote – 2010-04-27T23:41:43.457

this should be on [softwarerecs.se] – phuclv – 2019-03-24T09:31:09.300

@phuclv Yes, but it didn't exist when this was asked – endolith – 2019-03-25T14:24:59.587

Answers

8

I'm very impressed with Qalculate!.

Qalculate! screenshot

Paused until further notice.

Posted 2010-04-26T00:22:46.557

Reputation: 86 075

+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) – David Z – 2010-04-26T02:26:14.500

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. :) – endolith – 2010-04-27T23:05:37.270

1@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. – Paused until further notice. – 2010-04-28T04:28:56.757

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. – endolith – 2010-04-30T18:54:01.983

@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). – AstroFloyd – 2017-04-06T17:24:00.100

This is a really nice program. Now that I've stopped using Linux, I wish it were supported in Windows. :/ – endolith – 2017-05-02T13:33:14.060

There is now a Windows version: https://github.com/Qalculate/qalculate-gtk/releases

– endolith – 2019-11-25T21:18:38.493

5

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⁻¹)

shark.dp

Posted 2010-04-26T00:22:46.557

Reputation: 151

This thinks dB means "decibyte". :/ – endolith – 2017-05-02T13:31:47.177

1

@endolith: Thanks! Tracked here: https://github.com/sharkdp/insect/issues/67

– shark.dp – 2017-05-03T21:09:46.840

2

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)

Some comments about your "ideal calculator" inputs and responses:

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"

Ronald

Posted 2010-04-26T00:22:46.557

Reputation: 21

2

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

Benjamin Bannier

Posted 2010-04-26T00:22:46.557

Reputation: 13 999

Also see https://launchpad.net/python-quantities

– endolith – 2010-04-27T11:56:59.910

@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. – Benjamin Bannier – 2010-04-27T13:41:32.870

-1

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.

Majik

Posted 2010-04-26T00:22:46.557

Reputation: 252