Objective-c 中的计算未返回正确的值
请查看这段代码,更具体地说是 hourStep 计算。
int h = [[timeArray objectAtIndex:0] intValue];
int m = [[timeArray objectAtIndex:1] intValue];
int s = [[timeArray objectAtIndex:2] intValue];
int mm = [[timeArray objectAtIndex:3] intValue];
NSLog([NSString stringWithFormat:@"time h:%d, m:%d, s:%d, mm:%d", h, m, s, mm]);
//time h:13, m:7, s:55, mm:105
float hourStep1 = m / 60;
float hourStep2 = h + hourStep1;
float hourStep3 = hourStep2 / 24;
float hourStep4 = hourStep3 * 15;
int hour1 = ceil(hourStep4);
NSLog([NSString stringWithFormat:@"hourStep1: %f, hourStep2: %f, hourStep3: %f, hourStep4: %f result: %d", hourStep1, hourStep2, hourStep3, hourStep4, hour1]);
//hourStep1: 0.000000, hourStep2: 13.000000, hourStep3: 0.541667, hourStep4: 8.125000 result: 9
float hourStep5 = ((h + (m / 60)) / 24) * 15;
NSLog([NSString stringWithFormat:@"hourStep5: %f", hourStep5]);
//hourStep5: 0.000000
我已将计算分解为各个步骤以获得正确答案,但是任何人都可以解释为什么 hourStep5 不会产生 hourStep4 产生的结果吗?
Please check out this piece of code, more specifically the hourStep calculations.
int h = [[timeArray objectAtIndex:0] intValue];
int m = [[timeArray objectAtIndex:1] intValue];
int s = [[timeArray objectAtIndex:2] intValue];
int mm = [[timeArray objectAtIndex:3] intValue];
NSLog([NSString stringWithFormat:@"time h:%d, m:%d, s:%d, mm:%d", h, m, s, mm]);
//time h:13, m:7, s:55, mm:105
float hourStep1 = m / 60;
float hourStep2 = h + hourStep1;
float hourStep3 = hourStep2 / 24;
float hourStep4 = hourStep3 * 15;
int hour1 = ceil(hourStep4);
NSLog([NSString stringWithFormat:@"hourStep1: %f, hourStep2: %f, hourStep3: %f, hourStep4: %f result: %d", hourStep1, hourStep2, hourStep3, hourStep4, hour1]);
//hourStep1: 0.000000, hourStep2: 13.000000, hourStep3: 0.541667, hourStep4: 8.125000 result: 9
float hourStep5 = ((h + (m / 60)) / 24) * 15;
NSLog([NSString stringWithFormat:@"hourStep5: %f", hourStep5]);
//hourStep5: 0.000000
I have broken down the calculation into the various steps to get the correct answer but can anyone explain why hourStep5 doesn't produce what hourStep4 produces?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是整数除法和浮点除法的区别。
此行:
计算结果为
13.0f / 24
,结果为0.541667f
(浮点除法)。在组合计算中,您仅处理整数(不转换为中间的浮点数),因此
计算结果为
13 / 24
,等于0
(整数除法)。将其更改为,您将得到与上面相同的结果。
It's the difference between integer division and floating-point division.
This line:
evaluates to
13.0f / 24
which results in0.541667f
(floating-point division).In the combined calculation, you are only dealing with integers (without converting to floats in between), so
evaluates to
13 / 24
which equals0
(integer division). Change it toand you will get the same result as above.
在你的行中
计算是在
int
中执行的,而不是在float
中执行的。请注意,在 C 中(因此在 Objective-C 中)首先执行=
右侧的方程,而不关心左侧的类型(在本例中为浮动
。)请改用
。
In your line
The calculation is performed in
int
, not infloat
. Note that in C (and thus in Objective-C) the equation on the right hand side of=
is performed first, without caring whatsoever the type of the left hand side (in this case,float
.)Use
instead.
hourStep5 的整个计算将被视为整数。
尝试将 h 和 m 都转换为该行中的浮点数:
The whole calculation of hourStep5 will be treated as integer.
Try casting both h and m into floats in that line: