5

Until now this has not been a problem but from about today I ran into a strange issue that does not happen all the time, but rarely, I'd say one time out of 50, or even less.

I'm comparing two CGFloat:s like this:

const CGFloat MYFIRSTFLOAT = /* get the first value */
const CGFloat THESECONDFLOAT = /* get the second value */
if (MYFIRSTFLOAT < THESECONDFLOAT) {
// do something
}
else {
// Do something else
}

As you can see I'm not comparing using == or anything because these are floats... (like, it shouldn't be related to How dangerous is it to compare floating point values? now should it)

And the problem I'm having is I run into the occasion where the two values are exact, but the comparison evaluates to YES. In my case both of the values are "8926.5" - this value I got from LLDB. Is there a good explanation to this??? (I'm thinking that LLDB rounds off both values, meaning that they could be other values really - but why then would this also happen at runtime?)

This kind of puts me off cause I can't just see the problem... well, I'm thinking of forcing the floats to int only for this comparison, and it might be ok for my use this time, but I feel that this problem had better be totally understood :-P

Community
  • 1
  • 1
Jonny
  • 15,955
  • 18
  • 111
  • 232

2 Answers2

7

Try this:

const CGFloat a = 8926.5;
const CGFloat b = 8926.5;
if (a<b)
{
    NSLog(@"Yes");
}
else
{
    NSLog(@"No");
}

This will print No.

Now change the value like

const CGFloat a = 8926.5;
const CGFloat b = 8926.50007;

This will print Yes. But if you print these values using LLDB or po statement both will display 8926.5 . So issue is with the values, and LLDB wraps the value. That's why the issue happens.

Suggestion:

Change the if condition like:

if(fabsf(MYFIRSTFLOAT)<fabsf(THESECONDFLOAT))
{
  //Do something
}
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
0

yes, because these are float values &they have the problem of float point arithmetic. even the value seems same but there is difference in decimal point values while these stores on memory i.e. in memory representation

Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
  • The debugger LLDB shows same value twice. 8926.5 and 8926.5. It's not like the one is less than the other, but the comparison ends with a YES anyway. – Jonny May 23 '13 at 08:26
  • yes even both are same , there is some difference in float storage presentation – Ravindra Bagale May 23 '13 at 08:28
  • Cool, I accepted your answer. I'll just convert them to ints before comparison. Some suggest using fabs() etc... – Jonny May 23 '13 at 08:31
  • 1
    @RavindraBagale: This statement **"8926.5 & 8926.5 both are unequal because float point calculation"** is not correct. – Midhun MP May 23 '13 at 08:35
  • the whole second sentence makes absolutely no sense. – peko May 23 '13 at 09:05