Throw ArgumentException from Clamp when min is greater than max - #119
Merged
Merged
Conversation
Clamp returned min for an inverted range, a value that satisfies neither bound. Math.Clamp, decimal.Clamp and the default INumber<T>.Clamp all throw, so generic code behaved differently depending on T. Fixes #117 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01M4xENzmZT4DZE8d5Hu6kEN
|
This was referenced Sep 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #117
What changed
Clamp<TNumber>(min, max)now throwsArgumentException(withparamNamemin) whenmin > max. It checks after converting both bounds, and the message follows the BCL's'{min}' cannot be greater than {max}.INumber<T>.Clampdelegates to the instance method, so it gets the same check. Both carry<exception>docs.Before this change,
PreciseNumber.Clamp(2, 3, 1)returned3.Math.Clamp,decimal.Clampand the defaultINumber<T>.Clampall throw for an inverted range.Tests
TestClampThrowsWhenMinIsGreaterThanMaxcovers a value inside the inverted range and a value outside it, through both the static and the instance overload.TestClampAcceptsEqualBoundsguards themin == maxedge.Verification
TestClampThrowsWhenMinIsGreaterThanMaxfails. With the fix, it passes.dotnet testsuite passes: 387/387.🤖 Generated with Claude Code
https://claude.ai/code/session_01M4xENzmZT4DZE8d5Hu6kEN
Generated by Claude Code