When Is The CMath Mathematical Special Functions Header Used in Modern C++?
The math library
in C language is designed to be used in mathematical operations. From the first C language to the latest C++ Builder 12, there have been many changes and improvements in both hardware and software. We were able to use this math.h
library in C++ applications. After the C++17 standard, this library is modernized in the cmath library, Functions are declared in
header for compatibility reasons in modern C++. In this post, we explain what are the math.h
and cmath
libraries.
What is the math.h math library in C++?
In the early days of computers there was an FPU (Floating Point Unit) in addition to a CPU (Central Processing Unit). While the CPUs were slower in floating point operations (especially in trigonometric functions) FPUs were faster than CPUs in those days. The math library
in the C language is designed to be used in mathematical operations with these FPUs and CPUs. From the first C language to the latest CLANG C++ compiler, there have been many changes and improvements in both hardware and software. We were able to use this math.h
library in C++ applications.
The math library library functions are declared in math.h
header file and it is in the standard library of the C programming language. Most of the functions are trigonometric and basic math functions, and they mostly use floating point numbers such as float
, double
, or long double
variables. Trigonometric functions use radians in angular parameters and all functions take doubles for floatingpoint arguments unless otherwise specified.
In C++ (C++98, C++11, C++14), these C functions were begin used by the same header
. For example, if you want to use sin()
, cos()
, tan()
, exp()
, log()
, and pow()
functions you have to add
library to the C and C++11, C++14 applications.
Note that, some mathematical library functions that operate on integers are instead specified in the
header, such as abs
, labs
, div
, and ldiv
.
Here is a simple C example using the sin
function.
#include #include
int main() { double x = sin(1.0); }

What is the cmath mathematical special functions library in C++?
In C++11 and C++14, we were able to use the math.h
library in C++ applications. After the C++17 standard, this library is modernized in the cmath
library, and functions are declared in
header for compatibility reasons in modern C++, and the
The CMath Mathematical Special Functions Header
defines mathematical functions and symbols in the std namespace, and previous math functions are also included, it may also define them in the global namespace. You have to add a std namespace with 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.
Here is a simple C++ example using the sin
function.
#include #include
int main() { double x = std::sin(1.0); }

What is the difference between math.h and cmath in modern C++?
The CMath Mathematical Special Functions Header
defines mathematical functions and symbols in the std namespace, and previous math functions are also included, it may also define them in the global namespace. It can NOT be used with compilers that have C99, C++11, or C++14 options. You have to add a std
namespace with using namespace std;
or you should use the std::
prefix for each math function. They are modern in C++, and safe in memory allocation and usage. In general, they are mostly double functions and can be slower but they have more accurate results. In example sin()
is used double variables, sinf()
used with float variable (same as C++11, faster, less accurate), sinl()
used with long double
variables (same as C++11, slower, more accurate).
The C Math Library
When is the CMath mathematical special functions header used in modern C++?
If you are using C++17 or over, we highly recommend you to use
header and use std special math functions. If you have C++14, C++11, or before, you should use
. If you have an old application that uses
it can be compiled, we again highly recommend you modernize to using std math functions by using
header.
According to this paper;
“While these functions are clearly numerical in nature and will likely be most heavily used by the scientific and engineering communities, other communities of programmers also have needs, ranging from frequent to intermittent, for these functions. . . . [W]e believe that adoption of this proposal would send a clear message to the various numeric computing communities that, contrary to significant popular belief within these communities, C++ is an eminently suitable programming language for their problem domain, too.”
Is there an example of how to use the CMath mathematical special functions in modern C++?
Here is a simple example about how you can use cmath mathematical special functions with the std::
prefix,
#include #include
int main() { double x = std::sin(45*M_PI/180); double y = std::cos(60*M_PI/180); }

or you can use std namespace and you don’t need to add std::
prefix in every mathematical function usage,
#include #include
using namespace std;
int main() { double x = sin(45*M_PI/180); double y = cos(60*M_PI/180); }

If you want to speed up your calculations, you can use C++11 float functions (with a f suffix such as sinf
, cosf
, tanf
, etc) in C++17 and beyond as below.
#include #include
using namespace std;
int main() { float x = sinf(45*M_PI/180); // float (C++11) float y = cosf(60*M_PI/180); // float (C++11) }

If you want more accurate results, you can use C++11 long double
functions (with a l suffix such as sinl
, cosl
, tanl
, etc) in C++17 and beyond as shown below.
#include #include
using namespace std;
int main() { long double x = sinl(45*M_PI/180); // long double (C++11) long double y = cosl(60*M_PI/180); // long double (C++11) }

For more details, please see this https://www.openstd.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 awardwinning VCL framework for highperformance 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.