为什么下面的程序会打印它所打印的内容?
class Program
{
static void Main(string[] args)
{
float f1 = 0.09f*100f;
float f2 = 0.09f*99.999999f;
Console.WriteLine(f1 > f2);
}
}
输出为
false
最佳答案
float 的精度只有这么多位数。如果您看到 f1 == f2,这是因为任何差异都需要比 32 位浮点可以表示的精度更高的精度。
我建议阅读What Every Computer Scientist Should Read About Floating Point
关于c# - 为什么 C# 中的浮点运算不精确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59387659/