简体   繁体   中英

How to compare NSNumber with a decimal value?

I recently upgraded from iOS 5 to iOS 6 and found this unit test was failing

- (void)testCalculatesDistanceBetweenTwoPoints
{
    self.sut = [[DistanceCalculator alloc] init];
    CLLocationCoordinate2D start = {.latitude = 34.32, .longitude = 99.13};
    CLLocationCoordinate2D finish = {.latitude = 105.94, .longitude = 27.73};
    NSNumber *distance = [self.sut kilometresBetweenPlace1:start andPlace2:finish];
    NSNumber *expected = [NSNumber numberWithDouble:3822.23073702318];
    STAssertEqualObjects(distance, expected, @"");
}

The failing asserting

'3822.23073702318' should be equal to '3822.23073702318'

When I print the raw value of each number I get what looks like the same value

2012-10-23 20:01:42.970 HelloWorld[1573:c07] 1 3822.23073702318
2012-10-23 20:01:42.970 HelloWorld[1573:c07] 2 3822.23073702318

When I print the type of each number I get what looks like the same (could I be doing this wrong?)

2012-10-23 20:06:37.309 HelloWorld[1611:c07] 1 __NSCFNumber 
2012-10-23 20:06:37.309 HelloWorld[1611:c07] 2 __NSCFNumber

Here is the full blown implementation if that helps

- (NSNumber *)kilometresBetweenPlace1:(CLLocationCoordinate2D)place1 andPlace2:(CLLocationCoordinate2D)place2
{
    MKMapPoint start = MKMapPointForCoordinate(place1);
    MKMapPoint finish = MKMapPointForCoordinate(place2);

    double distance = MKMetersBetweenMapPoints(start, finish) / 1600;

    return [NSNumber numberWithDouble:distance];
}

For anyone who might follow this discussion -here is what I ended up with

- (void)testCalculatesDistanceBetweenTwoPoints
{
    double accuracy = 0.00000000001;
    self.sut = [[DistanceCalculator alloc] init];
    CLLocationCoordinate2D start = {.latitude = 34.32, .longitude = 99.13};
    CLLocationCoordinate2D finish = {.latitude = 105.94, .longitude = 27.73};
    NSNumber *distance = [self.sut kilometresBetweenPlace1:start andPlace2:finish];
    NSNumber *expected = [NSNumber numberWithDouble:3822.23073702318];
    STAssertEqualsWithAccuracy([distance doubleValue], [expected doubleValue], accuracy, @"");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM