What is the most reliable way of checking if a floating point variable is an integer?

孤人 提交于 2020-01-03 01:55:06

问题


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?


回答1:


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:

if (Math.Abs(Math.Round(floatingPoint) - floatingPoint) < 0.000001) ...



回答2:


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 a float. 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".




回答3:


Regardless of the reliability, the modulo method looks easy to understand (no need to read any specification) and fast (no function call).




回答4:


You will run into the classic problem of floating point numbers being only approximations. If you do this:

floatingPoint = 1.0/3.0;
floatingPoint *= 3;

you will end up with something close to, but not exactly, 1.




回答5:


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.




回答6:


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...)



来源:https://stackoverflow.com/questions/638376/what-is-the-most-reliable-way-of-checking-if-a-floating-point-variable-is-an-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!