Objective-c 中的计算未返回正确的值

发布于 2024-10-14 18:43:03 字数 991 浏览 2 评论 0原文

请查看这段代码,更具体地说是 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 技术交流群。

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

发布评论

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

评论(3

╭⌒浅淡时光〆 2024-10-21 18:43:03

这就是整数除法和浮点除法的区别。

此行:

float hourStep3 = hourStep2 / 24;

计算结果为 13.0f / 24,结果为 0.541667f(浮点除法)。

在组合计算中,您仅处理整数(不转换为中间的浮点数),因此

(h + (m / 60)) / 24

计算结果为 13 / 24,等于 0(整数除法)。将其更改为

(h + (m / 60)) / 24.0f

,您将得到与上面相同的结果。

It's the difference between integer division and floating-point division.

This line:

float hourStep3 = hourStep2 / 24;

evaluates to 13.0f / 24 which results in 0.541667f (floating-point division).

In the combined calculation, you are only dealing with integers (without converting to floats in between), so

(h + (m / 60)) / 24

evaluates to 13 / 24 which equals 0 (integer division). Change it to

(h + (m / 60)) / 24.0f

and you will get the same result as above.

無心 2024-10-21 18:43:03

在你的行中

float hourStep5 = ((h + (m / 60)) / 24) * 15; 

计算是在int中执行的,而不是在float中执行的。请注意,在 C 中(因此在 Objective-C 中)首先执行 = 右侧的方程,而不关心左侧的类型(在本例中为 浮动。)

请改用

float hourStep5 = ((h + (m / 60.0)) / 24) * 15; 

In your line

float hourStep5 = ((h + (m / 60)) / 24) * 15; 

The calculation is performed in int, not in float. 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

float hourStep5 = ((h + (m / 60.0)) / 24) * 15; 

instead.

断肠人 2024-10-21 18:43:03

hourStep5 的整个计算将被视为整数。

尝试将 h 和 m 都转换为该行中的浮点数:

float hourStep5 = (( (float) h + ( (float) m / 60)) / 24) * 15; 

The whole calculation of hourStep5 will be treated as integer.

Try casting both h and m into floats in that line:

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