Average Error: 41.0 → 0.4
Time: 2.5min
Precision: binary64
Cost: 1217
\[\frac{e^{x}}{e^{x} - 1}\]
\[\begin{array}{l} \mathbf{if}\;e^{x} \leq 1.0000018770852963:\\ \;\;\;\;\frac{\frac{-e^{x}}{x}}{-1 - x \cdot \left(x \cdot 0.16666666666666666 + 0.5\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{1 - e^{-x}}\\ \end{array}\]
\frac{e^{x}}{e^{x} - 1}
\begin{array}{l}
\mathbf{if}\;e^{x} \leq 1.0000018770852963:\\
\;\;\;\;\frac{\frac{-e^{x}}{x}}{-1 - x \cdot \left(x \cdot 0.16666666666666666 + 0.5\right)}\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{1 - e^{-x}}\\

\end{array}
(FPCore (x) :precision binary64 (/ (exp x) (- (exp x) 1.0)))
(FPCore (x)
 :precision binary64
 (if (<= (exp x) 1.0000018770852963)
   (/ (/ (- (exp x)) x) (- -1.0 (* x (+ (* x 0.16666666666666666) 0.5))))
   (/ 1.0 (- 1.0 (exp (- x))))))
double code(double x) {
	return exp(x) / (exp(x) - 1.0);
}
double code(double x) {
	double tmp;
	if (exp(x) <= 1.0000018770852963) {
		tmp = (-exp(x) / x) / (-1.0 - (x * ((x * 0.16666666666666666) + 0.5)));
	} else {
		tmp = 1.0 / (1.0 - exp(-x));
	}
	return tmp;
}

Error

Bits error versus x

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original41.0
Target40.4
Herbie0.4
\[\frac{1}{1 - e^{-x}}\]
Alternative 1
Accuracy1.2
Cost960
\[\frac{\frac{-e^{x}}{x}}{-1 - x \cdot \left(x \cdot 0.16666666666666666 + 0.5\right)}\]

Derivation

  1. Split input into 2 regimes
  2. if (exp.f64 x) < 1.00000187708529631

    1. Initial program 41.1

      \[\frac{e^{x}}{e^{x} - 1}\]
    2. Taylor expanded around 0 11.2

      \[\leadsto \frac{e^{x}}{\color{blue}{x + \left(0.5 \cdot {x}^{2} + 0.16666666666666666 \cdot {x}^{3}\right)}}\]
    3. Simplified0.3

      \[\leadsto \frac{e^{x}}{\color{blue}{x + x \cdot \left(x \cdot \left(0.5 + x \cdot 0.16666666666666666\right)\right)}}\]
    4. Using strategy rm
    5. Applied frac-2neg_binary64_7710.3

      \[\leadsto \color{blue}{\frac{-e^{x}}{-\left(x + x \cdot \left(x \cdot \left(0.5 + x \cdot 0.16666666666666666\right)\right)\right)}}\]
    6. Simplified0.3

      \[\leadsto \frac{-e^{x}}{\color{blue}{x \cdot \left(-1 - x \cdot \left(x \cdot 0.16666666666666666 + 0.5\right)\right)}}\]
    7. Using strategy rm
    8. Applied associate-/r*_binary64_7040.3

      \[\leadsto \color{blue}{\frac{\frac{-e^{x}}{x}}{-1 - x \cdot \left(x \cdot 0.16666666666666666 + 0.5\right)}}\]

    if 1.00000187708529631 < (exp.f64 x)

    1. Initial program 33.3

      \[\frac{e^{x}}{e^{x} - 1}\]
    2. Using strategy rm
    3. Applied clear-num_binary64_75933.3

      \[\leadsto \color{blue}{\frac{1}{\frac{e^{x} - 1}{e^{x}}}}\]
    4. Simplified2.4

      \[\leadsto \frac{1}{\color{blue}{1 - e^{-x}}}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.4

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{x} \leq 1.0000018770852963:\\ \;\;\;\;\frac{\frac{-e^{x}}{x}}{-1 - x \cdot \left(x \cdot 0.16666666666666666 + 0.5\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{1 - e^{-x}}\\ \end{array}\]

Reproduce

herbie shell --seed 2020322 
(FPCore (x)
  :name "expq2 (section 3.11)"
  :precision binary64

  :herbie-target
  (/ 1.0 (- 1.0 (exp (- x))))

  (/ (exp x) (- (exp x) 1.0)))