Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
可能的重复: JavaScript 的数学被破坏了吗?
为什么在某些编程语言中,标题中的表达式计算为真?我已经在 php、ruby 和 python 中尝试过。
请阅读What Every Programmer Should Know About Floating-Point Arithmetic。
What Every Programmer Should Know About Floating-Point Arithmetic
double TOLERANCE < 1.0E-10; if(fabs(0.1+0.7-0.8)< TOLERANCE) { std::cout << "0.1 + 0.7 == 0.8" << std::endl; } else { std::cout << "0.1 + 0.7 != 0.8" << std::endl; }
由于浮点数的内部解释,它们不完全等于您所写的。