Show HN: Insect – a high-precision scientific calculator with physical units
insect.sh
insect.sh
I would love to see two more things:
1. Propagation of uncertainty.[1] I often yearned for a calculator that automatically propagated uncertainties for me while writing my (high-school) lab reports. I think it would be life-saver functionality for many students at the very least.
2. True high-precision. I don't know how Insect works under the hood (so maybe you are already doing it this way) but instead of using high-precision results of the operations, store the operations themselves and calculate the final result at the end with the desired amount of precision.
I am aware that both requests requires a tremendous amount of change so you might as well think of them for the next major version! =)
[1]: https://en.wikipedia.org/wiki/Propagation_of_uncertainty
Unfortunately, it is not a simple change, no :-/
- Most existing calculators that offer propagation of uncertainty use a simplified formula -- the one with the square root and the partial derivatives. That's OK for lab courses. But in real life, the distributions of the input parameters are non-Gaussian, the formulas depend on each other in subtle ways that are easy to miss, and you have to deal with systematic and statistic errors (precision vs. accuracy). Most researches I know, myself included, sort-of fudge this with pen-and-paper and Excel, and end up underestimating their errors... In my view, the best solution is a Monte-Carlo type calculation, where you draw the input parameters from their respective distributions, then do the calculation, and repeat 10000 times. The mean and the std deviation gives you final result.
- Arbitrary precision is doable, but often unnecessary. To me, it was always more important to know when the result is numerical garbage (in which case I rearrange the equations to make them more computer-friendly), than to choose some precision up-front, hoping it would be enough. For me, interval arithmetic [1] is the best solution. All numbers are represented by the next higher and next lower floating point number, and each calculation results in a new interval, where the result is guaranteed to be in the interval. Often, the intervals blow up unnecessarily, but are still much smaller than the desired accuracy or precision. These days, whenever I get an unexpected result, I repeat the calculation with interval arithmetic in Mathematica or Matlab to exclude numerical errors. Interval arithmetic is not very costly in terms of run-time, and there are great libraries out there.
The unit-aware, uncertainty aware calculator that I ended up using for my PhD-thesis was Mathematica. Mathematica deals with units somewhat well and iterates over lists natively. You can write standard formulas and feed them lists for the Monte-Carlo calculation. However, unit calculations are extremely slow. My workaround split the calculation into one unit-calculation and 10000 unit-less calculations. However, I ran into so many Mathematica bugs and quirks that hosed the calculation ("transparent" Mathematica-updates that changed the result, file corruption (!), swalled minus-signs (!!)) that I cannot possibly recommend Mathematica for anything but toy projects.
Great work! I will keep a close eye on this awesom project!
EDIT: Missed the specific request for interval arithmetic part for representation of numbers. Thought that was a proposal for the error propagation (which requires actual distributions and not simple min and max intervals).
Also, frink does not support accurate interval arithmetic for all operations, and reverts to numerically accurate bounds instead (https://frinklang.org/#IntervalArithmeticStatus). I don't know if Mathematica is any better for those.
https://github.com/sharkdp/insect/blob/master/media/insect.s...
Also while you "can safely assume that butterflys are deformed when they are pinned", you can also safely assume vast majority of the world doesn't know that, and you can also assume many people might want to reconsider using a dead butterfly as a symbol once they learn the image doesn't show what they thought it shows.
AKA regular, friendly, and completely warranted pedantry.
if you look at the previous thread(o) i think gp is either directly of indirectly referencing most of the comments therein are of the "now you've watched it you can't unwatch it" variety(i)
now, should a meta joke be the top comment about this tool? i suppose that is for the forum to decide..
but the op may find it useful information.. perhaps thae missed the previous thread
op: awesome tool
Something I wish I'd had when I was studying Physics.
Currencies I can understand hitting a remote server, but general unit conversion and basic arithmetic?
Unfortunaetly, the conversion 30 miles per gallon in liter/100 km still displays an equal sign, which is technically wrong. However, this conversion is so useful, that I won't complain about it.
10EUR + 11 USD + 1500 USD + 500CAD in ZAR
https://i.redditmedia.com/4FDIQLPJFtaOSXIDOlj6I9lCpVYdz8O06Y...
GNU Units is available under homebrew (as "gunits" IIRC).
Old discussion: https://news.ycombinator.com/item?id=13909631
I have only one minor issue: the REPL scrolling conflicts with the default scrolling acceleration on macOS. So just a very small movement on the trackpad and it scrolls all the way up or down. I believe this is due the "jquery-mousewheel" plugin [1].
I'll look into this. Thank you for the reference.
≫ 6 megabytes to bytes
6 MB -> B
= 6000000 B
Assuming you're sticking with the power-of-ten unit, that means you should really grow support for the (sigh) "mebibytes" family of units; i.e., what some folks are retro-actively calling the power-of-two byte unit. ≫ 6 mebibytes to bytes
6 × mebibytes -> B
Unknown identifier: mebibytesIt is more properly referred to as "MeB" - "mebi", which became an ISO standard prefix in 2008.
But hmm..."Megameter" "Megaliter" "Megagram"
≫ e*1e15
e × 1000000000000000
= 2718280000000000
Needs a bit of work, I think. >>> tan(90 deg)
tan(90°)
= 14444300000000000
>>> tan(pi/2)
tan(pi / 2)
= -70710700000000000000
>>> tan(180 deg)
tan(180°)
= -1.38463e-16
tan(90°) and tan(π/2) should both be undefined, but in Insect, they a) have a value and b) are not even equal.Weirdly enough, cos(π) and cos(2π) are exactly -1 and 1.
This can easily run local. If you prefer online repl, it's available on repl.it [1]. There you can keep your scripts in the cloud for later, with rudimentary versioning.
≫ 1 Pa * 2 m^2 / N
1 Pa × (2 × m^2 / N)
= 2
but doesn't automatically convert a result to N? ≫ 1 Pa * 2 m^2
1 Pa × 2 × m^2
= 2 m²·Pa
Same with W, J, s and all their relations.Automatic simplification (as opposed to explicit conversion) is a complex topic. There are a few simplifications that are already applied, for example:
20 L / m^2
= 0.02 m
You are absolutely right, it'd be nice to have "Pa·m²" be converted to "N" automatically. I've been thinking about simply going through a list of simple, standardized units (SI units like Newton) while trying to convert the result into these simple units. However, notice that this is not always what you want. If someone likes to compute in imperial units, we would rather leave all quantities in imperial units.The Imperial system uses the same names for many units but the sizes of several important ones are different.
For instance Insect says:
≫ 1 gal/1litre 1 gal / 1 L = 3.78541
If this gallon were really Imperial then it would say.
≫ 1 gal/1litre 1 gal / 1 L = 4.54609
This is mostly because 1 pint US is 16 fluid ounce US but 1 pint imperial is 20 fluid ounce imperial.
But be careful because 1 fluid ounce imperial is 28.4131 ml, whereas 1 fluid ounce US is 29.5735 ml.
≫ exp(2*kg/s)
exp(2 × (kg / s))
Conversion error:
Cannot convert quantity of unit kg/s to a scalar
Excellent!Is there any way I can save a list of variables to file and then reload them?
I also would like to vote for supporting imaginary numbers (Issue #47).
> Is there any way I can save a list of variables to file and then reload hem?
Not yet, no - thank you for the suggestion. The command-line version can read from a file, though (https://github.com/sharkdp/insect/issues/40)
Plain and easy to understand interface and excellent use of colour and space. Two suggestions:
1. While I doubt it'll be used very much, consider adding calories for completeness if nothing else:
1000 kcal -> joules
or 1000 Cal -> joules
You might also throw in cal for just for fun[1]!2. Change Variables to Constants. I think this is more in keeping with standard jargon.
[1]: cal is based on the gram while Cal or kcal is based on the kilogram.
1. Yes, I'll definitely add calories, good suggestion.
2. Yes, that's probably a good idea. I should distinguish constants and (user-defined) variables.
≫ 1/12 c
1 / (12 × c)
= 2.7797e-10 s/mThis is on purpose (see operator precedence rules: https://github.com/sharkdp/insect#reference). Implicit multiplication (without an explicit multiplication operator) has a higher precedence than division in order for something like this to work:
tan(15cm/3m)
= tan(15cm/(3m))
On the other hand, explicit multiplication has a lower precedence than division, so you would have to write "1/12*c". I agree that it can be confusing at times (that's why there is a pretty printer), but I don't want the language/parser to be whitespace-aware. 1/10 m -> 0.1 / m
1|10 m -> 0.1 m
27 ^ 2/3 -> 243
27 ^ 2|3 -> 9
You get the gist of it - a division operator with insane precedence.1/4 s - > (1/4) s
1 m / 4 s - > (1 m) / (4 s)
1 cup of butter -> g
Not sure if it's quite scientific though... :) (density of gold) * (average volume of human)
-> 1282 kg
And if you do: (density of gold) * (average volume of human) * (price of gold)
It will even show you a plot of the time series of its value since the early 20th century.I'm trying to think of how you'd specify density of butter, perhaps "butterdensity", cognate, "earthmass".
I've got a number of geographic areas specified, so I can give you, say, paper size in milli manhattanarea.
Still, it's pretty good.
≫ sqrt(1/(eps0 mu0)) ->m/s
sqrt(1 / (eps0 × mu0)) -> m / s
= 299833000 m/s
≫ sqrt(mu0/eps0) ->ohm
sqrt(mu0 / eps0) -> Ω
= 377.031 Ω ≫ sqrt(mu0/eps0) / ohm
sqrt(mu0 / eps0) / Ω
= 376.73
Also seems like the constants (!) got changed a bit since you executed thisUS Engineering units of energy would also be help certain people: such as BTUs British Thermal Units etc.
They are useful, of course, when it comes to simple unit conversions. But they lead to all kinds of problems if you start using them in calculations (which is what Insect is mostly for).
If not, is the conversion logic in an npm package?
sin (30 rad)
sin × 30 rad
Unknown identifier: sin
Wouldn't it be great if software evaluated things, and if things didn't made sense, considered what the possible alternatives were, a bit like semantic checkers for spelling and grammar in text? sin(30 rad)
= -0.988032 2**100+1-2**100
Equals 0?The readme also advertises it can deal with 10^(10^10), which it can (doubles can't deal with this, so it's definitely not just using Javascript doubles). The part which prints out numbers rounds to 6 places, but I don't see any place which rounds intermediate results.
Edit: Found where 30 digits comes in. https://github.com/sharkdp/purescript-decimals/blob/ad719fc7...
I tried "4 tbsp to oz" and it interprets oz as mass instead of volume. Google correctly gives me 2 as the answer.
Having to learn that you can type "floz" is better than not being able to do it at all.
(You could also allow "fl oz", "fluid ounce", "fl ounce", etc. if you tweak your parser a little bit, but that's the deluxe version, and any functionality at all would be a big improvement.)
I use the similar Soulver on my Mac and iOS devices.
I probably mostly use Numi for unit conversions, but asked for in a 'human' way. eg "68 square meters in square feet"
Any hope of an Android app version soon?
Note: if you're on MacOS and are using the supplied 'units' utility, that is BSD, not GNU units. You're going to want to install gunits from Homebrew.
40000 km / c -> ms
= 133.426 msCan you stop the cursor blinking?
If you do something like pi 1e20, I think it should print out all the digits it has instead of printing zeros.
And it looks useful.
Unfortunately that means you will be inundated with requests for further improvements. :-)
I see that you have a built in constant for μ0, mu0.
But it evaluates to
0.00000125664 N/A²
for the ultimate in precision surely it should be expressed as 4π×10−7 N/A²
At least when I use mu0 in spreadsheets, etc., I always define it that way, easier to remember too. And sometimes it lets me see that the pi cancels out. Unknown identifier: rpm≫ round(pi * 1000000000000000000) = 3141590000000000000
(similar comment by @btown down there about e)
Floating point numbers are displayed with 6 significant digits
Integers are displayed with all digits, unless there are more than ~20 digits, then it is displayed with exponential notation
$ cat .units
period(len) units=[m;s] 2pi*sqrt(len/gravity) ; (period/2pi)^2 * gravity
$ units
2980 units, 109 prefixes, 97 nonlinear units
You have: period(20cm)
You want:
Definition: 0.89729351 s
You have: period(20cm)
You want: ms
* 897.29351
/ 0.0011144625
You have: period(2ft)
You want: ms
* 1566.5419
/ 0.00063834872
You have: 5 GiB
You want: bytes
* 5.3687091e+09
/ 1.8626451e-10
You have: 5 hundred million
You want:
Definition: 5e+08
You have: tempF(100)
You want: tempC
37.777778
is it supposed to be a competitor? learn how to use a new language? I don't get it.I like the idea of keeping units, but I'm not sure this makes things easier:
≫ V * A / J
V × (A / J)
= 1 V·A/J
You have: V*A/J
You want:
Definition: 1 / s {~} % qalc
> c to km s^(-1)
speed_of_light = approx. 299792.46(km / s)
> 10 kg to g
10 * kilogram = 10000 g
> $10 to euro
dollar * 10 = approx. EUR 8.9501477
> 100oF to oC
(((100 * kelvin) + (459.67 * kelvin)) * 5) / 9 = approx. 37.777778 oC> is it supposed to be a competitor? learn how to use a new language? I don't get it.
No reason to be confused. GNU units is a great tool. qalculate and speedcrunch are great, too. Insect is just my take at it. Each of these tools has advantages and disadvantages. As for insect, one of the advantages for me is that it is platform-agnostic and can be used from anywhere without installation. The "You have: X; You want: Y" interface is basically replaced by "X -> Y".
wouldn't emscripten work great then? maybe you could add support for definitions in "units" format?
Oh, common.
Someone wrote something that's similar to X. If you are so well versed in GNU tools, you surely old enough to know that this happens all the time, sometimes intentionally, sometimes not. People like scratching the proverbal itch and creating stuff. What's there to "not get" and what's the point in feigning a surprise and implying OP's ignorance along the way?
If you want to mention that X exists, just say that -"There's X, which is similar. Have you seen it?"
1. It's written in PureScript.
2. It's MIT licensed. (Actually I had to search the repo to find this - it would be nice if it had a LICENSE file at the top-level that GitHub could pick up.)
Anyway, IMO monocultures suck and competition is good. Kudos to the OP!
3:20:36 / 26.2
7:35 * 26.2
≫ U = 1e-6/60 * kat
U = 1.66667e-8 kat
Because now it does this ≫ 1 kat -> U
1 kat -> U
= 1 kat >>> U = 1e-6/60 * kat
U = 1.66667e-8 kat
>>> 1 kat / U
1 kat / U
= 60000000 ≫ 1 month -> days
1 month -> d
= 30.4167 d
≫ 1 year -> days
1 year -> d
= 365 dRemember Graphing Calculator? That could use such features.
Autodesk Inventor understands units in formulas, but mostly for length and angle. Everything becomes meters internally.
E.g. "6Mbit/s * 1.5h as GB"
I've been using it for a while now and I can't imagine going back to a traditional calculator
Wut? Looks like some strangeness for the parser.
I'd be cool to have this as a python script somewhere but I am not quite sure wether I would visit this site whenever I need to calculate some physical units, especially since google already covers most of my needs
dont get me wrong, I think otherwise it looks and feels great and is easy to use but I dont know who will use this.
* The REPL: I wanted an interactive terminal-style calculator where I could press Arrow-Up and easily change a few things. It also lets me define my own variables and use them in my calculations. It saves the history so I can come back and press Ctrl-R to find a recent calculation.
* Combination of Web- and terminal version. I often work in the terminal instead of the browser where I can easily use the CLI version of insect.
3 5
which was interpreted as 3 * 5. Seems a bit risky to me :) Otherwise looks neat. 2*sin(5*cm/(2*m)) ≫ 1 mile / 2 km
1 mi / 2 km
= 0.804672
Great, but it would be nice to show the result's unit. ≫ 1/2km
1 / 2 km
= 0.5 km⁻¹
It's been a long while since I did any real maths, so I'm slightly stumped as to why the -1 exponent is there.In your second example, the input gets parsed as "1/(2km)" which is equal to "0.5 km⁻¹". If you wanted half a kilometer, you'd have to enter "1/2*km"
>>> 6 Mbit/s * 1.5 hours to GB
(6 Mbit / s) × 1.5 h -> GB
= 4.05 GB 2 min + 35 s + 3 min + 54 s
= 6.48333 minHigh-precision!
Units is a bonus but really just the calculator is less frustrating than anything else I've ever seen. Google search is too unpredictable - morphing into a typical confusing 1970's style keypad design after your first calculation. I tried a couple of desktop applications and they were no better either. Come one calculator app designers - stop trying to copy a physical calculator. Those already have a terrible design and computers don't have the same constraints or freedoms as them.
Even better, it can act like a desktop Windows app simply by saving the page in Chrome! Beat that for latency, Google!
Meaning of life
Meaning × of × life
Unknown identifier: MeaningQuick workaround:
≫ Meaning = 6
≫ of = 1
≫ life = 7
Meaning = 7
of = 2
Life = 4
≫ 7.8L/100km -> miles/gallon
7.8 L / 100 km -> mi / gal
Conversion error:
Cannot convert unit L/km (base units: m²)
to unit mi/gal (base units: m⁻²)https://www.google.com/search?q=5+l/100km+to+mpg&ie=UTF-8&oe...
$ units
Currency exchange rates from www.timegenie.com on 2016-06-21
2926 units, 109 prefixes, 88 nonlinear units
You have: 7.8L/10km
You want: miles/gallon
reciprocal conversion
* 3.0155716
/ 0.33161209
I think it's fine for a calculator to make guesses as long as it makes it very clear that it's doing so. ≫ 100km/7.8L -> miles / gal
100 km / 7.8 L -> mi / gal
= 30.1557 mi/gal