在.NET中运行与双倍乘法相关的快速实验?并阅读了几篇关于C#字符串格式的文章,我认为这样:
{
double i = 10 * 0.69;
Console.WriteLine(i);
Console.WriteLine(String.Format(" {0:F20}", i));
Console.WriteLine(String.Format("+ {0:F20}", 6.9 - i));
Console.WriteLine(String.Format("= {0:F20}", 6.9));
}
Run Code Online (Sandbox Code Playgroud)
将是这个C代码的C#等价物:
{
double i = 10 * 0.69;
printf ( "%f\n", i );
printf ( " %.20f\n", i );
printf ( "+ %.20f\n", 6.9 - i );
printf ( "= %.20f\n", 6.9 );
}
Run Code Online (Sandbox Code Playgroud)
但是C#会产生输出:
6.9
6.90000000000000000000
+ 0.00000000000000088818
= 6.90000000000000000000
Run Code Online (Sandbox Code Playgroud)
尽管我在调试器中显示的值等于6.89999999999999946709(而不是6.9).
与C比较,显示格式要求的精度:
6.900000
6.89999999999999946709
+ 0.00000000000000088818
= 6.90000000000000035527
Run Code Online (Sandbox Code Playgroud)
这是怎么回事?
(Microsoft .NET Framework版本3.51 SP1/Visual …
.NET是否有内置方法来计算给定double或float 的ULP?
如果没有,最有效的方法是什么?