expq2 (section 3.11)

?

Percentage Accurate: 28.7% → 79.9%
Time: 11.3s
Precision: binary64
Cost: 19524

?

\[\frac{e^{x}}{e^{x} - 1} \]
\[\begin{array}{l} \mathbf{if}\;e^{x} \leq 10^{+299}:\\ \;\;\;\;\frac{e^{x}}{\mathsf{expm1}\left(x\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt[3]{\left(\frac{1}{x} + \frac{1.5}{x \cdot x}\right) + 0.375}\\ \end{array} \]
(FPCore (x) :precision binary64 (/ (exp x) (- (exp x) 1.0)))
(FPCore (x)
 :precision binary64
 (if (<= (exp x) 1e+299)
   (/ (exp x) (expm1 x))
   (cbrt (+ (+ (/ 1.0 x) (/ 1.5 (* x x))) 0.375))))
double code(double x) {
	return exp(x) / (exp(x) - 1.0);
}
double code(double x) {
	double tmp;
	if (exp(x) <= 1e+299) {
		tmp = exp(x) / expm1(x);
	} else {
		tmp = cbrt((((1.0 / x) + (1.5 / (x * x))) + 0.375));
	}
	return tmp;
}
public static double code(double x) {
	return Math.exp(x) / (Math.exp(x) - 1.0);
}
public static double code(double x) {
	double tmp;
	if (Math.exp(x) <= 1e+299) {
		tmp = Math.exp(x) / Math.expm1(x);
	} else {
		tmp = Math.cbrt((((1.0 / x) + (1.5 / (x * x))) + 0.375));
	}
	return tmp;
}
function code(x)
	return Float64(exp(x) / Float64(exp(x) - 1.0))
end
function code(x)
	tmp = 0.0
	if (exp(x) <= 1e+299)
		tmp = Float64(exp(x) / expm1(x));
	else
		tmp = cbrt(Float64(Float64(Float64(1.0 / x) + Float64(1.5 / Float64(x * x))) + 0.375));
	end
	return tmp
end
code[x_] := N[(N[Exp[x], $MachinePrecision] / N[(N[Exp[x], $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision]
code[x_] := If[LessEqual[N[Exp[x], $MachinePrecision], 1e+299], N[(N[Exp[x], $MachinePrecision] / N[(Exp[x] - 1), $MachinePrecision]), $MachinePrecision], N[Power[N[(N[(N[(1.0 / x), $MachinePrecision] + N[(1.5 / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + 0.375), $MachinePrecision], 1/3], $MachinePrecision]]
\frac{e^{x}}{e^{x} - 1}
\begin{array}{l}
\mathbf{if}\;e^{x} \leq 10^{+299}:\\
\;\;\;\;\frac{e^{x}}{\mathsf{expm1}\left(x\right)}\\

\mathbf{else}:\\
\;\;\;\;\sqrt[3]{\left(\frac{1}{x} + \frac{1.5}{x \cdot x}\right) + 0.375}\\


\end{array}

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Herbie found 3 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original28.7%
Target53.9%
Herbie79.9%
\[\frac{1}{1 - e^{-x}} \]

Derivation?

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

    1. Initial program 34.6%

      \[\frac{e^{x}}{e^{x} - 1} \]
    2. Simplified100.0%

      \[\leadsto \color{blue}{\frac{e^{x}}{\mathsf{expm1}\left(x\right)}} \]
      Step-by-step derivation

      [Start]34.6%

      \[ \frac{e^{x}}{e^{x} - 1} \]

      expm1-def [=>]100.0%

      \[ \frac{e^{x}}{\color{blue}{\mathsf{expm1}\left(x\right)}} \]

    if 1.0000000000000001e299 < (exp.f64 x)

    1. Initial program 0.0%

      \[\frac{e^{x}}{e^{x} - 1} \]
    2. Simplified0.0%

      \[\leadsto \color{blue}{\frac{e^{x}}{\mathsf{expm1}\left(x\right)}} \]
      Step-by-step derivation

      [Start]0.0%

      \[ \frac{e^{x}}{e^{x} - 1} \]

      expm1-def [=>]0.0%

      \[ \frac{e^{x}}{\color{blue}{\mathsf{expm1}\left(x\right)}} \]
    3. Applied egg-rr0.0%

      \[\leadsto \color{blue}{\sqrt[3]{{\left(\frac{e^{x}}{\mathsf{expm1}\left(x\right)}\right)}^{3}}} \]
      Step-by-step derivation

      [Start]0.0%

      \[ \frac{e^{x}}{\mathsf{expm1}\left(x\right)} \]

      add-cbrt-cube [=>]0.0%

      \[ \color{blue}{\sqrt[3]{\left(\frac{e^{x}}{\mathsf{expm1}\left(x\right)} \cdot \frac{e^{x}}{\mathsf{expm1}\left(x\right)}\right) \cdot \frac{e^{x}}{\mathsf{expm1}\left(x\right)}}} \]

      pow3 [=>]0.0%

      \[ \sqrt[3]{\color{blue}{{\left(\frac{e^{x}}{\mathsf{expm1}\left(x\right)}\right)}^{3}}} \]
    4. Taylor expanded in x around 0 20.1%

      \[\leadsto \sqrt[3]{\color{blue}{0.375 + \left(1.5 \cdot \frac{1}{{x}^{2}} + \left(\frac{1}{x} + \frac{1}{{x}^{3}}\right)\right)}} \]
    5. Simplified20.1%

      \[\leadsto \sqrt[3]{\color{blue}{\left(\frac{1.5}{x \cdot x} + \left(\frac{1}{x} + \frac{1}{{x}^{3}}\right)\right) + 0.375}} \]
      Step-by-step derivation

      [Start]20.1%

      \[ \sqrt[3]{0.375 + \left(1.5 \cdot \frac{1}{{x}^{2}} + \left(\frac{1}{x} + \frac{1}{{x}^{3}}\right)\right)} \]

      +-commutative [=>]20.1%

      \[ \sqrt[3]{\color{blue}{\left(1.5 \cdot \frac{1}{{x}^{2}} + \left(\frac{1}{x} + \frac{1}{{x}^{3}}\right)\right) + 0.375}} \]

      unpow2 [=>]20.1%

      \[ \sqrt[3]{\left(1.5 \cdot \frac{1}{\color{blue}{x \cdot x}} + \left(\frac{1}{x} + \frac{1}{{x}^{3}}\right)\right) + 0.375} \]

      associate-*r/ [=>]20.1%

      \[ \sqrt[3]{\left(\color{blue}{\frac{1.5 \cdot 1}{x \cdot x}} + \left(\frac{1}{x} + \frac{1}{{x}^{3}}\right)\right) + 0.375} \]

      metadata-eval [=>]20.1%

      \[ \sqrt[3]{\left(\frac{\color{blue}{1.5}}{x \cdot x} + \left(\frac{1}{x} + \frac{1}{{x}^{3}}\right)\right) + 0.375} \]
    6. Taylor expanded in x around inf 20.1%

      \[\leadsto \sqrt[3]{\color{blue}{\left(1.5 \cdot \frac{1}{{x}^{2}} + \frac{1}{x}\right)} + 0.375} \]
    7. Simplified20.1%

      \[\leadsto \sqrt[3]{\color{blue}{\left(\frac{1}{x} + \frac{1.5}{x \cdot x}\right)} + 0.375} \]
      Step-by-step derivation

      [Start]20.1%

      \[ \sqrt[3]{\left(1.5 \cdot \frac{1}{{x}^{2}} + \frac{1}{x}\right) + 0.375} \]

      +-commutative [=>]20.1%

      \[ \sqrt[3]{\color{blue}{\left(\frac{1}{x} + 1.5 \cdot \frac{1}{{x}^{2}}\right)} + 0.375} \]

      unpow2 [=>]20.1%

      \[ \sqrt[3]{\left(\frac{1}{x} + 1.5 \cdot \frac{1}{\color{blue}{x \cdot x}}\right) + 0.375} \]

      associate-*r/ [=>]20.1%

      \[ \sqrt[3]{\left(\frac{1}{x} + \color{blue}{\frac{1.5 \cdot 1}{x \cdot x}}\right) + 0.375} \]

      metadata-eval [=>]20.1%

      \[ \sqrt[3]{\left(\frac{1}{x} + \frac{\color{blue}{1.5}}{x \cdot x}\right) + 0.375} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification79.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{x} \leq 10^{+299}:\\ \;\;\;\;\frac{e^{x}}{\mathsf{expm1}\left(x\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt[3]{\left(\frac{1}{x} + \frac{1.5}{x \cdot x}\right) + 0.375}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy79.9%
Cost19524
\[\begin{array}{l} \mathbf{if}\;e^{x} \leq 10^{+299}:\\ \;\;\;\;\frac{e^{x}}{\mathsf{expm1}\left(x\right)}\\ \mathbf{else}:\\ \;\;\;\;\sqrt[3]{\left(\frac{1}{x} + \frac{1.5}{x \cdot x}\right) + 0.375}\\ \end{array} \]
Alternative 2
Accuracy54.3%
Cost320
\[\frac{1}{x} + 0.5 \]
Alternative 3
Accuracy50.9%
Cost192
\[\frac{1}{x} \]

Reproduce?

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

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

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