Float comparison

I am ashamed to ask about this, but I am running iOS 6.1 and the following line returns False:

if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.1) 

still returns True:

 if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.1f) 

Why?

+4
source share
3 answers

After a bit of reading, my understanding of the reason for this is as follows:

• C treats numbers like 1,2 as doubles and if they are modified with f eg 1.2f as floating • Neither doubles nor floats can be internally represented by the system with 100% accuracy • Representation errors for floats are higher than for doubling

Therefore, comparing a float with a double will basically lead to misleading results. For non-critical systems, a comparison of 2 floats is sufficient.

0
source

Before comparison, the floats are thrown before doubling. A value of f indicates the number of decimal places. You compare two different numbers when you change the number of decimal places

+1
source

In the first version, systemVersion is converted from float to double and its value depends on how many bits you use to represent it.

In the second you compare two floats

+1
source

All Articles