c++ - c++中的atan问题

标签 c++ math logic

在开始我的问题之前,我想指出几件事:1) 我知道 cmath 库中已经有一个 atan2 函数,这纯粹是为了我自己的练习,以及 2)我知道代码不占0。

好的,所以 tan(theta) = y/x,其中 y 和 x 是平面上的坐标...这意味着:

theta = atan(y/x) 在四边形 I 和 IV 中,以及 theta = atan(y/x) + 180 in Quads II 和 III

那么为什么当我使用以下代码时:

float atan(float y, float x)
 {
 float result = 0.0f;

if (x > 0) //quads I and IV if x is positive
 { 
result = atanf(y/x);
 }
 else if (x < 0)
 {
 result = atan(y/x) + 180; //quads II and III if x is negative
 }

return result;

 }

它吐我垃圾吗? 例如,对于坐标 (-4,4),它给了我结果:179.215,而它应该是 135:

atan(4/-4) = -45 度 + 180 度 = 135 度

但是正在发生的是计算

atan(4.0f/-4.0f) = -0.785398 + 180 度 = 179.215。

我是不是漏掉了一些步骤?

最佳答案

标准的 atan 和 atan2 函数,以及所有其他处理角度的 C 函数,处理的是弧度,而不是度数。

如果你想让自己的函数输出度数,你必须将atanf的返回值乘以180/pi;要以弧度表示所有内容,请添加 pi 而不是 180。

关于c++ - c++中的atan问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11484692/

相关文章:

math - GSL/BLAS : Multiply a matrix with an inverse matrix

logic - 给定短语 "Where NONE of the following are TRUE"和两个语句, bool 逻辑应该如何组成?

php - PHP中的类型杂耍和(严格)大于/小于比较

c++ - 模板类 + 运算符 + friend = Unresolved external

c++ - 处理所有二次公式结果

c++ - std::vector::resize() 与 gcc 4.7.0 的奇怪行为

javascript - 在javascript中计算旋转矩形的中点

c - 在 C 中将 "char"转换为 "int"的逻辑

c++ - 如何解码多字节 utf8 字符串? (C++)

c++ - 如何使用 wxWidgets 打印 wxImage?