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.
您可以通过将两个浮点数舍入到所需的精度来可靠地比较两个浮点数的等效性,而不是使用 epsilon 进行浮点比较?
例如:
round($float, 3) === round($otherFloat, 3)
不。如果你的数字刚好在函数将向上而不是向下舍入的值的相对两侧(如果你四舍五入到最接近的整数,则为半整数),那么无论多么接近,它们都会四舍五入到不同的数字他们在一起。