C++ 中使用 cmath 头文件来表示三角函数,它提供了 sin()、cos()、tan() 等函数来计算三角函数值。参数以弧度为单位,要使用度数需转换为弧度。用法示例:将度数转换为弧度,计算三角函数值,输出结果。

C++ 中表示三角函数
在 C++ 中,使用 cmath 头文件访问三角函数。此头文件提供了用于计算正弦、余弦、正切、反正切和其他三角函数的函数。
表示
cmath 头文件声明了以下三角函数:
立即学习“C++免费学习笔记(深入)”;
-
sin():计算角度的正弦值 -
cos():计算角度的余弦值 -
tan():计算角度的正切值 -
asin():计算正弦值的反正切值 -
acos():计算余弦值的反正切值 -
atan():计算正切值的反正切值
用法
三角函数的参数以弧度为单位。要使用度数,请将度数转换为弧度:
double degrees = 45.0; double radians = degrees * (M_PI / 180.0); // 转换为弧度
下面是使用三角函数的一些示例:
#includeusing namespace std; int main() { double angle = 30.0; // 以度数给定 // 转换为弧度 double radians = angle * (M_PI / 180.0); // 计算三角函数 double sin_value = sin(radians); double cos_value = cos(radians); double tan_value = tan(radians); // 输出结果 cout << "sin(" << angle << ") = " << sin_value << endl; cout << "cos(" << angle << ") = " << cos_value << endl; cout << "tan(" << angle << ") = " << tan_value << endl; return 0; }
输出:
sin(30) = 0.5 cos(30) = 0.866025 tan(30) = 0.57735











