exp2 (problem 3.3.7)

?

Percentage Accurate: 76.5% → 99.3%
Time: 6.5s
Precision: binary64
Cost: 26436

?

\[\left(e^{x} - 2\right) + e^{-x} \]
\[\begin{array}{l} t_0 := \left(e^{x} - 2\right) + e^{-x}\\ \mathbf{if}\;t_0 \leq 0:\\ \;\;\;\;x \cdot x\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
(FPCore (x) :precision binary64 (+ (- (exp x) 2.0) (exp (- x))))
(FPCore (x)
 :precision binary64
 (let* ((t_0 (+ (- (exp x) 2.0) (exp (- x))))) (if (<= t_0 0.0) (* x x) t_0)))
double code(double x) {
	return (exp(x) - 2.0) + exp(-x);
}
double code(double x) {
	double t_0 = (exp(x) - 2.0) + exp(-x);
	double tmp;
	if (t_0 <= 0.0) {
		tmp = x * x;
	} else {
		tmp = t_0;
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (exp(x) - 2.0d0) + exp(-x)
end function
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: t_0
    real(8) :: tmp
    t_0 = (exp(x) - 2.0d0) + exp(-x)
    if (t_0 <= 0.0d0) then
        tmp = x * x
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double x) {
	return (Math.exp(x) - 2.0) + Math.exp(-x);
}
public static double code(double x) {
	double t_0 = (Math.exp(x) - 2.0) + Math.exp(-x);
	double tmp;
	if (t_0 <= 0.0) {
		tmp = x * x;
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x):
	return (math.exp(x) - 2.0) + math.exp(-x)
def code(x):
	t_0 = (math.exp(x) - 2.0) + math.exp(-x)
	tmp = 0
	if t_0 <= 0.0:
		tmp = x * x
	else:
		tmp = t_0
	return tmp
function code(x)
	return Float64(Float64(exp(x) - 2.0) + exp(Float64(-x)))
end
function code(x)
	t_0 = Float64(Float64(exp(x) - 2.0) + exp(Float64(-x)))
	tmp = 0.0
	if (t_0 <= 0.0)
		tmp = Float64(x * x);
	else
		tmp = t_0;
	end
	return tmp
end
function tmp = code(x)
	tmp = (exp(x) - 2.0) + exp(-x);
end
function tmp_2 = code(x)
	t_0 = (exp(x) - 2.0) + exp(-x);
	tmp = 0.0;
	if (t_0 <= 0.0)
		tmp = x * x;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_] := N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]
code[x_] := Block[{t$95$0 = N[(N[(N[Exp[x], $MachinePrecision] - 2.0), $MachinePrecision] + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 0.0], N[(x * x), $MachinePrecision], t$95$0]]
\left(e^{x} - 2\right) + e^{-x}
\begin{array}{l}
t_0 := \left(e^{x} - 2\right) + e^{-x}\\
\mathbf{if}\;t_0 \leq 0:\\
\;\;\;\;x \cdot x\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}

Local Percentage Accuracy?

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 9 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original76.5%
Target100.0%
Herbie99.3%
\[4 \cdot {\sinh \left(\frac{x}{2}\right)}^{2} \]

Derivation?

  1. Split input into 2 regimes
  2. if (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x))) < 0.0

    1. Initial program 54.4%

      \[\left(e^{x} - 2\right) + e^{-x} \]
    2. Simplified54.4%

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

      [Start]54.4

      \[ \left(e^{x} - 2\right) + e^{-x} \]

      associate-+l- [=>]54.4

      \[ \color{blue}{e^{x} - \left(2 - e^{-x}\right)} \]

      sub-neg [=>]54.4

      \[ \color{blue}{e^{x} + \left(-\left(2 - e^{-x}\right)\right)} \]

      sub-neg [=>]54.4

      \[ e^{x} + \left(-\color{blue}{\left(2 + \left(-e^{-x}\right)\right)}\right) \]

      +-commutative [=>]54.4

      \[ e^{x} + \left(-\color{blue}{\left(\left(-e^{-x}\right) + 2\right)}\right) \]

      distribute-neg-in [=>]54.4

      \[ e^{x} + \color{blue}{\left(\left(-\left(-e^{-x}\right)\right) + \left(-2\right)\right)} \]

      remove-double-neg [=>]54.4

      \[ e^{x} + \left(\color{blue}{e^{-x}} + \left(-2\right)\right) \]

      metadata-eval [=>]54.4

      \[ e^{x} + \left(e^{-x} + \color{blue}{-2}\right) \]
    3. Taylor expanded in x around 0 100.0%

      \[\leadsto \color{blue}{{x}^{2}} \]
    4. Simplified100.0%

      \[\leadsto \color{blue}{x \cdot x} \]
      Step-by-step derivation

      [Start]100.0

      \[ {x}^{2} \]

      unpow2 [=>]100.0

      \[ \color{blue}{x \cdot x} \]

    if 0.0 < (+.f64 (-.f64 (exp.f64 x) 2) (exp.f64 (neg.f64 x)))

    1. Initial program 100.0%

      \[\left(e^{x} - 2\right) + e^{-x} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification100.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(e^{x} - 2\right) + e^{-x} \leq 0:\\ \;\;\;\;x \cdot x\\ \mathbf{else}:\\ \;\;\;\;\left(e^{x} - 2\right) + e^{-x}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy99.6%
Cost13448
\[\begin{array}{l} \mathbf{if}\;x \leq -1.65:\\ \;\;\;\;\mathsf{expm1}\left(-x\right)\\ \mathbf{elif}\;x \leq 0.00018:\\ \;\;\;\;x \cdot x\\ \mathbf{else}:\\ \;\;\;\;e^{x} + \left(e^{-x} + -2\right)\\ \end{array} \]
Alternative 2
Accuracy99.5%
Cost7432
\[\begin{array}{l} \mathbf{if}\;x \leq -2.6:\\ \;\;\;\;\mathsf{expm1}\left(-x\right)\\ \mathbf{elif}\;x \leq 2.6:\\ \;\;\;\;x \cdot x + \left(\left(1 + 0.08333333333333333 \cdot {x}^{4}\right) + -1\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(x\right)\\ \end{array} \]
Alternative 3
Accuracy99.6%
Cost6728
\[\begin{array}{l} \mathbf{if}\;x \leq -2.6:\\ \;\;\;\;\mathsf{expm1}\left(-x\right)\\ \mathbf{elif}\;x \leq 2.6:\\ \;\;\;\;x \cdot x + \left(x \cdot x\right) \cdot \left(\left(x \cdot x\right) \cdot 0.08333333333333333\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(x\right)\\ \end{array} \]
Alternative 4
Accuracy94.3%
Cost6596
\[\begin{array}{l} \mathbf{if}\;x \leq 2.6:\\ \;\;\;\;x \cdot x + \left(x \cdot x\right) \cdot \left(\left(x \cdot x\right) \cdot 0.08333333333333333\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(x\right)\\ \end{array} \]
Alternative 5
Accuracy80.5%
Cost836
\[\begin{array}{l} \mathbf{if}\;x \leq -1.5:\\ \;\;\;\;\left(x \cdot x\right) \cdot \left(0.5 + x \cdot -0.16666666666666666\right) - x\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \]
Alternative 6
Accuracy88.8%
Cost832
\[x \cdot x + \left(x \cdot x\right) \cdot \left(\left(x \cdot x\right) \cdot 0.08333333333333333\right) \]
Alternative 7
Accuracy76.8%
Cost192
\[x \cdot x \]
Alternative 8
Accuracy27.2%
Cost64
\[0 \]

Reproduce?

herbie shell --seed 2023161 
(FPCore (x)
  :name "exp2 (problem 3.3.7)"
  :precision binary64

  :herbie-target
  (* 4.0 (pow (sinh (/ x 2.0)) 2.0))

  (+ (- (exp x) 2.0) (exp (- x))))