Skip to content

Math.Round returns different values for the same input. #13144

@EgorBo

Description

@EgorBo

When mscorlib is prejitted (by default), the platform intrinsics are not used, e.g. MathF.Round won't use SSE (see dotnet/coreclr#25365). Then as far as I understand, it could be re-compiled for tier1 if it's in a hot method and will use SSE instead). Is it ok that it might start returning different values for the same input? E.g.

using System;
using System.Runtime.CompilerServices;
using System.Threading;

public class Program
{
    static void Main()
    {
        Thread.Sleep(1000);

        float x = 0.499999969f;
        for (int i = 0; i < 100; i++)
        {
            // on i=30 iteration it will re-compile MyRound and it will start returning 
            // different result for the same x
            Console.WriteLine(MyRound(x));

            Thread.Sleep(100);
        }
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    public static float MyRound(float x) => MathF.Round(x);
}

Output is:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
0
0
0
0
0
0
0
...

/cc @tannergooding

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions