News for hackers. Stuff that matters.
Re:Please put units in names by crdrost on Sunday March 20, 2022 @11:50PM (#30750012)
I mean in their defense I see almost nobody doing this right... Like to do full SI you really need 7 rational numbers, maybe a sort of inline “NaN” equivalent for when you add things with incommensurate units... you might also want display hints for preferred SI prefixes (might just be a dedicated format type), and while you're at it you might as well use a Decimal type instead of doubles, oh and probably these numbers should have an uncertainty in them, so probably you want a modular system where you can mixin units or mixin uncertainty and you can start from a base of doubles or decimals or, hell, you start experimenting with continued fractions...Sigh. The real world is complicated.

Every implementation of units is secretly trying to be Moment.js, basically.

Re:Please put units in names by int_19h on Monday March 21, 2022 @04:57AM (#30751823)
All you need is a language that actually incorporates units of measure into the type system - i.e. you can define units orthogonal to types (including relationships between units), and then you can specify both the unit and the underlying numeric type in a declaration.

https://docs.microsoft.com/en-us/dotnet/fsharp/language-refe...

This can also be pulled off with somewhat less pleasant syntax on top of a sufficiently flexible parametrized type system - e.g. C++ templates.

Re:Please put units in names by wnoise on Monday March 21, 2022 @12:14AM (#30750172)
And yet people actually do manage meaningful unit systems that don't allow this.
Re:Please put units in names by account42 on Monday March 21, 2022 @10:33AM (#30754578)
C++11's std::duration manages just fine without that much scope bloat. operator* for two durations is simpley not defined so will lead to a compile error.
Re:Please put units in names by tfigment on Monday March 21, 2022 @12:18AM (#30750206)
Company i worked for 20 years ago had commercial engineering modelling program that did all physical types correctly and did scaling for user but it was fairly unique in scope and like you say almost all programming languages fall short here and it had its own quirks.