#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 <math.h>
#include <iostream>
#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)角度结果:"<<resultAngle << "sin(pi/6)弧度结果:" << resultRad<< endl;
system("pause");
return 0;
}
结果截图:
所以其运算的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 <math.h>
#include <iostream>
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)角度结果:"<<resultAngle << "sin(pi/6)弧度结果:" << resultRad<< endl;
system("pause");
return 0;
}
运算结果相同:
<img src="https://img-blog.csdn.net/20151024103628755?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />