acos(), acosf(), acosl() — Calculate arccosine

Standards

Standards / Extensions C or C++ Dependencies

ISO C
POSIX.1
XPG4
XPG4.2
ISO/ANSI C++
C99
Single UNIX Specification, Version 3
C++ TR1 C99

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