Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For production code, call the platform’s exp function. A reliable exponential routine is more than a Taylor series: it handles special values and range limits, reduces the input to a small interval, approximates the function there, and reconstructs the result while controlling rounding and overflow. If you need ex − 1 for an input near zero, use a separate expm1 routine rather than subtracting 1 from exp(x).

Should you implement exp(x) yourself?

Usually, no. A platform math library has a routine tuned for its supported floating-point formats and runtime. Python’s official math documentation says math.exp(x) is usually more accurate than math.e ** x or pow(math.e, x). Use the library routine unless you have a specific reason to replace it, such as teaching, a constrained runtime, a particular precision or throughput target, or an accelerator that needs a custom implementation.

Before writing one, decide what “accurate” means for your use. Maximum error, error measured in units in the last place (ulps), correctly rounded output, speed, reproducibility across platforms, and support for subnormal values are distinct requirements. A routine optimized for throughput may not meet a correctly rounded requirement.

Why production implementations reduce the range

The exponential grows and shrinks rapidly across floating-point inputs. Approximating it with one polynomial over the entire representable range is impractical. Instead, a common design writes the input as x = k·ln(2) + r, where k is an integer and r is small. Then exp(x) = 2k·exp(r): approximate the function only on the small interval and recover the scale separately.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The fdlibm implementation documented in Chromium’s source reduces the argument so that |r| ≤ 0.5·ln(2) ≈ 0.34658. That is an algorithmic bound for its reduced argument, not an accuracy measurement or a universal requirement for every implementation.

How to build the finite-input path

  1. Specify the contract. Choose the floating-point format, accepted input range, error target, rounding expectations, and behavior for special values. State whether subnormal outputs are supported and whether results must be reproducible across platforms.
  2. Classify the input and check the range. Handle NaN and infinities explicitly, and identify finite inputs that will overflow or underflow under your contract. Do this before evaluating an approximation that assumes an ordinary finite argument.
  3. Choose the scale. Compute an integer k close to x/ln(2), using the rounding rule appropriate to the chosen reduction interval. Production implementations use carefully chosen split high and low parts of ln(2) so that the reduction does not lose too much precision.
  4. Form the reduced argument. Compute r = x − k·ln(2) with a correction term that accounts for the split constant and rounding. Confirm that r lies inside the interval for which the approximation was designed.
  5. Approximate on that interval. Evaluate exp(r) with a polynomial or rational approximation whose coefficients and degree are chosen for the required error bound. fdlibm documents a Remez-based approach for its reduced function. A short Taylor series can be suitable for a deliberately narrow educational range, but choosing an arbitrary number of Taylor terms does not establish production accuracy.
  6. Restore the scale safely. Form 2k·exp(r) using the floating-point operations available in the target environment. Handle overflow, underflow, and subnormal results according to the contract; do not assume a direct intermediate multiplication is safe for every k.

This is a design outline, not a drop-in implementation: the constants, coefficient set, rounding details, and exceptional paths must all match the target format and error requirement.

Why exp(x) - 1 loses precision near zero

When x is small, exp(x) is close to 1. Subtracting 1 from that rounded result can discard significant digits. Python’s documentation warns that this subtraction can cause significant precision loss and provides expm1(x) to compute the quantity to full precision. Oracle’s C library reference gives the same guidance for small inputs, and Boost.Math documents dedicated expm1 handling.

Use expm1(x) whenever the desired result is ex − 1, especially near zero. A custom implementation should give expm1 its own cancellation-safe path, typically using a small-argument approximation rather than computing exp(x) and then subtracting 1. The approximation must be validated for the format and range it claims to support.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Define behavior for special values and range limits

The expected result is not determined by the approximation alone. Specify the API’s behavior for each case, and distinguish the mathematical result from any language-specific error reporting.

  • NaN: Decide how NaN inputs propagate and whether the routine raises, flags, or otherwise reports an error. Oracle documents expm1 returning NaN for a NaN input.
  • Positive infinity: The mathematical result is positive infinity. Oracle documents this result for expm1 with positive infinity input.
  • Negative infinity: For exp, the mathematical limit is zero; for expm1, it is −1. Oracle documents −1 for the latter.
  • Signed zero: Decide whether the sign of zero is preserved. Oracle documents that expm1 preserves signed zero.
  • Overflow: Define the returned value and error or exception behavior when the finite result is too large. Oracle documents a range error for expm1 overflow. Do not assume another language or library reports it identically.
  • Underflow and subnormals: Specify whether a result may be subnormal, rounds to zero, or triggers an error or status flag. The answer depends on the target format and runtime; there is no single cross-platform contract in the cited documentation.

These Oracle behaviors describe its expm1 documentation; they should not be silently treated as a universal contract for every platform’s exp function.

Choose an approach that fits the requirement

Approach Appropriate use Main limitation
Platform exp and expm1 Application code that needs the runtime’s supported behavior and accuracy. Behavior, accuracy, and cross-platform reproducibility depend on the library and platform.
Short series or polynomial on a narrow interval Teaching or a deliberately restricted input range. It does not by itself solve wide-range scaling, special values, or production error guarantees.
Range reduction plus a minimax or Remez approximation A custom routine with a defined format and error target. Requires carefully selected constants and coefficients, explicit edge-case handling, and validation.

Compare implementations using maximum error or ulp behavior, latency and throughput, supported range and overflow threshold, subnormal and special-value behavior, reproducibility, code size, and whether correct rounding is mandatory. These criteria can conflict, so state which ones the implementation prioritizes.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Validate the implementation before relying on it

Compare results against a trusted high-precision reference across ordinary inputs and the boundaries of the stated contract. Include inputs near zero, near range limits, around reduction boundaries, and values whose outputs are subnormal, as well as NaN, infinities, and signed zero. Test expm1 separately near zero; tests of exp alone will not reveal cancellation in a later subtraction.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Measure the chosen error metric over the tested domain and report the format, input range, rounding mode, and platform. Do not claim a maximum error, ulp bound, or correct rounding unless the implementation has actually been checked against the corresponding criterion.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.