math.h问题 [英] Problems with math.h
问题描述
我正在编写一个使用math.h
库中的函数sin()
和cos()
的程序.但是,我注意到我得到了时髦的结果.经过四处搜寻并检查过数学后,我决定使用以下方法进行简单检查:
I'm writing a program that is using the functions sin()
and cos()
from the math.h
library. However, I noticed I was getting funky results. After searching around and checking my math multiple times, I decided to do a simple check with this:
int main()
{
cout << "sin(45.0) = " << sin(45) << endl;
cout << "cos(45.0) = " << cos(45) << endl;
return 0;
}
我得到以下输出:
sin(45) = 0.850904
cos(45) = 0.525322
这些应该相等吧? math.h
库有什么特别之处吗?我在做错什么吗?
These should be equal right? Is there something special about the math.h
library? Am I doing something wrong?
以下是WolframAlpha中的方程式:
Here are the equations in WolframAlpha:
推荐答案
您应使用 cmath
在C ++中,而不是旧的C标头.
You should use cmath
in C++, rather than the old C header.
std::sin()
和弧度.
此文件的GCC版本包含一个用于π(在C ++标准中不存在,但是),这将使您更轻松地将度数转换为弧度:
The GCC version of this file includes a handy constant for π, (which does not exist in the C++ standard but) which will make it easier for you to convert degrees to radian:
#include <cmath>
#include <iostream>
double degrees_to_radian(double deg)
{
return deg * M_PI / 180.0;
}
int main()
{
std::cout << "sin(45.0) = " << std::sin(degrees_to_radian(45)) << std::endl;
std::cout << "cos(45.0) = " << std::cos(degrees_to_radian(45)) << std::endl;
}
这篇关于math.h问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!