LLNL / units

A run-time C++ library for working with units of measurement and conversions between them and with string representations of units and measurements
https://units.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
146 stars 26 forks source link

Favoring SI base units in string formatting? #301

Closed SimonHeybrock closed 1 year ago

SimonHeybrock commented 1 year ago

A user reported a problem formatting units such as kJ / mol — this outputs kW / kat. This is unexpected, since mol is an SI base units, whereas kat is not. I would have expected that the string formatting tries to stick to SI (or other) base units, before moving on to derived units.

Obviously, both J and W are derived, but is there a way to prefer string outputs with the smaller number of derived units, i.e., "1 derived + 1 base" should be chosen over "2 derived"?

phlptp commented 1 year ago

I will have to think about how add prioritization for things like that. I suspect there is a bit of a bias in the code towards electrical engineering units ie kW over kJ.

phlptp commented 1 year ago

I made a few tweaks that will prioritize the to_string conversion with muplication/division of a single base unit over other possible conversions. That addresses the specific class of issues raised here. There is more things to be done in a similar fashion but that will take some tinkering, and I think I want to get another release out soon.

SimonHeybrock commented 1 year ago

Thanks, and a release would indeed be appreciated 👍