Standards / Extensions | C or C++ | Dependencies |
---|---|---|
ISO C |
both |
#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);
Calculates the arccosine of x, expressed in radians, in the range 0 to pi.
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.
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.
⁄* 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
Enter x
acos( 0.400000 ) = 1.159279