我发现 cmath
库中的 tan(float)
函数返回负值。
下面这段代码,运行时:
#include <cmath>
....
// some calculation here gives me a value between 0.0 to 1.0.
float tempSpeed = 0.5;
float tanValue = tan(tempSpeed * 60);
__android_log_print(ANDROID_LOG_INFO, "Log Me", "speed: %f", tanValue);
在我的日志文件中给我这个结果:
Log Me: speed `-6.4053311966`
据我所知
tan(0.5*60) = tan(30) = 1/squareroot(3);
有人可以帮助我解决为什么我看到的是负值吗?它与某些浮点大小错误有关吗?还是我在做一些非常愚蠢的事情?
最佳答案
在 C 中,tan
和其他三角函数需要弧度 作为它们的参数,而不是度数。您可以将度转换为弧度:
tan( 30. * M_PI / 180. ) == 0.57735026918962576450914878050196
关于android - 在 C 中,tan(30) 给我一个负值!为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11195039/