Home » C++ programming language

asin() function with example in C++

C++ asin() function: Here, we are going to learn about the asin() function with example of cmath header in C++ programming language?
Submitted by IncludeHelp, on April 28, 2019

C++ asin() function

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

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

Syntax of asin() function:

    asin(x);

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

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

Example:

    Input:
    float x = 0.65;
    
    Function call:
    asin(x);    
    
    Output:
    0.707584

C++ code to demonstrate the example of asin() function

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

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

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

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

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

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

Output

asin(-1): -1.5708
asin(-0.89): -1.09735
asin(0.65): 0.707584
asin(1): 1.5708

Example with domain error

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

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

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

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

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

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

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

Output

asin(-0.89): -1.09735
asin(2.65): nan
asin(-1.25): nan 

Reference: C++ asin() function



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.