News for hackers. Stuff that matters.
Re:Air traffic failure caused by two locations 3600nm apart ... by skykooler on Monday November 18, 2024 @06:33PM (#42178424)
"Hacker News failure caused by two units 12 orders of magnitude apart sharing 2-letter code"
Re:Air traffic failure caused by two locations 3600nm apart ... by Nevermark on Monday November 18, 2024 @07:08PM (#42178709)
We 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.

Re:Air traffic failure caused by two locations 3600nm apart ... by astrange on Monday November 18, 2024 @07:23PM (#42178834)
Some 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.)

Re:Air traffic failure caused by two locations 3600nm apart ... by KerrAvon on Monday November 18, 2024 @07:43PM (#42178978)
`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.

Re:Air traffic failure caused by two locations 3600nm apart ... by astrange on Monday November 18, 2024 @07:47PM (#42179005)
It 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!)

Re:Air traffic failure caused by two locations 3600nm apart ... by rkagerer on Tuesday November 19, 2024 @01:52PM (#42186852)
Whenever 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.
Re:Air traffic failure caused by two locations 3600nm apart ... by eru on Monday November 18, 2024 @09:53PM (#42179674)
> 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.

Re:Air traffic failure caused by two locations 3600nm apart ... by jjk166 on Tuesday November 19, 2024 @03:22PM (#42187689)
> 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.

Re:Air traffic failure caused by two locations 3600nm apart ... by eru on Tuesday November 19, 2024 @08:18PM (#42189887)
Thanks, 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.

Re:Air traffic failure caused by two locations 3600nm apart ... by jjk166 on Wednesday November 20, 2024 @12:26PM (#42196207)
I 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).
Re:Air traffic failure caused by two locations 3600nm apart ... by eru on Wednesday November 20, 2024 @08:43PM (#42200190)
The 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.)