特殊双精度值64bit

时间:2016-11-24 14:16:36

标签: double 32bit-64bit

虽然我知道有关MSDN网站(https://msdn.microsoft.com/en-us/library/kfsatb94(v=vs.110).aspx)上往返的64位特别通知,但我有一个在该保护伞下似乎非常特殊的数字。

价值-0.21943往返的双倍为" -0.21943"在32位库中,但它作为" -0.21942999999999999"在64位模式下,使用" R"或" G17"。只有在未格式化的情况下才会出现" -0.21943"。

最好的部分是,2个字符串值" -0.21943"和" -0.21942999999999999"两者似乎都是相同的,看起来是同义词。这在32位或64位模式下表示为TRUE:

double fiveDecimals = double.Parse("-0.21943");
double seventeenDecimals = double.Parse("-0.21942999999999999");

Console.WriteLine("Five with no formatting: {0}", fiveDecimals);
Console.WriteLine("Five with Roundtrip formatting: {0}", fiveDecimals.ToString("R"));

Console.WriteLine("Seventeen with no formatting: {0}", seventeenDecimals);
Console.WriteLine("Seventeen with Roundtrip formatting: {0}", seventeenDecimals.ToString("R"));

Console.WriteLine(fiveDecimals == seventeenDecimals);

32位结果:

Five with no formatting: -0.21943
Five with Round-trip formatting: -0.21943
Seventeen with no formatting: -0.21943
Seventeen with Round-trip formatting: -0.21943
True

64位结果:

Five with no formatting: -0.21943
Five with Round-trip formatting: -0.21942999999999999
Seventeen with no formatting: -0.21943
Seventeen with Round-trip formatting: -0.21942999999999999
True

任何人都可以帮忙解释为什么这个5位小数会受这个已知的64位差异影响吗?

0 个答案:

没有答案