本文介绍了为什么0.1 * 10-1不等于0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Console.WriteLine(0.5f * 2f); // 1
Console.WriteLine(0.5f * 2f - 1f); // 0
Console.WriteLine(0.1f * 10f); // 1
Console.WriteLine(0.1f * 10f - 1f); // 1.490116E-08
最后是 1.490116E-08
( 0.0000001490116
)?
推荐答案
See Wiki: Floating Point. float/double/decimal are relative precision types. Not all values (of which there are infinitely many) can be exactly stored. You are seeing the result of this loss of accuracy. This is why it is almost always correct to use |a - b| < small_delta
for float-point comparisons.
这篇关于为什么0.1 * 10-1不等于0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!