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

?

Percentage Accurate: 75.0% → 100.0%
Time: 4.6s
Precision: binary64
Cost: 6848

?

\[\left(-1000000000 \leq x \land x \leq 1000000000\right) \land \left(-1 \leq \varepsilon \land \varepsilon \leq 1\right)\]
\[{\left(x + \varepsilon\right)}^{2} - {x}^{2} \]
\[\mathsf{fma}\left(2 \cdot x, \varepsilon, \varepsilon \cdot \varepsilon\right) \]
(FPCore (x eps) :precision binary64 (- (pow (+ x eps) 2.0) (pow x 2.0)))
(FPCore (x eps) :precision binary64 (fma (* 2.0 x) eps (* eps eps)))
double code(double x, double eps) {
	return pow((x + eps), 2.0) - pow(x, 2.0);
}
double code(double x, double eps) {
	return fma((2.0 * x), eps, (eps * eps));
}
function code(x, eps)
	return Float64((Float64(x + eps) ^ 2.0) - (x ^ 2.0))
end
function code(x, eps)
	return fma(Float64(2.0 * x), eps, Float64(eps * eps))
end
code[x_, eps_] := N[(N[Power[N[(x + eps), $MachinePrecision], 2.0], $MachinePrecision] - N[Power[x, 2.0], $MachinePrecision]), $MachinePrecision]
code[x_, eps_] := N[(N[(2.0 * x), $MachinePrecision] * eps + N[(eps * eps), $MachinePrecision]), $MachinePrecision]
{\left(x + \varepsilon\right)}^{2} - {x}^{2}
\mathsf{fma}\left(2 \cdot x, \varepsilon, \varepsilon \cdot \varepsilon\right)

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 5 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

Derivation?

  1. Initial program 74.7%

    \[{\left(x + \varepsilon\right)}^{2} - {x}^{2} \]
  2. Simplified100.0%

    \[\leadsto \color{blue}{\varepsilon \cdot \mathsf{fma}\left(2, x, \varepsilon\right)} \]
    Step-by-step derivation

    [Start]74.7%

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

    unpow2 [=>]74.7%

    \[ \color{blue}{\left(x + \varepsilon\right) \cdot \left(x + \varepsilon\right)} - {x}^{2} \]

    unpow2 [=>]74.7%

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

    difference-of-squares [=>]74.7%

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

    *-commutative [=>]74.7%

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

    +-commutative [=>]74.7%

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

    associate--l+ [=>]100.0%

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

    +-inverses [=>]100.0%

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

    +-rgt-identity [=>]100.0%

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

    +-commutative [=>]100.0%

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

    associate-+r+ [=>]100.0%

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

    count-2 [=>]100.0%

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

    fma-def [=>]100.0%

    \[ \varepsilon \cdot \color{blue}{\mathsf{fma}\left(2, x, \varepsilon\right)} \]
  3. Applied egg-rr100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(2 \cdot x, \varepsilon, \varepsilon \cdot \varepsilon\right)} \]
    Step-by-step derivation

    [Start]100.0%

    \[ \varepsilon \cdot \mathsf{fma}\left(2, x, \varepsilon\right) \]

    fma-udef [=>]100.0%

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

    distribute-rgt-in [=>]99.9%

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

    fma-def [=>]100.0%

    \[ \color{blue}{\mathsf{fma}\left(2 \cdot x, \varepsilon, \varepsilon \cdot \varepsilon\right)} \]
  4. Final simplification100.0%

    \[\leadsto \mathsf{fma}\left(2 \cdot x, \varepsilon, \varepsilon \cdot \varepsilon\right) \]

Alternatives

Alternative 1
Accuracy100.0%
Cost6848
\[\mathsf{fma}\left(2 \cdot x, \varepsilon, \varepsilon \cdot \varepsilon\right) \]
Alternative 2
Accuracy90.6%
Cost585
\[\begin{array}{l} \mathbf{if}\;x \leq -6 \cdot 10^{-81} \lor \neg \left(x \leq 4.1 \cdot 10^{-98}\right):\\ \;\;\;\;2 \cdot \left(x \cdot \varepsilon\right)\\ \mathbf{else}:\\ \;\;\;\;\varepsilon \cdot \varepsilon\\ \end{array} \]
Alternative 3
Accuracy90.5%
Cost584
\[\begin{array}{l} \mathbf{if}\;x \leq -1.16 \cdot 10^{-79}:\\ \;\;\;\;x \cdot \left(\varepsilon + \varepsilon\right)\\ \mathbf{elif}\;x \leq 1.4 \cdot 10^{-97}:\\ \;\;\;\;\varepsilon \cdot \varepsilon\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \left(x \cdot \varepsilon\right)\\ \end{array} \]
Alternative 4
Accuracy100.0%
Cost448
\[\varepsilon \cdot \left(2 \cdot x + \varepsilon\right) \]
Alternative 5
Accuracy72.6%
Cost192
\[\varepsilon \cdot \varepsilon \]

Reproduce?

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