Repository navigation
INumber.Min(+QNaN, 1) returns 1 #98285
Copy link
Copy link
Closed
Labels
area-System.Numericsin-prThere is an active PR which will close this issue when it is mergedThere is an active PR which will close this issue when it is merged
Description
float qNaN = BitConverter.Int32BitsToSingle(0x7FC00000);
Console.WriteLine(float.Min(qNaN, 1));Double/Single/Half.Min(+QNaN, 1) correctly returns NaN but INumber.Min not:
runtime/src/libraries/System.Private.CoreLib/src/System/Numerics/INumber.cs
Lines 108 to 127 in 9699f39
| /// <summary>Compares two values to compute which is lesser.</summary> | |
| /// <param name="x">The value to compare with <paramref name="y" />.</param> | |
| /// <param name="y">The value to compare with <paramref name="x" />.</param> | |
| /// <returns><paramref name="x" /> if it is less than <paramref name="y" />; otherwise, <paramref name="y" />.</returns> | |
| /// <remarks>For <see cref="IFloatingPoint{TSelf}" /> this method matches the IEEE 754:2019 <c>minimum</c> function. This requires NaN inputs to be propagated back to the caller and for <c>-0.0</c> to be treated as less than <c>+0.0</c>.</remarks> | |
| static virtual TSelf Min(TSelf x, TSelf y) | |
| { | |
| // This matches the IEEE 754:2019 `minimum` function | |
| // | |
| // It propagates NaN inputs back to the caller and | |
| // otherwise returns the larger of the inputs. It | |
| // treats +0 as larger than -0 as per the specification. | |
| if ((x != y) && !TSelf.IsNaN(x)) | |
| { | |
| return x < y ? x : y; | |
| } | |
| return TSelf.IsNegative(x) ? x : y; | |
| } |
(another issue: search .. in INumber.cs to find a redundant dot)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
area-System.Numericsin-prThere is an active PR which will close this issue when it is mergedThere is an active PR which will close this issue when it is merged
Activity
stephentoub commentedon Feb 12, 2024
Can you please share a minimal repro showing the problem?
skyoxZ commentedon Feb 13, 2024
The output should be
NaN NaNbut now it'sNaN 1:stephentoub commentedon Feb 13, 2024
Did you purposefully implement < and > incorrectly?
?
stephentoub commentedon Feb 13, 2024
But, yes, the Min implementation does look a bit off.
skyoxZ commentedon Feb 13, 2024
No, it's a mistake. Interesting, I copied them from
runtime/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Numerics/DimTests.GenericMath.cs
Lines 391 to 392 in 6299d3e