Why does Splint (check C code) give an error when comparing float with int?

Both are mathematical values, however the float has higher accuracy. Is this the only reason for the error - the difference in accuracy? Or is there another potential (and more serious) problem?

+3
source share
6 answers

This is because the set of integer values ​​is not equal to the set of float values ​​for the types "int" and "float". For example, a float value of 0.5 has no equal in the integer set, and the integer value 4519245367 may not exist in the set of values ​​that the float can store. Thus, checker places this as a problem that the programmer should check.

+8
source

, , . ints; ints float.

+3

"" . , .

, - " " , , , . :

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

epsilon, DBL_EPSILON.

+2

float int, , : int float f, "i = f;", "if (i == f)" .

+1

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

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

Therefore, a floatcan represent any short, and a doublecan represent any long.

+1
source

If you need to get around this (you have a legitimate reason and you are satisfied, none of the problems mentioned in the other answers is a problem for you), then simply drop them from one type to another.

0
source

Source: https://habr.com/ru/post/1696621/


All Articles