In C++11 and C++14, we were able to use this math.h library in C++ applications. After the C++17 standard, this library modernized math operations with the cmath library. Functions are declared in <cmath>
header. For compatibility reasons the <math.h>
is an optional alternative to support older code. In this post, we list most of these mathematical functions declared in the <cmath> header of modern C++.
What is cmath mathematical functions library in C++?
The CMath Mathematical Special Functions Header <cmath>
defines mathematical functions and symbols in the std namespace. It includes the previous math functions. It may also define them in the global namespace. You have to add a std namespace using namespace std;
or you should use the std::
prefix for each math function.
Some of the mathematical special functions are added to the C++17 cmath
library header by the contents of the former international standard ISO/IEC 29124:2010 and math.h
functions added too. These are only available in namespace std
. If you do not use namespace you should add std::
prefix to use these modern math functions.
In general, they are mostly double
functions and can be slower, but they have more accurate results. For example sin()
uses double variables, sinf()
uses a float variable (same as C++11, faster, less accurate), while sinl()
is used with long double
variables (same as C++11, also slower, but more accurate).
Here is a simple C++ example using the sin
function.
What are the CMath mathematical special functions in modern C++ 17?
There are many new modern mathematical special functions in the C++17 cmath header. Such as functions for associated Laguerre polynomials, elliptic integral of the first kind functions, Cylindrical Bessel functions (of the first kind), Cylindrical Neumann functions, Exponential integral functions, Hermite polynomials functions, Legendre polynomials functions, Laguerre polynomials, Riemann zeta function, and some spherical functions. Here is a list of the CMath special functions.
Description | double | float | long double |
Associated Laguerre polynomials | assoc_laguerre | assoc_laguerref | assoc_laguerrel |
Associated Legendre polynomials | assoc_legendre | assoc_legendref | assoc_legendrel |
Beta function | beta | betaf | betal |
Elliptic integral of the first kind (complete) | comp_ellint_1 | comp_ellint_1f | comp_ellint_1l |
Elliptic integral of the second kind (complete) | comp_ellint_2 | comp_ellint_2f | comp_ellint_1l |
Elliptic integral of the third kind (complete) | comp_ellint_3 | comp_ellint_3f | comp_ellint_1l |
Regular modified cylindrical Bessel functions | cyl_bessel_i | cyl_bessel_if | cyl_bessel_il |
Cylindrical Bessel functions (of the first kind) | cyl_bessel_j | cyl_bessel_jf | cyl_bessel_jl |
Irregular modified cylindrical Bessel functions | cyl_bessel_k | cyl_bessel_kf | cyl_bessel_kl |
Cylindrical Neumann functions | cyl_neumann | cyl_neumannf | cyl_neumannl |
Elliptic integral of the first kind (incomplete) | ellint_1 | ellint_1f | ellint_1l |
Elliptic integral of the second kind (incomplete) | ellint_2 | ellint_2f | ellint_2l |
Elliptic integral of the third kind (incomplete) | ellint_3 | ellint_3f | ellint_3l |
Exponential integral | expint | expint | expint |
Hermite polynomials | hermite | hermitef | hermitel |
Legendre polynomials | legendre | legendref | legendrel |
Laguerre polynomials | laguerre | laguerref | laguerrel |
Riemann zeta function | riemann_zeta | riemann_zetaf | riemann_zetal |
spherical associated Legendre functions | sph_legendre | sph_legendref | sph_legendrel |
spherical Bessel functions (of the first kind) | sph_bessel | sph_besself | sph_bessell |
spherical Neumann functions | sph_neumann | sph_neumannf | sph_neumannl |
Note that, by the C++20 standard, only default names of math functions are used. For example, the laguerre()
is used for the float
, double
and long double
versions.
For more details about changes in C++17 standard, please see this https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0226r1.pdf
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.