检查浮点变量是否为整数的最可靠方法是什么?
我可以想到几种方法,例如。
Convert.ToInt32(floatingPoint) - floatingPoint == 0;
Math.Truncate(floatingPoint) - floatingPoint == 0;
floatingPoint % 1 == 0;
Math.Floor(floatingPoint) == floatingPoint;
//etc...
但哪种方法最可靠呢?
I can think of several ways, eg.
Convert.ToInt32(floatingPoint) - floatingPoint == 0;
Math.Truncate(floatingPoint) - floatingPoint == 0;
floatingPoint % 1 == 0;
Math.Floor(floatingPoint) == floatingPoint;
//etc...
But which method is most reliable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您不应该检查是否完全等于零,因为浮点数通常只包含与您分配给它的数字最接近的近似值。
例如,该类型可以表示的最接近 42 的可能值可能类似于 42.00000000000000662,您仍然希望将其计为整数值。
取该值与四舍五入值之间的差值,然后取其绝对值(以便它不是负数)并与一个小值进行比较:
You should not check for exact equality to zero, as a floating point number usually only contains the closest possible approximation to the number that you assigned to it.
For example, the closest possible value to 42 that the type could represent might be something like 42.00000000000000662, which you still would want to count as an integer value.
Take the difference between the value and the rounded value, then take the absolute value of that (so that it's not negative) and compare to a small value:
浮点无法精确表示某些整数,因此没有真正可靠的方法。 具体来说,任何大于 2^24 的
int
都不能用float
精确表示。 唉,这是您使用浮点表示所付出的代价的一部分!有关您应该注意的浮点各种问题的精彩总结,我鼓励您查看 “每个计算机科学家都应该了解浮点运算”。
Floating points can't represent some integers exactly, so there is no method that is truly reliable. Specifically, any
int
that is greater than 2^24 cannot be represented exactly by afloat
. Alas, that is part of the price you pay for using a floating-point representation!For an excellent summary of the various issues with floating point that you should be aware of, I encourage you to check out "What Every Computer Scientist Should Know About Floating-Point Arithmetic".
不管可靠性如何,取模方法看起来很容易理解(不需要阅读任何规范)并且快速(无需函数调用)。
Regardless of the reliability, the modulo method looks easy to understand (no need to read any specification) and fast (no function call).
您将遇到浮点数只是近似值的经典问题。 如果你这样做:
你最终会得到接近但不完全是 1 的结果。
You will run into the classic problem of floating point numbers being only approximations. If you do this:
you will end up with something close to, but not exactly, 1.
没有普遍可靠的方法。
由于浮点数(大部分)始终是先前计算的结果的近似值,因此正如其他人指出的那样,不存在简单的整数等价。
您必须考虑正在处理的 fp 和 int 值的范围和精度。 这完全取决于您想要实现的目标。
Guffa 有一个很好的方法,使用允许的精度范围进行 int 比较。
There is no generally reliable method.
Since floating point numbers are (mostly) always an approximation if they are the result of previous calculations, there is no simple integer equivalence, as others pointed out.
You have to consider range and precision of both fp and int values you're dealing with. It all depends of what you are trying to achieve.
Guffa has a good approach using an allowed range of precision for int comparison.
整数可以用浮点表示形式精确表示,因此如果您要检查精确的整数,那么任何一个都应该有效(我个人会使用第一个......)
Integers can be represented exactly in floating point representation so any of those should work if you're checking for an exact integer (personally I'd use the first one...)