Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two NSNumbers created from the same float aren't equal according to ==

I have a few floats which I convert to NSNumbers and add to an array, then sort in numerical order.

I then take the original floats and convert those to NSNumbers, but if I try to compare them if(numinarray = newnum){ it never goes anywhere. When I NSLog() out the value, the values match:

float distance  = [barnardCastle distanceFromLocation:loc1] / 1000;
float distance2 = [billingham distanceFromLocation:loc1] / 1000;
float distance3 = [bishopAukland distanceFromLocation:loc1] / 1000;
float distance4 = [cockerton distanceFromLocation:loc1] / 1000;
float distance5 = [darlington distanceFromLocation:loc1] / 1000;
float distance6 = [guisborough distanceFromLocation:loc1] / 1000;
float distance7 = [hartlepool distanceFromLocation:loc1] / 1000;
float distance8 = [middlesbrough distanceFromLocation:loc1] / 1000;
float distance9 = [northallerton distanceFromLocation:loc1] / 1000;
float distance10 = [redcar distanceFromLocation:loc1] / 1000;
float distance11 = [stockton distanceFromLocation:loc1] / 1000;
float distance12 = [yarm distanceFromLocation:loc1] / 1000;


NSArray *nums, *sorted;

nums = [NSArray arrayWithObjects:
        [NSNumber numberWithFloat:distance],
        [NSNumber numberWithFloat:distance2],
        [NSNumber numberWithFloat:distance3],
        [NSNumber numberWithFloat:distance4],
        [NSNumber numberWithFloat:distance5],
        [NSNumber numberWithFloat:distance6],
        [NSNumber numberWithFloat:distance7],
        [NSNumber numberWithFloat:distance8],
        [NSNumber numberWithFloat:distance9],
        [NSNumber numberWithFloat:distance10],
        [NSNumber numberWithFloat:distance11],
        [NSNumber numberWithFloat:distance12],
        nil];

sorted = [nums sortedArrayUsingSelector:@selector(compare:)];

NSLog(@"nums sorted");
NSLog(@"distance is %f",distance);
NSNumber *intdist = [NSNumber numberWithFloat:distance]; 
NSNumber *intdist2 = [NSNumber numberWithFloat:distance2];
NSNumber *intdist3 = [NSNumber numberWithFloat:distance3];
NSNumber *intdist4 = [NSNumber numberWithFloat:distance4];
NSNumber *intdist5 = [NSNumber numberWithFloat:distance5];
NSNumber *intdist6 = [NSNumber numberWithFloat:distance6];
NSNumber *intdist7 = [NSNumber numberWithFloat:distance7];
NSNumber *intdist8 = [NSNumber numberWithFloat:distance8];
NSNumber *intdist9 = [NSNumber numberWithFloat:distance9];
NSNumber *intdist10 = [NSNumber numberWithFloat:distance10];
NSNumber *intdist11 = [NSNumber numberWithFloat:distance11];
NSNumber *intdist12 = [NSNumber numberWithFloat:distance12];
NSLog(@"int dist %@",intdist);
NSLog(@"int dist2 %@",intdist2);
NSLog(@"int dist3 %@",intdist3);
NSLog(@"int dist4 %@",intdist4);
NSLog(@"int dist5 %@",intdist5);
NSLog(@"int dist6 %@",intdist6);
NSLog(@"int dist7 %@",intdist7);
NSLog(@"int dist8 %@",intdist8);
NSLog(@"int dist9 %@",intdist9);
NSLog(@"int dist10 %@",intdist10);
NSLog(@"int dist11 %@",intdist11);
NSLog(@"int dist12 %@",intdist12);


nearestLoc = [[NSMutableArray alloc]init];

NSLog(@"about to loop");
for (int i=0; i < 12; i++) {
    Branches *branches = [[Branches alloc]init];
    NSLog(@"looping");

    NSNumber *index = [sorted objectAtIndex:i];

    NSNumber *intdist = [NSNumber numberWithFloat:distance]; 
    NSNumber *intdist2 = [NSNumber numberWithFloat:distance2];
    NSNumber *intdist3 = [NSNumber numberWithFloat:distance3];
    NSNumber *intdist4 = [NSNumber numberWithFloat:distance4];
    NSNumber *intdist5 = [NSNumber numberWithFloat:distance5];
    NSNumber *intdist6 = [NSNumber numberWithFloat:distance6];
    NSNumber *intdist7 = [NSNumber numberWithFloat:distance7];
    NSNumber *intdist8 = [NSNumber numberWithFloat:distance8];
    NSNumber *intdist9 = [NSNumber numberWithFloat:distance9];
    NSNumber *intdist10 = [NSNumber numberWithFloat:distance10];
    NSNumber *intdist11 = [NSNumber numberWithFloat:distance11];
    NSNumber *intdist12 = [NSNumber numberWithFloat:distance12];
    NSLog(@"int dist %@",intdist);
    NSLog(@"int dist2 %@",intdist2);
    NSLog(@"int dist3 %@",intdist3);
    NSLog(@"int dist4 %@",intdist4);
    NSLog(@"int dist5 %@",intdist5);
    NSLog(@"int dist6 %@",intdist6);
    NSLog(@"int dist7 %@",intdist7);
    NSLog(@"int dist8 %@",intdist8);
    NSLog(@"int dist9 %@",intdist9);
    NSLog(@"int dist10 %@",intdist10);
    NSLog(@"int dist11 %@",intdist11);
    NSLog(@"int dist12 %@",intdist12);
    NSLog(@"index %@",index);


    if (index == intdist) {

        branches.branchLoc = @"Barnard Castle";
        branches.distance = distance;
        [nearestLoc addObject:branches];
        NSLog(@"adding barnard castle");
    }

    if (index == intdist2) {
        branches.branchLoc = @"Billingham";
        branches.distance = distance2;
        [nearestLoc addObject:branches];

        NSLog(@"adding billingham");
    }

Am I missing something with using NSNumber? If so, how would you get arround it?

like image 414
richard Stephenson Avatar asked Nov 28 '22 10:11

richard Stephenson


2 Answers

The test you are using compare the pointer not the value!

Comparing NSNumber should be done with

if([numInArray compare:newNum] == NSOrderedSame) {
    // do something
}
like image 42
j_freyre Avatar answered Dec 01 '22 00:12

j_freyre


Instead of using ==, call compare: or isEqualToNumber:. When you use ==, you are comparing the pointers.

like image 121
Daniel A. White Avatar answered Dec 01 '22 00:12

Daniel A. White