gpt4 book ai didi

objective-c - NSDecimalNumber 和 large unsigned long long(64 位)整数

转载 作者:太空狗 更新时间:2023-10-30 03:19:06 26 4
gpt4 key购买 nike

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

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

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 中得到任何大于最大有符号 long long 值的东西。它不喜欢大于 9223372036854775808 的值。但是,该数字似乎以全精度存储在 NSDecimalNumber 中,如:

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

我注意到 NSNumber 对象可以很好地处理这些大数字,并且 unsignedLongLongValue 可以正常工作。只是 NSDecimalNumbers(我坚持使用)不起作用。

如何从 NSDecimalNumbers 中获取正确的 unsigned long long 值?或者至少将它们转换为 unsignedLongLongValue 起作用的 NSNumber 对象。

最佳答案

我已通过开发者论坛收到 Apple 对此问题的回复:

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.

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 对象。

关于objective-c - NSDecimalNumber 和 large unsigned long long(64 位)整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6710469/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com