UnitsNet icon indicating copy to clipboard operation
UnitsNet copied to clipboard

Inferred division from defined multiplication relations

Open Muximize opened this issue 2 years ago โ€ข 5 comments

In #1329 this proposal came up:

Another idea: generate division operators based on multiplication. Right now we define:

ElectricPotential.Volt = ElectricCurrent.Ampere * ElectricResistance.Ohm (and generate the reverse)
ElectricCurrent.Ampere = ElectricPotential.Volt / ElectricResistance.Ohm
ElectricResistance.Ohm = ElectricPotential.Volt / ElectricCurrent.Ampere

But those last two could also be generated based on the first.

This PR is an experiment implementing this.

Muximize avatar Jan 07 '24 00:01 Muximize

I rebased this on the latest changes in #1329 which makes it much cleaner.

Example of an interesting situation that occurs here, we previously defined:

MassFlow.GramPerSecond = Area.SquareMeter * MassFlux.GramPerSecondPerSquareMeter
MassFlux.KilogramPerSecondPerSquareMeter = MassFlow.KilogramPerSecond / Area.SquareMeter

If we infer division from multiplication, one of those has to add or lose a kilo ๐Ÿ˜…

Muximize avatar Jan 25 '24 18:01 Muximize

Will review this next, hopefully in the next few days.

If we infer division from multiplication, one of those has to add or lose a kilo ๐Ÿ˜…

Haha ๐Ÿ˜† But it will still work, right? The inferred division will just use grams instead?

angularsen avatar Feb 05 '24 06:02 angularsen

Yes, it will work, except we're not sure why the original contributor chose a particular unit for an operator and if it matters, with precision for example.

Also, this PR is more of an experiment than a serious proposal. There's some questions about the "magic" of it, and I took some liberties with implicit/explicit conversion between Density and MassConcentration that I'm not quite sure about. Maybe @lipchev can chime in on this?

Muximize avatar Feb 05 '24 16:02 Muximize

Yes, it will work, except we're not sure why the original contributor chose a particular unit for an operator and if it matters, with precision for example.

Also, this PR is more of an experiment than a serious proposal. There's some questions about the "magic" of it, and I took some liberties with implicit/explicit conversion between Density and MassConcentration that I'm not quite sure about. Maybe @lipchev can chime in on this?

Although the Density and Mass Concentration share the same units, they are not interchangeable- they describe different types of ratios. Casting between them (either implicitly or explicitly) makes no sense..

lipchev avatar Feb 05 '24 22:02 lipchev

Thanks for the explanation! That makes the concept of inferred division a bit harder, given that we currently have:

Mass.Kilogram = Density.KilogramPerCubicMeter * Volume.CubicMeter
Mass.Kilogram = MassConcentration.KilogramPerCubicMeter * Volume.CubicMeter

which leads to these inferred ambiguous operators, which is what I tried to solve with my casting shenanigans:

Density.KilogramPerCubicMeter           = Mass.Kilogram / Volume.CubicMeter
MassConcentration.KilogramPerCubicMeter = Mass.Kilogram / Volume.CubicMeter

The latter is currently not defined, so skipping it would not be a breaking change. I've reworked this PR to do that and removed the casting stuff.

Muximize avatar Feb 06 '24 12:02 Muximize