Required Static Methodswift-numerics 1.0.3RealModule

expMinusOne(_:)

exp(x) - 1, computed in such a way as to maintain accuracy for small x.

ElementaryFunctions.swift:90
static func expMinusOne(_ x: Self) -> Self

When x is close to zero, the expression .exp(x) - 1 suffers from catastrophic cancellation and the result will not have full accuracy. The .expMinusOne(x) function gives you a means to address this problem.

As an example, consider the expression (x + 1)*exp(x) - 1. When x is smaller than .ulpOfOne, this expression evaluates to 0.0, when it should actually round to 2*x. We can get a full-accuracy result by using the following instead:

let t = .expMinusOne(x)
return x*(t+1) + t       // x*exp(x) + (exp(x)-1) = (x+1)*exp(x) - 1

This re-written expression delivers an accurate result for all values of x, not just for small values.

See also:

  • exp()

  • exp2() (for types conforming to RealFunctions)

  • exp10() (for types conforming to RealFunctions)