runtime: C# Math.Pow(-1, 2) doesn't output correct value on Windows 11 Insider Preview, Canary channel (27881.1000)

Description

The following is copied with extra bits from https://aka.ms/AAwwjwl (Feedback Hub) to raise awareness of this issue:

Hello, I’m a contributor to osu! (https://github.com/ppy/osu). A user reported a really weird problem with our game that only happens in the current version of Canary (27881.1000) in https://github.com/ppy/osu/issues/33948. I’ve narrowed it down to https://github.com/ppy/osu/issues/33948#issuecomment-3026432343, which is Math.Pow(-1, 2) returning -1 instead of 1 for some odd reason.

Used VSCode for C# installation of .NET 8 (what osu! uses). I’ve also tried the same math function in Python, but the value was correct (1). C++'s std::pow(-1,2) seems to also output -1, so it may be a bigger issue in Windows.

If you need to contact us better, joining the osu! Discord server would be best (https://discord.gg/ppy).

Reproduction Steps

Console.WriteLine(Math.Pow(-1, 2));

or

std::cout << std::pow(-1, 2) << std::endl;

Expected behavior

It should output 1.

Actual behavior

It outputs -1 on Windows 11 Insider Preview, Canary channel (27881.1000).

Regression?

No response

Known Workarounds

No response

Configuration

  • .NET SDK 8.0.411
  • Windows 11 Insider Preview, Canary channel (27881.1000)

Other information

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 341
  • Comments: 23 (10 by maintainers)

Most upvoted comments

The issue should be reported to MSVC instead, at https://developercommunity.visualstudio.com/ .

Maybe it should, but part of the reason why I personally thought it’s best to report it here too is that something of this severity should possibly be internally fast-tracked to a fix.

The main problem with all of these windows issue trackers and feedback mechanisms is that I don’t know about anyone else, but my reluctance to want to have anything to do with diagnosing this issue was very high precisely because it is very unclear whether real humans can be reached through any of these places. At least in dotnet/runtime I know there is a chance of directly reaching engineers.

Both Math.Pow and std::pow invokes the pow function in UCRT, which is shipped with Windows. The issue should be reported to MSVC instead, at https://developercommunity.visualstudio.com/ .

Update: It’s important to also report the issue to the owners more directly. It’s OK to also have the ticket at impacted system like .NET.

Maybe it should, but part of the reason why I personally thought it’s best to report it here too is that something of this severity should possibly be internally fast-tracked to a fix.

That’s not really how things work. Microsoft isn’t some small company where someone can necessarily pop next door and go “hey, I’ve got a problem”. It is a large enterprise with multiple divisions, some 200k employees, and it takes in tens of thousands (on the low end) of issues, feedback, and bug reports per day (many of which are low effort, don’t provide enough detail, are actually user error, etc).

While there are sometimes channels that can be taken for critical issues (namely security related due to disclosure considerations), a constant folding bug in a preview build isn’t going to meet that bar and we’re going to end up reporting along much the same avenues as the everyone else. We would do so because that is the preferred mechanism for feedback to be filed specifically so that there isn’t some difference between 1st and 3rd party reports. – It is the same general reason we, the .NET team, request that everyone file feedback on GitHub/VS Feedback. It is the same reason that similar bugs in the uCRT math library have been reported via the same avenues in the past; such as this one that was found for FMA on older hardware: https://developercommunity.visualstudio.com/t/MSVCs-fma-implementation-is-incorrect-o/10594003

i don’t suppose the patch is publicly accessible?

There won’t be anything until some future insider preview has been published. It is up to the Windows team on how regularly they do that or if they decide to ship a patch earlier via some other mechanism, such as Windows Update. Due to it being in a core component, manual patching is dangerous and not something that can trivially be done.

Can you link something? Searching for that number yields no other results

I expect that is the internal bug number filed to track the external bug report, there won’t be anything publicly visible or accessible on this.

The original reporter then made no effort to push back or explain why it actually was a bug.

The original reporter of fma bug here. VS feedback used to be a lot less responsive back then. After I got the “not a bug” response, 5 months after I reported the bug, I explained further why it actually was a bug with reference to the C standard and MSDN (click “Show comments” to see it) and hoped I could get further response in… a few months, which didn’t happen.

Issue: Math.Pow relies directly on the OS pow implementation

Location: [src/coreclr/classlibnative/float/floatdouble.cpp lines 232‑236] and [src/coreclr/classlibnative/float/floatsingle.cpp lines 207‑211]

COMDouble::Pow and COMSingle::Pow simply call pow/powf from the C runtime. On Windows 11 Insider Preview (build 27881.1000), these functions can return incorrect results (e.g., Math.Pow(-1, 2) giving -1). The JIT also uses these functions for constant folding, causing wrong constants to be embedded at compile time.

Suggested Fix: Introduce a managed fallback in COMDouble::Pow/COMSingle::Pow that handles negative bases with integral exponents, bypassing the faulty system call.

A simple approach:

FCIMPL2_VV(double, COMDouble::Pow, double x, double y)
{
    FCALL_CONTRACT;

    if ((x < 0.0) && (y == floor(y)))
    {
        double absResult = pow(-x, y);
        return fmod(fabs(y), 2.0) == 1.0 ? -absResult : absResult;
    }

    return pow(x, y);
}

Suggested Implementation:

  • Add the following code to src/coreclr/classlibnative/float/floatdouble.cpp below line 234 before the return pow:
    if ((x < 0.0) && (y == floor(y)))
    {
        double result = pow(-x, y);

        if (fmod(fabs(y), 2.0) != 0.0)
        {
            result = -result;
        }

        return result;
    }
  • Add the following code to src/coreclr/classlibnative/float/floatsingle.cpp below line 209 before the return powf:
   if ((x < 0.0f) && (y == floorf(y)))
    {
        float result = powf(-x, y);

        if (fmodf(fabsf(y), 2.0f) != 0.0f)
        {
            result = -result;
        }

        return result;
    }
  • Add the following code to src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/Math.cs below line 1124:
       [Fact]
        public static void Pow_NegativeBaseEvenExponent_ReturnsPositive()
        {
            Assert.Equal(1.0, Math.Pow(-1, 2));
            Assert.Equal(16.0, Math.Pow(-2, 4));
        }


This patch modifies the runtime implementations of Math.Pow so that the sign for negative bases with integer exponents is explicitly handled in both the double‐precision and single‐precision helpers.

In COMDouble::Pow the function now checks whether the base is negative and the exponent is an integer. If so, it computes the magnitude using pow(-x, y) and then sets the sign depending on whether the exponent is even or odd.

COMSingle::Pow mirrors this logic using the single-precision variants (powf, floorf, etc).

A new regression test verifies the expected positive results for even exponents with negative bases.

That’s not really how things work. Microsoft isn’t some small company where someone can necessarily pop next door and go “hey, I’ve got a problem”. It is a large enterprise with multiple divisions, some 200k employees, and it takes in tens of thousands (on the low end) of issues, feedback, and bug reports per day (many of which are low effort, don’t provide enough detail, are actually user error, etc).

Last time I checked, “the internet” and “message forwarding” are not some alien tech to be invented yet. Surely, an established multi-billion-dollar company could afford to use that occasionally, especially when it comes to such serious issues. Blaming the user for not using the correct issue tracker ought to be the lamest excuse. No one is to blame here, but Microsoft, for even letting that issue slip by in the first place.

This uCRT bug has been already reported through another channel a week ago and it got fixed since (OS #58189958: pow(-1, 2) returns -1). It might take a while to be available in the public insider builds

Edit 1: In other words, “Thank you for your report, this issue will be addressed in a future Windows Insider update”

Edit 2: Note that @huoyaoyuan is a .NET community contributor. Not entirely fair to lynch people donating their free time on the Internet, sharing valid pointers/experience. Here just like in any other GitHub project

It is perfectly fine to report issues like this one here. .NET runtime maintainers will route it as appropriate if it is found to be issue somewhere else.

We have dedicated tracking-external-issue label for this situation. You can see that it is not unusual situation - there are hundreds of issues marked with this label.

After it got reported again, 6 years later, by a Microsoft Employee, it got acknowledged within 2 days and fix released and deployed two weeks later.

This has nothing to do with it being reported by a Microsoft employee. Correlation is not causation.

The original 2018 was closed as not a bug with reasoning. That reasoning was valid and in some C implementation would have been correct. The original reporter then made no effort to push back or explain why it actually was a bug. Had the issue I opened been done in a similar fashion, it would’ve been duped against the original and closed with the same reasoning.

My issue was fixed because I provided reproduction steps, linked to the prior issue already, and (most importantly) gave concrete references to the technical specification (for both C and IEEE 754) that clearly state the requirement that FMA always be precise and that MSVC is opting into that behavior by defining __STDC_IEC_559__

It is the same reason that similar bugs in the uCRT math library have been reported via the same avenues in the past; such as this one that was found for FMA on older hardware: https://developercommunity.visualstudio.com/t/MSVCs-fma-implementation-is-incorrect-o/10594003

In that specific example, a random user reported it in 2018, and it got marked as Not a Bug and ignored. After it got reported again, 6 years later, by a Microsoft Employee, it got acknowledged within 2 days and fix released and deployed two weeks later.

Just reproducing for posterity here…

Non-MS employee, Nov 01, 2018 2:50 AM:

In C draft N1570 (emphasis mine): 7.12.13.1 “The fma functions compute (x × y) + z, rounded as one ternary operation: they compute the value (as if) to infinite precision and round once to the result format, according to the current rounding mode. A range error may occur.”; and F.10.10.1 (which applies to IEEE 754 conforming implementation) “fma(x, y, z) computes xy + z, correctly rounded once.”; On MSDN: “Multiplies two values together, adds a third value, and then rounds the result, without losing any precision due to intermediary rounding.” and “This function computes the value as though it were taken to infinite precision, and then rounds the final result.”.

Larry Hu [MSFT] Closed - Not a Bug Nov 01, 2018 4:12 AM:

Thank you for your feedback! We have determined that this issue is not a bug. Please see Steve’s comments!

–

MS employee, Feb 20, 2024 11:56 AM:

The fma functions compute (x * y) + z, rounded as one ternary operation: they compute the value (as if) to infinite precision and round once to the result format, according to the current rounding mode.

MS: fixes

MS employee: see, MS would fix it for anyone!

@tannergooding I don’t think this is any kind of complaint against an individual, but the management of bug reporting across Microsoft products as a whole. You’re right that it’s a lot of different divisions, and that they all handle it differently is a real pain in the butt to people that spot an issue.

For this bug alone it’s been:

  • Reported here
  • Replied at to be reported at a second location instead, since that’s more appropriate
  • Replied that it’s actually been filed at a third location which is internal
  • It was already fixed weeks ago and the poster wouldn’t have been able to see that

This has nothing to do with it being reported by a Microsoft employee. Correlation is not causation.

As long as the MS employee tags exist, the triage team will, consciously or not, treat MS employees as trusted and external people as unproven.

That reasoning was valid

No, it wasn’t. It’s understandable if the triage team is overworked and will occasionally make the wrong call, especially if the report is incomplete (not good, but understandable), but, as proven by the second report, that doesn’t make the rejection valid.

@nicuveo I can’t comment on behalf of the respective owners without the risk of misrepresenting something, so I won’t.

Just imagine something plausible, like a flawed performance optimization that affects specific inputs of specific math operations on a specific hardware… Weird things can happen in prerelease builds.

@s9w It does if you follow a short process at https://careers.microsoft.com/ j/k, yes, internal only tracker, sorry

How about Math.pow(-2, 2) then? does it return:

  • -4 ?
  • 4 ?
  • 42 ?
  • 27 ?

Wonder if its related? …

It’s totally an off-topic. Copilot is creating PR under the request of the user, and the assigned user for reviewing it is the actual acting initiator.

Unlike Windows Feedback Hub, the response at Visual Studio Feedback Hub is quite responsive. It’s OK to leave this issue here, but the only thing we can do is to forward the message to MSVC folks.

This is now fixed in Windows 11 Insider Preview Build 27898 (Canary Channel), see https://blogs.windows.com/windows-insider/2025/07/11/announcing-windows-11-insider-preview-build-27898-canary-channel/:

Fixed an underlying issue in the previous flight which could lead to Math.Pow() not outputting the correct result, which could lead to unexpected behavior in apps and games.

Closing as resolved.

@yirkha thanks for the update! would you have a link to the issue or more information about the fix? i don’t suppose the patch is publicly accessible?