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 <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="" />


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值