开发者

Subtracting 2 different doubles yields zero

开发者 https://www.devze.com 2023-02-19 15:10 出处:网络
I\'m building a loc-based app, using CLLocationManager and locationManager:didUpdateToLocation: fromLocation:

I'm building a loc-based app, using CLLocationManager and locationManager:didUpdateToLocation: fromLocation: I'm trying to determine if the user has moved enough to trigger some other processing. I'm using double variables. I learned long ago not to equate doubles, but to compare the delta to a threshold. I compare the abs( old - new ) to a delta of 0.0005f

But this code...

    double latDelta = abs(theConsumer.latLong.clLocation.coordinate.latitude  - newLocation.coordinate.latitude);
    double lonDelta = abs(theConsumer.latLong.clLocation.coordinate.longitude - newLocation.coordinate.longitude);

    NSString *fooBar = [NSString stringWithFormat: 
           @"???Delta LAT: abs(current (%f) - new (%f)) = delta %f.",
           theConsumer.latLong.clLocation.coordinate.latitude,
           newLocation.coordinate.latitude,
           latDelta];

yields this output: delta is zero.

GPSLogger.m:(26)> GPSLOG: GPS: 39.4425- North, 77.8132- West/n MeterAccuracyFormat 0

GPSLogger.m:(26)> GPSLOG:New Coordinates found: Lat:39.442494 Lon:-77.813175 DataModel.m:(197)> DistanceFromLoc: 710.517020 DataModel.m:(199)> Got a delta > 0.000500 DataModel.m:(214)> Delta LAT: abs(current (39.440120) - new (39.4开发者_运维知识库42494)) = delta 0.000000.

Are the numbers overflowing? Is there a better way to do this (i.e. one that works)?

Thanks.


abs() returns an int; you want fabs().

(You can also use the ABS() macro on iOS/OS X which returns a value whose type is the type of the argument.)

0

精彩评论

暂无评论...
验证码 换一张
取 消