4

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

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

yet the following returns True:

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

Why?

RunLoop
  • 20,288
  • 21
  • 96
  • 151
  • Double precision floating points are more precise than single precision floating points, so the values may differ. **There's a possible duplicate**, Look here: http://stackoverflow.com/questions/15581535/what-is-the-point-of-using-f-when-assigning-a-value-to-a-cgfloat/15581658 – Ramy Al Zuhouri Apr 03 '13 at 15:22
  • when working with iOS system version I retrieve the full string (6.1.3 for example, I split it into parts separated by '.', then I create an NSIndexPath from the values, you can then compare your system version using another NSIndexPath and aren't subject to real number precision problems. Sorry I don't have the code right now with me, but you should be able to duplicate my method from what I just told you – Jerome Diaz Apr 03 '13 at 15:49
  • 1
    possible duplicate of http://stackoverflow.com/questions/1614533/strange-problem-comparing-floats-in-objective-c – meth Apr 03 '13 at 17:14

3 Answers3

1

Floats are cast to doubles before comparing. The f signals the number of decimals. You are comparing two different numbers when you change the number of decimals

William Falcon
  • 9,813
  • 14
  • 67
  • 110
1

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

In the second one you are comparing two floats

pdrcabrod
  • 1,467
  • 1
  • 14
  • 22
0

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

• C treats numbers like 1.2 as double and if modified with f e.g. 1.2f as a float • Neither doubles nor floats can be internally represented by the system with 100% accuracy • The representation errors for floats are higher than those for doubles

Therefor comparing a float to a double will mostly lead to misleading results. For non-critical systems, comparing 2 floats is sufficient.

RunLoop
  • 20,288
  • 21
  • 96
  • 151