为什么acosf永远不会为我返回-ve值?

            xlight = cosf(angle_to_light);
            ylight = sinf(angle_to_light);
            xrotate = A*xlight;// - B*xrepel;// + C*xrandom;
            yrotate = A*ylight;// - B*yrepel;// + C*yrandom;

            angle_to_rotate = acosf( xrotate/( sqrtf(xrotate*xrotate + yrotate*yrotate) ) );
            //printf("xrotate = %f,yrotate = %f,rotate = %f\n",xrotate,yrotate,angle_to_rotate);
            if (angle_to_rotate>0) {
                set_motors(0,kilo_turn_right);
            }
            else if (angle_to_rotate<0) {
                printf("hello\n" );
                set_motors(kilo_turn_left,0);
            }

由于某种原因,我从没获得angle_to_rotate的负值。基本上我的代码从不打印“ hello”。 angle_to_light可以是+ ve或-ve弧度吗? acosf如何在c ++中正常工作?请帮助

derwfqg 回答:为什么acosf永远不会为我返回-ve值?

根据the documentationacosf函数返回[0;π]范围内的值。

从数学上讲,arccos是一个多值函数;特别是cos X == cos -X。 C ++函数只能返回一个代表值,它们决定了该范围。您可以通过添加2π和/或取反来获得其他值。

本文链接:https://www.f2er.com/3069164.html

大家都在问