Math.h Functions in C Library

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The C programming language had been designed to support structured programming as it is meant to work with large software systems. The C standard library included a group of functions defined in a header file that support the essential needs of developers to create solutions and effectively solve the problem with the right level of abstraction. One of the most used headers in the C standard library is predefined functions in math.h module, which defines common mathematical functions.

Important C library math.h functions

Performing mathematical operations is a frequent task in articulating an executable solution in the form of a C program. Computation is at the core of all software systems, thus a programming language must provide the set of necessary symbols to express a mode of computation. In an attempt to minimize the language size, top-shelf operations are defined at the core — relational operators, arithmetic operators, and logical operators —while various extensions can be performed with the C standard library.

The table summarizes the set of important math functions defined in math.h header file.

Math notationFunction SignatureDescription
x\lceil x \rceildouble ceil(double x)a function that returns the smallest integer value greater than or equal to xx in type double
x\lfloor x \rfloordouble floor(double x)a function that returns the largest integer value less than or equal to xx in type double
IxxIdouble fabs(double)a function that evaluates the absolute value of xx in type double
ln(x)\ln(x)double log(double x)a function that evaluates the natural logarithm of xx
log10(x)\log_{10}(x)double log10(double x)a function that evaluates the logarithm of xx in base 10
xmodyx \mod ydouble fmod(double x, double y)a function that returns a floating point value of the remainder of xx divided by yy
x\sqrt{x}double sqrt(double x)a function that evaluates the square root of xx
xyx^ydouble pow(double x, double y)a function that evaluates xx raised to the power yy
double modf(double x, double y)a function that breaks zz to its fractional part,integral part and returns the fractional part
exe^xdouble exp(double x)a function that evaluates e(2.718282) to the power xx
cos(x)\cos(x)double cos(double x)a function that evaluates cosine of xx
cos1(x)\cos^{-1}(x)double acos(double x)a function that evaluates the inverse cosine of xx
tanh(x)\tanh(x)double tanh(double x)a function that evaluates the hyperbolic tangent at xx

The math.h header also provides a set of predefined constants/ macros summarized in the table below.

SymbolExpression
M_Eee
M_LOG2Elog2(e)\log_2(e)
M_LOG10Elog10(e)\log_{10}(e)
M_LN2ln(2)\ln(2)
M_LN10ln(10)\ln(10)
M_PIπ\pi
M_PI_2π2\frac{\pi}{2}
M_PI_4π4\frac{\pi}{4}
M_1_PI1π\frac{1}{\pi}
M_2_PI2π\frac{2}{\pi}
M_2_SQRTPI2π\sqrt{\frac{2}{\pi}}
M_SQRT22\sqrt{2}
M_SQRT1_212\frac{1}{\sqrt{2}}

Let us take a look at some simple examples and familiarize ourselves with how we can use predefined math functions in math.h.

Code example: ceil, floor, and fabs

The above code gives us the following output:

Code example: log, and log10

The result of the code above is given as follow:

Code example: sqrt, exp, and pow

Evaluating the code would give us:

Code example: cos, acos and tanh

Evaluating some trigonometric expressions yields to:

Code example: modf, and fmod

Evaluating the code would resolve to:

Summary

  • The design of C has been economical because it gives you the necessary tools to tackle your problem without giving you too much "bloat" in your system.
  • The C standard library provides a set of tools that extends C to sufficiently solve a problem at hand.
  • The math.h header file provides a suite of mathematical functions that allows you to solve problems at a sufficient level of abstraction. In other words, if you wish to express a function double pow(double x, double y), you will not need to think about implementing a function that performs a computation for pow() instead, you can add the library and use the function.