Why does Splint (the C code checker) give an error when comparing a float to an int?

StackOverflow https://stackoverflow.com/questions/38027

  •  09-06-2019
  •  | 
  •  

Question

Both are mathematical values, however the float does have more precision. Is that the only reason for the error - the difference in precision? Or is there another potential (and more serious) problem?

Was it helpful?

Solution

It's because the set of integer values does not equal the set of float values for the 'int' and 'float' types. For example, the float value 0.5 has no equal in the integer set and the integer value 4519245367 might not exist in the set of values a float can store. So, the checker flags this as an issue to be checked by the programmer.

OTHER TIPS

Because it probably isn't a very good idea. Not all floats can be truncated to ints; not all ints can be converted to floats.

When doing the comparison, the integer value will get "promoted" to a floating point value. At that point you are doing a exact equality comparison between two floating point numbers, which is almost always a bad thing.

You should generally have some sort of "epsilon ball", or range of acceptable values, and you do the comparison if the two vaues are close enough to each other to be considered equal. You need a function roughly like this:

int double_equals(double a, double b, double epsilon)
{
   return ( a > ( b - epsilon ) && a < ( b + epsilon ) );
}

If your application doesn't have an obvious choice of epsilon, then use DBL_EPSILON.

Because floats can't store an exact int value, so if you have two variables, int i and float f, even if you assign "i = f;", the comparison "if (i == f)" probably won't return true.

Assuming signed integers and IEEE floating point format, the magnitudes of integers that can be represented are:

short  -> 15 bits
float  -> 23 bits
long   -> 31 bits
double -> 52 bits

Therefore a float can represent any short and a double can represent any long.

If you need to get around this (you have a legitimate reason and are happy none of the issues mentioned in the other answers are an issue for you) then just cast from one type to another.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top