c# - 为什么 C# 中的浮点运算不精确?

标签 c# floating-point

为什么下面的程序会打印它所打印的内容?

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/

相关文章:

c# - 使整个 LinkLabel 区域可点击

floating-point - IEEE浮点,如何计算偏差

math - Golang 游学平方根

c++ - 将 float\double 数字格式化为零填充和预设位数的 CString

c# - RestSharp 发布 ByteArray/Stream 数据

c# - TimeZoneInfo 和国家代码

c# - 使用 ASP.NET Core 在单元测试中模拟 POST 请求

math - float 学有问题吗?

gcc 9.3.0 printf %Lf 输出不正确

c# - 在Unity中序列化和反序列化Json和Json数组