C Math Functions: Complete Guide
The math.h header file in the C language provides a set of mathematical functions for various mathematical operations. Common math functions include trigonometric functions, logarithmic functions, exponential functions, and power functions.
Some common mathematical functions are as follows:
- sin(x): Calculate the value of the sine function.
- cos(x): calculate the value of the cosine function.
- Calculate the value of the tangent function, tan(x).
- Calculate the natural logarithm of x.
- exp(x): calculate the power of e to the x.
- Calculate the power of x to the yth degree.
- Calculate the square root of x.
When using these mathematical functions, it is necessary to include the
#include <stdio.h>
#include <math.h>
int main() {
double x = 3.0;
double y = 2.0;
printf("sin(%.2f) = %.2f\n", x, sin(x));
printf("cos(%.2f) = %.2f\n", x, cos(x));
printf("tan(%.2f) = %.2f\n", x, tan(x));
printf("log(%.2f) = %.2f\n", x, log(x));
printf("exp(%.2f) = %.2f\n", x, exp(x));
printf("pow(%.2f, %.2f) = %.2f\n", x, y, pow(x, y));
printf("sqrt(%.2f) = %.2f\n", x, sqrt(x));
return 0;
}
The above code demonstrates how to use some common mathematical functions. Different mathematical functions can be used in the program based on specific needs.