C programming - standard library math.h

ww‮w‬.theitroad.com

The "math.h" header file in C programming provides a wide range of mathematical functions and constants for performing mathematical operations in C programs.

Here are some of the commonly used functions and constants defined in the "math.h" header file:

  1. Constants: "M_PI", "M_E", "M_LOG2E", "M_LOG10E", "M_LN2", "M_LN10", "M_SQRT2", and "M_SQRT1_2" - These are mathematical constants that represent the values of π, e, and other important mathematical constants.

  2. Trigonometric functions: "sin", "cos", "tan", "asin", "acos", and "atan" - These functions compute the sine, cosine, tangent, and their inverse functions for a given angle, measured in radians.

  3. Exponential and logarithmic functions: "exp", "log", "log10", and "pow" - These functions compute the exponential, natural logarithm, base-10 logarithm, and power of a given number.

  4. Other mathematical functions: "sqrt", "fabs", "ceil", "floor", "round", "fmod", and "trunc" - These functions compute the square root, absolute value, ceiling, floor, rounding, modulo, and truncation of a given number.

Here is an example of using the "math.h" header file in a C program:

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

int main() {
    double x = 4.0;
    double y = 2.0;

    printf("sqrt(%lf) = %lf\n", x, sqrt(x));
    printf("pow(%lf, %lf) = %lf\n", x, y, pow(x, y));
    printf("log(%lf) = %lf\n", x, log(x));
    printf("sin(%lf) = %lf\n", x, sin(x));
    printf("floor(%lf) = %lf\n", x/y, floor(x/y));
    printf("M_PI = %lf\n", M_PI);

    return 0;
}

In this example, the "math.h" header file functions are used to compute the square root, power, logarithm, sine, floor, and value of pi. The output of the program will be:

sqrt(4.000000) = 2.000000
pow(4.000000, 2.000000) = 16.000000
log(4.000000) = 1.386294
sin(4.000000) = -0.756802
floor(2.000000) = 2.000000
M_PI = 3.141593

Note that the actual output of the program may vary slightly depending on the implementation and platform.