开发者

How to check whether a huge floating point number is an integer?

I have a very large floating poin开发者_开发技巧t number (around 20 digits) and I want to check whether it is an integer or not. For example, if I have a number like 154.0 then it is an integer while 154.123123 is not an integer.

I need to check for very huge floating point numbers (20 digits or more) which means I can't first convert it into a long long datatype and see if both of them are same. Please shove me into right direction. I would appreciate answers only in C/C++. Thank you! :)


Well, what's "huge"? If the number is really huge in a sense that the number of digits is greater than the number representable by the mantissa of your floating-point number, then your floating-point number is always an integer.

For example, the IEEE 754 double-precision format has a 52-bit mantissa, which is sufficient for about 16 decimal digits. If your numbers have 20 decimal digits then any attempt to squeeze such numbers into a double will result in rounding, effectively turning your numbers into "integers".

You mention that your numbers are too large to fit into the long long datatype. If you are referring to 64-bit long long datatype, then it automatically means that your numbers are so large that they'll never have any fractional part when represented by a typical double type, i.e. they will always be "integers" if represented by double values.

P.S. Are you are using some exotic floating-point type with an extra-wide mantissa?


Just test whether x == floor(x)?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜