-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Improve ArgumentOutOfRange exception in Math.Clamp #118824
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
Improve ArgumentOutOfRange exception in Math.Clamp #118824
Conversation
Replaces e.g. "System.ArgumentException: '0' cannot be greater than -2." with System.ArgumentException: 'minValue = 0' cannot be greater than maxValue = -2 This follows the behavior of System.Math.Random Fix dotnet#115337
Tagging subscribers to this area: @dotnet/area-system-numerics |
Should the same changes be made in runtime/src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs Lines 3967 to 3970 in 10bc776
|
Use Min/Max for value clamping References: dotnet#118824 (comment)
Use Min/Max for value clamping References: dotnet#118824 (comment)
Co-authored-by: skyoxZ <skyoxZ@qq.com>
* Apply the Math.cs change to BigInteger.Clamp * Use 'nameof'
src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs
Outdated
Show resolved
Hide resolved
better replace ThrowMinMaxException with runtime/src/libraries/System.Private.CoreLib/src/System/ArgumentOutOfRangeException.cs Line 111 in ed96395
|
Co-authored-by: kasperk81 <83082615+kasperk81@users.noreply.github.com>
* Use ArgumentOutOfRangeException.ThrowLess in Math.cs and BigInteger.cs
…tOfRangeException: ThrowIfLessThan
This is to support throwing the error from INumber.Clamp as ArgumentOutOfRangeException.ThrowIfLessThan has a type constraint IComparable<T> so it cannot be used with nullable types. All other places use ArgumentOutOfRangeException.ThrowIfLessThan directly.
Assert that Math.Clamp throws ArgumentOutOfRangeException
@tannergooding I'm going to merge this improvement to the exception message and I'll file a new issue recommending we relax Math.Clamp to have the logic you described instead of throwing. |
Looks like there are large regressions in code quality for floating point types, see: https://csharp.godbolt.org/z/K4P4xvWdK. |
That'd be because it uses |
Yeah, this PR also changes behaviour in relation to |
It sounds like that this PR should be reverted? |
Yeah, runtime/src/libraries/System.Private.CoreLib/src/System/Numerics/INumber.cs Lines 25 to 38 in 4b8a478
|
@tannergooding It is quite unintuitive that |
It's that there are multiple types of relational comparisons, just like there are multiple types of "equality" ( You have different considerations based on what the spec may require and what may be required for sorting or for use in hash sets, etc. You may need to even customize this which is why we also define https://learn.microsoft.com/en-us/dotnet/api/system.numerics.totalorderieee754comparer-1?view=net-9.0. There is no single right answer and its not something we can document absolutely everywhere that takes |
…8824)" (dotnet#119276) This reverts commit 1d56b2a.
Replaces e.g. "System.ArgumentException: '0' cannot be greater than -2." with
System.ArgumentException: 'minValue = 0' cannot be greater than maxValue = -2
This follows the behavior of System.Math.Random
Fix #115337