tags:

views:

251

answers:

6

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?

+3  A: 

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.

DrPizza
+8  A: 

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.

Skizz

Skizz
+1  A: 

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.

Paul Tomblin
A: 

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.

Tim Ring
+1  A: 

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.

smh
+2  A: 

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.

Chris Arguin
Warning: the function double_equals lacks an important property of "regular" equality. It is not transitive. (With regular equality, if x == y and y == z, then x == z. That's no longer true when using double_equals.)
bortzmeyer