我正在使用以下代码:
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。这有点奇怪?">
并不像你想的那么可怕。"实时"编译器或JiT的一项工作是剪切死代码。虽然在调试构建过程中,例程通常会变成"很少的优化",但有些例程仍然存在。我曾经编写过这段代码,以迫使运行时运行到"2GiB"的极限。什么也没发生,直到我真的添加了一个输出:
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);
}
}
}
请注意,通常这是一个非常好的工作部分。但不幸的是,对于极简主义的测试示例,它很容易导致这样的问题。