Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

lint

splint

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?

like image 417
Thomas Owens Avatar asked Dec 07 '22 09:12

Thomas Owens


2 Answers

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.

like image 163
Skizz Avatar answered Jan 12 '23 01:01

Skizz


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.

like image 26
DrPizza Avatar answered Jan 11 '23 23:01

DrPizza