我在 .net (4.6.1) 中有一个场景,其中解析 6dp 浮点值的字符串表示形式会在 32 位和 64 位模式下产生不同的结果。
[Fact]
public void ParseTest()
{
var numText = "51.580133";
double.Parse(numText)
.ToString("R")
.Should().Be(numText);
}
此测试在 32 位模式下通过,但在 64 位模式下失败,因为生成的文本为:“51.580132999999996”
我预计无理数或通过方程导出的数字会出现这样的舍入问题,但这里浮点的长度和精度没有任何歧义。
这是在旧系统内,因此将所有内容更改为十进制将需要付出巨大的努力。
问题:
- 为什么会发生这种情况?
- 有哪些选项可以可靠地舍入/截断该值至 6dp?
更新 这有效,并产生与 ToString("G6") 不同的输出:
[Fact]
public void ParseText()
{
var numText = "51.580133";
double.Parse(numText)
.ToString("G8")
.Should().Be(numText);
}
最佳答案
我从 Microsoft 发现了这个有趣的观点,这可能可以解释这个问题
In some cases, Double values formatted with the "R" standard numeric format string do not successfully round-trip if compiled using the /platform:x64 or /platform:anycpu switches and run on 64-bit systems.
To work around this problem, you can format Double values by using the "G17" standard numeric format string. The following example uses the "R" format string with a Double value that does not round-trip successfully, and also uses the "G17" format string to successfully round-trip the original value.
可以在此处找到评论和示例:https://msdn.microsoft.com/en-us/library/kfsatb94(v=vs.110).aspx
关于c# - 往返格式说明符 "R"的 64 位与 32 位双重解析问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45373425/