Skip to content

INumber.MinNumber and INumber.MaxNumber return NaN in Release configuration #98068

@PavielKraskouski

Description

@PavielKraskouski

Description

When running a program in Release configuration, INumber.MinNumber and INumber.MaxNumber return NaN if one of the numbers is not NaN. In Debug configuration these methods work as expected.

Reproduction Steps

  1. Create a new console project, specify .NET 8 as the target platform.
  2. Add the following code:
int numbers = 0;
int NaN = 0;

for (int i = 0; i < 100000; i++)
{
    float minLength = 0;
    float maxLength = 0;

    if (!float.IsNaN(float.MaxNumber(maxLength / minLength, 1)))
    {
        numbers++;
    }
    else
    {
        NaN++;
    }
}

Console.WriteLine($"Numbers: {numbers}");
Console.WriteLine($"NaN: {NaN}");
  1. Run the program.

Expected behavior

The output of the program should be as follows:

Numbers: 100000
NaN: 0

Actual behavior

Here is the result of the program in Release configuration:

Numbers: 9999
NaN: 90001

Regression?

The bug could not be reproduced in .NET 7. The program works as expected in both configurations.

Known Workarounds

You can use INumberBase.MinMagnitudeNumber and INumberBase.MaxMagnitudeNumber if you intend to work with only positive numbers.

Configuration

.NET:
Microsoft .NET SDK 8.0.101 (x64) from Visual Studio (8.1.123.58017)
Microsoft .NET SDK 8.0.200-preview.23624.5 (x64) from Visual Studio (8.2.23.62405)

OS:
Windows 11 Pro 23H2 22631.3085
Windows 11 Pro 22H2 22621.3085

Architecture:
x64
x64

Other information

The bug is not reproduced if you replace the first argument of MaxNumber with one of the following:

if (!float.IsNaN(float.MaxNumber(0f / 0f, 1)))
{
    numbers++;
}
else
{
    NaN++;
}
if (!float.IsNaN(float.MaxNumber(float.NaN, 1)))
{
    numbers++;
}
else
{
    NaN++;
}

If we run the code in SharpLab and Compiler Explorer, we get 100,000 NaN. On my machines I get 9,999 numbers and 90,001 NaN.

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions