Skip to content

Conversation

jkotas
Copy link
Member

@jkotas jkotas commented Sep 2, 2025

Reverts #118824

@Copilot Copilot AI review requested due to automatic review settings September 2, 2025 16:19
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR reverts changes from PR #118824 that improved ArgumentOutOfRange exceptions in Math.Clamp methods. The revert restores the original behavior of throwing a generic ArgumentException instead of the more specific ArgumentOutOfRangeException when the minimum value is greater than the maximum value in clamp operations.

Key changes:

  • Reverts exception type from ArgumentOutOfRangeException back to ArgumentException for invalid min/max parameters
  • Restores custom error message formatting instead of using the standard ArgumentOutOfRangeException helper
  • Updates corresponding test expectations to match the reverted behavior

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/Math.cs Updates test expectations to verify ArgumentException is thrown instead of ArgumentOutOfRangeException
src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs Reverts BigInteger.Clamp to use custom exception throwing with ArgumentException
src/libraries/System.Runtime.Numerics/src/Resources/Strings.resx Adds back the custom error message format for min/max validation
src/libraries/System.Private.CoreLib/src/System/UInt128.cs Reverts UInt128.Clamp to use Math.ThrowMinMaxException helper
src/libraries/System.Private.CoreLib/src/System/Single.cs Reverts Single.Clamp and ClampNative to use Math.ThrowMinMaxException helper
src/libraries/System.Private.CoreLib/src/System/Math.cs Reverts all Math.Clamp overloads and updates ThrowMinMaxException to throw ArgumentException
src/libraries/System.Private.CoreLib/src/System/Int128.cs Reverts Int128.Clamp to use Math.ThrowMinMaxException helper
src/libraries/System.Private.CoreLib/src/System/Half.cs Reverts Half.ClampNative to use Math.ThrowMinMaxException helper
src/libraries/System.Private.CoreLib/src/System/Double.cs Reverts Double.Clamp and ClampNative to use Math.ThrowMinMaxException helper

@jkotas
Copy link
Member Author

jkotas commented Sep 2, 2025

Context: #118824 (comment)

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-numerics
See info in area-owners.md if you want to be subscribed.

@xtqqczze
Copy link
Contributor

xtqqczze commented Sep 2, 2025

If the implementation from the PR consistently threw when either min or max is NaN, that might be reasonable. However, it does not behave consistently, see sharplab.

@tannergooding
Copy link
Member

The nuance around NaN handling and other considerations is part of why I think #119256 would be goodness.

It fixes a scenario that's often important for perf to be consistent across the stack (whether Scalar, Vector128, Tensors, etc), makes it consistent with what hardware typically does, and allows developers to easily define their own wrapper if they do want to throw/fail.

The biggest potential problem is it's a 4 year old API (added in .NET 6) that people may be depending on it throwing by this point.

@jkotas jkotas merged commit 9bb62bc into main Sep 2, 2025
145 checks passed
@jkotas jkotas deleted the revert-118824-115337-math-clamp-exception branch September 2, 2025 21:10
filipnavara pushed a commit to filipnavara/runtime that referenced this pull request Sep 5, 2025
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