C acos()

acos() 函数在 math.h 头文件中定义。它有助于返回给定数字的弧余弦值(以弧度表示)。弧余弦表示反余弦值。


double acos(double x); #where x can be in int, float or long double

此外,acosf() 和 acosl() 两个函数分别用于 float 和 long double 类型。


float acosf(float x); 
long double acosl(long double x); 

acos() 参数

acos() 函数接受一个介于 1 ≥ x ≥ -1 范围内的单个参数。使用类型转换运算符,我们可以将类型显式转换为 double,以查找 int、float 或 long double 类型的反余弦值。

参数 描述 必需/可选
双精度浮点数值 一个介于 -1 和 +1 之间的双精度值 必需

acos() 返回值

acos() 函数的返回值范围为 [0.0, π](以弧度表示)。

输入 返回值
x = [-1, +1] [0, π](以弧度表示)
-1 > x 或 x > 1 NaN (非数字)

acos() 示例

示例 1:C 语言中 acos() 函数的工作原理是什么?


#include <stdio.h>
#include <math.h>
int main()
{
    
    const double PI =  3.1415926;
    double v, output;

    v =  -0.5;
    output = acos(v);
    printf("The Inverse of cos(%.2f) = %.2lf in radians\n", v, output);

    //radians to degree convertion
    output = acos(v)*180/PI;
    printf("The Inverse of cos(%.2f) = %.2lf in degrees\n", v, output);

    // paramter not in range
    v = 1.2;
    output = acos(v);
    printf("The Inverse of cos(%.2f) = %.2lf", v, output);

    return 0;
}

输出


The Inverse of cos(-0.50) = 2.09 in radians
The Inverse of cos(-0.50) = 120.00 in degrees
The Inverse of cos(1.20) = nan

示例 2:C 语言中 acosf() 和 acosl() 函数的工作原理是什么?


#include <stdio.h>
#include <math.h>
int main()
{
    float f, fcosx;
    long double l, lcosx;

    // type float
    f = -0.505405;
    fcosx = acosf(f);

    //type long double
    l = -0.50540593;
    lcosx = acosl(l);

    printf("acosf(x) is equal to %f in radians\n", fcosx);
    printf("acosl(x) is equal to %Lf in radians", lcosx);

    return 0;
}

输出


acosf(x) is equal to 2.100648 in radians
acosl(x) is equal to 2.100649 in radians