acos() Function
The acos()
function computes the arc cosine of a given value and returns the principal value expressed in radians. It serves as the inverse of the cosine function and is commonly used to determine an angle from its cosine value.
Syntax of acos()
double acos(double x);
Parameters
Parameter | Description |
---|---|
x | Value for which the arc cosine is computed. The value must lie within the interval [-1, 1]. A domain error occurs if the input is outside this range. |
Return Value
The function returns the principal value of the arc cosine, expressed in radians within the interval [0, π]. One radian is equivalent to 180/π degrees.
Important: The function only accepts values within the interval [-1, 1]. Inputs outside this range will trigger a domain error.
Examples for acos()
Example 1: Calculating the Arc Cosine of 0.5
This example demonstrates how to compute the arc cosine for a typical value within the valid range.
Program
#include <stdio.h>
#include <math.h>
int main(void) {
double value = 0.5;
double angle = acos(value);
printf("Arc cosine of %f is %f radians\n", value, angle);
return 0;
}
Explanation:
- The program initializes a variable with the value 0.5.
acos()
computes the arc cosine of 0.5, returning the corresponding angle in radians.- The computed angle is stored and then printed to the console.
- The output displays the arc cosine of 0.5 in radians.
Output:
Arc cosine of 0.500000 is 1.047198 radians
Example 2: Calculating the Arc Cosine at the Boundary Value of 1.0
This example shows how acos()
behaves when the input is at the upper boundary of the valid interval.
Program
#include <stdio.h>
#include <math.h>
int main(void) {
double value = 1.0;
double angle = acos(value);
printf("Arc cosine of %f is %f radians\n", value, angle);
return 0;
}
Explanation:
- The program assigns the boundary value 1.0 to the variable.
acos()
computes the arc cosine of 1.0, which is 0 radians since cos(0) equals 1.- The resulting angle is stored and printed.
- The output confirms that the arc cosine of 1.0 is 0 radians.
Output:
Arc cosine of 1.000000 is 0.000000 radians
Example 3: Handling an Out-of-Range Input for acos()
This example demonstrates how to handle a domain error when the input value is outside the valid range.
Program
#include <stdio.h>
#include <math.h>
#include <errno.h>
int main(void) {
errno = 0;
double value = 2.0;
double angle = acos(value);
if (errno == EDOM) {
printf("Domain error: input %f is out of range for acos()\n", value);
} else {
printf("Arc cosine of %f is %f radians\n", value, angle);
}
return 0;
}
Explanation:
- The program resets
errno
to 0 and uses an out-of-range value (2.0) as input. acos()
is called with this invalid input, triggering a domain error.- The error is detected by checking if
errno
is set toEDOM
. - A message is printed indicating that the input is out of range for
acos()
.
Output:
Domain error: input 2.000000 is out of range for acos()