C++20 constexpr math functions

In C++20, many math functions from the <cmath> library were made constexpr. This enables complex mathematical operations at compile time, which can lead to more efficient and optimized code.

The main advantage of using constexpr math functions is that they enable calculations at compile time rather than at runtime. This can lead to performance improvements because the compiler can optimize the code based on the known constant values. Additionally, because the values are known at compile time, they can be used in places where a constant expression is needed, such as in array sizes and template arguments.

Here are some important points to remember about constexpr math functions:

  1. Only a subset of math functions from the <cmath> library are constexpr in C++20. Other functions may still be evaluated at runtime.

  2. The arguments provided to a constexpr function must be constant expressions themselves. Otherwise, the function call will not be evaluated at compile time.

  3. constexpr math functions are subject to the same floating-point rounding and accuracy limitations as their runtime counterparts. In other words, you should be aware of potential floating-point inaccuracies when using constexpr functions in calculations.

  4. Some compilers may not yet fully support C++20 or all of its constexpr math functions. Be sure to check the documentation of the compiler being used to ensure that it supports the specific functions.

Here is a list of selected math functions that became constexpr in C++20. Note that this list is not exhaustive, but it covers some of the most commonly used functions. Once again, these functions became constexpr in C++20, not C++17.

Here's the table sorted by function name in ascending order:

FunctionDescriptionSince
absAbsolute valueC++20
acosArc cosine functionC++20
acoshInverse hyperbolic cosine functionC++20
asinArc sine functionC++20
asinhInverse hyperbolic sine functionC++20
atanArc tangent functionC++20
atan2Arc tangent function with two parametersC++20
atanhInverse hyperbolic tangent functionC++20
cbrtCube rootC++20
ceilCeiling functionC++20
copysignCopy sign of a numberC++20
cosCosine functionC++20
coshHyperbolic cosine functionC++20
divIntegral divisionC++20
dremDeprecated; use remainder insteadC++20
erfError functionC++20
erfcComplementary error functionC++20
expExponential functionC++20
exp2Base-2 exponential functionC++20
expm1Exponential function minus 1C++20
fdimPositive differenceC++20
floorFloor functionC++20
fmaFused multiply-addC++20
fmaxMaximum of two floating-point valuesC++20
fminMinimum of two floating-point valuesC++20
fmodFloating-point remainder (modulo)C++20
frexpBreak floating-point number into fractionC++20
gammaDeprecated; use tgamma insteadC++20
gamma_rDeprecated; use lgamma insteadC++20
hypotHypotenuseC++20
ilogbIntegral logarithm of exponent base-2C++20
j0Bessel function of the first kind of order 0C++20
j1Bessel function of the first kind of order 1C++20
jnBessel function of the first kind of order nC++20
ldexpMultiply by integral power of 2C++20
lgammaNatural logarithm of the absolute value of the gamma functionC++20
llrintRound to long long integral valueC++20
llroundRound to nearest long long integerC++20
logNatural logarithmC++20
log10Base-10 logarithmC++20
log1pNatural logarithm of 1 plus argumentC++20
log2Base-2 logarithmC++20
logbBase-2 logarithm of exponentC++20
lrintRound to long integral valueC++20
lroundRound to nearest long integerC++20
maxMaximum of two valuesC++20
minMinimum of two valuesC++20
modfDecompose a floating-point number into its integer and fractional partsC++20
nanGenerate quiet NaNC++20
nearbyintRound to integral value in current rounding modeC++20
nextafterNext representable floating-point valueC++20
nexttowardNext representable floating-point value toward a long doubleC++20
powPower functionC++20
remainderRemainder of the floating-point divisionC++20
remquoRemainder and quotient of the floating-point divisionC++20
rintRound to integral valueC++20
roundRound to nearest integerC++20
scalbDeprecated; use scalbn or scalbln insteadC++20
scalblnScale floating-point number by a power of FLT_RADIX as a long integerC++20
scalbnScale floating-point number by a power of FLT_RADIXC++20
significandGet the significand of a floating-point numberC++20
sinSine functionC++20
sinhHyperbolic sine functionC++20
sqrtSquare rootC++20
tanTangent functionC++20
tanhHyperbolic tangent functionC++20
tgammaGamma functionC++20
truncTruncate functionC++20
y0Bessel function of the second kind of order 0C++20
y1Bessel function of the second kind of order 1C++20
ynBessel function of the second kind of order nC++20