-
Notifications
You must be signed in to change notification settings - Fork 387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v4.117.0 breaks quantity comparison between different units #1376
Comments
Generally speaking, you should avoid combining The problem is that any conversion in Units.NET typically goes via a base unit. You can define direct conversions between specific units, but it would require a lot of work to define direct conversions between all units for each quantity. In your example, MinValue Celsius is converted to Kelvins by adding 273.15, Then Kelvin is converted to MillidegreeCelsius by first subtracting 273.15, getting back to In essence: // UnitsNet\GeneratedCode\Quantities\Temperature.g.cs:845
(TemperatureUnit.DegreeCelsius, TemperatureUnit.Kelvin) => new Temperature(_value + 273.15, TemperatureUnit.Kelvin),
(TemperatureUnit.Kelvin, TemperatureUnit.MillidegreeCelsius) => new Temperature((_value - 273.15) * 1000, TemperatureUnit.MillidegreeCelsius), This could be solved by UnitsNet automatically generating direct conversions between all prefix units, so it simply multiplies by On a side note, in v6 we are moving towards allowing In summary, don't use |
Actually, using var min = Temperature.FromDegreesCelsius(double.Infinity);
foreach(var temperature in myTemperatures) // myTemperatures units are unknown, could be millidegrees
{
if(temperature < min)
{
min = temperature;
}
} And the comparison wouldn't throw? If so I think we can close this one. |
Give 6.0.0-pre003 a try and see if it helps
|
Closing as fixed in v6, where infinity values are once again supported. |
Describe the bug
Comparison with a smaller unit (e.g.
MillidegreesCelsius
) fails when the larger unit (e.g.DegreesCelsius
) is set usingdouble.minValue
ordouble.maxValue
.To Reproduce
Steps to reproduce the behavior (just an example):
System.ArgumentException: PositiveInfinity or NegativeInfinity is not a valid number. (Parameter 'value')
Expected behavior
I'd expect this not to throw and to just do the comparison as expected.
Looks like it fails because the comparison now attempts to convert
double.MinValue
degrees to millidegrees. In both versions, this will throw:Additional context
We do this comparison when we're trying to keep a running min while iterating over a list. There are workarounds, like initializing min to the smallest unit we would possibly use or setting it to the first value in the list. I'm reporting this anyways as I think the comparison should still work, even if the use case isn't common.
Traced back to (I think) #1023
The text was updated successfully, but these errors were encountered: