50 ms·
"Hacker News failure caused by two units 12 orders of magnitude apart sharing 2-letter code"
by skykooler 2y ago
"Hacker News failure caused by two units 12 orders of magnitude apart sharing 2-letter code"
- Nevermark 2y agoWe better get this sorted out before open source manned Mars missions. That all programming languages, down to statically typed assembly, don’t support something as simple to validate as unit consistency says something strange about how the science of replacing unreliable manual processes with automated systems is really bad at the practice of replacing its own risky manual processes with automated systems. If numeric types just required a given unit, without even supporting automated conversions, it would make incorrectly unit-ed/scaled literals vastly less likely.
- astrange 2y agoSome languages do; F# and Ada have units. I agree no sexy languages have it, and almost all languages have terrible support or anti-support for correctness in numerical programming. It's very strange. (By anti-support I mean things that waste your time and make it harder. For instance, a lot of languages think "static typing" means they need to prevent you from doing `int a,b; short c = a * b;` even if this is totally well-defined.)
- KerrAvon 2y ago`short c = a * b` can be both well-defined and a serious bug if (a * b) is greater than `sizeof(short)`. Whether it _is_ a bug depends on what you're doing. Swift has units as part of the standard library. In the sense that matters here, Rust and C++ could also have units. It requires a level of expressiveness in the type system that most modern languages do have, if you put it to use.
- astrange 2y agoIt can be a serious bug if it overflows and you didn't intend it to happen or you expected overflow behavior to do something different. But that's also true of `int c = a*b`, and yet that's not a compiler error. int/short should be thought of as storage size optimizations for memory. They're very bad ways to specify the correct range of values for a variable. (Ada has explicitly ranged integers though!)
- rkagerer 2y agoWhenever it's vague, I include the units as part of the parameter name (eg. delay_ms). Not perfect, but for practical purposes it helps. It's simple and I'm not sure why there aren't more people/libraries doing this.
- eru 2y ago> That all programming languages, down to statically typed assembly, don’t support something as simple to validate as unit consistency [...] Many programming languages are flexible and strong enough support this. We just don't do it by default, and you'd need libraries. Btw, units by themselves are useful, but not enough. Eg angular momentum and energy have the same units of Newton * metre, but adding them up is not recommended.
- jjk166 2y ago> Eg angular momentum and energy have the same units of Newton * metre, but adding them up is not recommended. The unit of angular momentum is kg.m^2.s^-1, you're thinking of torque. Although even then we distinguish the Newton meter (Nm) from the Joule (J) even if they have the same dimensionality.
- eru 2y agoThanks, yes, I meant torque. Well, 1 J = 1 Nm, the differentiation you mention only helps humans a bit, but would be really hard to make work for a computer.
- jjk166 2y agoI don't think it would be too difficult for a computer to handle. We already deal with situations like float(1) = int(1), it doesn't seem any harder to handle torque(1) = energy(1).
- eru 2y agoThe problem is that you want torque not being equal to energy. Specifically you have: torque = force * distance energy = force * distance The only difference being that in the former the farce is perpendicular to the distance, and in the latter it's in line with the distance. A vector based system could distinguish the two, but you don't always want to deal with vectors in your computations. (And I'm fairly sure there are problems where even using vectors ain't enough to avoid this problem.)