为什么double x * double x不等于int y

时间:2019-07-24 10:28:17

标签: c# loops console

我正在这段代码中,但是我需要解释为什么

            int n = 6;
            int count = 0;
            double end = Math.Sqrt(n);
            for (int i = 1; i < end; i++)
            {
                if (n % i == 0)
                    count += 2;
            }
            Console.WriteLine(end * end + " - " + n);
            Console.WriteLine(end * end == n);
            if (end * end == 6)  
                Console.WriteLine("why");

在第二个false中返回Console.Writeline,但是如果n=9返回true

1 个答案:

答案 0 :(得分:0)

end是一个双精度值,并且等于6的平方根。即使end * end应该是6,您仍需将doubleint=标志是一个很大的禁忌。
this答案中,您可以看到这样做的正确方法。

相关问题