4 ms·
In general I think software engineers do a bad job with units. We shouldn't work with floating point numbers, we should work with a number class that has a valu
by krschultz 10y ago
In general I think software engineers do a bad job with units. We shouldn't work with floating point numbers, we should work with a number class that has a value and a unit.
Prior to working on Android & iOS I basically didn't bother with i18n. Now I never hardcode a string, it's always a reference to a string resource that is easily translated. I think this is a mindset shift brought about because the tools & libraries make it easy. The same is true for datetime formatting and unicode. We're collectively much better at supporting i18n in those areas than we were 15 years ago. I think the next step is for us to do the same but with numbers.
- derefr 10y ago> we should work with a number class that has a value and a unit. Not even a unit. I've always wanted a (majorly-adopted) language and platform where "Integer" or even "Number" are low-level mostly-ignored value types, and you instead work with a type that might be called (something snappier, but equivalent to) "RationalComplexPolynomialUnitedMeasurementWithPrecision". Like, why can't my stdlib functions all take: (-5/6±3.5 + i±1/2) km^2/s^2 ...and do something sensible (stoichiometrically) with it?
- simcop2387 10y agoClosest I can come is my esoteric language I made. It's a rough clone of Frink, but diverged during development in a number of ways: http://search.cpan.org/dist/Language-Farnsworth/lib/Language/Farnsworth/Docs/Syntax.pm http://search.cpan.org/dist/Language-Farnsworth/lib/Language... It's missing the ranged/bracketed number support, but will do the rest of that. 13:27:11 < simcop2387> farnsworth: (-5/6+3.5 + i-1/2) km^2/s^2 -> "kJ/kg" 13:27:12 < farnsworth> simcop2387: (2166.66666666666 + 1000.00000000000*I) kJ/kg It'll auto-convert things into floats if you use them, but it's 100 digit accuracy (configurable). Development has halted, mostly because I couldn't decide how I wanted to tackle adding objects as a first class data type and had a number of issues with the parser I needed to solve to make my life saner (easy way to force a difference between a variable and unit named "kg" etc.) I might pick it up again and try using the parser combinators in Rust and actually compile the language down instead of an interpreter (though i'd remove eval[] to do that).
- Someone 10y agoBecause it gets messy very soon. To start with, you would have to pick a set of distributions that is closed under the set of operations you want to perform. If you pick 'all normal distributions' as your set, that means that you can only allow addition, subtraction, and multiplication by a constant (= a normal distribution with zero variance); you won't be able to compute x^2, take a square root, compute |x|, etc. That rules out, for example, computing the variance of a set of inexactly known numbers. To make matters worse, to do this really well, you have to track where values come from. You really want: x = 6±3.5m z = x - x to have z equal to 0±0m, but x = 6±3.5m y = 6±3.5m z = x - y to have z equal to 0±4.9m. Similarly, x+x should be 12±7m, and x+y should be 12±4.9m The best you probably can do is to forget about the second issue, and do interval arithmetic. That is useful, but gives you extremely pessimistic results. Edit: a good solution also would resolve the question of how to handle conditionals. if x > 0 could pick each of its branches according to the probability that x is larger than zero, or even execute both branches partially (whatever that would mean), according to the distribution of x.
- simcop2387 10y agoWhy would x+y be 12+-4.9m? shouldn't it be the same +-7m?
- nialo 10y agoThe parent is taking the error numbers as something like 1 standard deviation on a normal distribution, rather than an absolute limit. so, x = 6±3.5m means something like "x is equal to 6 + a random number taken from a normal distribution with standard deviation 3.5 centered around 0" and y is the same, but with a _different_ random number. (I'm a little hazy on exactly what distribution is being assumed, but the basic idea of x and y being different random numbers is the important thing) In principle one could also interpret x = 6±3.5m as "x is equal to 6 + a random number taken uniformly from the rand -3.5 to 3.5" or something, and then x+y would be 12+-7, but the real world numbers this is nominally supposed to represent don't really work that way. I think this is what is mean by "The best you probably can do is to forget about the second issue, and do interval arithmetic. That is useful, but gives you extremely pessimistic results."
- ced 10y agousing SIUnits, SIUnits.ShortUnits using Measurements ((-5/6±0.1) + 1im)km^2/s^2 > -833333.3333333334 ± 100000.0 + 1.0e6 ± 0.0im m²s⁻² typeof(((-5/6±0.1) + 1im)km^2/s^2) > SIUnits.SIQuantity{Complex{Measurements.Measurement{Float64}},2,0,-2,0,0,0,0,0,0} In Julia.
- khaled 10y agoAll measurements in TeX must have a unit, even if the value is zero. Only counters take unitless numbers.
- haasn 10y agoSome languages, like Haskell, let you extend the numerical types to whatever representation works well for your causes. A few years ago I wrote a library for embedding units of measure into every figure (at the type level), including e.g. auto-converting between units. It's somewhat bitrotten but it may still be interesting if you're into this sort of thing: See [1] for an example of how it looks in practice. [1] https://github.com/haasn/units/blob/master/tests/Tests.hs https://github.com/haasn/units/blob/master/tests/Tests.hs