atan(x)给了我错误的价值观?

Jon*_*lay 3 c trigonometry

我在我的代码中得到了这个函数:

-(void)printAngle
{
    int width = p2_t.x-cp1_t.x;
    int height = p2_t.y-cp1_t.y;
    if (width != 0) {
        CCLOG(@"%i * %i", width, height);
        CCLOG(@"%f", (float)atan(height/width)*(180/M_PI));
    }
}
Run Code Online (Sandbox Code Playgroud)

下面是输出的片段:

2011-12-06 20:46:52.074 x[12392:c503] 24 * 13
2011-12-06 20:46:52.074 x[12392:c503] 0.000000
2011-12-06 20:46:52.108 x[12392:c503] 25 * 15
2011-12-06 20:46:52.108 x[12392:c503] 0.000000
Run Code Online (Sandbox Code Playgroud)

这意味着arctan(13/24)度数为0.这是不正确的.那我为什么得到0呢?这类型的东西我做错了吗?

它有一些正确的角度:

2011-12-06 20:51:11.956 x[12436:c503] 12 * 129
2011-12-06 20:51:11.957 x[12436:c503] 84.289404
2011-12-06 20:51:11.989 x[12436:c503] 10 * 132
2011-12-06 20:51:11.990 x[12436:c503] 85.601292
Run Code Online (Sandbox Code Playgroud)

Lil*_*ard 12

您正在使用整数数学并期望浮点结果.这不行.更改widthheightfloat不是,你应该得到预期的结果.

如果您不知道,在C中,使用整数进行除法会产生整数结果.所以评估13/24是生产0而不是0.541666667.通过(float)在执行除法之前将其中一个变量转换为或者仅使用float开始,您将得到您期望的浮点数.