C++C++11C++14C++17C++20Learn C++Syntax

What Are The CMath Mathematical Functions in Modern C++?

What Are The CMath Mathematical Functions in Modern C++

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 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 header to support legacy code. In this post, we list most of these mathematical functions declared in the <cmath> header of modern C++.

What is the cmath mathematical functions library in 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. 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 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. In the example sin() is used with double variables, sinf() used with float variables (same as C++11, faster, less accurate), and sinl() used with long double variables (same as C++11, slower, more accurate).

Here is a simple C++ example using the sin function.

What Are The CMath Mathematical Functions in Modern C++?

Here are some of most used mathematical functions defined in cmath header.

Basic Math Functions

Here are basic functions,

Descriptiondouble floatlong double
absolute valuefabsfabsf / absfabsl
remainderfmodfmodffmodl
signed remainderreminderreminderfreminderl
signed remainder as well as the three last bits of the division operationremquoremquofremquol
fused multiply-addfmafmaffmal
maximum value of two parametersfmaxfmaxffmaxl
minimum value of two parametersfminfminffminl
positive difference of two parametersfdimfdimffdiml
NaN (not-a-number)nannanfnanl

Exponential Functions

Here are exponential functions,

Descriptiondouble floatlong double
power of eexpexpfexpl
power of 2exp2exp2fexp2l
power of e minus oneexpm1expm1fexpm1l
ln x, logarithm (base e)loglogflogl
logarithm (base 10)log10log10flog10l
logarithm (base 2)log2log2flog2l
ln( 1 + x ) , natural logarithm (base e) log1plog1pflog1pl

Power Functions

Here are power functions.

Descriptiondouble floatlong double
x power ypowpowfpowl
square root xsqrtsqrtfsqrtl
cubic root xcbrtcbrtfcbrtl
hypotenuse of x, y or x, y, zhypothypotfhypotl

Trigonometric Functions

Here are trigonometric functions.

Descriptiondouble floatlong double
sine xsinsinfsinl
cosine xcoscosfcosl
tangent xtantanftanl
arcs sine xasinasinfasinl
arc cosine xacosacosfacosl
arc tangent xatanatanfatanl
arc tangent of x and yatan2atan2fatan2l

Hyperbolic Trigonometric Functions

Here are hyperbolic trigonometric functions,\.

Descriptiondouble floatlong double
hyperbolic sine xsinhsinhfsinhl
hyperbolic cosine xcoshcoshfcoshl
hyperbolic tangent xtanhtanhftanhl
hyperbolic arcs sine xasinhasinhfasinhl
hyperbolic arc cosine xacoshacoshfacoshl
hyperbolic arc tangent xatanhatanhfatanhl

Nearest Functions

Here are nearest functions.

Descriptiondouble floatlong double
nearest integer not less than xceilsinfsinl
nearest integer not greater than xfloorcosfcosl
nearest integer not greater in magnitude than xtrunctanftanl
nearest integer, rounding away from zero in halfway casesround
lround
llround
roundf
lroundf
llroundf
roundl
lroundl
llroundl
nearest integer using current rounding modenearbyintnearbyintfnearbyintl
nearest integer using current rounding mode with
exception if the result differs
rint
lrint
llrint
rintf
lrintf
llrintf
rintl
lrintl
llrintl

Note that, by the C++20 standard, only default names of math functions are used. For example, sin() is used for the float, double and long double versions.

Other Modern Functions in C++

There are many other functions and new modern mathematical functions in the C++17 cmath header. Such as, functions for associated Laguerre polynomials, The 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 of them.

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

What Are The CMath Mathematical Functions 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++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?