繁体   English   中英

Math.Pow(10,1.946)返回0

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

我使用以下代码:

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

问题是此代码返回0而不是88.30799004 此外,当使用以下代码时,它将返回0

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

我正在使用Xamarin,我在变量上设置了一个断点。 使用完全相同的代码它会消失,但返回0 ,为什么会这样?

“没有使用Debug.Write()并跨过断点:它保持为0.当我添加Debug.Write()并跨过断点时,它确实返回了正确的值88.30799004。这有点奇怪吗?”

没有你想象的那么奇怪。 “Just in Time”编译器或JiT的一项工作是删除死代码。 虽然在调试版本中通常将例程转换为“非常小的优化”,但仍有一些例程。 我曾经制作此代码以强制运行时运行到“2 GiB”限制。 没有任何事情发生,直到我实际上添加了一个输出:

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);
        }
    }
}

请注意,通常它是一个相当不错且工作良好的部分。 但遗憾的是,对于极简主义的测试示例,它很容易导致此类问题。

暂无
暂无

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

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