News for hackers. Stuff that matters.
Re:Please put units in names by deepsun on Monday March 21, 2022 @03:40AM (#30751323)
Nitpicks:

Why would your type system have encoded unit for kilo-pascal, but not hecto-pascal, mega-pascal, micro-pascal etc?

If you only encode base units (e.g. seconds), then we should use exact-precision arithmetic instead of f32 or f64, which is sometimes an overkill.

If encoding all the modulos (kilo/milli/mega etc) I feel like there are some units may have name clashes (e.g. "Gy" -- is it giga-years, or gray)?

Should we encode only SI units, or pounds/ounces/pints as well?

Re:Please put units in names by stinos on Monday March 21, 2022 @03:43AM (#30751337)
(e.g. "Gy" -- is it giga-years, or gray)

In my opinion this is not a real problem, since ideally no-one should such meaningless abbreviations in code. Just write giga_years or GigaYears or whatever your style is, problem solved, doesn't get any clearer than that.

Re:Please put units in names by timthorn on Monday March 21, 2022 @04:01AM (#30751481)
In defence of Gy, it isn't meaningless in astronomy - it's a very well used unit. Though I do agree that it might be less common in code.
Re:Please put units in names by gattr on Monday March 21, 2022 @07:32AM (#30752792)
I myself can't remember seeing Gy in astronomical papers, but I've seen Ga (gigaannum).

https://en.wikipedia.org/wiki/Year#SI_prefix_multipliers

Re:Please put units in names by timthorn on Monday March 21, 2022 @07:43AM (#30752903)
Re:Please put units in names by Aeolun on Monday March 21, 2022 @04:07AM (#30751518)
Isn’t Gy a bit much even in astronomy? With 15 Gy you have the age of the universe right?
Re:Please put units in names by em3rgent0rdr on Monday March 21, 2022 @04:30AM (#30751656)
Your comment piqued my curiosity, and I looked at https://en.m.wikipedia.org/wiki/Future_of_an_expanding_unive... and found:

"Stars are expected to form normally for 10^12 to 10^14 (1–100 trillion) years"

So it seems Gy and even Ty units will be a reasonable scale for events during the period of star formation.

Re:Please put units in names by Aeolun on Monday March 21, 2022 @04:55AM (#30751810)
Ah, that is a good point. For some reason I was thinking only backwards. I never considered that there’s orders of magnitude more time in front of us.
Re:Please put units in names by sparkie on Monday March 21, 2022 @04:10AM (#30751526)
Does the type system handle equivalent units (dimensional analysis)? eg, N.m = kg.m^2.s^-2.

Does the type system do orientational analysis? If not you to assign a value of work to a value of torque and vice versa, as they both have the above unit.

There are several other similar gotchas with the SI. I think descriptive names are better than everyone attempting to implement an incomplete/broken type system.

Re:Please put units in names by DiogenesKynikos on Monday March 21, 2022 @04:43AM (#30751736)
The Python package astropy does all these things. There's a graph of equivalencies between units.

0. https://docs.astropy.org/en/stable/units/index.html

Re:Please put units in names by nuccy on Monday March 21, 2022 @05:44AM (#30752097)
Speaking of astropy units. I had a hilarious issue last week, which was quite hard to identify (simplified code to reproduce):

  from astropy import units as u
  a = 3 * u.s
  b = 2 * u.s
  c = 1 * u.s
  d = 4 * u.s
  m = min([a, b, c, d])
  a -= m
  b -= m
  c -= m
  d -= m
  print(a,b,c,d)
Output: 2.0 s 1.0 s 0.0 s 4.0 s

Note the last 4.0, while min value is 1.0

The issue is that a, b, c, d are objects when astropy units are applied and min (or max) returns not the value but an object with the minimal value, thus m is c (in this particular case c has the smallest value) so c -= m makes m = 0, so d remains unchanged. It was very hard to spot especially when values change and occasionally either one of a, b, c or d has the smallest value.

In-place augmentation of a working code with units may be very tricky and can create unexpected bugs.

Re:Please put units in names by DiogenesKynikos on Monday March 21, 2022 @08:40AM (#30753384)
This is really an issue with Python in general (specifically, mutable types).

You'd get the exact same behavior with numpy ndarrays (of which astropy Quantities are a subclass).

Re:Please put units in names by dragonwriter on Monday March 21, 2022 @12:05PM (#30755690)
> This is really an issue with Python in general (specifically, mutable types).

Unit-aware values as a type where assignment as mutation is an odd choice though (normal mutable types do not exhibit this behavior, it’s a whole separate behavior which has to be deliberately implemented.) It may make sense in the expected use case (and as you note reflects the behavior of the underlying type), but more generally it's not what someone wanting unit-aware values would probably expect.

Re:Please put units in names by necovek on Monday March 21, 2022 @07:25AM (#30752728)
That sounds like a bug in astropy type definitions: did you get a chance to report it as one?

While it can sometimes be undefined behavior (a minimum of incompatible units), in cases like these it should DTRT.

Re:Please put units in names by deepsun on Tuesday March 22, 2022 @04:26AM (#30763824)
Mutable types are hard.
Re:Please put units in names by sparkie on Monday March 21, 2022 @04:57AM (#30751816)
I see dimensional analysis, but in this table[1], torque and work have the same unit, and that unit is J.

The SI itself states[2]: "...For example, the quantity torque is the cross product of a position vector and a force vector. The SI unit is newton metre. Even though torque has the same dimension as energy (SI unit joule), the joule is never used for expressing torque."

[1]:https://docs.astropy.org/en/stable/units/index.html#module-a... [2]:https://www.bipm.org/documents/20126/41483022/SI-Brochure-9-...

Re:Please put units in names by gpderetta on Monday March 21, 2022 @07:38AM (#30752859)
This is the answer from boost units:

https://www.boost.org/doc/libs/1_78_0/doc/html/boost_units/F...

tl;dr is uses some sort of pseudounits to make dimensionally similar but incompatible units, well, incompatible.

Re:Please put units in names by Ekaros on Monday March 21, 2022 @03:58AM (#30751458)
Time units are messy.

Once we get to Gigayears, what is the size of single year? Just 365 days? Or which of Julian, Gregorian, Tropical, Sidereal? At even kilo prefix the differences do add up. Or would you need to specify it?

Days, weeks and months are also fun mess to think of.

Re:Please put units in names by dragonwriter on Monday March 21, 2022 @12:13PM (#30755804)
> Once we get to Gigayears, what is the size of single year?

The appropriate system of units is context-dependent. The astronomical system, for instance, has Days of 86,400 SI seconds and Julian years of exactly 365.25 Days; if you have a general and extensible units library, then this isn't really a difficulty, you just need to make a choice based on requirements.

Re:Please put units in names by BlueTemplar on Monday March 21, 2022 @07:47AM (#30752937)
This is going to depend on the precision that you need.

(Calculations without associated uncertainty calculations are worse than worthless anyway - misleading due to the inherent trust we tend to put in numbers regardless of whether they are garbage.)

Re:Please put units in names by usrusr on Monday March 21, 2022 @04:59AM (#30751830)
You'll want to leave the point floating to floating point numbers, but whenever you interact with legacy APIs or protocols you want a type representing the scale they use natively. You wouldn't want to deal with anything based on TCP for example with only seconds (even if client code holds them in doubles), or with only nanoseconds. But you certainly won't ever miss a type for kiloseconds.
Re:Please put units in names by BlueTemplar on Monday March 21, 2022 @08:01AM (#30753044)
Isn't floating point specifically for dealing with large orders of magnitude ?

(Economics of floating vs fixed point chips might distort things though ?)

Also, in the case that you meant this : you might need fractions of even base units for internal calculations :

IIRC banking, which doesn't accept any level of uncertainty, and so uses exclusively fixed precision, uses tens of cents rather than cents as a base unit ?

Re:Please put units in names by jltsiren on Monday March 21, 2022 @05:07AM (#30751866)
The standard symbol for a year is "a". Using "y" or "yr" is non-standard and ambiguous, and they should be avoided in situations where precision and clarity matter.
Re:Please put units in names by deepsun on Tuesday March 22, 2022 @04:23AM (#30763807)
Thanks, didn't know. Although in astronomy they use Gy often. PS: Don't know why people downvote, your comment is useful.
Re:Please put units in names by gpderetta on Monday March 21, 2022 @07:35AM (#30752834)
Given a powerful enough type system, you can parameterize your types by the ratio to the unit and any exponent. Then you can allow only the conversions that make sense.
Re:Please put units in names by resonious on Monday March 21, 2022 @03:51AM (#30751400)
I don't think the parent meant to exclude hecto-pascals from their hypothetical type system.
Re:Please put units in names by Juliate on Monday March 21, 2022 @05:04AM (#30751854)
Why wouldn't the type system be able to take care of that?