-
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
Temperature.Equals method uses the wrong Unit for the tolerance parameter #1325
Comments
Indeed the conversion to C (-273.15) results in a negative absolute tolerance. That said, it is working as expected by the current design. We could add an overload with In the meantime, you can use var equals = t1.Equals(t2, tolerance: Temperature.FromDegreesCelsius(0)); or just var equals = t1.Equals(t2); |
Hi Tristan, thank you for the swift reply. I must disagree with your suggested alternatives. passing |
Fair point on the first one. I have been away from the code for a bit, and just checked It seems without modifications to UnitsNet your only option would be var equals = t1.Equals(t2.ToUnit(t1.Unit)); Which is not exactly elegant :) |
The suggested approach would work for me. But I am also looking to prevent the pitfall of someone else using the recommended I can see that this is a quirk that is unique to Temperature and TemperatureDelta where they do not have the same conversion, and thus is a problem that requires some specialized logic for just those cases. But I also believe that it should not stay the way it is because the method does not return the right results. I was going to do a PR with a suggestion for a fix as well, but then realised the problem is a bit bigger than just changing a method's parameter from Temperature to TemperatureUnit and adding a bit of conversion logic. |
Closing this due to inactivity. We need a design proposal for how this should work, to move this forward. |
Describe the bug
Temperature.Equals(Temperature other, Temperature tolerance)
does the wrong conversion, and should beTemperature.Equals(Temperature other, TemperatureDelta tolerance)
To Reproduce
var t1 = Temperature.FromDegreesCelsius(10);
var t2 = Temperature.FromDegreesCelsius(10);
var equals = t1.Equals(t2, tolerance: Temperature.Zero);
Expected behavior
equals should be true
Actual behavior
throws
System.ArgumentOutOfRangeException
"Tolerance must be greater than or equal to 0 (Parameter 'tolerance')"Additional context
So the problem is that
Temperature.Zero
is converterd fromTemperatureUnit.Kelvin
toTemperatureUnit.DegreeCelsius
becauset1
was specified usingTemperature.FromDegreesCelsius
.This uses the wrong conversion, the tolerance parameter should be of type
TemperatureDelta
which converts correctly.To fix this I see two problems that have to be solved:
Equals
method is not done such that a special delta unit can be specified in the quantity json, so the schema would need to be adaptedtolerance.As(this.Unit)
becausethis.Unit
is of typeTemperatureUnit
and notTemperatureDeltaUnit
TemperatureUnit
andTemperatureDeltaUnit
. ButTemperatureDeltaUnit
has no equivalent forTemperatureUnit.SolarTemperature
, which would have to be addedThe text was updated successfully, but these errors were encountered: