c++ - 为什么 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++ 中是如何工作的?请帮忙
解决方案
根据文档,该acosf
函数返回 [0;π] 范围内的值。
在数学上,arccos 是一个多值函数;特别是,cos X == cos -X。C++ 函数只能返回一个代表值,他们决定了那个范围。您可以通过添加 2π 和/或否定来获得其他值。
推荐阅读
- c++ - C ++:程序在获取输入字符串的中间终止
- python - While循环嵌套在另一个while循环中,不允许其他变量在python中增加
- excel - 当我从 excel 提取到 PowerPoint 时显示自动化错误
- r - 悬停板未在ggplotly中显示
- docker - docker-compose 命令未在“up”上运行,但在进入容器命令时运行
- linux-kernel - CFS 调度程序如何选择接下来要运行的任务组/cgroup?
- r - 对 tibble 的随机行进行子样本
- python - TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是 '_io.TextIOWrapper'
- twilio - 使用 Twilio Say 和用户提供的输入时如何避免命令注入?
- javascript - Google Analytics 从购物车操作中删除应该如何工作?