C语言 C语言中是否有计算度数/弧度的函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14920675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:22:24  来源:igfitidea点击:

Is there a function in C language to calculate degrees/radians?

c

提问by Antun Tun

I need to calculate an angle in C programm. Here is a method from JAVA that I need to convert to C.

我需要在 C 程序中计算一个角度。这是我需要转换为 C 的 JAVA 方法。

private static double calculateDirection(double x, double y)
{
    return Math.toDegrees(Math.atan2(y, x));
}

Is there a function like toDegrees in C language so I don't have to write all the code by myself? Thank you

C 语言中是否有像 toDegrees 这样的函数,所以我不必自己编写所有代码?谢谢

回答by Emanuele Paolini

#include <math.h>

inline double to_degrees(double radians) {
    return radians * (180.0 / M_PI);
}

回答by antonijn

There is no need to use such a method. Converting to degrees is very simple:

没有必要使用这样的方法。转换为度非常简单:

double radians = 2.0;
double degrees = radians * 180.0 / M_PI;

Turn that into a function if you want to.

如果你愿意,把它变成一个函数。

M_PIis* defined in math.hby the way.

M_PIis*math.h顺便定义。



* in most compilers.

* 在大多数编译器中。

回答by Demitri

If your preference is to just copy/paste a couple of macros:

如果您只想复制/粘贴几个宏:

#include <math.h>
#define degToRad(angleInDegrees) ((angleInDegrees) * M_PI / 180.0)
#define radToDeg(angleInRadians) ((angleInRadians) * 180.0 / M_PI)

And if you want to omit the #include, replace that line with this which was copied from the math.hheader:

如果您想省略#include,请将该行替换为从math.h标题中复制的此行:

#define M_PI   3.14159265358979323846264338327950288