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

Percentage Accurate: 87.8% → 87.8%
Time: 1.6s
Alternatives: 5
Speedup: 1.0×

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);
}
real(8) function code(x, eps)
    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: 87.8% 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);
}
real(8) function code(x, eps)
    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: 87.8% 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);
}
real(8) function code(x, eps)
    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}
Derivation
  1. Initial program 86.8%

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

Alternative 2: 86.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := {\left(x + \varepsilon\right)}^{5}\\ \mathbf{if}\;\varepsilon \leq -8.8 \cdot 10^{-94}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\varepsilon \leq 1.25 \cdot 10^{-81}:\\ \;\;\;\;{t\_0}^{5}\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x eps)
 :precision binary64
 (let* ((t_0 (pow (+ x eps) 5.0)))
   (if (<= eps -8.8e-94) t_0 (if (<= eps 1.25e-81) (pow t_0 5.0) t_0))))
double code(double x, double eps) {
	double t_0 = pow((x + eps), 5.0);
	double tmp;
	if (eps <= -8.8e-94) {
		tmp = t_0;
	} else if (eps <= 1.25e-81) {
		tmp = pow(t_0, 5.0);
	} else {
		tmp = t_0;
	}
	return tmp;
}
real(8) function code(x, eps)
    real(8), intent (in) :: x
    real(8), intent (in) :: eps
    real(8) :: t_0
    real(8) :: tmp
    t_0 = (x + eps) ** 5.0d0
    if (eps <= (-8.8d-94)) then
        tmp = t_0
    else if (eps <= 1.25d-81) then
        tmp = t_0 ** 5.0d0
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double x, double eps) {
	double t_0 = Math.pow((x + eps), 5.0);
	double tmp;
	if (eps <= -8.8e-94) {
		tmp = t_0;
	} else if (eps <= 1.25e-81) {
		tmp = Math.pow(t_0, 5.0);
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(x, eps):
	t_0 = math.pow((x + eps), 5.0)
	tmp = 0
	if eps <= -8.8e-94:
		tmp = t_0
	elif eps <= 1.25e-81:
		tmp = math.pow(t_0, 5.0)
	else:
		tmp = t_0
	return tmp
function code(x, eps)
	t_0 = Float64(x + eps) ^ 5.0
	tmp = 0.0
	if (eps <= -8.8e-94)
		tmp = t_0;
	elseif (eps <= 1.25e-81)
		tmp = t_0 ^ 5.0;
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(x, eps)
	t_0 = (x + eps) ^ 5.0;
	tmp = 0.0;
	if (eps <= -8.8e-94)
		tmp = t_0;
	elseif (eps <= 1.25e-81)
		tmp = t_0 ^ 5.0;
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[x_, eps_] := Block[{t$95$0 = N[Power[N[(x + eps), $MachinePrecision], 5.0], $MachinePrecision]}, If[LessEqual[eps, -8.8e-94], t$95$0, If[LessEqual[eps, 1.25e-81], N[Power[t$95$0, 5.0], $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := {\left(x + \varepsilon\right)}^{5}\\
\mathbf{if}\;\varepsilon \leq -8.8 \cdot 10^{-94}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;\varepsilon \leq 1.25 \cdot 10^{-81}:\\
\;\;\;\;{t\_0}^{5}\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if eps < -8.80000000000000004e-94 or 1.24999999999999995e-81 < eps

    1. Initial program 84.9%

      \[{\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} + {\varepsilon}^{4}\right) + {\varepsilon}^{5}} \]
    4. Applied rewrites82.5%

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

    if -8.80000000000000004e-94 < eps < 1.24999999999999995e-81

    1. Initial program 87.6%

      \[{\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} + {\varepsilon}^{4}\right) + {\varepsilon}^{5}} \]
    4. Applied rewrites75.2%

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

      \[\leadsto {\left(\varepsilon + x\right)}^{5} \]
    6. Applied rewrites87.0%

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

Alternative 3: 80.2% accurate, 2.0× speedup?

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

\\
{\left(x + \varepsilon\right)}^{5}
\end{array}
Derivation
  1. Initial program 86.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} + {\varepsilon}^{4}\right) + {\varepsilon}^{5}} \]
  4. Applied rewrites77.4%

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

Alternative 4: 64.1% accurate, 2.0× speedup?

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

\\
{x}^{5}
\end{array}
Derivation
  1. Initial program 86.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. Applied rewrites62.2%

    \[\leadsto \color{blue}{{x}^{5}} \]
  5. Add Preprocessing

Alternative 5: 4.7% accurate, 52.3× speedup?

\[\begin{array}{l} \\ x + \varepsilon \end{array} \]
(FPCore (x eps) :precision binary64 (+ x eps))
double code(double x, double eps) {
	return x + eps;
}
real(8) function code(x, eps)
    real(8), intent (in) :: x
    real(8), intent (in) :: eps
    code = x + eps
end function
public static double code(double x, double eps) {
	return x + eps;
}
def code(x, eps):
	return x + eps
function code(x, eps)
	return Float64(x + eps)
end
function tmp = code(x, eps)
	tmp = x + eps;
end
code[x_, eps_] := N[(x + eps), $MachinePrecision]
\begin{array}{l}

\\
x + \varepsilon
\end{array}
Derivation
  1. Initial program 86.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} + \varepsilon \cdot \left(2 \cdot {\varepsilon}^{2} + 4 \cdot {\varepsilon}^{2}\right)\right) + {\varepsilon}^{4}\right)\right) + {\varepsilon}^{5}} \]
  4. Applied rewrites4.6%

    \[\leadsto \color{blue}{x + \varepsilon} \]
  5. Add Preprocessing

Reproduce

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