为什么 [NSDecimalNumber longLongValue] 返回最小值

发布于 2024-10-20 13:10:35 字数 271 浏览 5 评论 0原文

NSDecimalNumber *dn = [[[NSDecimalNumber alloc] initWithString:@"9223372036854775806"] autorelease];
long long llVal = [dn longLongValue]; 

为什么 llVal 是 -9223372036854775808 ?

NSDecimalNumber 扩展了 NSNumber 因此它应该处理 long long 类型。不是吗?

NSDecimalNumber *dn = [[[NSDecimalNumber alloc] initWithString:@"9223372036854775806"] autorelease];
long long llVal = [dn longLongValue]; 

why llVal is -9223372036854775808 ?

NSDecimalNumber extends NSNumber so it supposes to handle long long type. doesn't it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘羡 2024-10-27 13:10:36

我相信您在 -longLong 中遇到了一个小的 base10->base2 舍入错误。我会在 bugreport.apple.com 上打开一个缺陷。您应该能够在很长的范围内往返一个数字。

如果您完全保留在 NSDecimalNumber 中,您会注意到它不会出现舍入误差。这是一些我认为非常清楚地显示问题的代码:

unsigned long long longlong = 9223372036854775806;
NSLog(@"longlong: %lu", longlong);
NSDecimalNumber *dn = [NSDecimalNumber decimalNumberWithMantissa:longlong exponent:0 isNegative:NO];
NSLog(@"dn-dn: %@", dn);    // Round-trips fine
NSLog(@"dn+10: %@", [dn decimalNumberByAdding:[NSDecimalNumber decimalNumberWithString:@"10"]]);    // Even does math
NSLog(@"dn-lu: %lu", [dn unsignedLongValue]); // has rounding error

2011-03-07 23:56:15.132 Untitled[16059:a0f] longlong: 9223372036854775806
2011-03-07 23:56:15.135 Untitled[16059:a0f] dn-dn: 9223372036854775806
2011-03-07 23:56:15.135 Untitled[16059:a0f] dn+10: 9223372036854775816
2011-03-07 23:56:15.136 Untitled[16059:a0f] dn-lu: 9223372036854775808

I believe you are running into a small base10->base2 rounding error in -longLong. I would open a defect at bugreport.apple.com. You should be able to round-trip a number within the long long range.

If you stay entirely in NSDecimalNumber, you will note that it does not suffer the rounding error. Here is some code that I think shows the problem very clearly:

unsigned long long longlong = 9223372036854775806;
NSLog(@"longlong: %lu", longlong);
NSDecimalNumber *dn = [NSDecimalNumber decimalNumberWithMantissa:longlong exponent:0 isNegative:NO];
NSLog(@"dn-dn: %@", dn);    // Round-trips fine
NSLog(@"dn+10: %@", [dn decimalNumberByAdding:[NSDecimalNumber decimalNumberWithString:@"10"]]);    // Even does math
NSLog(@"dn-lu: %lu", [dn unsignedLongValue]); // has rounding error

2011-03-07 23:56:15.132 Untitled[16059:a0f] longlong: 9223372036854775806
2011-03-07 23:56:15.135 Untitled[16059:a0f] dn-dn: 9223372036854775806
2011-03-07 23:56:15.135 Untitled[16059:a0f] dn+10: 9223372036854775816
2011-03-07 23:56:15.136 Untitled[16059:a0f] dn-lu: 9223372036854775808
我三岁 2024-10-27 13:10:36

我认为这是因为 longLongValue 方法可以在 NSStringNSNumber 上使用,而不是在 NSDecimalNumber 上使用。

我不确定,但我认为这就是变量的值始终为  -9223372036854775808 的原因,即使您更改 dn 上的字符串

I think its because longLongValue method can be used at NSString and NSNumber and not on NSDecimalNumber.

I dont know it for sure but i think thats the reason the value of your variable is always  -9223372036854775808 even if you change the string on dn

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文