acos(), acosf(), acosl() — Calculate arccosine
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
ISO C |
both |
Format
#include <math.h>
double acos(double x);
float acos(float x); /* C++ only */
long double acos(long double x); /* C++ only */
float acosf(float x);
long double acosl(long double x);
General description
Calculates the arccosine of x, expressed in radians, in the range 0 to pi.
The
value of x must be between -1 and 1 inclusive.
Note: These functions work in both IEEE Binary Floating-Point and
hexadecimal floating-point formats. See IEEE binary floating-point for
more information about IEEE Binary Floating-Point.
Returned value
Special behavior for C/370™: If x is less than -1 or greater than 1, the function sets errno to EDOM and returns 0. If the correct value would cause underflow, zero is returned and the value ERANGE is stored in errno.
Special behavior for XPG4.2: If successful, the function returns the arccosine of x, in the range [0,pi] radians.
If the value of x is
not in the range [-1,1], the function returns 0.0 and sets errno to
the following value. No other errors will occur.
- Error Code
- Description
- EDOM
- The value x is not in the range [-1,1].
Special behavior for IEEE: If successful, the function returns the arccosine of the argument x.
If x is less than -1 or greater than 1, the function sets errno to EDOM and returns NaNQ (Not a Number Quiet). No other errors will occur.
Example
CELEBA04
/* CELEBA04
This example prompts for a value for x.
It prints an error message if x is greater than 1 or
less than -1; otherwise, it assigns the arccosine of
x to y.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 1.0
#define MIN -1.0
int main(void)
{
double x, y;
printf( "Enter x\n" );
scanf( "%lf", &x );
/* Output error if not in range */
if ( x > MAX )
printf( "Error: %f too large for acos\n", x );
else if ( x < MIN )
printf( "Error: %f too small for acos\n", x );
else {
y = acos( x );
printf( "acos( %f ) = %f\n", x, y );
}
}
Output
Expected result if 0.4 is entered:
Enter x
acos( 0.400000 ) = 1.159279
Related information
- math.h
- acosh(), acoshf(), acoshl() — Calculate hyperbolic arccosine
- asin(), asinf(), asinl() — Calculate arcsine
- asinh(), asinhf(), asinhl() — Calculate hyperbolic arcsine
- atan(), atanf(), atanl(), atan2(), atan2f(), atan2l() — Calculate arctangent
- atanh(), atanhf(), atanhl() — Calculate hyperbolic arctangent
- cos(), cosf(), cosl() — Calculate cosine
- cosh(), coshf(), coshl() — Calculate hyperbolic cosine
- sin(), sinf(), sinl() — Calculate sine
- sinh(), sinhf(), sinhl() — Calculate hyperbolic sine
- tan(), tanf(), tanl() — Calculate tangent
- tanh(), tanhf(), tanhl() — Calculate hyperbolic tangent