跳到主要内容

C++ tan() 正切函数

这个函数定义在 <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); // 对于整数类型

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;
// 将度数转换为弧度并使用 tan() 函数
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