Collect all scale factors for a unit #382
Labels
⬇️ affects: code (implementation)
Affects implementation details of the code
📁 kind: cleanup
Making the library nicer in some way, without affecting functionality much or at all
💪 effort: small
Milestone
We have anonymous scaled units, which don't get a special name, but do get an automatic label which is just that scale factor plus the unscaled unit's symbol. In these instances, we shouldn't be treating the number as part of the "name" of the unit. Instead, we should gather all numbers outside and simplify, and perform products and powers on the unscaled versions of the units.
We already do this to some extent with the new common unit formulation. For example, the common unit of
feet * mag<10>()
andfeet * mag<6>()
is simplyfeet * mag<2>()
. Bringing this same approach to products and powers will just make us more consistent.For example, consider this code, where we first define a few custom symbols, and then execute some unit expressions:
This currently prints:
I think it should print:
The first case, I actually like a little less:
"Liters per hundred km" is a good and understandable unit of measure, where as "one hundredth liter per kilometer" is not. However, if we stick with this, then we get the second case:
This feels pretty bad. "km liters per hundred km" should just be "one hundredth liter" --- we clearly do want to cancel here. (This is distinct from something like (cm / m), where applying a prefix changes the name of the unit, and we don't want to try to get clever.)
If we did this change, and gave up getting the "nice" behavior of the first one by default, we could still recover it by making a new strong-typed unit for the "100 km" part, whose label is
[100 km]
. This would have the same pitfall of not cancelling when we multiply by km, but I think that's fine... especially because these are all just anonymous intermediate results anyway, and users who cared about the units would assign to a quantity ofLiters
or something.The third case shows how it works in the case of powers:
The old one might be slightly nicer here, in that it expresses the side length of the cube with this volume unit, which makes it easier to picture. But I think that (a) the sort of "ideological consistency" that numbers are not part of names is worth giving this up to get; (b) the "new" way is likely to perform better when we combine it with other units; and, (c) there is still the same workaround of making a strong-typed unit
[10 ft]
for users who really want to preserve this automatic behavior.Finally, the fourth case is a fanciful construction of MPH in terms of ft/sec:
Here, too, there's no "slam dunk" best. The old way makes it easy to guess the source units, if someone is really familiar with units. But the new way is surely more concise and illuminating.
Overall, I'm going to get the implementation landable (I did a proof of concept late last year), double check the compile time impact, and plan to land it unless I find a compelling reason not to.
The text was updated successfully, but these errors were encountered: