为什么longLongValue返回不正确的值 [英] Why is longLongValue returning the incorrect value

查看:98
本文介绍了为什么longLongValue返回不正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSDictionary,其中包含一个值为4937446359977427944的密钥.我尝试获取它的值很长,然后又得到4937446359977427968吗?

I have a NSDictionary that contains a key with a value of 4937446359977427944. I try and get the value of it as a long long and get 4937446359977427968 back?

NSLog(@"value1 = %@", [dict objectForKey"MyKey"]); // prints 4937446359977427944 

long long lv = [dict objectForKey:@"MyKey"] longLongValue];

NSLog(@"value2 = %lld", lv); // prints 4937446359977427968

正在做

NSLog(@"%lld", [@"4937446359977427944" longLongValue]); // prints 4937446359977427944

我认为这是一个四舍五入的问题,因为低位似乎已经清除了,我只是不知道如何阻止它(或者为什么会发生这种情况).

I'm assuming it is some kind of round off issue since the lower bits seems to be cleared, I just don't know how to stop it (or why it's happening).

正在使用NSJSONSerialization创建字典,并且JSON对象(正确)包含"MyKey": 4937446359977427944条目,并且dict对象正确.

The dictionary is being created using NSJSONSerialization and the JSON object does (correctly) contain a "MyKey": 4937446359977427944 entry and the dict object is correct.

NSDictionary中保存的值是NSDecimalNumber

某物是否在后台转换为浮点数?

Is something being convert to a float behind the scenes?

推荐答案

NSDecimalValue不存储为double,它是64位无符号整数尾数,8位以10为底的有符号整数指数和一个符号位.

NSDecimalValue is not stored as a double, it's a 64 bits unsigned integer mantissa, an 8 bit signed integer exponent of base 10, and a sign bit.

问题在于,NSDecimalValue的确切值只能表示为... an NSDecimalValue.

The problem is that an exact value of an NSDecimalValue is only representable as ... an NSDecimalValue.

使用方法doubleValue可以获得大约64位的IEE754值.

You can get an approximate 64 bits IEE754 value with method doubleValue.

当您尝试使用longLongValue时,可以有效地将转换为IEE754近似值的结果很长.

When you try to use longLongValue you effectively get the result of casting to a long long int the approximate IEE754 value.

您可能会或可能不会认为它是实施NSDecimalValue的错误(并最终提出雷达要求苹果使用其他转换例程).但是严格来说,这不是错误:这是设计决定.

You may or may not consider it a bug in the implementation of NSDecimalValue (and eventually file a radar and ask Apple to use a different conversion routine). But strictly speaking this is not a bug: it's a design decision.

您应该将NSDecimalValue视为一种浮点十进制.实际上,它与IEEE754称为扩展精度浮点十进制数字的软件实现非常相似,不同之处在于它不符合该定义(因为它不具有至少支持值的指数)介于−6143和+6144之间,因为它不支持NAN和无限).

You should think of NSDecimalValue as a sort of floating point decimal. In fact it's very similar to a software implementation of what IEEE754 would call an extended precision floating point decimal number, except that it does not conform to that definition (because it does not have an exponent supporting at least values between −6143 and +6144 and because it does not support NANs and infinites).

换句话说,它不是整数的扩展实现,它是双精度的扩展(但缺少NAN和无穷大)实现. Apple本身仅提供对double的近似转换(这意味着对于精度超过53位的任何值,转换为long long int可能是正确的,也可能是不正确的),这并不是一个错误.

In other words, it's not an extended implementation of an integer, it's an extended (but lacking NANs and infinites) implementation of a double. The fact that Apple natively only provides an approximate conversion to double (implying that the conversion to long long int may or may not be exact for any value that exceed 53 bits of precision) is not a bug.

您可能想要也可能不想自己实施其他转换(带有类别).

You may or may not want to implement a different conversion yourself (with a category).

另一种可能的观点是认为该问题是您所使用的JSon实现中的错误.但这也是值得商bat的:它为您提供了NSDecimalValue,可以说是正确的表示.您可以使用NSDecimalValue 对其进行任何转换.

Another possible point of view is to consider the problem being a bug in the JSon implementation you used. But this is also highly debatable: it gave you a NSDecimalValue and that's arguably a correct representation. Either you operate with the NSDecimalValue or you are responsible for any conversion of it.

这篇关于为什么longLongValue返回不正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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