Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards



#include <boost/math/special_functions/expm1.hpp>
namespace boost{ namespace math{

template <class T>
calculated-result-type expm1(T x);

template <class T, class Policy>
calculated-result-type expm1(T x, const Policy&);

}} // namespaces

Returns ex - 1.

The return type of this function is computed using the result type calculation rules: the return is double when x is an integer type and T otherwise.

The final Policy argument is optional and can be used to control the behaviour of the function: how it handles errors, what level of precision to use etc. Refer to the policy documentation for more details.

For small x, then ex is very close to 1, as a result calculating ex - 1 results in catastrophic cancellation errors when x is small. expm1 calculates ex - 1 using rational approximations (for up to 128-bit long doubles), otherwise via a series expansion when x is small (giving an accuracy of less than 2ɛ).

Finally when BOOST_HAS_EXPM1 is defined then the float/double/long double specializations of this template simply forward to the platform's native (POSIX) implementation of this function.

The following graph illustrates the behaviour of expm1:


For built in floating point types expm1 should have approximately 1 epsilon accuracy.

Table 6.79. Error rates for expm1

Microsoft Visual C++ version 12.0

GNU C++ version 5.1.0

GNU C++ version 5.1.0
long double

Sun compiler version 0x5130
Sun Solaris
long double

Random test data

Max = 0.996ε (Mean = 0.283ε)

(<math.h>: Max = 1.31ε (Mean = 0.496ε))

Max = 0.793ε (Mean = 0.126ε)

(Rmath 3.0.2: Max = 0.793ε (Mean = 0.126ε))
(Cephes: Max = 1.53ε (Mean = 0.535ε))

Max = 0.992ε (Mean = 0.402ε)

(<tr1/cmath>: Max = 1.26e+19ε (Mean = 4.89e+18ε) And other failures.)
(<math.h>: Max = 0.992ε (Mean = 0.402ε))

Max = 1.31ε (Mean = 0.406ε)

(<math.h>: Max = 0.996ε (Mean = 0.426ε))


A mixture of spot test sanity checks, and random high precision test values calculated using NTL::RR at 1000-bit precision.