Cast as an NSNumber doubleValue adding extra [phantom] precision
iPhone sdk 3.2.1
NSMutableDictionary *myDict = [NSMutableDictionary dictionaryWithCapacity:2];
[myDict setObject:[NSNumber numberWithDouble:0.1f] forKey:@"testDoubleObject"];
[myDict setValue:[NSNumber numberWithDouble:0.1f] forKey:@"testDoubleValue"];
Printing the description of myDict to the console yields this:
Printing description of myDict: {type = mutable, count = 2, capacity = 3, pairs = ( 0 : {contents = "testDoubleObject"} = {value = +0.10000000149011611938, type = kCFNumberFloat64Type} 1 : {contents = "testDoubleValue"} = {value = +0.10000000149011611938, type = kCFNumberFloat64Ty开发者_开发百科pe} )}
Pulling the doubles back out of the dict shows these phantom digits are being preserved:
NSNumber *doubleFromObject = [myDict objectForKey:@"testDoubleObject"];
NSNumber *doubleFromValue = [myDict valueForKey:@"testDoubleValue"];
Printing doubleFromObject and doubleFromValue yields this in the console:
Printing description of doubleFromObject: {value = +0.10000000149011611938, type = kCFNumberFloat64Type}
Printing description of doubleFromValue: {value = +0.10000000149011611938, type = kCFNumberFloat64Type}
The same thing happens when I try to create an NSNumber doubleValue from a string:
NSString *doubleAsAString = @"0.1";
NSNumber *doubleAsANumber = [NSNumber numberWithDouble:[doubleAsAString doubleValue]];
Printing doubleAsANumber yields this in the console:
Printing description of doubleAsANumber: {value = +0.10000000000000000555, type = kCFNumberFloat64Type}
These extra phantom digits are causing me problems.
1) What is causing this to happen?
2) How can I get rid of those extra phantom digits?
1. What is causing this to happen?
These "phantom" digits as you say are the by-product of representing a base-10 number in base-2.
2. How can I get rid of those extra phantom digits?
The best way to get rid of them would be to use a fixed-precision type, such as NSDecimal.
精彩评论