在VC中使用sin,为什么得到的是错误的结果,程序如下:
来源:百度知道 编辑:UC知道 时间:2024/05/14 17:16:49
#include <iostream>
#include "math.h"
using namespace std;
void main()
{
float x=7;
float y=255;
cout << x/y << endl;
cout << sin(180.0) << endl;
}
结果显示:0.027451
-0.801153
Press any key to continue
#include "math.h"
using namespace std;
void main()
{
float x=7;
float y=255;
cout << x/y << endl;
cout << sin(180.0) << endl;
}
结果显示:0.027451
-0.801153
Press any key to continue
函数sin计算的是弧度的正弦值
角度 弧度转换为
弧度= 角度*π/180;
因为这里的sin是按弧度算的,不是按角度算的.
结果并不错.
你是期望sin(180.0)=0吗?注意:sin是以弧度为单位的.
#include <iostream>
#include "math.h"
#define PI 3.1416
using namespace std;
void main()
{
float x=7;
float y=255;
float ans;
char sans[10];
cout << x/y << endl;
//printf("%.2f",sin((PI*180.0)/180.0)); //或者
sprintf(sans,"%.2f",sin((PI*180.0)/180.0));
ans = atof(sans);
cout<<ans;
}