c++ acos函数_acos()函数以及C ++中的示例

C++ acos() 函数属于 cmath 头文件,用于计算给定数值的反余弦主要值,返回以弧度表示的结果。注意输入值必须在 -1 到 +1 范围内,否则会导致域错误。示例代码中展示了如何使用该函数,当输入值超出范围时,将返回 nan。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

c++ acos函数

C ++ acos()函数 (C++ acos() function)

acos() function is a library function of cmath header, it is used to find the principal value of the arc cosine of the given number, it accepts a number (x) and returns the principal value of the arc cosine of x in radians.

ACOS()函数CMATH报头的库函数,它被用于查找给定数的反余弦的主值,它接受一个数字(x)和返回x的以弧度为单位的反余弦的主要值。

Note: Value (x) must be between -1 to +1, else it will return a domain error (nan).

注意:值( x )必须介于-1到+1之间,否则它将返回域错误( nan )。

Syntax of acos() function:

acos()函数的语法:

    acos(x);

Parameter(s): x – is the value whose arc cosine to be calculated.

参数: x –是要计算其反余弦值的值。

Return value: double – it returns double type value that is the principal value of the arc cosine of the given number x.

返回值: double-它返回double类型值,该类型值是给定数字x的反余弦的主要值。

Example:

例:

    Input:
    float x = 0.65;
    
    Function call:
    acos(x);    
    
    Output:
    0.863212

C ++代码演示acos()函数的示例 (C++ code to demonstrate the example of acos() function)

// C++ code to demonstrate the example of 
// acos() function

#include <iostream>
#include <cmath>
using namespace std;

// main() section
int main()
{
    float x;
    
    x = -1.0;
    cout<<"acos("<<x<<"): "<<acos(x)<<endl;

    x = -0.89;
    cout<<"acos("<<x<<"): "<<acos(x)<<endl;    

    x = 0.65;
    cout<<"acos("<<x<<"): "<<acos(x)<<endl;    

    x = 1;
    cout<<"acos("<<x<<"): "<<acos(x)<<endl;        
    
    return 0;
}

Output

输出量

acos(-1): 3.14159
acos(-0.89): 2.66814
acos(0.65): 0.863212
acos(1): 0

Example with domain error

域错误示例

If we provide the value out of the range (except -1 to +1), it returns nan.

如果我们提供的值超出范围(-1到+1除外),它将返回nan 。

// C++ code to demonstrate the example of 
// acos() function

#include <iostream>
#include <cmath>
using namespace std;

// main() section
int main()
{
    float x;

    x = -0.89;  //no error
    cout<<"acos("<<x<<"): "<<acos(x)<<endl;    

    x = 2.65;   //error
    cout<<"acos("<<x<<"): "<<acos(x)<<endl;    

    x = -1.25;  //error
    cout<<"acos("<<x<<"): "<<acos(x)<<endl;        
    
    return 0;
}

Output

输出量

acos(-0.89): 2.66814
acos(2.65): nan
acos(-1.25): nan

Reference: C++ acos() function

参考: C ++ acos()函数

翻译自: https://www.includehelp.com/cpp-tutorial/acos-function-with-example.aspx

c++ acos函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值