NSDecimalNumber和大无符号长整数(64位) [英] NSDecimalNumber and large unsigned long long (64-bit) integers

查看:515
本文介绍了NSDecimalNumber和大无符号长整数(64位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理来自JSON源的大型64位无符号整数,它们被解析为NSDecimalNumbers,这显然是忠实表示任意精度数字。

I'm handling large 64 bit unsigned integers from a JSON source which are being parsed into NSDecimalNumbers, which is apparently to "faithfully represent arbitrary-precision numbers".

我遇到的问题是,我无法得到正确的数字从这个类。例如(使用尽可能大的值):

The problem I'm having is that I cant get the correct numbers out of this class. For example (using the largest value possible):

print (unsigned long long) [[NSDecimalNumber decimalNumberWithString:@"18446744073709551615"] unsignedLongLongValue]
= 0 // Incorrect
print (unsigned long long) [[NSDecimalNumber decimalNumberWithString:@"9223372036854775808"] unsignedLongLongValue]
= 9223372036854775808 // Correct
print (unsigned long long) [[NSDecimalNumber decimalNumberWithString:@"9223372036854775810"] unsignedLongLongValue]
= 9223372036854775808 // Incorrect

看起来我不能得到任何大于NSDecimalNumber中的最大signed long long值。它不喜欢大于9223372036854775808的值。但是,似乎该数字以NSDecimalNumber中的完整精度存储,如下所示:

It would appear that I can't get anything larger than the maximum signed long long value out of an NSDecimalNumber. It's not liking values larger than 9223372036854775808. However it seems that the number is being stored in the full precision within an NSDecimalNumber, as:

po [[NSDecimalNumber decimalNumberWithString:@"18446744073709551615"] stringValue]
= 18446744073709551615

I注意到NSNumber对象可以处理这些大数字和unsignedLongLongValue正确工作。

I've noticed that NSNumber objects can handle these large numbers fine and unsignedLongLongValue works correctly. It's just NSDecimalNumbers (which I am stuck with) which aren't working.

如何从NSDecimalNumbers中获取正确的无符号长整型值?

How can I get correct unsigned long long values out of NSDecimalNumbers? Or at least convert them into NSNumber objects where unsignedLongLongValue does work.

推荐答案

我已经得到苹果对此问题的回复开发人员论坛:

I've had a response from Apple on this issue via the developer forums:


这是NSDecimalNumber的常见问题,简单的
访问器(如[unsigned] longLongValue)通过doubleValue
访问器 - 任何需要超过53位精度的值
将被不适当地舍入。请随时报告关于此
问题的错误,并提及错误编号8220543.

This is a standing issue with NSDecimalNumber where the simple accessors (such as [unsigned]longLongValue) go through the doubleValue accessor – this any value requiring more than 53 bits of precision will be inappropriately rounded. Feel free to Report a Bug about this issue and mention bug number 8220543.

也就是说,如果你只是
获得64位数字从JSON,你应该能够使用
NSNumber而不是NSDecimalNumber。

That said, if you are just getting 64-bit numbers from JSON, you should be able to just use NSNumber instead of NSDecimalNumber.

解析器从SBJson到JSONKit,这不仅快得多,它还将数字映射到NSNumber对象而不是NSDecimalNumber对象。

So I'm solving it by changing parser from SBJson to JSONKit, which is not only much faster, it also maps numbers into NSNumber objects instead of NSDecimalNumber objects.

这篇关于NSDecimalNumber和大无符号长整数(64位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆