News for hackers. Stuff that matters.
Re:Please put units in names by svnpenn on Sunday March 20, 2022 @11:59PM (#30750078)
If you exponent time.Duration, that's a user error.

What you're suggesting would be like a compiler error for multiplying two int64s

Re:Please put units in names by linkdd on Monday March 21, 2022 @12:34AM (#30750312)
What they're suggesting is that `time.Duration` should not be an int64.

  1 second * 1 second = 1 second²
  1 meter * 1 meter = 1 meter²
  1 meter / 1 second = 1 m/s
Those are not user errors, those are physical values. A physical value is two things:

  - a scalar (int64, float, ...)
  - a unit (meter, second, inches, ...)
If the type system of your programming language does not allow you to define units, this should at least be a structure with a scalar and an enum, and functions to cast from one unit to another (if possible).

Working with units is a common thing in science.

Re:Please put units in names by skissane on Monday March 21, 2022 @12:48AM (#30750409)
> A physical value is two things:

I would argue it is actually three things: a scalar, a unit, and an indication of error – which is at least another scalar, but there are multiple ways of expressing error, so it might require more than just a single scalar (such as an interval and the probability the actual value lies within that interval.)

> If the type system of your programming language does not allow you to define units, this should at least be a structure with a scalar and an enum

Ideally more than just an enum – Newton = kg*m*s^-2 (equivalently kg^1*m^1*s^-2) – which suggests a set of pairs (unit and exponent).

Re:Please put units in names by linkdd on Monday March 21, 2022 @01:02AM (#30750485)
Yes, thank you for the precisions, this just make my point stronger: int64/float are very ill-suited to represent such values.

And it's especially true for time units.

For example, "how many seconds is one month?" does not make sense, but "how many seconds is january/february/march?" does make sense. The unit "month" does not really exist, each calendar month is its own unit.

And "february" is not even a "stable" unit because sometimes it's 28 days, sometimes it's 29 days. Even a minute can some rare times be 61 seconds.

This is why in physics, we use seconds multiplied by powers of 10 and nothing else.

To my knowledge, there is not a single programming language that differentiate a "scalar" and a "quantity" (scalar, unit, error).

Re:Please put units in names by conradludgate on Monday March 21, 2022 @03:10AM (#30751113)
It should be fine to multiply two durations, but it should not return the same time.Duration type