exp neg sub

Percentage Accurate: 100.0% → 100.0%
Time: 3.5s
Alternatives: 6
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 6 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, 0.5× speedup?

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

\\
{\left(e^{x + -1}\right)}^{\left(x + 1\right)}
\end{array}
Derivation
  1. Initial program 99.9%

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

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

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

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

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

    \[\leadsto \color{blue}{e^{x \cdot x + -1}} \]
  4. Step-by-step derivation
    1. difference-of-sqr--199.9%

      \[\leadsto e^{\color{blue}{\left(x + 1\right) \cdot \left(x - 1\right)}} \]
    2. sub-neg99.9%

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

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

    \[\leadsto e^{\color{blue}{\left(x + 1\right) \cdot \left(x + -1\right)}} \]
  6. Step-by-step derivation
    1. *-commutative99.9%

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

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

    \[\leadsto \color{blue}{{\left(e^{x + -1}\right)}^{\left(x + 1\right)}} \]
  8. Final simplification100.0%

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

Alternative 2: 99.3% accurate, 1.0× speedup?

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

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

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


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

    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.8%

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

    if 0.93999999999999995 < (*.f64 x x)

    1. Initial program 99.9%

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

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

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

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

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

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

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

        \[\leadsto e^{\color{blue}{x \cdot x}} \]
    6. Simplified99.4%

      \[\leadsto e^{\color{blue}{x \cdot x}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.1%

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

Alternative 3: 100.0% accurate, 1.0× speedup?

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

\\
e^{\left(x + -1\right) \cdot \left(x + 1\right)}
\end{array}
Derivation
  1. Initial program 99.9%

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

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

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

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

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

    \[\leadsto \color{blue}{e^{x \cdot x + -1}} \]
  4. Step-by-step derivation
    1. difference-of-sqr--199.9%

      \[\leadsto e^{\color{blue}{\left(x + 1\right) \cdot \left(x - 1\right)}} \]
    2. sub-neg99.9%

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

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

    \[\leadsto e^{\color{blue}{\left(x + 1\right) \cdot \left(x + -1\right)}} \]
  6. Final simplification99.9%

    \[\leadsto e^{\left(x + -1\right) \cdot \left(x + 1\right)} \]

Alternative 4: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ e^{-1 + x \cdot x} \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(-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^{-1 + x \cdot x}
\end{array}
Derivation
  1. Initial program 99.9%

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

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

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

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

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

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

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

Alternative 5: 51.3% 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 99.9%

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

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

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

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

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

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

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

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

Alternative 6: 10.5% accurate, 106.0× speedup?

\[\begin{array}{l} \\ 1 \end{array} \]
(FPCore (x) :precision binary64 1.0)
double code(double x) {
	return 1.0;
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = 1.0d0
end function
public static double code(double x) {
	return 1.0;
}
def code(x):
	return 1.0
function code(x)
	return 1.0
end
function tmp = code(x)
	tmp = 1.0;
end
code[x_] := 1.0
\begin{array}{l}

\\
1
\end{array}
Derivation
  1. Initial program 99.9%

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

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

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

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

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

    \[\leadsto \color{blue}{e^{x \cdot x + -1}} \]
  4. Step-by-step derivation
    1. add-cube-cbrt99.9%

      \[\leadsto e^{\color{blue}{\left(\sqrt[3]{x \cdot x + -1} \cdot \sqrt[3]{x \cdot x + -1}\right) \cdot \sqrt[3]{x \cdot x + -1}}} \]
    2. exp-prod100.0%

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

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

      \[\leadsto {\left(e^{{\left(\sqrt[3]{\color{blue}{\mathsf{fma}\left(x, x, -1\right)}}\right)}^{2}}\right)}^{\left(\sqrt[3]{x \cdot x + -1}\right)} \]
    5. fma-def100.0%

      \[\leadsto {\left(e^{{\left(\sqrt[3]{\mathsf{fma}\left(x, x, -1\right)}\right)}^{2}}\right)}^{\left(\sqrt[3]{\color{blue}{\mathsf{fma}\left(x, x, -1\right)}}\right)} \]
  5. Applied egg-rr100.0%

    \[\leadsto \color{blue}{{\left(e^{{\left(\sqrt[3]{\mathsf{fma}\left(x, x, -1\right)}\right)}^{2}}\right)}^{\left(\sqrt[3]{\mathsf{fma}\left(x, x, -1\right)}\right)}} \]
  6. Taylor expanded in x around inf 10.4%

    \[\leadsto \color{blue}{1} \]
  7. Final simplification10.4%

    \[\leadsto 1 \]

Reproduce

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