Math.h Functions in C Library
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 notation | Function Signature | Description |
---|---|---|
double ceil(double x) | a function that returns the smallest integer value greater than or equal to in type double | |
double floor(double x) | a function that returns the largest integer value less than or equal to in type double | |
II | double fabs(double) | a function that evaluates the absolute value of in type double |
double log(double x) | a function that evaluates the natural logarithm of | |
double log10(double x) | a function that evaluates the logarithm of in base 10 | |
double fmod(double x, double y) | a function that returns a floating point value of the remainder of divided by | |
double sqrt(double x) | a function that evaluates the square root of | |
double pow(double x, double y) | a function that evaluates raised to the power | |
double modf(double x, double y) | a function that breaks to its fractional part,integral part and returns the fractional part | |
double exp(double x) | a function that evaluates e(2.718282) to the power | |
double cos(double x) | a function that evaluates cosine of | |
double acos(double x) | a function that evaluates the inverse cosine of | |
double tanh(double x) | a function that evaluates the hyperbolic tangent at |
The math.h header also provides a set of predefined constants/ macros summarized in the table below.
Symbol | Expression |
---|---|
M_E | |
M_LOG2E | |
M_LOG10E | |
M_LN2 | |
M_LN10 | |
M_PI | |
M_PI_2 | |
M_PI_4 | |
M_1_PI | |
M_2_PI | |
M_2_SQRTPI | |
M_SQRT2 | |
M_SQRT1_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.