为什么 [NSDecimalNumber longLongValue] 返回最小值
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您在
-longLong
中遇到了一个小的 base10->base2 舍入错误。我会在 bugreport.apple.com 上打开一个缺陷。您应该能够在很长的范围内往返一个数字。如果您完全保留在
NSDecimalNumber
中,您会注意到它不会出现舍入误差。这是一些我认为非常清楚地显示问题的代码: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:我认为这是因为
longLongValue
方法可以在NSString
和NSNumber
上使用,而不是在NSDecimalNumber
上使用。我不确定,但我认为这就是变量的值始终为
-9223372036854775808
的原因,即使您更改dn
上的字符串I think its because
longLongValue
method can be used atNSString
andNSNumber
and not onNSDecimalNumber
.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 ondn