C <math.h> library


The <math.h> header file in C is part of the C Standard Library and provides a wide range of mathematical functions. These functions facilitate various mathematical operations, including trigonometric calculations, logarithmic functions, power functions, and other mathematical utilities.

Key Features of <math.h>

  1. Basic Mathematical Functions: Functions for performing basic arithmetic and mathematical operations.
  2. Trigonometric Functions: Functions for calculating trigonometric values like sine, cosine, and tangent.
  3. Exponential and Logarithmic Functions: Functions for calculating exponential values and logarithms.
  4. Special Functions: Functions for handling operations like square roots and rounding.

Commonly Used Functions in <math.h>

Here are some of the most commonly used functions provided by <math.h>:

1. Basic Mathematical Functions

  • fabs(): Computes the absolute value of a floating-point number.

    Syntax:

    double fabs(double x);

    Example:

    double result = fabs(-5.3); // result = 5.3
  • ceil(): Rounds a floating-point number up to the nearest integer.

    Syntax:

    double ceil(double x);

    Example:

    double result = ceil(5.3); // result = 6.0
  • floor(): Rounds a floating-point number down to the nearest integer.

    Syntax:

    double floor(double x);

    Example:

    double result = floor(5.8); // result = 5.0
  • round(): Rounds a floating-point number to the nearest integer.

    Syntax:

    double round(double x);

    Example:

    double result = round(5.5); // result = 6.0

2. Trigonometric Functions

  • sin(): Computes the sine of an angle (in radians).

    Syntax:

    double sin(double x);

    Example:

    double result = sin(M_PI / 2); // result = 1.0
  • cos(): Computes the cosine of an angle (in radians).

    Syntax:

    double cos(double x);

    Example:

    double result = cos(0); // result = 1.0
  • tan(): Computes the tangent of an angle (in radians).

    Syntax:

    double tan(double x);

    Example:

    double result = tan(M_PI / 4); // result = 1.0
  • asin(): Computes the arcsine (inverse sine) of a value.

    Syntax:

    double asin(double x);

    Example:

    double result = asin(1.0); // result = π/2
  • acos(): Computes the arccosine (inverse cosine) of a value.

    Syntax:

    double acos(double x);

    Example:

    double result = acos(1.0); // result = 0.0
  • atan(): Computes the arctangent (inverse tangent) of a value.

    Syntax:

    double atan(double x);

    Example:

    double result = atan(1.0); // result = π/4

3. Exponential and Logarithmic Functions

  • exp(): Computes the value of e raised to the power of x.

    Syntax:

    double exp(double x);

    Example:

    double result = exp(1); // result = e (approximately 2.71828)
  • log(): Computes the natural logarithm (base e) of a value.

    Syntax:

    double log(double x);

    Example:

    double result = log(e); // result = 1.0
  • log10(): Computes the base-10 logarithm of a value.

    Syntax:

    double log10(double x);

    Example:

    double result = log10(100); // result = 2.0
  • pow(): Computes the value of x raised to the power of y.

    Syntax:

    double pow(double x, double y);

    Example:

    double result = pow(2, 3); // result = 8.0
  • sqrt(): Computes the square root of a number.

    Syntax:

    double sqrt(double x);

    Example:

    double result = sqrt(16); // result = 4.0

4. Miscellaneous Functions

  • fmod(): Computes the remainder of the division of two floating-point numbers.

    Syntax:

    double fmod(double x, double y);

    Example:

    double result = fmod(5.3, 2.0); // result = 1.3
  • hypot(): Computes the length of the hypotenuse of a right triangle given the lengths of the other two sides.

    Syntax:

    double hypot(double x, double y);

    Example:

    double result = hypot(3.0, 4.0); // result = 5.0

Using <math.h>

To use the functions defined in <math.h>, include the header file at the beginning of your C source file:

#include <math.h>

Important Notes

  • Many functions in <math.h> require the input to be of type double. If you use a different type, make sure to cast it to double where necessary.
  • When using trigonometric functions, remember that the input angle should be in radians. To convert degrees to radians, you can use the formula: radians=degrees×π180\text{radians} = \text{degrees} \times \frac{\pi}{180} You can include #define _USE_MATH_DEFINES before including <math.h> to access constants like M_PI.

Summary

  • <math.h> provides essential mathematical functions for performing various mathematical operations.
  • Functions for basic arithmetic, trigonometry, exponentiation, and logarithmic calculations are included.
  • Understanding and effectively using <math.h> is vital for mathematical computations in C programming.