跳到主要内容

C++ asin() 反正弦函数

这个函数定义在 <cmath> 头文件中。

[Mathematics] sin-1x = asin(x) [In C++ Programming];

asin() 函数原型 [C++ 11 标准起]

double asin(double x);
float asin(float x);
long double asin(long double x);
double asin (T x);

asin() 参数

asin() 函数接受一个必需的参数,范围在 [-1, 1] 内。

这是因为正弦值的范围在 1 和 -1 之间。

asin() 返回值

考虑到参数在 [-1, 1] 的范围内,asin() 函数返回的值在 [-π/2, π/2] 的范围内。

如果参数大于 1 或小于 -1,asin() 返回 NaN,即非数字。

参数 (x)返回值
x = [-1, 1][-π/2, π/2],以弧度计
-1 > x 或 x > 1NaN(非数字)

示例 1:asin() 如何工作?

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
double x = 0.25, result;
result = asin(x);

cout << "asin(x) = " << result << " 弧度" << endl;
// 结果转换为度
cout << "asin(x) = " << result*180/3.1415 << " 度" << endl;

return 0;
}

当你运行程序时,输出将会是:

asin(x) = 0.25268 弧度
asin(x) = 14.4779

示例 2:对整数类型使用 asin() 函数

#include <iostream>
#include <cmath>
#define PI 3.141592654

using namespace std;

int main()
{
int x = 1;
double result;

result = asin(x);

cout << "asin(x) = " << result << " 弧度" << endl;
// 将结果转换为度
cout << "asin(x) = " << result*180/PI << " 度";

return 0;
}

当你运行程序时,输出将会是:

asin(x) = 1.5708 弧度
asin(x) = 90