exp neg sub

Percentage Accurate: 100.0% → 100.0%
Time: 3.3s
Alternatives: 3
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ e^{-\left(1 - x \cdot x\right)} \end{array} \]
(FPCore (x) :precision binary64 (exp (- (- 1.0 (* x x)))))
double code(double x) {
	return exp(-(1.0 - (x * x)));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = exp(-(1.0d0 - (x * x)))
end function
public static double code(double x) {
	return Math.exp(-(1.0 - (x * x)));
}
def code(x):
	return math.exp(-(1.0 - (x * x)))
function code(x)
	return exp(Float64(-Float64(1.0 - Float64(x * x))))
end
function tmp = code(x)
	tmp = exp(-(1.0 - (x * x)));
end
code[x_] := N[Exp[(-N[(1.0 - N[(x * x), $MachinePrecision]), $MachinePrecision])], $MachinePrecision]
\begin{array}{l}

\\
e^{-\left(1 - x \cdot x\right)}
\end{array}

Sampling outcomes in binary64 precision:

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.

Accuracy vs Speed?

Herbie found 3 alternatives:

AlternativeAccuracySpeedup
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.

Initial Program: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ e^{-\left(1 - x \cdot x\right)} \end{array} \]
(FPCore (x) :precision binary64 (exp (- (- 1.0 (* x x)))))
double code(double x) {
	return exp(-(1.0 - (x * x)));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = exp(-(1.0d0 - (x * x)))
end function
public static double code(double x) {
	return Math.exp(-(1.0 - (x * x)));
}
def code(x):
	return math.exp(-(1.0 - (x * x)))
function code(x)
	return exp(Float64(-Float64(1.0 - Float64(x * x))))
end
function tmp = code(x)
	tmp = exp(-(1.0 - (x * x)));
end
code[x_] := N[Exp[(-N[(1.0 - N[(x * x), $MachinePrecision]), $MachinePrecision])], $MachinePrecision]
\begin{array}{l}

\\
e^{-\left(1 - x \cdot x\right)}
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ e^{x \cdot x + -1} \end{array} \]
(FPCore (x) :precision binary64 (exp (+ (* x x) -1.0)))
double code(double x) {
	return exp(((x * x) + -1.0));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = exp(((x * x) + (-1.0d0)))
end function
public static double code(double x) {
	return Math.exp(((x * x) + -1.0));
}
def code(x):
	return math.exp(((x * x) + -1.0))
function code(x)
	return exp(Float64(Float64(x * x) + -1.0))
end
function tmp = code(x)
	tmp = exp(((x * x) + -1.0));
end
code[x_] := N[Exp[N[(N[(x * x), $MachinePrecision] + -1.0), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
e^{x \cdot x + -1}
\end{array}
Derivation
  1. Initial program 100.0%

    \[e^{-\left(1 - x \cdot x\right)} \]
  2. Step-by-step derivation
    1. neg-sub0100.0%

      \[\leadsto e^{\color{blue}{0 - \left(1 - x \cdot x\right)}} \]
    2. associate--r-100.0%

      \[\leadsto e^{\color{blue}{\left(0 - 1\right) + x \cdot x}} \]
    3. metadata-eval100.0%

      \[\leadsto e^{\color{blue}{-1} + x \cdot x} \]
    4. +-commutative100.0%

      \[\leadsto e^{\color{blue}{x \cdot x + -1}} \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{e^{x \cdot x + -1}} \]
  4. Final simplification100.0%

    \[\leadsto e^{x \cdot x + -1} \]

Alternative 2: 75.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \cdot x \leq 1:\\ \;\;\;\;e^{-1}\\ \mathbf{else}:\\ \;\;\;\;x \cdot \frac{x}{e}\\ \end{array} \end{array} \]
(FPCore (x) :precision binary64 (if (<= (* x x) 1.0) (exp -1.0) (* x (/ x E))))
double code(double x) {
	double tmp;
	if ((x * x) <= 1.0) {
		tmp = exp(-1.0);
	} else {
		tmp = x * (x / ((double) M_E));
	}
	return tmp;
}
public static double code(double x) {
	double tmp;
	if ((x * x) <= 1.0) {
		tmp = Math.exp(-1.0);
	} else {
		tmp = x * (x / Math.E);
	}
	return tmp;
}
def code(x):
	tmp = 0
	if (x * x) <= 1.0:
		tmp = math.exp(-1.0)
	else:
		tmp = x * (x / math.e)
	return tmp
function code(x)
	tmp = 0.0
	if (Float64(x * x) <= 1.0)
		tmp = exp(-1.0);
	else
		tmp = Float64(x * Float64(x / exp(1)));
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if ((x * x) <= 1.0)
		tmp = exp(-1.0);
	else
		tmp = x * (x / 2.71828182845904523536);
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[N[(x * x), $MachinePrecision], 1.0], N[Exp[-1.0], $MachinePrecision], N[(x * N[(x / E), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 1:\\
\;\;\;\;e^{-1}\\

\mathbf{else}:\\
\;\;\;\;x \cdot \frac{x}{e}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 x x) < 1

    1. Initial program 100.0%

      \[e^{-\left(1 - x \cdot x\right)} \]
    2. Step-by-step derivation
      1. neg-sub0100.0%

        \[\leadsto e^{\color{blue}{0 - \left(1 - x \cdot x\right)}} \]
      2. associate--r-100.0%

        \[\leadsto e^{\color{blue}{\left(0 - 1\right) + x \cdot x}} \]
      3. metadata-eval100.0%

        \[\leadsto e^{\color{blue}{-1} + x \cdot x} \]
      4. +-commutative100.0%

        \[\leadsto e^{\color{blue}{x \cdot x + -1}} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{e^{x \cdot x + -1}} \]
    4. Taylor expanded in x around 0 98.5%

      \[\leadsto \color{blue}{e^{-1}} \]

    if 1 < (*.f64 x x)

    1. Initial program 100.0%

      \[e^{-\left(1 - x \cdot x\right)} \]
    2. Step-by-step derivation
      1. neg-sub0100.0%

        \[\leadsto e^{\color{blue}{0 - \left(1 - x \cdot x\right)}} \]
      2. associate--r-100.0%

        \[\leadsto e^{\color{blue}{\left(0 - 1\right) + x \cdot x}} \]
      3. metadata-eval100.0%

        \[\leadsto e^{\color{blue}{-1} + x \cdot x} \]
      4. +-commutative100.0%

        \[\leadsto e^{\color{blue}{x \cdot x + -1}} \]
    3. Simplified100.0%

      \[\leadsto \color{blue}{e^{x \cdot x + -1}} \]
    4. Step-by-step derivation
      1. exp-sum100.0%

        \[\leadsto \color{blue}{e^{x \cdot x} \cdot e^{-1}} \]
      2. exp-prod100.0%

        \[\leadsto \color{blue}{{\left(e^{x}\right)}^{x}} \cdot e^{-1} \]
    5. Applied egg-rr100.0%

      \[\leadsto \color{blue}{{\left(e^{x}\right)}^{x} \cdot e^{-1}} \]
    6. Taylor expanded in x around 0 54.4%

      \[\leadsto \color{blue}{e^{-1} \cdot {x}^{2} + e^{-1}} \]
    7. Step-by-step derivation
      1. *-commutative54.4%

        \[\leadsto \color{blue}{{x}^{2} \cdot e^{-1}} + e^{-1} \]
      2. distribute-lft1-in54.4%

        \[\leadsto \color{blue}{\left({x}^{2} + 1\right) \cdot e^{-1}} \]
      3. metadata-eval54.4%

        \[\leadsto \left({x}^{2} + 1\right) \cdot e^{\color{blue}{-1}} \]
      4. rec-exp54.4%

        \[\leadsto \left({x}^{2} + 1\right) \cdot \color{blue}{\frac{1}{e^{1}}} \]
      5. e-exp-154.4%

        \[\leadsto \left({x}^{2} + 1\right) \cdot \frac{1}{\color{blue}{e}} \]
      6. associate-*r/54.4%

        \[\leadsto \color{blue}{\frac{\left({x}^{2} + 1\right) \cdot 1}{e}} \]
      7. distribute-lft1-in54.4%

        \[\leadsto \frac{\color{blue}{{x}^{2} \cdot 1 + 1}}{e} \]
      8. *-rgt-identity54.4%

        \[\leadsto \frac{\color{blue}{{x}^{2}} + 1}{e} \]
      9. unpow254.4%

        \[\leadsto \frac{\color{blue}{x \cdot x} + 1}{e} \]
    8. Simplified54.4%

      \[\leadsto \color{blue}{\frac{x \cdot x + 1}{e}} \]
    9. Taylor expanded in x around inf 54.4%

      \[\leadsto \color{blue}{\frac{{x}^{2}}{e}} \]
    10. Step-by-step derivation
      1. unpow254.4%

        \[\leadsto \frac{\color{blue}{x \cdot x}}{e} \]
      2. *-rgt-identity54.4%

        \[\leadsto \frac{\color{blue}{\left(x \cdot x\right) \cdot 1}}{e} \]
      3. associate-*r/54.4%

        \[\leadsto \color{blue}{\left(x \cdot x\right) \cdot \frac{1}{e}} \]
      4. e-exp-154.4%

        \[\leadsto \left(x \cdot x\right) \cdot \frac{1}{\color{blue}{e^{1}}} \]
      5. rec-exp54.4%

        \[\leadsto \left(x \cdot x\right) \cdot \color{blue}{e^{-1}} \]
      6. metadata-eval54.4%

        \[\leadsto \left(x \cdot x\right) \cdot e^{\color{blue}{-1}} \]
      7. associate-*l*54.4%

        \[\leadsto \color{blue}{x \cdot \left(x \cdot e^{-1}\right)} \]
      8. metadata-eval54.4%

        \[\leadsto x \cdot \left(x \cdot e^{\color{blue}{-1}}\right) \]
      9. rec-exp54.4%

        \[\leadsto x \cdot \left(x \cdot \color{blue}{\frac{1}{e^{1}}}\right) \]
      10. e-exp-154.4%

        \[\leadsto x \cdot \left(x \cdot \frac{1}{\color{blue}{e}}\right) \]
      11. associate-*r/54.4%

        \[\leadsto x \cdot \color{blue}{\frac{x \cdot 1}{e}} \]
      12. *-rgt-identity54.4%

        \[\leadsto x \cdot \frac{\color{blue}{x}}{e} \]
    11. Simplified54.4%

      \[\leadsto \color{blue}{x \cdot \frac{x}{e}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification75.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot x \leq 1:\\ \;\;\;\;e^{-1}\\ \mathbf{else}:\\ \;\;\;\;x \cdot \frac{x}{e}\\ \end{array} \]

Alternative 3: 51.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ e^{-1} \end{array} \]
(FPCore (x) :precision binary64 (exp -1.0))
double code(double x) {
	return exp(-1.0);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = exp((-1.0d0))
end function
public static double code(double x) {
	return Math.exp(-1.0);
}
def code(x):
	return math.exp(-1.0)
function code(x)
	return exp(-1.0)
end
function tmp = code(x)
	tmp = exp(-1.0);
end
code[x_] := N[Exp[-1.0], $MachinePrecision]
\begin{array}{l}

\\
e^{-1}
\end{array}
Derivation
  1. Initial program 100.0%

    \[e^{-\left(1 - x \cdot x\right)} \]
  2. Step-by-step derivation
    1. neg-sub0100.0%

      \[\leadsto e^{\color{blue}{0 - \left(1 - x \cdot x\right)}} \]
    2. associate--r-100.0%

      \[\leadsto e^{\color{blue}{\left(0 - 1\right) + x \cdot x}} \]
    3. metadata-eval100.0%

      \[\leadsto e^{\color{blue}{-1} + x \cdot x} \]
    4. +-commutative100.0%

      \[\leadsto e^{\color{blue}{x \cdot x + -1}} \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{e^{x \cdot x + -1}} \]
  4. Taylor expanded in x around 0 49.0%

    \[\leadsto \color{blue}{e^{-1}} \]
  5. Final simplification49.0%

    \[\leadsto e^{-1} \]

Reproduce

?
herbie shell --seed 2023174 
(FPCore (x)
  :name "exp neg sub"
  :precision binary64
  (exp (- (- 1.0 (* x x)))))