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

?

Percentage Accurate: 87.9% → 99.3%
Time: 7.4s
Precision: binary64
Cost: 40009

?

\[\left(-1000000000 \leq x \land x \leq 1000000000\right) \land \left(-1 \leq \varepsilon \land \varepsilon \leq 1\right)\]
\[{\left(x + \varepsilon\right)}^{5} - {x}^{5} \]
\[\begin{array}{l} t_0 := {\left(x + \varepsilon\right)}^{5} - {x}^{5}\\ \mathbf{if}\;t_0 \leq -2 \cdot 10^{-313} \lor \neg \left(t_0 \leq 0\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\varepsilon \cdot \left({x}^{4} + 4 \cdot {x}^{4}\right)\\ \end{array} \]
(FPCore (x eps) :precision binary64 (- (pow (+ x eps) 5.0) (pow x 5.0)))
(FPCore (x eps)
 :precision binary64
 (let* ((t_0 (- (pow (+ x eps) 5.0) (pow x 5.0))))
   (if (or (<= t_0 -2e-313) (not (<= t_0 0.0)))
     t_0
     (* eps (+ (pow x 4.0) (* 4.0 (pow x 4.0)))))))
double code(double x, double eps) {
	return pow((x + eps), 5.0) - pow(x, 5.0);
}
double code(double x, double eps) {
	double t_0 = pow((x + eps), 5.0) - pow(x, 5.0);
	double tmp;
	if ((t_0 <= -2e-313) || !(t_0 <= 0.0)) {
		tmp = t_0;
	} else {
		tmp = eps * (pow(x, 4.0) + (4.0 * pow(x, 4.0)));
	}
	return tmp;
}
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
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) - (x ** 5.0d0)
    if ((t_0 <= (-2d-313)) .or. (.not. (t_0 <= 0.0d0))) then
        tmp = t_0
    else
        tmp = eps * ((x ** 4.0d0) + (4.0d0 * (x ** 4.0d0)))
    end if
    code = tmp
end function
public static double code(double x, double eps) {
	return Math.pow((x + eps), 5.0) - Math.pow(x, 5.0);
}
public static double code(double x, double eps) {
	double t_0 = Math.pow((x + eps), 5.0) - Math.pow(x, 5.0);
	double tmp;
	if ((t_0 <= -2e-313) || !(t_0 <= 0.0)) {
		tmp = t_0;
	} else {
		tmp = eps * (Math.pow(x, 4.0) + (4.0 * Math.pow(x, 4.0)));
	}
	return tmp;
}
def code(x, eps):
	return math.pow((x + eps), 5.0) - math.pow(x, 5.0)
def code(x, eps):
	t_0 = math.pow((x + eps), 5.0) - math.pow(x, 5.0)
	tmp = 0
	if (t_0 <= -2e-313) or not (t_0 <= 0.0):
		tmp = t_0
	else:
		tmp = eps * (math.pow(x, 4.0) + (4.0 * math.pow(x, 4.0)))
	return tmp
function code(x, eps)
	return Float64((Float64(x + eps) ^ 5.0) - (x ^ 5.0))
end
function code(x, eps)
	t_0 = Float64((Float64(x + eps) ^ 5.0) - (x ^ 5.0))
	tmp = 0.0
	if ((t_0 <= -2e-313) || !(t_0 <= 0.0))
		tmp = t_0;
	else
		tmp = Float64(eps * Float64((x ^ 4.0) + Float64(4.0 * (x ^ 4.0))));
	end
	return tmp
end
function tmp = code(x, eps)
	tmp = ((x + eps) ^ 5.0) - (x ^ 5.0);
end
function tmp_2 = code(x, eps)
	t_0 = ((x + eps) ^ 5.0) - (x ^ 5.0);
	tmp = 0.0;
	if ((t_0 <= -2e-313) || ~((t_0 <= 0.0)))
		tmp = t_0;
	else
		tmp = eps * ((x ^ 4.0) + (4.0 * (x ^ 4.0)));
	end
	tmp_2 = tmp;
end
code[x_, eps_] := N[(N[Power[N[(x + eps), $MachinePrecision], 5.0], $MachinePrecision] - N[Power[x, 5.0], $MachinePrecision]), $MachinePrecision]
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, -2e-313], N[Not[LessEqual[t$95$0, 0.0]], $MachinePrecision]], t$95$0, N[(eps * N[(N[Power[x, 4.0], $MachinePrecision] + N[(4.0 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
{\left(x + \varepsilon\right)}^{5} - {x}^{5}
\begin{array}{l}
t_0 := {\left(x + \varepsilon\right)}^{5} - {x}^{5}\\
\mathbf{if}\;t_0 \leq -2 \cdot 10^{-313} \lor \neg \left(t_0 \leq 0\right):\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\varepsilon \cdot \left({x}^{4} + 4 \cdot {x}^{4}\right)\\


\end{array}

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.

Herbie found 8 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

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.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation?

  1. Split input into 2 regimes
  2. if (-.f64 (pow.f64 (+.f64 x eps) 5) (pow.f64 x 5)) < -1.99999999998e-313 or 0.0 < (-.f64 (pow.f64 (+.f64 x eps) 5) (pow.f64 x 5))

    1. Initial program 97.9%

      \[{\left(x + \varepsilon\right)}^{5} - {x}^{5} \]

    if -1.99999999998e-313 < (-.f64 (pow.f64 (+.f64 x eps) 5) (pow.f64 x 5)) < 0.0

    1. Initial program 84.0%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;{\left(x + \varepsilon\right)}^{5} - {x}^{5} \leq -2 \cdot 10^{-313} \lor \neg \left({\left(x + \varepsilon\right)}^{5} - {x}^{5} \leq 0\right):\\ \;\;\;\;{\left(x + \varepsilon\right)}^{5} - {x}^{5}\\ \mathbf{else}:\\ \;\;\;\;\varepsilon \cdot \left({x}^{4} + 4 \cdot {x}^{4}\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy99.3%
Cost40009
\[\begin{array}{l} t_0 := {\left(x + \varepsilon\right)}^{5} - {x}^{5}\\ \mathbf{if}\;t_0 \leq -2 \cdot 10^{-313} \lor \neg \left(t_0 \leq 0\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\varepsilon \cdot \left({x}^{4} + 4 \cdot {x}^{4}\right)\\ \end{array} \]
Alternative 2
Accuracy99.3%
Cost39881
\[\begin{array}{l} t_0 := {\left(x + \varepsilon\right)}^{5} - {x}^{5}\\ \mathbf{if}\;t_0 \leq -2 \cdot 10^{-313} \lor \neg \left(t_0 \leq 0\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\varepsilon \cdot \left(\left(x \cdot 5\right) \cdot {x}^{3}\right)\\ \end{array} \]
Alternative 3
Accuracy97.1%
Cost7176
\[\begin{array}{l} \mathbf{if}\;x \leq -8.5 \cdot 10^{-39}:\\ \;\;\;\;\varepsilon \cdot \left(\left(x \cdot x\right) \cdot \left(5 \cdot \left(x \cdot x\right)\right)\right)\\ \mathbf{elif}\;x \leq 2.65 \cdot 10^{-75}:\\ \;\;\;\;{\varepsilon}^{5}\\ \mathbf{else}:\\ \;\;\;\;\varepsilon \cdot \left(\left(x \cdot 5\right) \cdot {x}^{3}\right)\\ \end{array} \]
Alternative 4
Accuracy97.1%
Cost7176
\[\begin{array}{l} \mathbf{if}\;x \leq -8.5 \cdot 10^{-39}:\\ \;\;\;\;\varepsilon \cdot \left(\left(x \cdot x\right) \cdot \left(5 \cdot \left(x \cdot x\right)\right)\right)\\ \mathbf{elif}\;x \leq 2.65 \cdot 10^{-75}:\\ \;\;\;\;{\varepsilon}^{5}\\ \mathbf{else}:\\ \;\;\;\;{x}^{3} \cdot \left(\varepsilon \cdot \left(x \cdot 5\right)\right)\\ \end{array} \]
Alternative 5
Accuracy97.1%
Cost7048
\[\begin{array}{l} \mathbf{if}\;x \leq -8.5 \cdot 10^{-39}:\\ \;\;\;\;\varepsilon \cdot \left(\left(x \cdot x\right) \cdot \left(5 \cdot \left(x \cdot x\right)\right)\right)\\ \mathbf{elif}\;x \leq 2.65 \cdot 10^{-75}:\\ \;\;\;\;{\varepsilon}^{5}\\ \mathbf{else}:\\ \;\;\;\;5 \cdot \left(\varepsilon \cdot {x}^{4}\right)\\ \end{array} \]
Alternative 6
Accuracy97.1%
Cost7048
\[\begin{array}{l} \mathbf{if}\;x \leq -8.5 \cdot 10^{-39}:\\ \;\;\;\;\varepsilon \cdot \left(\left(x \cdot x\right) \cdot \left(5 \cdot \left(x \cdot x\right)\right)\right)\\ \mathbf{elif}\;x \leq 2.65 \cdot 10^{-75}:\\ \;\;\;\;{\varepsilon}^{5}\\ \mathbf{else}:\\ \;\;\;\;{x}^{4} \cdot \left(\varepsilon \cdot 5\right)\\ \end{array} \]
Alternative 7
Accuracy97.1%
Cost6793
\[\begin{array}{l} \mathbf{if}\;x \leq -8.5 \cdot 10^{-39} \lor \neg \left(x \leq 2.65 \cdot 10^{-75}\right):\\ \;\;\;\;\varepsilon \cdot \left(\left(x \cdot x\right) \cdot \left(5 \cdot \left(x \cdot x\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;{\varepsilon}^{5}\\ \end{array} \]
Alternative 8
Accuracy82.9%
Cost704
\[\varepsilon \cdot \left(\left(x \cdot x\right) \cdot \left(5 \cdot \left(x \cdot x\right)\right)\right) \]

Reproduce?

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