In: Computer Science
acos() Prototype
double acos(double x);
To find arc cosine of type int, float or long double, you can explicitly convert the type to double using cast operator.
int x = 0; double result; result = acos(double(x));
int x = 0; double result; result = acos(double(x));
acos() Parameter
The acos() function takes a single argument in the range of [-1, +1]. It's because the value of cosine is in the range of 1 and -1.
Parameter | Description |
---|---|
double value | Required. A double value between - 1 and +1 inclusive. |
acos() Return Value
The acos() functions returns the value in range of [0.0, π] in radians. If the parameter passed to the acos() function is less than -1 or greater than 1, the function returns NaN (not a number).
Parameter (x) | Return Value |
---|---|
x = [-1, +1] | [0, π] in radians |
-1 > x or x > 1 | NaN (not a number) |
Example 1: acos() function with different parameters
#include <stdio.h> #include <math.h> int main() { // constant PI is defined const double PI = 3.1415926; double x, result; x = -0.5; result = acos(x); printf("Inverse of cos(%.2f) = %.2lf in radians\n", x, result); // converting radians to degree result = acos(x)*180/PI; printf("Inverse of cos(%.2f) = %.2lf in degrees\n", x, result); // paramter not in range x = 1.2; result = acos(x); printf("Inverse of cos(%.2f) = %.2lf", x, result); return 0; }
Output
Inverse of cos(-0.50) = 2.09 in radians Inverse of cos(-0.50) = 120.00 in degrees Inverse of cos(1.20) = nan
give an example of (math.h) function from the c language library and explain
1- prototype
2-input type (parameters)
3-output or return type
4- number of arguments/parameters
5- example
There are many functions available in math.h function here are the list of some important functions of math.h:
1. sqrt(x)
2. exp(x)
3. log(x)
4. log10(x)
5. fabs(x)
6. feil(x)
7. floor(x)
8. fow(x,y)
9. fmod(x)
10. sin(x)
11. cos(x)
12. tan(x)
1. sqrt(): The sqrt() function computes the square root of a number.
sqrt() Prototype:
double sqrt(double arg);
To find the square root of int, float or long double data types, you can explicitly convert the type to double using cast operator.
int x = 0;
double result;
result = sqrt(double(x));
sqrt() input type (parameters):
The sqrt() function takes a single argument (in double) and returns its square root (also in double).
[Mathematics] √x = sqrt(x) [In C Programming]
double sqrt(double x)
sqrt() output/return type:
The sqrt function returns the square root of argument (in double). If argument is negative, the sqrt function will return a domain error.
You can also use the sqrtf() function to work specifically with float and sqrtl() to work with long double type.
long double sqrtl(long double arg );
float sqrtf(float arg );
Example:
#include <math.h>
#include <stdio.h>
int main()
{
double number, squareRoot;
printf("Enter a number: ");
scanf("%lf", &number);
// computing the square root
squareRoot = sqrt(number);
printf("Square root of %.2lf = %.2lf", number, squareRoot);
return 0;
}
OUTPUT:
Enter a number: 23.4
Square root of 23.40 = 4.84
2. exp(): The exp() function computes the exponential (Euler's number) raised to the given argument.
exp() Prototype:
double exp( double arg );
In order to calculate the exp() for long double or float, you can use the following prototype
long double expl( long double arg);
float expf( float arg);
sqrt() input type (parameters):
The exp(arg) takes a single argument and returns the value in type double.
[Mathematics] ex = exp(x)
double exp(double x);
exp() output/return type:
The exp function returns the result of e raised to the power of x.
The exp() function returns the value in the range of [0, inf]. It shows error when we pass more then one argument in exp function
Example:
#include <stdio.h>
#include <math.h>
int main()
{
double x = 12.0, result;
result = exp(x);
printf("Exponential of %.2lf = %.2lf", x, result);
return 0;
}
OUTPUT:
Enter the value of x to find e^x: 12
Exponential of 12.00 = 162754.79
3. log(): The log() function computes the natural logarithm of an argument.
log() Prototype:
double log( double arg );
In order to find the log() of long double or float numbers, you can use the following prototype.
long double logl( long double arg);
float logf(float arg);
log() input type (parameters):
The log() function takes a single argument and returns a value of type float.
Argument |
Remarks |
arg>0 (Greater Than Zero) |
Finds the log of the Argument |
arg<0 (Less than Zero) |
Shows run-time error |
[Mathematics] logex = log(x)
double log(double x);
log() output/return type:
The log function returns the logarithm of x to the base of e.
Log() function returns value according to the following conditions. If arg > 0, finds the log of the argument. If argument<0, then it shows error.
Example:
#include <stdio.h>
#include <math.h>
int main()
{
double num = 5.6, result;
result = log(num);
printf("log(%.1f) = %.2f", num, result);
return 0;
}
OUTPUT:
log(5.6) = 1.72