C++C++17C++20Introduction to C++Learn C++

When Is The CMath Mathematical Special Functions Header Used in Modern C++?

The math library <math.h> 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 <cmath> 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 <math.h> 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 floating-point arguments unless otherwise specified.

In C++ (C++98, C++11, C++14), these C functions were begin used by the same header <math.h>. For example, if you want to use sin(), cos(), tan(), exp(), log(), and pow() functions you have to add <math.h> 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 <stdlib.h> header, such as abs, labs, div, and ldiv.

Here is a simple C example using the sin function.

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 <cmath> header for compatibility reasons in modern C++, and the <math.h> is an optional old header to support some old codes.

The CMath Mathematical Special Functions Header <cmath> 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.

What is the difference between math.h and cmath in modern C++?

The CMath Mathematical Special Functions Header <cmath> 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 <math.h> defines mathematical functions and symbols in the global namespace, and may also define them in the std namespace. It be can used with C99, C++11, and C++14 standards options. It can be used with C++17 and over too, but we highly recommend using a modernized version <cmath>. You don’t need to add namespace std and you don’t need to use std:: prefix with them. They are mostly C related and some modern functions are absent, not as modern as the cmath functions for C++. In default, they are mostly float functions, faster but they have less accurate results.

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 <cmath> header and use std special math functions. If you have C++14, C++11, or before, you should use <math.h>. If you have an old application that uses <math.h> it can be compiled, we again highly recommend you modernize to using std math functions by using <cmath> 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,

or you can use std namespace and you don’t need to add std:: prefix in every mathematical function usage,

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.

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.

For more details, please see this https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0226r1.pdf

When Is The CMath Mathematical Special Functions Header Used in Modern C++ C++ Builder logo

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17Language FeatureLearn C++

How To Use Skia Shader SkSL Shading Language Code in C++ Builder?

Artificial Intelligence TechC++C++17C++20Learn C++

How To Create Simple Generative AI Code In C++

C++C++17Language FeatureLearn C++

How To Use The SVG Image Format With Skia In C++ Builder

C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?