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:

  1. sin(x): Calculate the value of the sine function.
  2. cos(x): calculate the value of the cosine function.
  3. Calculate the value of the tangent function, tan(x).
  4. Calculate the natural logarithm of x.
  5. exp(x): calculate the power of e to the x.
  6. Calculate the power of x to the yth degree.
  7. Calculate the square root of x.

When using these mathematical functions, it is necessary to include the header file in the program and pass the corresponding parameters according to the function’s requirements. An example code is provided below:

#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.

bannerAds