ENA, Section 1.4, Exercise 4b, n=2

Percentage Accurate: 74.6% → 100.0%
Time: 6.5s
Alternatives: 6
Speedup: 20.9×

Specification

?
\[\left(-1000000000 \leq x \land x \leq 1000000000\right) \land \left(-1 \leq \varepsilon \land \varepsilon \leq 1\right)\]
\[\begin{array}{l} \\ {\left(x + \varepsilon\right)}^{2} - {x}^{2} \end{array} \]
(FPCore (x eps) :precision binary64 (- (pow (+ x eps) 2.0) (pow x 2.0)))
double code(double x, double eps) {
	return pow((x + eps), 2.0) - pow(x, 2.0);
}
real(8) function code(x, eps)
    real(8), intent (in) :: x
    real(8), intent (in) :: eps
    code = ((x + eps) ** 2.0d0) - (x ** 2.0d0)
end function
public static double code(double x, double eps) {
	return Math.pow((x + eps), 2.0) - Math.pow(x, 2.0);
}
def code(x, eps):
	return math.pow((x + eps), 2.0) - math.pow(x, 2.0)
function code(x, eps)
	return Float64((Float64(x + eps) ^ 2.0) - (x ^ 2.0))
end
function tmp = code(x, eps)
	tmp = ((x + eps) ^ 2.0) - (x ^ 2.0);
end
code[x_, eps_] := N[(N[Power[N[(x + eps), $MachinePrecision], 2.0], $MachinePrecision] - N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
{\left(x + \varepsilon\right)}^{2} - {x}^{2}
\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: 74.6% accurate, 1.0× speedup?

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

\\
{\left(x + \varepsilon\right)}^{2} - {x}^{2}
\end{array}

Alternative 1: 100.0% accurate, 13.9× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(x, \varepsilon, \left(\varepsilon + x\right) \cdot \varepsilon\right) \end{array} \]
(FPCore (x eps) :precision binary64 (fma x eps (* (+ eps x) eps)))
double code(double x, double eps) {
	return fma(x, eps, ((eps + x) * eps));
}
function code(x, eps)
	return fma(x, eps, Float64(Float64(eps + x) * eps))
end
code[x_, eps_] := N[(x * eps + N[(N[(eps + x), $MachinePrecision] * eps), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(x, \varepsilon, \left(\varepsilon + x\right) \cdot \varepsilon\right)
\end{array}
Derivation
  1. Initial program 75.3%

    \[{\left(x + \varepsilon\right)}^{2} - {x}^{2} \]
  2. Add Preprocessing
  3. Taylor expanded in eps around 0

    \[\leadsto \color{blue}{\varepsilon \cdot \left(\varepsilon + 2 \cdot x\right)} \]
  4. Step-by-step derivation
    1. distribute-rgt-inN/A

      \[\leadsto \color{blue}{\varepsilon \cdot \varepsilon + \left(2 \cdot x\right) \cdot \varepsilon} \]
    2. unpow2N/A

      \[\leadsto \color{blue}{{\varepsilon}^{2}} + \left(2 \cdot x\right) \cdot \varepsilon \]
    3. *-rgt-identityN/A

      \[\leadsto \color{blue}{{\varepsilon}^{2} \cdot 1} + \left(2 \cdot x\right) \cdot \varepsilon \]
    4. *-rgt-identityN/A

      \[\leadsto {\varepsilon}^{2} \cdot 1 + \left(2 \cdot x\right) \cdot \color{blue}{\left(\varepsilon \cdot 1\right)} \]
    5. *-inversesN/A

      \[\leadsto {\varepsilon}^{2} \cdot 1 + \left(2 \cdot x\right) \cdot \left(\varepsilon \cdot \color{blue}{\frac{\varepsilon}{\varepsilon}}\right) \]
    6. associate-/l*N/A

      \[\leadsto {\varepsilon}^{2} \cdot 1 + \left(2 \cdot x\right) \cdot \color{blue}{\frac{\varepsilon \cdot \varepsilon}{\varepsilon}} \]
    7. unpow2N/A

      \[\leadsto {\varepsilon}^{2} \cdot 1 + \left(2 \cdot x\right) \cdot \frac{\color{blue}{{\varepsilon}^{2}}}{\varepsilon} \]
    8. associate-/l*N/A

      \[\leadsto {\varepsilon}^{2} \cdot 1 + \color{blue}{\frac{\left(2 \cdot x\right) \cdot {\varepsilon}^{2}}{\varepsilon}} \]
    9. associate-*l/N/A

      \[\leadsto {\varepsilon}^{2} \cdot 1 + \color{blue}{\frac{2 \cdot x}{\varepsilon} \cdot {\varepsilon}^{2}} \]
    10. associate-*r/N/A

      \[\leadsto {\varepsilon}^{2} \cdot 1 + \color{blue}{\left(2 \cdot \frac{x}{\varepsilon}\right)} \cdot {\varepsilon}^{2} \]
    11. *-commutativeN/A

      \[\leadsto {\varepsilon}^{2} \cdot 1 + \color{blue}{{\varepsilon}^{2} \cdot \left(2 \cdot \frac{x}{\varepsilon}\right)} \]
    12. distribute-lft-inN/A

      \[\leadsto \color{blue}{{\varepsilon}^{2} \cdot \left(1 + 2 \cdot \frac{x}{\varepsilon}\right)} \]
    13. unpow2N/A

      \[\leadsto \color{blue}{\left(\varepsilon \cdot \varepsilon\right)} \cdot \left(1 + 2 \cdot \frac{x}{\varepsilon}\right) \]
    14. associate-*l*N/A

      \[\leadsto \color{blue}{\varepsilon \cdot \left(\varepsilon \cdot \left(1 + 2 \cdot \frac{x}{\varepsilon}\right)\right)} \]
    15. *-commutativeN/A

      \[\leadsto \color{blue}{\left(\varepsilon \cdot \left(1 + 2 \cdot \frac{x}{\varepsilon}\right)\right) \cdot \varepsilon} \]
    16. lower-*.f64N/A

      \[\leadsto \color{blue}{\left(\varepsilon \cdot \left(1 + 2 \cdot \frac{x}{\varepsilon}\right)\right) \cdot \varepsilon} \]
  5. Applied rewrites100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(2, x, \varepsilon\right) \cdot \varepsilon} \]
  6. Step-by-step derivation
    1. Applied rewrites100.0%

      \[\leadsto \left(\left(\varepsilon + x\right) + x\right) \cdot \varepsilon \]
    2. Step-by-step derivation
      1. Applied rewrites100.0%

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\varepsilon}, \left(\varepsilon + x\right) \cdot \varepsilon\right) \]
      2. Add Preprocessing

      Alternative 2: 97.4% accurate, 0.9× speedup?

      \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;{\left(\varepsilon + x\right)}^{2} - {x}^{2} \leq 0:\\ \;\;\;\;\left(2 \cdot \varepsilon\right) \cdot x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\varepsilon + x, \varepsilon, 0\right)\\ \end{array} \end{array} \]
      (FPCore (x eps)
       :precision binary64
       (if (<= (- (pow (+ eps x) 2.0) (pow x 2.0)) 0.0)
         (* (* 2.0 eps) x)
         (fma (+ eps x) eps 0.0)))
      double code(double x, double eps) {
      	double tmp;
      	if ((pow((eps + x), 2.0) - pow(x, 2.0)) <= 0.0) {
      		tmp = (2.0 * eps) * x;
      	} else {
      		tmp = fma((eps + x), eps, 0.0);
      	}
      	return tmp;
      }
      
      function code(x, eps)
      	tmp = 0.0
      	if (Float64((Float64(eps + x) ^ 2.0) - (x ^ 2.0)) <= 0.0)
      		tmp = Float64(Float64(2.0 * eps) * x);
      	else
      		tmp = fma(Float64(eps + x), eps, 0.0);
      	end
      	return tmp
      end
      
      code[x_, eps_] := If[LessEqual[N[(N[Power[N[(eps + x), $MachinePrecision], 2.0], $MachinePrecision] - N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision], 0.0], N[(N[(2.0 * eps), $MachinePrecision] * x), $MachinePrecision], N[(N[(eps + x), $MachinePrecision] * eps + 0.0), $MachinePrecision]]
      
      \begin{array}{l}
      
      \\
      \begin{array}{l}
      \mathbf{if}\;{\left(\varepsilon + x\right)}^{2} - {x}^{2} \leq 0:\\
      \;\;\;\;\left(2 \cdot \varepsilon\right) \cdot x\\
      
      \mathbf{else}:\\
      \;\;\;\;\mathsf{fma}\left(\varepsilon + x, \varepsilon, 0\right)\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if (-.f64 (pow.f64 (+.f64 x eps) #s(literal 2 binary64)) (pow.f64 x #s(literal 2 binary64))) < 0.0

        1. Initial program 60.2%

          \[{\left(x + \varepsilon\right)}^{2} - {x}^{2} \]
        2. Add Preprocessing
        3. Taylor expanded in eps around 0

          \[\leadsto \color{blue}{2 \cdot \left(\varepsilon \cdot x\right)} \]
        4. Step-by-step derivation
          1. *-commutativeN/A

            \[\leadsto \color{blue}{\left(\varepsilon \cdot x\right) \cdot 2} \]
          2. lower-*.f64N/A

            \[\leadsto \color{blue}{\left(\varepsilon \cdot x\right) \cdot 2} \]
          3. lower-*.f6499.6

            \[\leadsto \color{blue}{\left(\varepsilon \cdot x\right)} \cdot 2 \]
        5. Applied rewrites99.6%

          \[\leadsto \color{blue}{\left(\varepsilon \cdot x\right) \cdot 2} \]
        6. Step-by-step derivation
          1. Applied rewrites99.6%

            \[\leadsto \left(\varepsilon \cdot 2\right) \cdot \color{blue}{x} \]

          if 0.0 < (-.f64 (pow.f64 (+.f64 x eps) #s(literal 2 binary64)) (pow.f64 x #s(literal 2 binary64)))

          1. Initial program 99.7%

            \[{\left(x + \varepsilon\right)}^{2} - {x}^{2} \]
          2. Add Preprocessing
          3. Step-by-step derivation
            1. lift--.f64N/A

              \[\leadsto \color{blue}{{\left(x + \varepsilon\right)}^{2} - {x}^{2}} \]
            2. sub-negN/A

              \[\leadsto \color{blue}{{\left(x + \varepsilon\right)}^{2} + \left(\mathsf{neg}\left({x}^{2}\right)\right)} \]
            3. lift-pow.f64N/A

              \[\leadsto \color{blue}{{\left(x + \varepsilon\right)}^{2}} + \left(\mathsf{neg}\left({x}^{2}\right)\right) \]
            4. unpow2N/A

              \[\leadsto \color{blue}{\left(x + \varepsilon\right) \cdot \left(x + \varepsilon\right)} + \left(\mathsf{neg}\left({x}^{2}\right)\right) \]
            5. lift-+.f64N/A

              \[\leadsto \left(x + \varepsilon\right) \cdot \color{blue}{\left(x + \varepsilon\right)} + \left(\mathsf{neg}\left({x}^{2}\right)\right) \]
            6. +-commutativeN/A

              \[\leadsto \left(x + \varepsilon\right) \cdot \color{blue}{\left(\varepsilon + x\right)} + \left(\mathsf{neg}\left({x}^{2}\right)\right) \]
            7. distribute-lft-inN/A

              \[\leadsto \color{blue}{\left(\left(x + \varepsilon\right) \cdot \varepsilon + \left(x + \varepsilon\right) \cdot x\right)} + \left(\mathsf{neg}\left({x}^{2}\right)\right) \]
            8. associate-+l+N/A

              \[\leadsto \color{blue}{\left(x + \varepsilon\right) \cdot \varepsilon + \left(\left(x + \varepsilon\right) \cdot x + \left(\mathsf{neg}\left({x}^{2}\right)\right)\right)} \]
            9. lower-fma.f64N/A

              \[\leadsto \color{blue}{\mathsf{fma}\left(x + \varepsilon, \varepsilon, \left(x + \varepsilon\right) \cdot x + \left(\mathsf{neg}\left({x}^{2}\right)\right)\right)} \]
            10. lift-+.f64N/A

              \[\leadsto \mathsf{fma}\left(\color{blue}{x + \varepsilon}, \varepsilon, \left(x + \varepsilon\right) \cdot x + \left(\mathsf{neg}\left({x}^{2}\right)\right)\right) \]
            11. +-commutativeN/A

              \[\leadsto \mathsf{fma}\left(\color{blue}{\varepsilon + x}, \varepsilon, \left(x + \varepsilon\right) \cdot x + \left(\mathsf{neg}\left({x}^{2}\right)\right)\right) \]
            12. lower-+.f64N/A

              \[\leadsto \mathsf{fma}\left(\color{blue}{\varepsilon + x}, \varepsilon, \left(x + \varepsilon\right) \cdot x + \left(\mathsf{neg}\left({x}^{2}\right)\right)\right) \]
            13. lower-fma.f64N/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \color{blue}{\mathsf{fma}\left(x + \varepsilon, x, \mathsf{neg}\left({x}^{2}\right)\right)}\right) \]
            14. lift-+.f64N/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\color{blue}{x + \varepsilon}, x, \mathsf{neg}\left({x}^{2}\right)\right)\right) \]
            15. +-commutativeN/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\color{blue}{\varepsilon + x}, x, \mathsf{neg}\left({x}^{2}\right)\right)\right) \]
            16. lower-+.f64N/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\color{blue}{\varepsilon + x}, x, \mathsf{neg}\left({x}^{2}\right)\right)\right) \]
            17. lift-pow.f64N/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\varepsilon + x, x, \mathsf{neg}\left(\color{blue}{{x}^{2}}\right)\right)\right) \]
            18. unpow2N/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\varepsilon + x, x, \mathsf{neg}\left(\color{blue}{x \cdot x}\right)\right)\right) \]
            19. distribute-lft-neg-inN/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\varepsilon + x, x, \color{blue}{\left(\mathsf{neg}\left(x\right)\right) \cdot x}\right)\right) \]
            20. lower-*.f64N/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\varepsilon + x, x, \color{blue}{\left(\mathsf{neg}\left(x\right)\right) \cdot x}\right)\right) \]
            21. lower-neg.f6499.7

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\varepsilon + x, x, \color{blue}{\left(-x\right)} \cdot x\right)\right) \]
          4. Applied rewrites99.7%

            \[\leadsto \color{blue}{\mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\varepsilon + x, x, \left(-x\right) \cdot x\right)\right)} \]
          5. Taylor expanded in eps around 0

            \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \color{blue}{-1 \cdot {x}^{2} + {x}^{2}}\right) \]
          6. Step-by-step derivation
            1. distribute-lft1-inN/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \color{blue}{\left(-1 + 1\right) \cdot {x}^{2}}\right) \]
            2. metadata-evalN/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \color{blue}{0} \cdot {x}^{2}\right) \]
            3. mul0-lft94.9

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \color{blue}{0}\right) \]
          7. Applied rewrites94.9%

            \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \color{blue}{0}\right) \]
        7. Recombined 2 regimes into one program.
        8. Final simplification97.8%

          \[\leadsto \begin{array}{l} \mathbf{if}\;{\left(\varepsilon + x\right)}^{2} - {x}^{2} \leq 0:\\ \;\;\;\;\left(2 \cdot \varepsilon\right) \cdot x\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\varepsilon + x, \varepsilon, 0\right)\\ \end{array} \]
        9. Add Preprocessing

        Alternative 3: 100.0% accurate, 17.4× speedup?

        \[\begin{array}{l} \\ \left(\left(\varepsilon + x\right) + x\right) \cdot \varepsilon \end{array} \]
        (FPCore (x eps) :precision binary64 (* (+ (+ eps x) x) eps))
        double code(double x, double eps) {
        	return ((eps + x) + x) * eps;
        }
        
        real(8) function code(x, eps)
            real(8), intent (in) :: x
            real(8), intent (in) :: eps
            code = ((eps + x) + x) * eps
        end function
        
        public static double code(double x, double eps) {
        	return ((eps + x) + x) * eps;
        }
        
        def code(x, eps):
        	return ((eps + x) + x) * eps
        
        function code(x, eps)
        	return Float64(Float64(Float64(eps + x) + x) * eps)
        end
        
        function tmp = code(x, eps)
        	tmp = ((eps + x) + x) * eps;
        end
        
        code[x_, eps_] := N[(N[(N[(eps + x), $MachinePrecision] + x), $MachinePrecision] * eps), $MachinePrecision]
        
        \begin{array}{l}
        
        \\
        \left(\left(\varepsilon + x\right) + x\right) \cdot \varepsilon
        \end{array}
        
        Derivation
        1. Initial program 75.3%

          \[{\left(x + \varepsilon\right)}^{2} - {x}^{2} \]
        2. Add Preprocessing
        3. Taylor expanded in eps around 0

          \[\leadsto \color{blue}{\varepsilon \cdot \left(\varepsilon + 2 \cdot x\right)} \]
        4. Step-by-step derivation
          1. distribute-rgt-inN/A

            \[\leadsto \color{blue}{\varepsilon \cdot \varepsilon + \left(2 \cdot x\right) \cdot \varepsilon} \]
          2. unpow2N/A

            \[\leadsto \color{blue}{{\varepsilon}^{2}} + \left(2 \cdot x\right) \cdot \varepsilon \]
          3. *-rgt-identityN/A

            \[\leadsto \color{blue}{{\varepsilon}^{2} \cdot 1} + \left(2 \cdot x\right) \cdot \varepsilon \]
          4. *-rgt-identityN/A

            \[\leadsto {\varepsilon}^{2} \cdot 1 + \left(2 \cdot x\right) \cdot \color{blue}{\left(\varepsilon \cdot 1\right)} \]
          5. *-inversesN/A

            \[\leadsto {\varepsilon}^{2} \cdot 1 + \left(2 \cdot x\right) \cdot \left(\varepsilon \cdot \color{blue}{\frac{\varepsilon}{\varepsilon}}\right) \]
          6. associate-/l*N/A

            \[\leadsto {\varepsilon}^{2} \cdot 1 + \left(2 \cdot x\right) \cdot \color{blue}{\frac{\varepsilon \cdot \varepsilon}{\varepsilon}} \]
          7. unpow2N/A

            \[\leadsto {\varepsilon}^{2} \cdot 1 + \left(2 \cdot x\right) \cdot \frac{\color{blue}{{\varepsilon}^{2}}}{\varepsilon} \]
          8. associate-/l*N/A

            \[\leadsto {\varepsilon}^{2} \cdot 1 + \color{blue}{\frac{\left(2 \cdot x\right) \cdot {\varepsilon}^{2}}{\varepsilon}} \]
          9. associate-*l/N/A

            \[\leadsto {\varepsilon}^{2} \cdot 1 + \color{blue}{\frac{2 \cdot x}{\varepsilon} \cdot {\varepsilon}^{2}} \]
          10. associate-*r/N/A

            \[\leadsto {\varepsilon}^{2} \cdot 1 + \color{blue}{\left(2 \cdot \frac{x}{\varepsilon}\right)} \cdot {\varepsilon}^{2} \]
          11. *-commutativeN/A

            \[\leadsto {\varepsilon}^{2} \cdot 1 + \color{blue}{{\varepsilon}^{2} \cdot \left(2 \cdot \frac{x}{\varepsilon}\right)} \]
          12. distribute-lft-inN/A

            \[\leadsto \color{blue}{{\varepsilon}^{2} \cdot \left(1 + 2 \cdot \frac{x}{\varepsilon}\right)} \]
          13. unpow2N/A

            \[\leadsto \color{blue}{\left(\varepsilon \cdot \varepsilon\right)} \cdot \left(1 + 2 \cdot \frac{x}{\varepsilon}\right) \]
          14. associate-*l*N/A

            \[\leadsto \color{blue}{\varepsilon \cdot \left(\varepsilon \cdot \left(1 + 2 \cdot \frac{x}{\varepsilon}\right)\right)} \]
          15. *-commutativeN/A

            \[\leadsto \color{blue}{\left(\varepsilon \cdot \left(1 + 2 \cdot \frac{x}{\varepsilon}\right)\right) \cdot \varepsilon} \]
          16. lower-*.f64N/A

            \[\leadsto \color{blue}{\left(\varepsilon \cdot \left(1 + 2 \cdot \frac{x}{\varepsilon}\right)\right) \cdot \varepsilon} \]
        5. Applied rewrites100.0%

          \[\leadsto \color{blue}{\mathsf{fma}\left(2, x, \varepsilon\right) \cdot \varepsilon} \]
        6. Step-by-step derivation
          1. Applied rewrites100.0%

            \[\leadsto \left(\left(\varepsilon + x\right) + x\right) \cdot \varepsilon \]
          2. Add Preprocessing

          Alternative 4: 75.6% accurate, 20.9× speedup?

          \[\begin{array}{l} \\ \mathsf{fma}\left(\varepsilon + x, \varepsilon, 0\right) \end{array} \]
          (FPCore (x eps) :precision binary64 (fma (+ eps x) eps 0.0))
          double code(double x, double eps) {
          	return fma((eps + x), eps, 0.0);
          }
          
          function code(x, eps)
          	return fma(Float64(eps + x), eps, 0.0)
          end
          
          code[x_, eps_] := N[(N[(eps + x), $MachinePrecision] * eps + 0.0), $MachinePrecision]
          
          \begin{array}{l}
          
          \\
          \mathsf{fma}\left(\varepsilon + x, \varepsilon, 0\right)
          \end{array}
          
          Derivation
          1. Initial program 75.3%

            \[{\left(x + \varepsilon\right)}^{2} - {x}^{2} \]
          2. Add Preprocessing
          3. Step-by-step derivation
            1. lift--.f64N/A

              \[\leadsto \color{blue}{{\left(x + \varepsilon\right)}^{2} - {x}^{2}} \]
            2. sub-negN/A

              \[\leadsto \color{blue}{{\left(x + \varepsilon\right)}^{2} + \left(\mathsf{neg}\left({x}^{2}\right)\right)} \]
            3. lift-pow.f64N/A

              \[\leadsto \color{blue}{{\left(x + \varepsilon\right)}^{2}} + \left(\mathsf{neg}\left({x}^{2}\right)\right) \]
            4. unpow2N/A

              \[\leadsto \color{blue}{\left(x + \varepsilon\right) \cdot \left(x + \varepsilon\right)} + \left(\mathsf{neg}\left({x}^{2}\right)\right) \]
            5. lift-+.f64N/A

              \[\leadsto \left(x + \varepsilon\right) \cdot \color{blue}{\left(x + \varepsilon\right)} + \left(\mathsf{neg}\left({x}^{2}\right)\right) \]
            6. +-commutativeN/A

              \[\leadsto \left(x + \varepsilon\right) \cdot \color{blue}{\left(\varepsilon + x\right)} + \left(\mathsf{neg}\left({x}^{2}\right)\right) \]
            7. distribute-lft-inN/A

              \[\leadsto \color{blue}{\left(\left(x + \varepsilon\right) \cdot \varepsilon + \left(x + \varepsilon\right) \cdot x\right)} + \left(\mathsf{neg}\left({x}^{2}\right)\right) \]
            8. associate-+l+N/A

              \[\leadsto \color{blue}{\left(x + \varepsilon\right) \cdot \varepsilon + \left(\left(x + \varepsilon\right) \cdot x + \left(\mathsf{neg}\left({x}^{2}\right)\right)\right)} \]
            9. lower-fma.f64N/A

              \[\leadsto \color{blue}{\mathsf{fma}\left(x + \varepsilon, \varepsilon, \left(x + \varepsilon\right) \cdot x + \left(\mathsf{neg}\left({x}^{2}\right)\right)\right)} \]
            10. lift-+.f64N/A

              \[\leadsto \mathsf{fma}\left(\color{blue}{x + \varepsilon}, \varepsilon, \left(x + \varepsilon\right) \cdot x + \left(\mathsf{neg}\left({x}^{2}\right)\right)\right) \]
            11. +-commutativeN/A

              \[\leadsto \mathsf{fma}\left(\color{blue}{\varepsilon + x}, \varepsilon, \left(x + \varepsilon\right) \cdot x + \left(\mathsf{neg}\left({x}^{2}\right)\right)\right) \]
            12. lower-+.f64N/A

              \[\leadsto \mathsf{fma}\left(\color{blue}{\varepsilon + x}, \varepsilon, \left(x + \varepsilon\right) \cdot x + \left(\mathsf{neg}\left({x}^{2}\right)\right)\right) \]
            13. lower-fma.f64N/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \color{blue}{\mathsf{fma}\left(x + \varepsilon, x, \mathsf{neg}\left({x}^{2}\right)\right)}\right) \]
            14. lift-+.f64N/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\color{blue}{x + \varepsilon}, x, \mathsf{neg}\left({x}^{2}\right)\right)\right) \]
            15. +-commutativeN/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\color{blue}{\varepsilon + x}, x, \mathsf{neg}\left({x}^{2}\right)\right)\right) \]
            16. lower-+.f64N/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\color{blue}{\varepsilon + x}, x, \mathsf{neg}\left({x}^{2}\right)\right)\right) \]
            17. lift-pow.f64N/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\varepsilon + x, x, \mathsf{neg}\left(\color{blue}{{x}^{2}}\right)\right)\right) \]
            18. unpow2N/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\varepsilon + x, x, \mathsf{neg}\left(\color{blue}{x \cdot x}\right)\right)\right) \]
            19. distribute-lft-neg-inN/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\varepsilon + x, x, \color{blue}{\left(\mathsf{neg}\left(x\right)\right) \cdot x}\right)\right) \]
            20. lower-*.f64N/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\varepsilon + x, x, \color{blue}{\left(\mathsf{neg}\left(x\right)\right) \cdot x}\right)\right) \]
            21. lower-neg.f6466.5

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\varepsilon + x, x, \color{blue}{\left(-x\right)} \cdot x\right)\right) \]
          4. Applied rewrites66.5%

            \[\leadsto \color{blue}{\mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\varepsilon + x, x, \left(-x\right) \cdot x\right)\right)} \]
          5. Taylor expanded in eps around 0

            \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \color{blue}{-1 \cdot {x}^{2} + {x}^{2}}\right) \]
          6. Step-by-step derivation
            1. distribute-lft1-inN/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \color{blue}{\left(-1 + 1\right) \cdot {x}^{2}}\right) \]
            2. metadata-evalN/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \color{blue}{0} \cdot {x}^{2}\right) \]
            3. mul0-lft76.3

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \color{blue}{0}\right) \]
          7. Applied rewrites76.3%

            \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \color{blue}{0}\right) \]
          8. Add Preprocessing

          Alternative 5: 72.2% accurate, 34.8× speedup?

          \[\begin{array}{l} \\ \varepsilon \cdot \varepsilon \end{array} \]
          (FPCore (x eps) :precision binary64 (* eps eps))
          double code(double x, double eps) {
          	return eps * eps;
          }
          
          real(8) function code(x, eps)
              real(8), intent (in) :: x
              real(8), intent (in) :: eps
              code = eps * eps
          end function
          
          public static double code(double x, double eps) {
          	return eps * eps;
          }
          
          def code(x, eps):
          	return eps * eps
          
          function code(x, eps)
          	return Float64(eps * eps)
          end
          
          function tmp = code(x, eps)
          	tmp = eps * eps;
          end
          
          code[x_, eps_] := N[(eps * eps), $MachinePrecision]
          
          \begin{array}{l}
          
          \\
          \varepsilon \cdot \varepsilon
          \end{array}
          
          Derivation
          1. Initial program 75.3%

            \[{\left(x + \varepsilon\right)}^{2} - {x}^{2} \]
          2. Add Preprocessing
          3. Taylor expanded in eps around inf

            \[\leadsto \color{blue}{{\varepsilon}^{2}} \]
          4. Step-by-step derivation
            1. unpow2N/A

              \[\leadsto \color{blue}{\varepsilon \cdot \varepsilon} \]
            2. lower-*.f6472.9

              \[\leadsto \color{blue}{\varepsilon \cdot \varepsilon} \]
          5. Applied rewrites72.9%

            \[\leadsto \color{blue}{\varepsilon \cdot \varepsilon} \]
          6. Add Preprocessing

          Alternative 6: 38.5% accurate, 209.0× speedup?

          \[\begin{array}{l} \\ 0 \end{array} \]
          (FPCore (x eps) :precision binary64 0.0)
          double code(double x, double eps) {
          	return 0.0;
          }
          
          real(8) function code(x, eps)
              real(8), intent (in) :: x
              real(8), intent (in) :: eps
              code = 0.0d0
          end function
          
          public static double code(double x, double eps) {
          	return 0.0;
          }
          
          def code(x, eps):
          	return 0.0
          
          function code(x, eps)
          	return 0.0
          end
          
          function tmp = code(x, eps)
          	tmp = 0.0;
          end
          
          code[x_, eps_] := 0.0
          
          \begin{array}{l}
          
          \\
          0
          \end{array}
          
          Derivation
          1. Initial program 75.3%

            \[{\left(x + \varepsilon\right)}^{2} - {x}^{2} \]
          2. Add Preprocessing
          3. Step-by-step derivation
            1. lift--.f64N/A

              \[\leadsto \color{blue}{{\left(x + \varepsilon\right)}^{2} - {x}^{2}} \]
            2. sub-negN/A

              \[\leadsto \color{blue}{{\left(x + \varepsilon\right)}^{2} + \left(\mathsf{neg}\left({x}^{2}\right)\right)} \]
            3. lift-pow.f64N/A

              \[\leadsto \color{blue}{{\left(x + \varepsilon\right)}^{2}} + \left(\mathsf{neg}\left({x}^{2}\right)\right) \]
            4. unpow2N/A

              \[\leadsto \color{blue}{\left(x + \varepsilon\right) \cdot \left(x + \varepsilon\right)} + \left(\mathsf{neg}\left({x}^{2}\right)\right) \]
            5. lift-+.f64N/A

              \[\leadsto \left(x + \varepsilon\right) \cdot \color{blue}{\left(x + \varepsilon\right)} + \left(\mathsf{neg}\left({x}^{2}\right)\right) \]
            6. +-commutativeN/A

              \[\leadsto \left(x + \varepsilon\right) \cdot \color{blue}{\left(\varepsilon + x\right)} + \left(\mathsf{neg}\left({x}^{2}\right)\right) \]
            7. distribute-lft-inN/A

              \[\leadsto \color{blue}{\left(\left(x + \varepsilon\right) \cdot \varepsilon + \left(x + \varepsilon\right) \cdot x\right)} + \left(\mathsf{neg}\left({x}^{2}\right)\right) \]
            8. associate-+l+N/A

              \[\leadsto \color{blue}{\left(x + \varepsilon\right) \cdot \varepsilon + \left(\left(x + \varepsilon\right) \cdot x + \left(\mathsf{neg}\left({x}^{2}\right)\right)\right)} \]
            9. lower-fma.f64N/A

              \[\leadsto \color{blue}{\mathsf{fma}\left(x + \varepsilon, \varepsilon, \left(x + \varepsilon\right) \cdot x + \left(\mathsf{neg}\left({x}^{2}\right)\right)\right)} \]
            10. lift-+.f64N/A

              \[\leadsto \mathsf{fma}\left(\color{blue}{x + \varepsilon}, \varepsilon, \left(x + \varepsilon\right) \cdot x + \left(\mathsf{neg}\left({x}^{2}\right)\right)\right) \]
            11. +-commutativeN/A

              \[\leadsto \mathsf{fma}\left(\color{blue}{\varepsilon + x}, \varepsilon, \left(x + \varepsilon\right) \cdot x + \left(\mathsf{neg}\left({x}^{2}\right)\right)\right) \]
            12. lower-+.f64N/A

              \[\leadsto \mathsf{fma}\left(\color{blue}{\varepsilon + x}, \varepsilon, \left(x + \varepsilon\right) \cdot x + \left(\mathsf{neg}\left({x}^{2}\right)\right)\right) \]
            13. lower-fma.f64N/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \color{blue}{\mathsf{fma}\left(x + \varepsilon, x, \mathsf{neg}\left({x}^{2}\right)\right)}\right) \]
            14. lift-+.f64N/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\color{blue}{x + \varepsilon}, x, \mathsf{neg}\left({x}^{2}\right)\right)\right) \]
            15. +-commutativeN/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\color{blue}{\varepsilon + x}, x, \mathsf{neg}\left({x}^{2}\right)\right)\right) \]
            16. lower-+.f64N/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\color{blue}{\varepsilon + x}, x, \mathsf{neg}\left({x}^{2}\right)\right)\right) \]
            17. lift-pow.f64N/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\varepsilon + x, x, \mathsf{neg}\left(\color{blue}{{x}^{2}}\right)\right)\right) \]
            18. unpow2N/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\varepsilon + x, x, \mathsf{neg}\left(\color{blue}{x \cdot x}\right)\right)\right) \]
            19. distribute-lft-neg-inN/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\varepsilon + x, x, \color{blue}{\left(\mathsf{neg}\left(x\right)\right) \cdot x}\right)\right) \]
            20. lower-*.f64N/A

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\varepsilon + x, x, \color{blue}{\left(\mathsf{neg}\left(x\right)\right) \cdot x}\right)\right) \]
            21. lower-neg.f6466.5

              \[\leadsto \mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\varepsilon + x, x, \color{blue}{\left(-x\right)} \cdot x\right)\right) \]
          4. Applied rewrites66.5%

            \[\leadsto \color{blue}{\mathsf{fma}\left(\varepsilon + x, \varepsilon, \mathsf{fma}\left(\varepsilon + x, x, \left(-x\right) \cdot x\right)\right)} \]
          5. Taylor expanded in eps around 0

            \[\leadsto \color{blue}{-1 \cdot {x}^{2} + {x}^{2}} \]
          6. Step-by-step derivation
            1. distribute-lft1-inN/A

              \[\leadsto \color{blue}{\left(-1 + 1\right) \cdot {x}^{2}} \]
            2. metadata-evalN/A

              \[\leadsto \color{blue}{0} \cdot {x}^{2} \]
            3. mul0-lft38.8

              \[\leadsto \color{blue}{0} \]
          7. Applied rewrites38.8%

            \[\leadsto \color{blue}{0} \]
          8. Add Preprocessing

          Reproduce

          ?
          herbie shell --seed 2024276 
          (FPCore (x eps)
            :name "ENA, Section 1.4, Exercise 4b, n=2"
            :precision binary64
            :pre (and (and (<= -1000000000.0 x) (<= x 1000000000.0)) (and (<= -1.0 eps) (<= eps 1.0)))
            (- (pow (+ x eps) 2.0) (pow x 2.0)))