Skip to content

Conversation

lipchev
Copy link
Collaborator

@lipchev lipchev commented Aug 17, 2025

  • refactored the clamp function: no longer converting using the boxing ToUnit(Enum) and no longer comparing with the CompareTo(object)
  • updated the tests

Length clampedMin = UnitMath.Clamp(value, min, max);

Assert.Equal(-1, clampedMin.Value);
Assert.Equal(LengthUnit.Meter, clampedMin.Unit);
Copy link
Owner

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.

Copy link
Collaborator Author

@lipchev lipchev Aug 17, 2025

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..

Copy link
Collaborator Author

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.

Copy link
Contributor

@Muximize Muximize Aug 18, 2025

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.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree

@lipchev lipchev changed the title Refactored the clamp function: no longer converting the clamped result Refactored the clamp function (preserving the unit) Aug 18, 2025
Edit class xmldoc, no longer much math operations left here.
@angularsen angularsen merged commit 9597b34 into angularsen:master Aug 19, 2025
1 check was pending
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants