两个 NSInteger 之和的值不正确
我确信我错过了一些东西,而且答案很简单,但我似乎无法理解为什么会发生这种情况。我正在尝试计算日期的平均值:
NSInteger runningSum =0;
NSInteger count=0;
for (EventoData *event in self.events) {
NSDate *dateFromString = [[NSDate alloc] init];
if (event.date != nil) {
dateFromString = [dateFormatter dateFromString:event.date];
runningSum += (NSInteger)[dateFromString timeIntervalSince1970];
count += 1;
}
}
if (count>0) {
NSLog(@"average is: %@",[NSDate dateWithTimeIntervalSince1970:(NSInteger)((CGFloat)runningAverage/count)]);
}
一切似乎都正常,除了 runningSum += (NSInteger)[dateFromString timeIntervalSince1970] ,它给出了错误的结果。如果我在取两个相等日期的平均值时设置断点(例如,2009-10-10,时间间隔为 1255125600),则 runningSum 为 -1784716096,而不是预期的 2510251200。
我尝试使用 NSNumber 和 I得到相同的结果。有人能指出我正确的方向吗?
谢谢!
安东尼奥
I'm sure I'm missing something and the answer is very simple, but I can't seem to understand why this is happening. I'm trying to make an average of dates:
NSInteger runningSum =0;
NSInteger count=0;
for (EventoData *event in self.events) {
NSDate *dateFromString = [[NSDate alloc] init];
if (event.date != nil) {
dateFromString = [dateFormatter dateFromString:event.date];
runningSum += (NSInteger)[dateFromString timeIntervalSince1970];
count += 1;
}
}
if (count>0) {
NSLog(@"average is: %@",[NSDate dateWithTimeIntervalSince1970:(NSInteger)((CGFloat)runningAverage/count)]);
}
Everything seems to work OK, except for runningSum += (NSInteger)[dateFromString timeIntervalSince1970], which gives an incorrect result. If I put a breakpoint when taking the average of two equal dates (2009-10-10, for example, which is a timeInterval of 1255125600), runningSum is -1784716096, instead of the expected 2510251200.
I've tried using NSNumber and I get the same result. Can anybody point me in the right direction?
Thanks!
Antonio
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否有什么理由对 NSInteger 犹豫不决?
[NSDate timeIntervalSince1970] 返回一个 NSTimeInterval,它基本上是一个 double。清理代码并看看是否有帮助。
Is there some reason you are fudging about with NSInteger?
[NSDate timeIntervalSince1970] returns an NSTimeInterval, which is basically a double. Clean up the code and see if that helps.
NSInteger 可以保存的值最高可达 INT_MAX 限制,即 2147483647 - 因此您的值会溢出整数类型限制 - 请记住 timeInterval 是 double 类型。
您可以尝试在所有计算中使用 double 类型或使用
-timeIntervalSinceReferenceDate
方法 - 它返回自2001 年 1 月 1 日
以来的时间间隔,您也可以避免溢出。如果您的
events
对象是一个数组或其他允许获取其大小的类型,那么您可以添加已经除以计数的时间间隔值 - 这也可能有助于避免溢出:NSInteger can hold values up to the
INT_MAX
limit that equals 2147483647 - so your value overflows the integer types limit - remember that timeInterval is a double type.You can try to use double type in all your calculations or use
-timeIntervalSinceReferenceDate
method - it returns interval since1 January 2001
and you might avoid overflow as well.If your
events
object is an array or other type that allows to get its size then you can add time interval values already divided by count - that also may help to avoid overflow:NSInteger
是 iPhone 上的 32 位有符号整数,因此限制为 -2147483648 到 +2147483647 之间的值。您可以使用
NSUInteger
获得所需的结果,它是一个无符号 32 位整数,能够包含 0 到 +4294967295 之间的值。但是,您应该注意循环的运行次数,以免对值进行换行。
NSInteger
is a 32 bit signed integer on iPhone and is therefore limited to values between −2147483648 and +2147483647.You may get the desired result by using
NSUInteger
which is an unsigned 32 bit integer able to contain values between 0 and +4294967295.You should however pay attention to the number of runs through the loop so you don't wrap the values.