Dr. Roger Ianjamasimanana

The math.h functions in C

By Dr. Roger Ianjamasimanana

1. The math.h library in C

The C standard library provides math.h to offer a variety of mathematical functions for computing trigonometric expressions, logarithms, exponentials, power-related functions, rounding, and more. Below, we’ll explore many of the common (and some less common) functions provided by math.h, along with short examples of how to use them.

✍️
Return value

All the functions discussed here typically operate on double arguments and return double values. C99 and later also define single-precision (float, typically suffix f) and extended-precision (long double, typically suffix l) variants for most of these functions, such as sin, sinl, sinf, etc.


2. Trigonometric functions

Function Description Example
sin(x) Computes the sine of x, where x is in radians.
#include <stdio.h>
#include <math.h>

int main() {
    double angle = M_PI / 2; // 90 degrees in radians
    double result = sin(angle);
    printf("sin(π/2) = %f\n", result); // Expect ~1.000000
    return 0;
}
cos(x) Computes the cosine of x, where x is in radians.
#include <stdio.h>
#include <math.h>

int main() {
    double angle = M_PI; // 180 degrees
    double result = cos(angle);
    printf("cos(π) = %f\n", result); // Expect ~-1.000000
    return 0;
}
tan(x) Computes the tangent of x, where x is in radians.
#include <stdio.h>
#include <math.h>

int main() {
    double angle = M_PI / 4; // 45 degrees
    double result = tan(angle);
    printf("tan(π/4) = %f\n", result); // Expect ~1.000000
    return 0;
}
asin(x) Computes the arcsine of x in radians. Domain: -1 to 1.
#include <stdio.h>
#include <math.h>

int main() {
    double value = 1.0;
    double angle = asin(value);
    printf("asin(1.0) = %f radians\n", angle); // Expect ~1.570796 (π/2)
    return 0;
}
acos(x) Computes the arccosine of x in radians. Domain: -1 to 1.
#include <stdio.h>
#include <math.h>

int main() {
    double value = -1.0;
    double angle = acos(value);
    printf("acos(-1.0) = %f radians\n", angle); // Expect ~3.141593 (π)
    return 0;
}
atan(x) Computes the arctangent of x in radians.
#include <stdio.h>
#include <math.h>

int main() {
    double value = 1.0;
    double angle = atan(value);
    printf("atan(1.0) = %f radians\n", angle); // Expect ~0.785398 (π/4)
    return 0;
}
atan2(y, x) Computes the arc tangent of y/x taking into account the signs of x and y, returning an angle in the correct quadrant. Useful for converting Cartesian coordinates to polar.
#include <stdio.h>
#include <math.h>

int main() {
    double y = 1.0, x = -1.0;
    double angle = atan2(y, x);
    printf("atan2(1.0, -1.0) = %f radians\n", angle);
    return 0;
}

3. Hyperbolic functions

Function Description Example
sinh(x) Computes the hyperbolic sine of x.
#include <stdio.h>
#include <math.h>

int main() {
    double value = 1.0;
    double result = sinh(value);
    printf("sinh(1.0) = %f\n", result);
    return 0;
}
cosh(x) Computes the hyperbolic cosine of x.
#include <stdio.h>
#include <math.h>

int main() {
    double value = 1.0;
    double result = cosh(value);
    printf("cosh(1.0) = %f\n", result);
    return 0;
}
tanh(x) Computes the hyperbolic tangent of x.
#include <stdio.h>
#include <math.h>

int main() {
    double value = 1.0;
    double result = tanh(value);
    printf("tanh(1.0) = %f\n", result);
    return 0;
}
asinh(x) Computes the inverse hyperbolic sine of x.
#include <stdio.h>
#include <math.h>

int main() {
    double value = 1.0;
    double result = asinh(value);
    printf("asinh(1.0) = %f\n", result);
    return 0;
}
acosh(x) Computes the inverse hyperbolic cosine of x. Domain: x ≥ 1.
#include <stdio.h>
#include <math.h>

int main() {
    double value = 2.0;
    double result = acosh(value);
    printf("acosh(2.0) = %f\n", result);
    return 0;
}
atanh(x) Computes the inverse hyperbolic tangent of x. Domain: -1 < x < 1.
#include <stdio.h>
#include <math.h>

int main() {
    double value = 0.5;
    double result = atanh(value);
    printf("atanh(0.5) = %f\n", result);
    return 0;
}

4. Exponential and logarithmic functions

Function Description Example
exp(x) Computes the exponential function: ex.
#include <stdio.h>
#include <math.h>

int main() {
    double value = 1.0;
    double result = exp(value);
    printf("exp(1.0) = %f\n", result); // ~2.71828
    return 0;
}
exp2(x) Computes 2x.
#include <stdio.h>
#include <math.h>

int main() {
    double value = 3.0;
    double result = exp2(value);
    printf("exp2(3.0) = %f\n", result); // 8.0
    return 0;
}
log(x) Computes the natural logarithm of x (base e).
#include <stdio.h>
#include <math.h>

int main() {
    double value = 2.718281828;
    double result = log(value);
    printf("log(%f) = %f\n", value, result); // ~1.000000
    return 0;
}
log10(x) Computes the base-10 logarithm of x.
#include <stdio.h>
#include <math.h>

int main() {
    double value = 100.0;
    double result = log10(value);
    printf("log10(100.0) = %f\n", result); // 2.0
    return 0;
}
log2(x) Computes the base-2 logarithm of x.
#include <stdio.h>
#include <math.h>

int main() {
    double value = 8.0;
    double result = log2(value);
    printf("log2(8.0) = %f\n", result); // 3.0
    return 0;
}
log1p(x) Computes the natural logarithm of 1 + x with higher precision for small x.
#include <stdio.h>
#include <math.h>

int main() {
    double value = 0.000001;
    double result = log1p(value);
    printf("log1p(%f) = %f\n", value, result);
    return 0;
}

5. Power functions

Function Description Example
pow(x, y) Computes xy.
#include <stdio.h>
#include <math.h>

int main() {
    double base = 2.0, exponent = 5.0;
    double result = pow(base, exponent);
    printf("pow(2.0, 5.0) = %f\n", result); // 32.0
    return 0;
}
sqrt(x) Computes the square root of x. Domain: x >= 0.
#include <stdio.h>
#include <math.h>

int main() {
    double value = 16.0;
    double result = sqrt(value);
    printf("sqrt(16.0) = %f\n", result); // 4.0
    return 0;
}
cbrt(x) Computes the cube root of x.
#include <stdio.h>
#include <math.h>

int main() {
    double value = 27.0;
    double result = cbrt(value);
    printf("cbrt(27.0) = %f\n", result); // 3.0
    return 0;
}
hypot(x, y) Computes sqrt(x*x + y*y). Often used to find the length of the hypotenuse of a right-angled triangle.
#include <stdio.h>
#include <math.h>

int main() {
    double x = 3.0, y = 4.0;
    double result = hypot(x, y);
    printf("hypot(3.0, 4.0) = %f\n", result); // 5.0
    return 0;
}

6. Rounding and remainder functions

Function Description Example
ceil(x) Rounds x upward to the nearest integer value.
#include <stdio.h>
#include <math.h>

int main() {
    double value = 3.2;
    double result = ceil(value);
    printf("ceil(3.2) = %f\n", result); // 4.0
    return 0;
}
floor(x) Rounds x downward to the nearest integer value.
#include <stdio.h>
#include <math.h>

int main() {
    double value = 3.8;
    double result = floor(value);
    printf("floor(3.8) = %f\n", result); // 3.0
    return 0;
}
trunc(x) Truncates x toward zero (removes any fractional part).

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

int main() {
    double value = -3.8;
    double result = trunc(value);
    printf("trunc(-3.8) = %f\n", result); // -3.0
    return 0;
}
round(x) Rounds x to the nearest integer value, away from zero if midway.
#include <stdio.h>
#include <math.h>

int main() {
    double value = 3.5;
    double result = round(value);
    printf("round(3.5) = %f\n", result); // 4.0 on most implementations
    return 0;
}
fmod(x, y) Computes the remainder of x divided by y. The sign of the result is the same as x.
#include <stdio.h>
#include <math.h>

int main() {
    double x = 5.3, y = 2.0;
    double result = fmod(x, y);
    printf("fmod(5.3, 2.0) = %f\n", result); // ~1.3
    return 0;
}
remainder(x, y) Similar to fmod, but the result may differ in sign if x / y is halfway.
#include <stdio.h>
#include <math.h>

int main() {
    double x = 5.3, y = 2.0;
    double result = remainder(x, y);
    printf("remainder(5.3, 2.0) = %f\n", result);
    return 0;
}

7. Special functions

Function Description Example
erf(x) Computes the error function of x, important in probability and statistics.
#include <stdio.h>
#include <math.h>

int main() {
    double value = 0.5;
    double result = erf(value);
    printf("erf(0.5) = %f\n", result);
    return 0;
}
erfc(x) Computes the complementary error function: 1 - erf(x).
#include <stdio.h>
#include <math.h>

int main() {
    double value = 0.5;
    double result = erfc(value);
    printf("erfc(0.5) = %f\n", result);
    return 0;
}
tgamma(x) Computes the gamma function Γ(x), used in advanced mathematics (generalization of factorial).
#include <stdio.h>
#include <math.h>

int main() {
    double value = 5.0;
    double result = tgamma(value);
    printf("tgamma(5.0) = %f\n", result); // 24.0 (since Γ(5) = 4!)
    return 0;
}
lgamma(x) Computes the natural log of the absolute value of the gamma function: ln|Γ(x)|.
#include <stdio.h>
#include <math.h>

int main() {
    double value = 5.0;
    double result = lgamma(value);
    printf("lgamma(5.0) = %f\n", result); // ~3.178054, which is ln(24)
    return 0;
}

8. Floating-point classification and constants

Besides the functions above, math.h also provides:

  • isinf(x), isnan(x), isfinite(x): to check whether x is infinite, Not-a-Number (NaN), or finite.
  • HUGE_VAL, INFINITY, NAN: constants representing positive infinity and NaN values.

For instance:

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

int main() {
    double val = log(0.0); // This may result in -Infinity or an error
    if(isinf(val)) {
        printf("The value is infinite.\n");
    }
    return 0;
}

9. Conclusion

The math.h library in C provides a wide array of functions for performing mathematical calculations, from basic trigonometry to advanced functions like the gamma function. While we've covered a significant subset of the standard library’s mathematical repertoire, remember that different C standards (C99, C11, etc.) may introduce more or variants of these functions (like float and long double versions).

When working with these functions, always keep an eye on the domains of each function (e.g., log(x) requires x > 0), and handle special cases like infinity or NaN appropriately with classification functions. This awareness ensures robust and bug-free numerical computations in your C programs.

feature-top
Readers’ comment
feature-top
Log in to add a comment
🔐 Access