Why is floating point arithmetic in C# imprecise?

后端 未结 3 1215
感情败类
感情败类 2020-11-21 23:06

Why does the following program print what it prints?

class Program
{
    static void Main(string[] args)
    {
        float f1 = 0.09f*100f;
        float f         


        
3条回答
  •  感情败类
    2020-11-21 23:25

    Floating point only has so many digits of precision. If you're seeing f1 == f2, it is because any difference requires more precision than a 32-bit float can represent.

    I recommend reading What Every Computer Scientist Should Read About Floating Point

提交回复
热议问题