简体   繁体   English

Math.Pow(10,1.946)返回0

[英]Math.Pow(10,1.946) returns 0

I am using the following code: 我使用以下代码:

float test = (float)Math.Pow(10,1.946);

The problem is that this code returns 0 instead of 88.30799004 . 问题是此代码返回0而不是88.30799004 Also when using the following code, it will return 0 : 此外,当使用以下代码时,它将返回0

double test = Math.Pow(10,1.946);

I am using Xamarin and I have set a breakpoint at the variable. 我正在使用Xamarin,我在变量上设置了一个断点。 With the exact same code it does go off, but returns 0 , why is this? 使用完全相同的代码它会消失,但返回0 ,为什么会这样?

"Didn't use Debug.Write() and stepped over the breakpoint: it stayed 0. When I added Debug.Write() and stepped over the breakpoint, it did return the right value 88.30799004. That's kinda weird?" “没有使用Debug.Write()并跨过断点:它保持为0.当我添加Debug.Write()并跨过断点时,它确实返回了正确的值88.30799004。这有点奇怪吗?”

Not as wierd as you think. 没有你想象的那么奇怪。 One job of the "Just in Time" compiler or JiT is it to cut out dead code. “Just in Time”编译器或JiT的一项工作是删除死代码。 While usually the routines are turned to "very little optimisation" during debug builds, some is still there. 虽然在调试版本中通常将例程转换为“非常小的优化”,但仍有一些例程。 I once made this code to force the Runtime to run into the "2 GiB" limit. 我曾经制作此代码以强制运行时运行到“2 GiB”限制。 And nothing happened untill I actually added a output: 没有任何事情发生,直到我实际上添加了一个输出:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OOM_32_forced
{
    class Program
    {
        static void Main(string[] args)
        {
            //each short is 2 byte big, Int32.MaxValue is 2^31.
            //So this will require a bit above 2^32 byte, or 2 GiB
            short[] Array = new short[Int32.MaxValue];

            /*need to actually access that array
            Otherwise JIT compiler and optimisations will just skip
            the array definition and creation */
            foreach (short value in Array)
                Console.WriteLine(value);
        }
    }
}

Note that usually it is a quite nice and well working part. 请注意,通常它是一个相当不错且工作良好的部分。 But with minimalistic test examples it is prone to cause such issues, unfortunately. 但遗憾的是,对于极简主义的测试示例,它很容易导致此类问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM