android - 在 C 中,tan(30) 给我一个负值!为什么?

标签 android c math android-ndk cmath

我发现 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/

相关文章:

android - 在 Android Jetpack Compose 中使用 State 时出现 java.lang.IllegalStateException

android - 制作 Android 内核 - 模块准备

java - 抛出奇怪的 org.threeten.bp.DateTimeException?

c - flockfile() 一个在 Linux 上被 fclosed()ed 的 FILE* 是否安全?

python - 在嵌入式 python 中创建新的 None 对象

math - 如何制作有理数的 "working"重复十进制表示?

Android 警报对话框文本不可见

c - MPI 和 C : loop through file of commands in file

ruby - 如何找到 Ruby 整数数组/散列的异常值?

javascript - Javascript 中的累积分布函数