此函数定义在 <cmath> 头文件中。
[Mathematics] tan x = tan(x) [In C++ Programming]
tan() 原型(C++ 11 标准)
double tan(double x); float tan(float x); long double tan(long double x); double tan (T x); // For integral type
tan() 参数
tan() 函数接受一个以弧度为单位的必需参数(可以是正数、负数或 0)。
tan() 返回值
tan() 函数返回 [-∞, ∞] 范围内的值。
示例 1:C++ 中的 tan() 如何工作?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
long double x = 0.99999, result;
result = tan(x);
cout << "tan(x) = " << result << endl;
double xDegrees = 60.0;
// converting degree to radians and using tan() fucntion
result = tan(xDegrees*3.14159/180);
cout << "tan(x) = " << result << endl;
return 0;
}
运行程序后,输出将是
tan(x) = 1.55737 tan(x) = 1.73205
示例 2:带整数类型的 tan() 函数
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
long int x = 6;
double result;
result = tan(x);
cout << "tan(x) = " << result;
return 0;
}
运行程序后,输出将是
tan(x) = -0.291006
另请阅读