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

Percentage Accurate: 74.7% → 100.0%
Time: 4.7s
Alternatives: 4
Speedup: 23.2×

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);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, eps)
use fmin_fmax_functions
    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 4 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.7% 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);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(x, eps)
use fmin_fmax_functions
    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, 17.4× speedup?

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

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

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

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

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

    Alternative 2: 97.3% accurate, 0.9× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;{\left(x + \varepsilon\right)}^{2} - {x}^{2} \leq 0:\\ \;\;\;\;\left(x + x\right) \cdot \varepsilon\\ \mathbf{else}:\\ \;\;\;\;\left(\varepsilon + x\right) \cdot \varepsilon\\ \end{array} \end{array} \]
    (FPCore (x eps)
     :precision binary64
     (if (<= (- (pow (+ x eps) 2.0) (pow x 2.0)) 0.0)
       (* (+ x x) eps)
       (* (+ eps x) eps)))
    double code(double x, double eps) {
    	double tmp;
    	if ((pow((x + eps), 2.0) - pow(x, 2.0)) <= 0.0) {
    		tmp = (x + x) * eps;
    	} else {
    		tmp = (eps + x) * eps;
    	}
    	return tmp;
    }
    
    module fmin_fmax_functions
        implicit none
        private
        public fmax
        public fmin
    
        interface fmax
            module procedure fmax88
            module procedure fmax44
            module procedure fmax84
            module procedure fmax48
        end interface
        interface fmin
            module procedure fmin88
            module procedure fmin44
            module procedure fmin84
            module procedure fmin48
        end interface
    contains
        real(8) function fmax88(x, y) result (res)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(x, max(x, y), y /= y), x /= x)
        end function
        real(4) function fmax44(x, y) result (res)
            real(4), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(y, merge(x, max(x, y), y /= y), x /= x)
        end function
        real(8) function fmax84(x, y) result(res)
            real(8), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
        end function
        real(8) function fmax48(x, y) result(res)
            real(4), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
        end function
        real(8) function fmin88(x, y) result (res)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(x, min(x, y), y /= y), x /= x)
        end function
        real(4) function fmin44(x, y) result (res)
            real(4), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(y, merge(x, min(x, y), y /= y), x /= x)
        end function
        real(8) function fmin84(x, y) result(res)
            real(8), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
        end function
        real(8) function fmin48(x, y) result(res)
            real(4), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
        end function
    end module
    
    real(8) function code(x, eps)
    use fmin_fmax_functions
        real(8), intent (in) :: x
        real(8), intent (in) :: eps
        real(8) :: tmp
        if ((((x + eps) ** 2.0d0) - (x ** 2.0d0)) <= 0.0d0) then
            tmp = (x + x) * eps
        else
            tmp = (eps + x) * eps
        end if
        code = tmp
    end function
    
    public static double code(double x, double eps) {
    	double tmp;
    	if ((Math.pow((x + eps), 2.0) - Math.pow(x, 2.0)) <= 0.0) {
    		tmp = (x + x) * eps;
    	} else {
    		tmp = (eps + x) * eps;
    	}
    	return tmp;
    }
    
    def code(x, eps):
    	tmp = 0
    	if (math.pow((x + eps), 2.0) - math.pow(x, 2.0)) <= 0.0:
    		tmp = (x + x) * eps
    	else:
    		tmp = (eps + x) * eps
    	return tmp
    
    function code(x, eps)
    	tmp = 0.0
    	if (Float64((Float64(x + eps) ^ 2.0) - (x ^ 2.0)) <= 0.0)
    		tmp = Float64(Float64(x + x) * eps);
    	else
    		tmp = Float64(Float64(eps + x) * eps);
    	end
    	return tmp
    end
    
    function tmp_2 = code(x, eps)
    	tmp = 0.0;
    	if ((((x + eps) ^ 2.0) - (x ^ 2.0)) <= 0.0)
    		tmp = (x + x) * eps;
    	else
    		tmp = (eps + x) * eps;
    	end
    	tmp_2 = tmp;
    end
    
    code[x_, eps_] := If[LessEqual[N[(N[Power[N[(x + eps), $MachinePrecision], 2.0], $MachinePrecision] - N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision], 0.0], N[(N[(x + x), $MachinePrecision] * eps), $MachinePrecision], N[(N[(eps + x), $MachinePrecision] * eps), $MachinePrecision]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    \mathbf{if}\;{\left(x + \varepsilon\right)}^{2} - {x}^{2} \leq 0:\\
    \;\;\;\;\left(x + x\right) \cdot \varepsilon\\
    
    \mathbf{else}:\\
    \;\;\;\;\left(\varepsilon + x\right) \cdot \varepsilon\\
    
    
    \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 58.5%

        \[{\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. lift-pow.f64N/A

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

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

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

          \[\leadsto \left(x + \varepsilon\right) \cdot \left(x + \varepsilon\right) - \color{blue}{x \cdot x} \]
        6. difference-of-squaresN/A

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

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

          \[\leadsto \color{blue}{\left(\left(x + \varepsilon\right) + x\right)} \cdot \left(\left(x + \varepsilon\right) - x\right) \]
        9. lift-+.f64N/A

          \[\leadsto \left(\color{blue}{\left(x + \varepsilon\right)} + x\right) \cdot \left(\left(x + \varepsilon\right) - x\right) \]
        10. +-commutativeN/A

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

          \[\leadsto \left(\color{blue}{\left(\varepsilon + x\right)} + x\right) \cdot \left(\left(x + \varepsilon\right) - x\right) \]
        12. lower--.f6458.5

          \[\leadsto \left(\left(\varepsilon + x\right) + x\right) \cdot \color{blue}{\left(\left(x + \varepsilon\right) - x\right)} \]
        13. lift-+.f64N/A

          \[\leadsto \left(\left(\varepsilon + x\right) + x\right) \cdot \left(\color{blue}{\left(x + \varepsilon\right)} - x\right) \]
        14. +-commutativeN/A

          \[\leadsto \left(\left(\varepsilon + x\right) + x\right) \cdot \left(\color{blue}{\left(\varepsilon + x\right)} - x\right) \]
        15. lower-+.f6458.5

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

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

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

          \[\leadsto \left(\left(\varepsilon + x\right) + x\right) \cdot \color{blue}{\varepsilon} \]
        2. Taylor expanded in x around inf

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

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

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

          1. Initial program 98.1%

            \[{\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. lift-pow.f64N/A

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

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

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

              \[\leadsto \left(x + \varepsilon\right) \cdot \left(x + \varepsilon\right) - \color{blue}{x \cdot x} \]
            6. difference-of-squaresN/A

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

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

              \[\leadsto \color{blue}{\left(\left(x + \varepsilon\right) + x\right)} \cdot \left(\left(x + \varepsilon\right) - x\right) \]
            9. lift-+.f64N/A

              \[\leadsto \left(\color{blue}{\left(x + \varepsilon\right)} + x\right) \cdot \left(\left(x + \varepsilon\right) - x\right) \]
            10. +-commutativeN/A

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

              \[\leadsto \left(\color{blue}{\left(\varepsilon + x\right)} + x\right) \cdot \left(\left(x + \varepsilon\right) - x\right) \]
            12. lower--.f6498.2

              \[\leadsto \left(\left(\varepsilon + x\right) + x\right) \cdot \color{blue}{\left(\left(x + \varepsilon\right) - x\right)} \]
            13. lift-+.f64N/A

              \[\leadsto \left(\left(\varepsilon + x\right) + x\right) \cdot \left(\color{blue}{\left(x + \varepsilon\right)} - x\right) \]
            14. +-commutativeN/A

              \[\leadsto \left(\left(\varepsilon + x\right) + x\right) \cdot \left(\color{blue}{\left(\varepsilon + x\right)} - x\right) \]
            15. lower-+.f6498.2

              \[\leadsto \left(\left(\varepsilon + x\right) + x\right) \cdot \left(\color{blue}{\left(\varepsilon + x\right)} - x\right) \]
          4. Applied rewrites98.2%

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

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

              \[\leadsto \left(\left(\varepsilon + x\right) + x\right) \cdot \color{blue}{\varepsilon} \]
            2. Taylor expanded in x around 0

              \[\leadsto \left(\color{blue}{\varepsilon} + x\right) \cdot \varepsilon \]
            3. Step-by-step derivation
              1. Applied rewrites90.0%

                \[\leadsto \left(\color{blue}{\varepsilon} + x\right) \cdot \varepsilon \]
            4. Recombined 2 regimes into one program.
            5. Add Preprocessing

            Alternative 3: 75.6% accurate, 23.2× speedup?

            \[\begin{array}{l} \\ \left(\varepsilon + x\right) \cdot \varepsilon \end{array} \]
            (FPCore (x eps) :precision binary64 (* (+ eps x) eps))
            double code(double x, double eps) {
            	return (eps + x) * eps;
            }
            
            module fmin_fmax_functions
                implicit none
                private
                public fmax
                public fmin
            
                interface fmax
                    module procedure fmax88
                    module procedure fmax44
                    module procedure fmax84
                    module procedure fmax48
                end interface
                interface fmin
                    module procedure fmin88
                    module procedure fmin44
                    module procedure fmin84
                    module procedure fmin48
                end interface
            contains
                real(8) function fmax88(x, y) result (res)
                    real(8), intent (in) :: x
                    real(8), intent (in) :: y
                    res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                end function
                real(4) function fmax44(x, y) result (res)
                    real(4), intent (in) :: x
                    real(4), intent (in) :: y
                    res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                end function
                real(8) function fmax84(x, y) result(res)
                    real(8), intent (in) :: x
                    real(4), intent (in) :: y
                    res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
                end function
                real(8) function fmax48(x, y) result(res)
                    real(4), intent (in) :: x
                    real(8), intent (in) :: y
                    res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
                end function
                real(8) function fmin88(x, y) result (res)
                    real(8), intent (in) :: x
                    real(8), intent (in) :: y
                    res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                end function
                real(4) function fmin44(x, y) result (res)
                    real(4), intent (in) :: x
                    real(4), intent (in) :: y
                    res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                end function
                real(8) function fmin84(x, y) result(res)
                    real(8), intent (in) :: x
                    real(4), intent (in) :: y
                    res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
                end function
                real(8) function fmin48(x, y) result(res)
                    real(4), intent (in) :: x
                    real(8), intent (in) :: y
                    res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
                end function
            end module
            
            real(8) function code(x, eps)
            use fmin_fmax_functions
                real(8), intent (in) :: x
                real(8), intent (in) :: eps
                code = (eps + x) * eps
            end function
            
            public static double code(double x, double eps) {
            	return (eps + x) * eps;
            }
            
            def code(x, eps):
            	return (eps + x) * eps
            
            function code(x, eps)
            	return Float64(Float64(eps + x) * eps)
            end
            
            function tmp = code(x, eps)
            	tmp = (eps + x) * eps;
            end
            
            code[x_, eps_] := N[(N[(eps + x), $MachinePrecision] * eps), $MachinePrecision]
            
            \begin{array}{l}
            
            \\
            \left(\varepsilon + x\right) \cdot \varepsilon
            \end{array}
            
            Derivation
            1. Initial program 73.1%

              \[{\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. lift-pow.f64N/A

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

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

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

                \[\leadsto \left(x + \varepsilon\right) \cdot \left(x + \varepsilon\right) - \color{blue}{x \cdot x} \]
              6. difference-of-squaresN/A

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

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

                \[\leadsto \color{blue}{\left(\left(x + \varepsilon\right) + x\right)} \cdot \left(\left(x + \varepsilon\right) - x\right) \]
              9. lift-+.f64N/A

                \[\leadsto \left(\color{blue}{\left(x + \varepsilon\right)} + x\right) \cdot \left(\left(x + \varepsilon\right) - x\right) \]
              10. +-commutativeN/A

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

                \[\leadsto \left(\color{blue}{\left(\varepsilon + x\right)} + x\right) \cdot \left(\left(x + \varepsilon\right) - x\right) \]
              12. lower--.f6473.1

                \[\leadsto \left(\left(\varepsilon + x\right) + x\right) \cdot \color{blue}{\left(\left(x + \varepsilon\right) - x\right)} \]
              13. lift-+.f64N/A

                \[\leadsto \left(\left(\varepsilon + x\right) + x\right) \cdot \left(\color{blue}{\left(x + \varepsilon\right)} - x\right) \]
              14. +-commutativeN/A

                \[\leadsto \left(\left(\varepsilon + x\right) + x\right) \cdot \left(\color{blue}{\left(\varepsilon + x\right)} - x\right) \]
              15. lower-+.f6473.1

                \[\leadsto \left(\left(\varepsilon + x\right) + x\right) \cdot \left(\color{blue}{\left(\varepsilon + x\right)} - x\right) \]
            4. Applied rewrites73.1%

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

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

                \[\leadsto \left(\left(\varepsilon + x\right) + x\right) \cdot \color{blue}{\varepsilon} \]
              2. Taylor expanded in x around 0

                \[\leadsto \left(\color{blue}{\varepsilon} + x\right) \cdot \varepsilon \]
              3. Step-by-step derivation
                1. Applied rewrites72.8%

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

                Alternative 4: 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;
                }
                
                module fmin_fmax_functions
                    implicit none
                    private
                    public fmax
                    public fmin
                
                    interface fmax
                        module procedure fmax88
                        module procedure fmax44
                        module procedure fmax84
                        module procedure fmax48
                    end interface
                    interface fmin
                        module procedure fmin88
                        module procedure fmin44
                        module procedure fmin84
                        module procedure fmin48
                    end interface
                contains
                    real(8) function fmax88(x, y) result (res)
                        real(8), intent (in) :: x
                        real(8), intent (in) :: y
                        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                    end function
                    real(4) function fmax44(x, y) result (res)
                        real(4), intent (in) :: x
                        real(4), intent (in) :: y
                        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                    end function
                    real(8) function fmax84(x, y) result(res)
                        real(8), intent (in) :: x
                        real(4), intent (in) :: y
                        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
                    end function
                    real(8) function fmax48(x, y) result(res)
                        real(4), intent (in) :: x
                        real(8), intent (in) :: y
                        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
                    end function
                    real(8) function fmin88(x, y) result (res)
                        real(8), intent (in) :: x
                        real(8), intent (in) :: y
                        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                    end function
                    real(4) function fmin44(x, y) result (res)
                        real(4), intent (in) :: x
                        real(4), intent (in) :: y
                        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                    end function
                    real(8) function fmin84(x, y) result(res)
                        real(8), intent (in) :: x
                        real(4), intent (in) :: y
                        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
                    end function
                    real(8) function fmin48(x, y) result(res)
                        real(4), intent (in) :: x
                        real(8), intent (in) :: y
                        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
                    end function
                end module
                
                real(8) function code(x, eps)
                use fmin_fmax_functions
                    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 73.1%

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

                  \[\leadsto \color{blue}{{\varepsilon}^{2}} \]
                4. Step-by-step derivation
                  1. Applied rewrites69.2%

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

                  Reproduce

                  ?
                  herbie shell --seed 2025019 
                  (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)))