NSDecimalNumber corruption when extracting valid long long ints

Originator:sbrautaset
Number:rdar://14544998 Date Originated:25-Jul-2013 10:47 AM
Status:Open Resolved:
Product:iOS SDK Product Version:iOS 6.1
Classification:Serious Bug Reproducible:Always
 
Summary:

For very large (long long) integers with more than 17 digits NSDecimalNumber will corrupt incorrectly when asked for a long long (or unsigned long long) representation, even if the number would normally fit in a long long without issue. For example this number: 348188955398766593

Steps to Reproduce:

	NSDecimalNumber *num = [NSDecimalNumber decimalNumberWithString:@"348188955398766593"];

Expected Results:

	(lldb) p (long long)[num longLongValue]
	(long long) $9 = 348188955398766593

	(lldb) p (unsigned long long)[num unsignedLongLongValue]
	(unsigned long long) $11 = 348188955398766593

Actual Results:

	(lldb) p (long long)[num longLongValue]
	(long long) $9 = 348188955398766592

	(lldb) p (unsigned long long)[num unsignedLongLongValue]
	(unsigned long long) $11 = 348188955398766592

Regression:

Lack of precision when converting from NSDecimalNumber to long long integer.

Notes:

It looks like NSDecimalNumber conversions to long long go via double, which has only 17 digits of precision. 

See also this bug:

https://github.com/stig/json-framework/pull/171

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!