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

Percentage Accurate: 88.1% → 98.8%
Time: 16.9s
Alternatives: 5
Speedup: N/A×

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)}^{5} - {x}^{5} \end{array} \]
(FPCore (x eps) :precision binary64 (- (pow (+ x eps) 5.0) (pow x 5.0)))
double code(double x, double eps) {
	return pow((x + eps), 5.0) - pow(x, 5.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) ** 5.0d0) - (x ** 5.0d0)
end function
public static double code(double x, double eps) {
	return Math.pow((x + eps), 5.0) - Math.pow(x, 5.0);
}
def code(x, eps):
	return math.pow((x + eps), 5.0) - math.pow(x, 5.0)
function code(x, eps)
	return Float64((Float64(x + eps) ^ 5.0) - (x ^ 5.0))
end
function tmp = code(x, eps)
	tmp = ((x + eps) ^ 5.0) - (x ^ 5.0);
end
code[x_, eps_] := N[(N[Power[N[(x + eps), $MachinePrecision], 5.0], $MachinePrecision] - N[Power[x, 5.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

\[\begin{array}{l} \\ {\left(x + \varepsilon\right)}^{5} - {x}^{5} \end{array} \]
(FPCore (x eps) :precision binary64 (- (pow (+ x eps) 5.0) (pow x 5.0)))
double code(double x, double eps) {
	return pow((x + eps), 5.0) - pow(x, 5.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) ** 5.0d0) - (x ** 5.0d0)
end function
public static double code(double x, double eps) {
	return Math.pow((x + eps), 5.0) - Math.pow(x, 5.0);
}
def code(x, eps):
	return math.pow((x + eps), 5.0) - math.pow(x, 5.0)
function code(x, eps)
	return Float64((Float64(x + eps) ^ 5.0) - (x ^ 5.0))
end
function tmp = code(x, eps)
	tmp = ((x + eps) ^ 5.0) - (x ^ 5.0);
end
code[x_, eps_] := N[(N[Power[N[(x + eps), $MachinePrecision], 5.0], $MachinePrecision] - N[Power[x, 5.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

Alternative 1: 98.8% accurate, N/A× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := {\left(x + \varepsilon\right)}^{5} - {x}^{5}\\ \mathbf{if}\;t\_0 \leq -4 \cdot 10^{-317} \lor \neg \left(t\_0 \leq 0\right):\\ \;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left({\varepsilon}^{4}, 4, \mathsf{fma}\left(\mathsf{fma}\left({\varepsilon}^{3}, 4, \mathsf{fma}\left(\left(\varepsilon \cdot \varepsilon\right) \cdot 10, x, \left(\left(\varepsilon \cdot \varepsilon\right) \cdot 6\right) \cdot \varepsilon\right)\right), x, {\varepsilon}^{4}\right)\right), x, {\varepsilon}^{5}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left({x}^{4}, 4, \mathsf{fma}\left(\mathsf{fma}\left({x}^{3}, 4, \mathsf{fma}\left(\mathsf{fma}\left(x \cdot x, 10, \left(5 \cdot x\right) \cdot \varepsilon\right), \varepsilon, \left(\left(x \cdot x\right) \cdot 6\right) \cdot x\right)\right), \varepsilon, {x}^{4}\right)\right) \cdot \varepsilon\\ \end{array} \end{array} \]
(FPCore (x eps)
 :precision binary64
 (let* ((t_0 (- (pow (+ x eps) 5.0) (pow x 5.0))))
   (if (or (<= t_0 -4e-317) (not (<= t_0 0.0)))
     (fma
      (fma
       (pow eps 4.0)
       4.0
       (fma
        (fma
         (pow eps 3.0)
         4.0
         (fma (* (* eps eps) 10.0) x (* (* (* eps eps) 6.0) eps)))
        x
        (pow eps 4.0)))
      x
      (pow eps 5.0))
     (*
      (fma
       (pow x 4.0)
       4.0
       (fma
        (fma
         (pow x 3.0)
         4.0
         (fma (fma (* x x) 10.0 (* (* 5.0 x) eps)) eps (* (* (* x x) 6.0) x)))
        eps
        (pow x 4.0)))
      eps))))
double code(double x, double eps) {
	double t_0 = pow((x + eps), 5.0) - pow(x, 5.0);
	double tmp;
	if ((t_0 <= -4e-317) || !(t_0 <= 0.0)) {
		tmp = fma(fma(pow(eps, 4.0), 4.0, fma(fma(pow(eps, 3.0), 4.0, fma(((eps * eps) * 10.0), x, (((eps * eps) * 6.0) * eps))), x, pow(eps, 4.0))), x, pow(eps, 5.0));
	} else {
		tmp = fma(pow(x, 4.0), 4.0, fma(fma(pow(x, 3.0), 4.0, fma(fma((x * x), 10.0, ((5.0 * x) * eps)), eps, (((x * x) * 6.0) * x))), eps, pow(x, 4.0))) * eps;
	}
	return tmp;
}
function code(x, eps)
	t_0 = Float64((Float64(x + eps) ^ 5.0) - (x ^ 5.0))
	tmp = 0.0
	if ((t_0 <= -4e-317) || !(t_0 <= 0.0))
		tmp = fma(fma((eps ^ 4.0), 4.0, fma(fma((eps ^ 3.0), 4.0, fma(Float64(Float64(eps * eps) * 10.0), x, Float64(Float64(Float64(eps * eps) * 6.0) * eps))), x, (eps ^ 4.0))), x, (eps ^ 5.0));
	else
		tmp = Float64(fma((x ^ 4.0), 4.0, fma(fma((x ^ 3.0), 4.0, fma(fma(Float64(x * x), 10.0, Float64(Float64(5.0 * x) * eps)), eps, Float64(Float64(Float64(x * x) * 6.0) * x))), eps, (x ^ 4.0))) * eps);
	end
	return tmp
end
code[x_, eps_] := Block[{t$95$0 = N[(N[Power[N[(x + eps), $MachinePrecision], 5.0], $MachinePrecision] - N[Power[x, 5.0], $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, -4e-317], N[Not[LessEqual[t$95$0, 0.0]], $MachinePrecision]], N[(N[(N[Power[eps, 4.0], $MachinePrecision] * 4.0 + N[(N[(N[Power[eps, 3.0], $MachinePrecision] * 4.0 + N[(N[(N[(eps * eps), $MachinePrecision] * 10.0), $MachinePrecision] * x + N[(N[(N[(eps * eps), $MachinePrecision] * 6.0), $MachinePrecision] * eps), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * x + N[Power[eps, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * x + N[Power[eps, 5.0], $MachinePrecision]), $MachinePrecision], N[(N[(N[Power[x, 4.0], $MachinePrecision] * 4.0 + N[(N[(N[Power[x, 3.0], $MachinePrecision] * 4.0 + N[(N[(N[(x * x), $MachinePrecision] * 10.0 + N[(N[(5.0 * x), $MachinePrecision] * eps), $MachinePrecision]), $MachinePrecision] * eps + N[(N[(N[(x * x), $MachinePrecision] * 6.0), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * eps + N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * eps), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := {\left(x + \varepsilon\right)}^{5} - {x}^{5}\\
\mathbf{if}\;t\_0 \leq -4 \cdot 10^{-317} \lor \neg \left(t\_0 \leq 0\right):\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left({\varepsilon}^{4}, 4, \mathsf{fma}\left(\mathsf{fma}\left({\varepsilon}^{3}, 4, \mathsf{fma}\left(\left(\varepsilon \cdot \varepsilon\right) \cdot 10, x, \left(\left(\varepsilon \cdot \varepsilon\right) \cdot 6\right) \cdot \varepsilon\right)\right), x, {\varepsilon}^{4}\right)\right), x, {\varepsilon}^{5}\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left({x}^{4}, 4, \mathsf{fma}\left(\mathsf{fma}\left({x}^{3}, 4, \mathsf{fma}\left(\mathsf{fma}\left(x \cdot x, 10, \left(5 \cdot x\right) \cdot \varepsilon\right), \varepsilon, \left(\left(x \cdot x\right) \cdot 6\right) \cdot x\right)\right), \varepsilon, {x}^{4}\right)\right) \cdot \varepsilon\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (pow.f64 (+.f64 x eps) #s(literal 5 binary64)) (pow.f64 x #s(literal 5 binary64))) < -3.99999993e-317 or 0.0 < (-.f64 (pow.f64 (+.f64 x eps) #s(literal 5 binary64)) (pow.f64 x #s(literal 5 binary64)))

    1. Initial program 99.8%

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

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

        \[\leadsto \left(4 \cdot {\varepsilon}^{4} + \left(x \cdot \left(4 \cdot {\varepsilon}^{3} + \left(\varepsilon \cdot \left(2 \cdot {\varepsilon}^{2} + 4 \cdot {\varepsilon}^{2}\right) + x \cdot \left(2 \cdot {\varepsilon}^{2} + 8 \cdot {\varepsilon}^{2}\right)\right)\right) + {\varepsilon}^{4}\right)\right) \cdot x + {\color{blue}{\varepsilon}}^{5} \]
      2. lower-fma.f64N/A

        \[\leadsto \mathsf{fma}\left(4 \cdot {\varepsilon}^{4} + \left(x \cdot \left(4 \cdot {\varepsilon}^{3} + \left(\varepsilon \cdot \left(2 \cdot {\varepsilon}^{2} + 4 \cdot {\varepsilon}^{2}\right) + x \cdot \left(2 \cdot {\varepsilon}^{2} + 8 \cdot {\varepsilon}^{2}\right)\right)\right) + {\varepsilon}^{4}\right), \color{blue}{x}, {\varepsilon}^{5}\right) \]
    5. Applied rewrites100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left({\varepsilon}^{4}, 4, \mathsf{fma}\left(\mathsf{fma}\left({\varepsilon}^{3}, 4, \mathsf{fma}\left(\left(\varepsilon \cdot \varepsilon\right) \cdot 10, x, \left(\left(\varepsilon \cdot \varepsilon\right) \cdot 6\right) \cdot \varepsilon\right)\right), x, {\varepsilon}^{4}\right)\right), x, {\varepsilon}^{5}\right)} \]

    if -3.99999993e-317 < (-.f64 (pow.f64 (+.f64 x eps) #s(literal 5 binary64)) (pow.f64 x #s(literal 5 binary64))) < 0.0

    1. Initial program 83.8%

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

      \[\leadsto \color{blue}{\varepsilon \cdot \left(4 \cdot {x}^{4} + \left(\varepsilon \cdot \left(4 \cdot {x}^{3} + \left(\varepsilon \cdot \left(2 \cdot {x}^{2} + \left(8 \cdot {x}^{2} + \varepsilon \cdot \left(x + 4 \cdot x\right)\right)\right) + x \cdot \left(2 \cdot {x}^{2} + 4 \cdot {x}^{2}\right)\right)\right) + {x}^{4}\right)\right)} \]
    4. Applied rewrites99.9%

      \[\leadsto \color{blue}{\mathsf{fma}\left({x}^{4}, 4, \mathsf{fma}\left(\mathsf{fma}\left({x}^{3}, 4, \mathsf{fma}\left(\mathsf{fma}\left(x \cdot x, 10, \left(5 \cdot x\right) \cdot \varepsilon\right), \varepsilon, \left(\left(x \cdot x\right) \cdot 6\right) \cdot x\right)\right), \varepsilon, {x}^{4}\right)\right) \cdot \varepsilon} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification100.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;{\left(x + \varepsilon\right)}^{5} - {x}^{5} \leq -4 \cdot 10^{-317} \lor \neg \left({\left(x + \varepsilon\right)}^{5} - {x}^{5} \leq 0\right):\\ \;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left({\varepsilon}^{4}, 4, \mathsf{fma}\left(\mathsf{fma}\left({\varepsilon}^{3}, 4, \mathsf{fma}\left(\left(\varepsilon \cdot \varepsilon\right) \cdot 10, x, \left(\left(\varepsilon \cdot \varepsilon\right) \cdot 6\right) \cdot \varepsilon\right)\right), x, {\varepsilon}^{4}\right)\right), x, {\varepsilon}^{5}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left({x}^{4}, 4, \mathsf{fma}\left(\mathsf{fma}\left({x}^{3}, 4, \mathsf{fma}\left(\mathsf{fma}\left(x \cdot x, 10, \left(5 \cdot x\right) \cdot \varepsilon\right), \varepsilon, \left(\left(x \cdot x\right) \cdot 6\right) \cdot x\right)\right), \varepsilon, {x}^{4}\right)\right) \cdot \varepsilon\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 98.7% accurate, N/A× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := {\left(x + \varepsilon\right)}^{5} - {x}^{5}\\ \mathbf{if}\;t\_0 \leq -4 \cdot 10^{-317} \lor \neg \left(t\_0 \leq 0\right):\\ \;\;\;\;\mathsf{fma}\left({\varepsilon}^{4}, {\varepsilon}^{1}, \mathsf{fma}\left({\varepsilon}^{4}, 4, \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(6, \varepsilon, 10 \cdot x\right), \varepsilon \cdot \varepsilon, {\varepsilon}^{3} \cdot 4\right), x, {\varepsilon}^{4}\right)\right) \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left({x}^{4}, 4, \mathsf{fma}\left(\mathsf{fma}\left({x}^{3}, 4, \mathsf{fma}\left(\mathsf{fma}\left(x \cdot x, 10, \left(5 \cdot x\right) \cdot \varepsilon\right), \varepsilon, \left(\left(x \cdot x\right) \cdot 6\right) \cdot x\right)\right), \varepsilon, {x}^{4}\right)\right) \cdot \varepsilon\\ \end{array} \end{array} \]
(FPCore (x eps)
 :precision binary64
 (let* ((t_0 (- (pow (+ x eps) 5.0) (pow x 5.0))))
   (if (or (<= t_0 -4e-317) (not (<= t_0 0.0)))
     (fma
      (pow eps 4.0)
      (pow eps 1.0)
      (*
       (fma
        (pow eps 4.0)
        4.0
        (fma
         (fma (fma 6.0 eps (* 10.0 x)) (* eps eps) (* (pow eps 3.0) 4.0))
         x
         (pow eps 4.0)))
       x))
     (*
      (fma
       (pow x 4.0)
       4.0
       (fma
        (fma
         (pow x 3.0)
         4.0
         (fma (fma (* x x) 10.0 (* (* 5.0 x) eps)) eps (* (* (* x x) 6.0) x)))
        eps
        (pow x 4.0)))
      eps))))
double code(double x, double eps) {
	double t_0 = pow((x + eps), 5.0) - pow(x, 5.0);
	double tmp;
	if ((t_0 <= -4e-317) || !(t_0 <= 0.0)) {
		tmp = fma(pow(eps, 4.0), pow(eps, 1.0), (fma(pow(eps, 4.0), 4.0, fma(fma(fma(6.0, eps, (10.0 * x)), (eps * eps), (pow(eps, 3.0) * 4.0)), x, pow(eps, 4.0))) * x));
	} else {
		tmp = fma(pow(x, 4.0), 4.0, fma(fma(pow(x, 3.0), 4.0, fma(fma((x * x), 10.0, ((5.0 * x) * eps)), eps, (((x * x) * 6.0) * x))), eps, pow(x, 4.0))) * eps;
	}
	return tmp;
}
function code(x, eps)
	t_0 = Float64((Float64(x + eps) ^ 5.0) - (x ^ 5.0))
	tmp = 0.0
	if ((t_0 <= -4e-317) || !(t_0 <= 0.0))
		tmp = fma((eps ^ 4.0), (eps ^ 1.0), Float64(fma((eps ^ 4.0), 4.0, fma(fma(fma(6.0, eps, Float64(10.0 * x)), Float64(eps * eps), Float64((eps ^ 3.0) * 4.0)), x, (eps ^ 4.0))) * x));
	else
		tmp = Float64(fma((x ^ 4.0), 4.0, fma(fma((x ^ 3.0), 4.0, fma(fma(Float64(x * x), 10.0, Float64(Float64(5.0 * x) * eps)), eps, Float64(Float64(Float64(x * x) * 6.0) * x))), eps, (x ^ 4.0))) * eps);
	end
	return tmp
end
code[x_, eps_] := Block[{t$95$0 = N[(N[Power[N[(x + eps), $MachinePrecision], 5.0], $MachinePrecision] - N[Power[x, 5.0], $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, -4e-317], N[Not[LessEqual[t$95$0, 0.0]], $MachinePrecision]], N[(N[Power[eps, 4.0], $MachinePrecision] * N[Power[eps, 1.0], $MachinePrecision] + N[(N[(N[Power[eps, 4.0], $MachinePrecision] * 4.0 + N[(N[(N[(6.0 * eps + N[(10.0 * x), $MachinePrecision]), $MachinePrecision] * N[(eps * eps), $MachinePrecision] + N[(N[Power[eps, 3.0], $MachinePrecision] * 4.0), $MachinePrecision]), $MachinePrecision] * x + N[Power[eps, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision], N[(N[(N[Power[x, 4.0], $MachinePrecision] * 4.0 + N[(N[(N[Power[x, 3.0], $MachinePrecision] * 4.0 + N[(N[(N[(x * x), $MachinePrecision] * 10.0 + N[(N[(5.0 * x), $MachinePrecision] * eps), $MachinePrecision]), $MachinePrecision] * eps + N[(N[(N[(x * x), $MachinePrecision] * 6.0), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * eps + N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * eps), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := {\left(x + \varepsilon\right)}^{5} - {x}^{5}\\
\mathbf{if}\;t\_0 \leq -4 \cdot 10^{-317} \lor \neg \left(t\_0 \leq 0\right):\\
\;\;\;\;\mathsf{fma}\left({\varepsilon}^{4}, {\varepsilon}^{1}, \mathsf{fma}\left({\varepsilon}^{4}, 4, \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(6, \varepsilon, 10 \cdot x\right), \varepsilon \cdot \varepsilon, {\varepsilon}^{3} \cdot 4\right), x, {\varepsilon}^{4}\right)\right) \cdot x\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left({x}^{4}, 4, \mathsf{fma}\left(\mathsf{fma}\left({x}^{3}, 4, \mathsf{fma}\left(\mathsf{fma}\left(x \cdot x, 10, \left(5 \cdot x\right) \cdot \varepsilon\right), \varepsilon, \left(\left(x \cdot x\right) \cdot 6\right) \cdot x\right)\right), \varepsilon, {x}^{4}\right)\right) \cdot \varepsilon\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (pow.f64 (+.f64 x eps) #s(literal 5 binary64)) (pow.f64 x #s(literal 5 binary64))) < -3.99999993e-317 or 0.0 < (-.f64 (pow.f64 (+.f64 x eps) #s(literal 5 binary64)) (pow.f64 x #s(literal 5 binary64)))

    1. Initial program 99.8%

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

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

        \[\leadsto \left(4 \cdot {\varepsilon}^{4} + \left(x \cdot \left(4 \cdot {\varepsilon}^{3} + \left(\varepsilon \cdot \left(2 \cdot {\varepsilon}^{2} + 4 \cdot {\varepsilon}^{2}\right) + x \cdot \left(2 \cdot {\varepsilon}^{2} + 8 \cdot {\varepsilon}^{2}\right)\right)\right) + {\varepsilon}^{4}\right)\right) \cdot x + {\color{blue}{\varepsilon}}^{5} \]
      2. lower-fma.f64N/A

        \[\leadsto \mathsf{fma}\left(4 \cdot {\varepsilon}^{4} + \left(x \cdot \left(4 \cdot {\varepsilon}^{3} + \left(\varepsilon \cdot \left(2 \cdot {\varepsilon}^{2} + 4 \cdot {\varepsilon}^{2}\right) + x \cdot \left(2 \cdot {\varepsilon}^{2} + 8 \cdot {\varepsilon}^{2}\right)\right)\right) + {\varepsilon}^{4}\right), \color{blue}{x}, {\varepsilon}^{5}\right) \]
    5. Applied rewrites100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\mathsf{fma}\left({\varepsilon}^{4}, 4, \mathsf{fma}\left(\mathsf{fma}\left({\varepsilon}^{3}, 4, \mathsf{fma}\left(\left(\varepsilon \cdot \varepsilon\right) \cdot 10, x, \left(\left(\varepsilon \cdot \varepsilon\right) \cdot 6\right) \cdot \varepsilon\right)\right), x, {\varepsilon}^{4}\right)\right), x, {\varepsilon}^{5}\right)} \]
    6. Applied rewrites99.5%

      \[\leadsto \mathsf{fma}\left({\varepsilon}^{4}, \color{blue}{{\varepsilon}^{1}}, \mathsf{fma}\left({\varepsilon}^{4}, 4, \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(6, \varepsilon, 10 \cdot x\right), \varepsilon \cdot \varepsilon, {\varepsilon}^{3} \cdot 4\right), x, {\varepsilon}^{4}\right)\right) \cdot x\right) \]

    if -3.99999993e-317 < (-.f64 (pow.f64 (+.f64 x eps) #s(literal 5 binary64)) (pow.f64 x #s(literal 5 binary64))) < 0.0

    1. Initial program 83.8%

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

      \[\leadsto \color{blue}{\varepsilon \cdot \left(4 \cdot {x}^{4} + \left(\varepsilon \cdot \left(4 \cdot {x}^{3} + \left(\varepsilon \cdot \left(2 \cdot {x}^{2} + \left(8 \cdot {x}^{2} + \varepsilon \cdot \left(x + 4 \cdot x\right)\right)\right) + x \cdot \left(2 \cdot {x}^{2} + 4 \cdot {x}^{2}\right)\right)\right) + {x}^{4}\right)\right)} \]
    4. Applied rewrites99.9%

      \[\leadsto \color{blue}{\mathsf{fma}\left({x}^{4}, 4, \mathsf{fma}\left(\mathsf{fma}\left({x}^{3}, 4, \mathsf{fma}\left(\mathsf{fma}\left(x \cdot x, 10, \left(5 \cdot x\right) \cdot \varepsilon\right), \varepsilon, \left(\left(x \cdot x\right) \cdot 6\right) \cdot x\right)\right), \varepsilon, {x}^{4}\right)\right) \cdot \varepsilon} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;{\left(x + \varepsilon\right)}^{5} - {x}^{5} \leq -4 \cdot 10^{-317} \lor \neg \left({\left(x + \varepsilon\right)}^{5} - {x}^{5} \leq 0\right):\\ \;\;\;\;\mathsf{fma}\left({\varepsilon}^{4}, {\varepsilon}^{1}, \mathsf{fma}\left({\varepsilon}^{4}, 4, \mathsf{fma}\left(\mathsf{fma}\left(\mathsf{fma}\left(6, \varepsilon, 10 \cdot x\right), \varepsilon \cdot \varepsilon, {\varepsilon}^{3} \cdot 4\right), x, {\varepsilon}^{4}\right)\right) \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left({x}^{4}, 4, \mathsf{fma}\left(\mathsf{fma}\left({x}^{3}, 4, \mathsf{fma}\left(\mathsf{fma}\left(x \cdot x, 10, \left(5 \cdot x\right) \cdot \varepsilon\right), \varepsilon, \left(\left(x \cdot x\right) \cdot 6\right) \cdot x\right)\right), \varepsilon, {x}^{4}\right)\right) \cdot \varepsilon\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 83.0% accurate, N/A× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left({x}^{4}, 4, \mathsf{fma}\left(\mathsf{fma}\left({x}^{3}, 4, \mathsf{fma}\left(\mathsf{fma}\left(x \cdot x, 10, \left(5 \cdot x\right) \cdot \varepsilon\right), \varepsilon, \left(\left(x \cdot x\right) \cdot 6\right) \cdot x\right)\right), \varepsilon, {x}^{4}\right)\right) \cdot \varepsilon \end{array} \]
(FPCore (x eps)
 :precision binary64
 (*
  (fma
   (pow x 4.0)
   4.0
   (fma
    (fma
     (pow x 3.0)
     4.0
     (fma (fma (* x x) 10.0 (* (* 5.0 x) eps)) eps (* (* (* x x) 6.0) x)))
    eps
    (pow x 4.0)))
  eps))
double code(double x, double eps) {
	return fma(pow(x, 4.0), 4.0, fma(fma(pow(x, 3.0), 4.0, fma(fma((x * x), 10.0, ((5.0 * x) * eps)), eps, (((x * x) * 6.0) * x))), eps, pow(x, 4.0))) * eps;
}
function code(x, eps)
	return Float64(fma((x ^ 4.0), 4.0, fma(fma((x ^ 3.0), 4.0, fma(fma(Float64(x * x), 10.0, Float64(Float64(5.0 * x) * eps)), eps, Float64(Float64(Float64(x * x) * 6.0) * x))), eps, (x ^ 4.0))) * eps)
end
code[x_, eps_] := N[(N[(N[Power[x, 4.0], $MachinePrecision] * 4.0 + N[(N[(N[Power[x, 3.0], $MachinePrecision] * 4.0 + N[(N[(N[(x * x), $MachinePrecision] * 10.0 + N[(N[(5.0 * x), $MachinePrecision] * eps), $MachinePrecision]), $MachinePrecision] * eps + N[(N[(N[(x * x), $MachinePrecision] * 6.0), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * eps + N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * eps), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left({x}^{4}, 4, \mathsf{fma}\left(\mathsf{fma}\left({x}^{3}, 4, \mathsf{fma}\left(\mathsf{fma}\left(x \cdot x, 10, \left(5 \cdot x\right) \cdot \varepsilon\right), \varepsilon, \left(\left(x \cdot x\right) \cdot 6\right) \cdot x\right)\right), \varepsilon, {x}^{4}\right)\right) \cdot \varepsilon
\end{array}
Derivation
  1. Initial program 87.0%

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

    \[\leadsto \color{blue}{\varepsilon \cdot \left(4 \cdot {x}^{4} + \left(\varepsilon \cdot \left(4 \cdot {x}^{3} + \left(\varepsilon \cdot \left(2 \cdot {x}^{2} + \left(8 \cdot {x}^{2} + \varepsilon \cdot \left(x + 4 \cdot x\right)\right)\right) + x \cdot \left(2 \cdot {x}^{2} + 4 \cdot {x}^{2}\right)\right)\right) + {x}^{4}\right)\right)} \]
  4. Applied rewrites81.3%

    \[\leadsto \color{blue}{\mathsf{fma}\left({x}^{4}, 4, \mathsf{fma}\left(\mathsf{fma}\left({x}^{3}, 4, \mathsf{fma}\left(\mathsf{fma}\left(x \cdot x, 10, \left(5 \cdot x\right) \cdot \varepsilon\right), \varepsilon, \left(\left(x \cdot x\right) \cdot 6\right) \cdot x\right)\right), \varepsilon, {x}^{4}\right)\right) \cdot \varepsilon} \]
  5. Add Preprocessing

Alternative 4: 82.8% accurate, N/A× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \frac{\varepsilon \cdot \varepsilon}{x}\\ \left(\mathsf{fma}\left(t\_0, 2, \mathsf{fma}\left(t\_0, 8, 4 \cdot \varepsilon\right)\right) + \varepsilon\right) \cdot {x}^{4} \end{array} \end{array} \]
(FPCore (x eps)
 :precision binary64
 (let* ((t_0 (/ (* eps eps) x)))
   (* (+ (fma t_0 2.0 (fma t_0 8.0 (* 4.0 eps))) eps) (pow x 4.0))))
double code(double x, double eps) {
	double t_0 = (eps * eps) / x;
	return (fma(t_0, 2.0, fma(t_0, 8.0, (4.0 * eps))) + eps) * pow(x, 4.0);
}
function code(x, eps)
	t_0 = Float64(Float64(eps * eps) / x)
	return Float64(Float64(fma(t_0, 2.0, fma(t_0, 8.0, Float64(4.0 * eps))) + eps) * (x ^ 4.0))
end
code[x_, eps_] := Block[{t$95$0 = N[(N[(eps * eps), $MachinePrecision] / x), $MachinePrecision]}, N[(N[(N[(t$95$0 * 2.0 + N[(t$95$0 * 8.0 + N[(4.0 * eps), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + eps), $MachinePrecision] * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \frac{\varepsilon \cdot \varepsilon}{x}\\
\left(\mathsf{fma}\left(t\_0, 2, \mathsf{fma}\left(t\_0, 8, 4 \cdot \varepsilon\right)\right) + \varepsilon\right) \cdot {x}^{4}
\end{array}
\end{array}
Derivation
  1. Initial program 87.0%

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

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

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

      \[\leadsto \left(\varepsilon + \left(2 \cdot \frac{{\varepsilon}^{2}}{x} + \left(4 \cdot \varepsilon + 8 \cdot \frac{{\varepsilon}^{2}}{x}\right)\right)\right) \cdot \color{blue}{{x}^{4}} \]
  5. Applied rewrites81.3%

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

Alternative 5: 67.0% accurate, N/A× speedup?

\[\begin{array}{l} \\ \left(\left(-\mathsf{fma}\left(-5, x, \frac{\mathsf{fma}\left(x \cdot x, 10, \frac{\mathsf{fma}\left(\frac{{x}^{4}}{\varepsilon}, 5, {x}^{3} \cdot 10\right)}{\varepsilon}\right)}{-\varepsilon}\right)\right) \cdot {\varepsilon}^{3}\right) \cdot \varepsilon \end{array} \]
(FPCore (x eps)
 :precision binary64
 (*
  (*
   (-
    (fma
     -5.0
     x
     (/
      (fma
       (* x x)
       10.0
       (/ (fma (/ (pow x 4.0) eps) 5.0 (* (pow x 3.0) 10.0)) eps))
      (- eps))))
   (pow eps 3.0))
  eps))
double code(double x, double eps) {
	return (-fma(-5.0, x, (fma((x * x), 10.0, (fma((pow(x, 4.0) / eps), 5.0, (pow(x, 3.0) * 10.0)) / eps)) / -eps)) * pow(eps, 3.0)) * eps;
}
function code(x, eps)
	return Float64(Float64(Float64(-fma(-5.0, x, Float64(fma(Float64(x * x), 10.0, Float64(fma(Float64((x ^ 4.0) / eps), 5.0, Float64((x ^ 3.0) * 10.0)) / eps)) / Float64(-eps)))) * (eps ^ 3.0)) * eps)
end
code[x_, eps_] := N[(N[((-N[(-5.0 * x + N[(N[(N[(x * x), $MachinePrecision] * 10.0 + N[(N[(N[(N[Power[x, 4.0], $MachinePrecision] / eps), $MachinePrecision] * 5.0 + N[(N[Power[x, 3.0], $MachinePrecision] * 10.0), $MachinePrecision]), $MachinePrecision] / eps), $MachinePrecision]), $MachinePrecision] / (-eps)), $MachinePrecision]), $MachinePrecision]) * N[Power[eps, 3.0], $MachinePrecision]), $MachinePrecision] * eps), $MachinePrecision]
\begin{array}{l}

\\
\left(\left(-\mathsf{fma}\left(-5, x, \frac{\mathsf{fma}\left(x \cdot x, 10, \frac{\mathsf{fma}\left(\frac{{x}^{4}}{\varepsilon}, 5, {x}^{3} \cdot 10\right)}{\varepsilon}\right)}{-\varepsilon}\right)\right) \cdot {\varepsilon}^{3}\right) \cdot \varepsilon
\end{array}
Derivation
  1. Initial program 87.0%

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

    \[\leadsto \color{blue}{\varepsilon \cdot \left(4 \cdot {x}^{4} + \left(\varepsilon \cdot \left(4 \cdot {x}^{3} + \left(\varepsilon \cdot \left(2 \cdot {x}^{2} + \left(8 \cdot {x}^{2} + \varepsilon \cdot \left(x + 4 \cdot x\right)\right)\right) + x \cdot \left(2 \cdot {x}^{2} + 4 \cdot {x}^{2}\right)\right)\right) + {x}^{4}\right)\right)} \]
  4. Applied rewrites81.3%

    \[\leadsto \color{blue}{\mathsf{fma}\left({x}^{4}, 4, \mathsf{fma}\left(\mathsf{fma}\left({x}^{3}, 4, \mathsf{fma}\left(\mathsf{fma}\left(x \cdot x, 10, \left(5 \cdot x\right) \cdot \varepsilon\right), \varepsilon, \left(\left(x \cdot x\right) \cdot 6\right) \cdot x\right)\right), \varepsilon, {x}^{4}\right)\right) \cdot \varepsilon} \]
  5. Taylor expanded in eps around -inf

    \[\leadsto \left(-1 \cdot \left({\varepsilon}^{3} \cdot \left(-5 \cdot x + -1 \cdot \frac{-1 \cdot \frac{-1 \cdot \left(4 \cdot {x}^{3} + 6 \cdot {x}^{3}\right) + -1 \cdot \frac{4 \cdot {x}^{4} + {x}^{4}}{\varepsilon}}{\varepsilon} + 10 \cdot {x}^{2}}{\varepsilon}\right)\right)\right) \cdot \varepsilon \]
  6. Applied rewrites66.6%

    \[\leadsto \left(-\mathsf{fma}\left(-5, x, -\frac{\mathsf{fma}\left(x \cdot x, 10, -\frac{-1 \cdot \mathsf{fma}\left(\frac{{x}^{4}}{\varepsilon}, 5, {x}^{3} \cdot 10\right)}{\varepsilon}\right)}{\varepsilon}\right) \cdot {\varepsilon}^{3}\right) \cdot \varepsilon \]
  7. Final simplification66.6%

    \[\leadsto \left(\left(-\mathsf{fma}\left(-5, x, \frac{\mathsf{fma}\left(x \cdot x, 10, \frac{\mathsf{fma}\left(\frac{{x}^{4}}{\varepsilon}, 5, {x}^{3} \cdot 10\right)}{\varepsilon}\right)}{-\varepsilon}\right)\right) \cdot {\varepsilon}^{3}\right) \cdot \varepsilon \]
  8. Add Preprocessing

Reproduce

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