C语言中cos(x)或sin(x),x输入的是弧度,怎么输入角度

#define PI (3.14159265358979323846)
#define PI_DIV_180 (0.017453292519943296)//π/180
#define DegToRad(x) ((x)*PI_DIV_180)//角度转换为弧度

让我们稍微写几行代码就可以验证其结果:

// sin(x)中x角度还是弧度问题.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include 
#include 
#define  PI 3.1415926
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	float angle = 30;//角度
	float rad = PI / 6;//弧度
	float resultAngle = sin(angle);
	float resultRad = sin(rad);
	cout <<"sin(30)角度结果:"<

结果截图:

C语言中cos(x)或sin(x),x输入的是弧度,怎么输入角度_第1张图片

所以其运算的x因该是弧度值。因此有时我们需要自己写个函数实现由角度值转换为弧度值,再调用math.h库中的cos(x)或者sin(x)等,其具体实现如下

添加:

#define PI (3.14159265358979323846)
#define PI_DIV_180 (0.017453292519943296)//π/180
#define DegToRad(x) ((x)*PI_DIV_180)//角度转换为弧度

// sin(x)中x角度还是弧度问题.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include 
#include 
using namespace std;

#define PI (3.14159265358979323846)
#define PI_DIV_180 (0.017453292519943296)//π/180
#define DegToRad(x)	((x)*PI_DIV_180)//角度转换为弧度


int _tmain(int argc, _TCHAR* argv[])
{
	float angle = 30;//角度
	float rad = PI / 6;//弧度
	float resultAngle = sin(DegToRad(angle));
	float resultRad = sin(rad);
	cout <<"sin(30)角度结果:"<

运算结果相同:



你可能感兴趣的:(C/C++,STL等相关技术)