How to use the math library functions in the C language?

The math library functions in the C language can be used by including the header file . Here are some commonly used math library functions and their usage methods:

  1. Mathematical constants:
  2. PI: The mathematical constant pi can be defined using the macro M_PI.
  3. E: The natural constant e can be defined using the M_E macro.
  4. Mathematical functions:
  5. Parameters and return values of mathematical functions are typically of double precision floating-point type.
  6. abs(x): Calculate the absolute value of x.
  7. square root of x: find the square root of x.
  8. pow(x, y): to calculate the y power of x.
  9. Calculate the exponentiation of e to the power of x, denoted as exp(x).
  10. Calculate the natural logarithm of x.
  11. log10(x): calculating the logarithm of x with base 10.
  12. sin(x): calculate the sine value of x, where x is given in radians.
  13. cos(x): calculate the cosine value of x, where x is in radians.
  14. tan(x): computes the tangent value of x in radians.
  15. ceil(x): rounding up, returning the smallest integer not less than x.
  16. floor(x): rounds down to the nearest integer less than or equal to x.
  17. round(x): round to the nearest integer, returning the closest integer to x.
  18. random number function
  19. The random number function needs to be initialized with the srand function to set the seed.
  20. srand(seed) is used to set the seed for the random number generator, with seed being an integer.
  21. rand(): Generates a random integer between 0 and RAND_MAX. You can obtain a random number within a specific range by using the modulus operation.

For example, calculating the value of the sine function:

#include <stdio.h>
#include <math.h>

int main() {
    double x = 1.0;
    double result = sin(x);
    printf("sin(%f) = %f\n", x, result);
    return 0;
}

This code uses the math library function sin to calculate the sine value of x, and then uses the printf function to output the result.

bannerAds