-
Notifications
You must be signed in to change notification settings - Fork 403
Refactored the clamp function (preserving the unit) #1608
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
Conversation
- updated the tests
UnitsNet.Tests/UnitMathTests.cs
Outdated
Length clampedMin = UnitMath.Clamp(value, min, max); | ||
|
||
Assert.Equal(-1, clampedMin.Value); | ||
Assert.Equal(LengthUnit.Meter, clampedMin.Unit); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, not sure I like that we change the unit of value
here. I'd expect it to keep its unit, but clamp it to the bounds of min/max.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did ask you about the two options..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, not sure I like that we change the unit of
value
here. I'd expect it to keep its unit, but clamp it to the bounds of min/max.
I've reverted the behavior, although there is a strong argument against the extra conversions: if preserving the unit is required, the user can always do UnitMath.Clamp(value, min, max).ToUnit(value.Unit)
(or pass in min.ToUnit(value.Unit)
, ..), while the opposite, i.e. avoiding the return of a huge value with a tiny unit, is not possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reference, here's the discussion about this a few years ago when I authored Clamp
:
#1157 (comment)
I guess the principle of least surprise should still apply here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree
Edit class xmldoc, no longer much math operations left here.
ToUnit(Enum)
and no longer comparing with theCompareTo(object)