4

I noticed the following in a chunk of code I'm maintaining/extending:

float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (systemVersion > 3.2 || systemVersion == 3.2 ) {
    //Stuff
}

I know floating point can result in some odd comparison behaviors due to precision, but would the above behave any differently than the chunk of code below?

float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (systemVersion >= 3.2) {
    //Stuff
}
thegrinner
  • 11,546
  • 5
  • 41
  • 64
  • Seems to be the same even if you throw NaNs and infinities at it. :) – Mysticial Sep 04 '12 at 18:30
  • 2
    Please tell me you don't have to deal with code that stores version numbers as floats... :-( – Mark Dickinson Sep 04 '12 at 18:46
  • @MarkDickinson It looks like the version number is stored as an NSString, which someone before me decided to turn into a float (clobbering the minor version number)... As I go through the code I'm hoping to change it to match [this answer](http://stackoverflow.com/a/5337804/264775), but I'm hesitant to change this outside the segments I'm working in. – thegrinner Sep 04 '12 at 19:14

2 Answers2

2

It's the same thing. In many compilers, the resulting machine instructions are exactly the same (although I can't say for sure that's true for clang).

Husker Jeff
  • 857
  • 1
  • 5
  • 6
1

No, it wouldn't. I think this code, that turned out this way by accident (maybe after a couple of small adjustments).

GolezTrol
  • 114,394
  • 18
  • 182
  • 210