Comparing floats to ints in C

Associate
Joined
24 Aug 2011
Posts
251
Location
Newcastle
Hi. This is literally making me go insane trying to get something working which seems really simple. Below is a snippet from some C code.

I am trying to test weather an integer is between two floats:

float x,y;
int startx=15;

if((x >=startx) && (x <= (startx+15)))
{
printf("It is in range\n");
}

So startx min and max values are 15 and 30, however I am getting values outside of these ranges. I think it must be a problem with floats.

Any Suggestions?
 
Soldato
Joined
19 Dec 2009
Posts
2,669
Location
Lancashire
I notice you haven't assigned a value to either of your two floats in that snippet. If you've done this in your full program, then neither of the conditions in your if statement will be true and the printf statement will never be executed. Otherwise, without seeing more of your code, I can't really say what your problem might be.
 
Caporegime
Joined
18 Oct 2002
Posts
32,623
Not sure what your error is here, your code looks incomplete if it is causing an issue.


Anyway, you should compare an int to a float without casting. The compiler should do th right thing but will also give a warning (for good reasons, an x value of 14.999 would be cast to 14 but due to floating point errors depending on your application you might want to perform some kind of rounding), better to manually cast:

I would do something like:
int x_int = (int) x;
if ((x_int > startx.....

That is what the compiler will do, but you have made it explicit and removed the warning.
You might want to perform some rounding (int_x = (int) (x+0.5) might be sufficient.
Or depending on your application you might want a custom rounding function.


That is assuming you want to compare against an int, of course you can go th otherway with startx a float.
 
Associate
Joined
9 Jun 2004
Posts
423
D.P. -- that's not correct, in the expression "x >=startx", startx is promoted to float for the comparison, not the other way around. Casting is unlikely to be what you want here.
 
Caporegime
Joined
18 Oct 2002
Posts
32,623
D.P. -- that's not correct, in the expression "x >=startx", startx is promoted to float for the comparison, not the other way around. Casting is unlikely to be what you want here.

Your right, I was multitasking and not thinking.

I would still try to mKe the cast explicit rather than let the compiler do it, not least due to floating point accuracy issues I would want to take care, e.g 0.6/0.2 => 3 will fail so I would want to do a fuzzy comparison.
 
Last edited:
Soldato
Joined
13 Feb 2003
Posts
6,157
if you're going to cast, at least use a proper static_cast and not c-style casts.

to the op: as ever with programming help, always post a complete compilable example otherwise we're not all on the same page...
 
Last edited:
Caporegime
Joined
18 Oct 2002
Posts
32,623
if you're going to cast, at least use a proper static_cast and not c-style casts.

to the op: as ever with programming help, always post a complete compilable example otherwise we're not all on the same page...

The OP is programming C, a c-style cast is appropriate.
 
Soldato
Joined
23 Dec 2002
Posts
2,806
Location
Bristol
Looks like a casting error to me as well,

It is good practice to not rely on the compiler for the promotion of variables always specify it your self.


Are the out of range values 30.x and or 14.x? if so it sounds like a rounding error caused by the conversion of float to int.

The compiler may be changing the x to a int for the comparison or it could be changing the startx to a float you don't know. Try forcing the compiler to promote the startx to a float and see if it fixes the issue.

Code:
if((x >=(float)startx) && (x <= (float)(startx+15)))
{
     printf("It is in range\n");
}
 
Last edited:
Associate
Joined
9 Jun 2004
Posts
423
The compiler may be changing the x to a int for the comparison or it could be changing the startx to a float you don't know.

Then the compiler would not be compliant to the standard in a pretty fundamental way -- these rules are in the C standard, so you *do* know, we're not dealing with undefined behaviour here.
 
Back
Top Bottom