Check if float is an integer

How to check if a float variable contains an integer value? So far I have used:

 float f = 4.5886; if (f-(int)f == 0) printf("yes\n"); else printf("no\n"); 

But I wonder if there is a better solution, or if it has some (or many) disadvantages.

+54
c floating-point int
Apr 26 '11 at 22:00
source share
8 answers

Besides the subtle answers that are already set, you can also use ceilf(f) == f or floorf(f) == f . Both expressions return true if f is an integer. They also return false for NaNs ( NaN always compare unequal ) and true for ± infinity and have no problem with overflowing the integer type used to hold the truncated result, because floorf() / ceilf() returns float s.

+59
Apr 26 '11 at 10:23
source share

Keep in mind that most of the methods here are valid, assuming that rounding error due to previous calculations is not a factor. For example, you can use roundf , for example:

 float z = 1.0f; if (roundf(z) == z) { printf("integer\n"); } else { printf("fraction\n"); } 

The problem with this and other similar methods (such as ceilf , casting to long , etc.) of ceilf is that although they work fine for integer constants, they will fail if that number is the result of the calculation, rounding error rounding. For example:

 float z = powf(powf(3.0f, 0.05f), 20.0f); if (roundf(z) == z) { printf("integer\n"); } else { printf("fraction\n"); } 

It prints a "fraction", although (3 1/20 ) 20 should be 3, because the actual calculation result was 2.9999992847442626953125.

Any similar method, be it fmodf or something else, obeys this. In applications that perform complex or round-robin calculations, usually what you want to do is define a certain “tolerance” value for what constitutes an “integer” (this applies to general floating point comparisons in general). We often call this tolerance epsilon. For example, let's say that we skip the computer to +/- 0.00001 rounding errors. Then, if we test z , we can choose epsilon 0,00001 and do:

 if (fabsf(roundf(z) - z) <= 0.00001f) { printf("integer\n"); } else { printf("fraction\n"); } 

You really don't want to use ceilf here, because for example ceilf(1.0000001) is 2 not 1, and ceilf(-1.99999999) is -1 not -2.

You can use rintf instead of roundf if you want.

Select a tolerance value suitable for your application (and yes, sometimes a zero tolerance is appropriate). For more information, check out this article on comparing floating point numbers .

+14
Aug 12 '14 at 22:08
source share

stdlib float modf (float x, float * ipart) is divided into two parts, check if the return value (fractional part) == 0.

+8
Apr 26 2018-11-21T00:
source share
 if (fmod(f, 1) == 0.0) { ... } 

Do not forget math.h and libm .

+6
Apr 26 '11 at 22:05
source share
 if (f <= LONG_MIN || f >= LONG_MAX || f == (long)f) /* it an integer */ 
+4
Apr 26 '11 at 10:12
source share

I'm not 100% sure, but when you impose f on int and subtract it from f, I believe that it returns to float. In this case, it probably does not matter, but there may be a problem in the line if you expect it to be for some reason.

I do not know if this is the best solution as such, but instead you can use the math modulo, for example: float f = 4.5886; bool isInt; isInt = (f % 1.0 != 0) ? false : true; float f = 4.5886; bool isInt; isInt = (f % 1.0 != 0) ? false : true; depending on your compiler, you may or may not need .0 after 1, the whole implicit throw appears again. In this code, bool isInt should be true if the right side of the decimal point is zero, and false otherwise.

0
Apr 26 '11 at 10:25
source share
 #define twop22 (0x1.0p+22) #define ABS(x) (fabs(x)) #define isFloatInteger(x) ((ABS(x) >= twop22) || (((ABS(x) + twop22) - twop22) == ABS(x))) 
0
Nov 30
source share

It's good that I recently ran into this problem and, surprisingly, found any solutions for you on the Internet. So here is my solution. For example, if the value is 3.00, and we need only 3, then we need to check whether followed by "." have zeros or not. If all this can be converted to int or otherwise, it will remain floating. Therefore enable this function

 int isInt(float val) { int i = (int) val; if( i != val) return 0; // its not an integer else return 1; // is an integer } int main() { float i = 3.9; // not this is an example. if(isInt()) printf("%d", i); else printf("%f", i); return 0; } 

Hope this helps!

http://anandkhinvasara.com/check-whether-number-integer-float-c/

-2
Dec 04 '14 at 6:50
source share



All Articles