首页 > 解决方案 > 为什么 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++ 中是如何工作的?请帮忙

标签: c++debuggingtrigonometry

解决方案


根据文档,该acosf函数返回 [0;π] 范围内的值。

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


推荐阅读