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

问题描述 投票:0回答:1
            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 的弧度可以为正值或负值? acosf 在 C++ 中到底如何工作?

c++ trigonometry
1个回答
0
投票

根据文档

acosf
函数返回[0;π]范围内的值。

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

© www.soinside.com 2019 - 2024. All rights reserved.