Difference of squares

?

Percentage Accurate: 93.8% → 96.9%
Time: 2.2s
Precision: binary64
Cost: 6784

?

\[a \cdot a - b \cdot b \]
\[\mathsf{fma}\left(a, a, b \cdot \left(-b\right)\right) \]
(FPCore (a b) :precision binary64 (- (* a a) (* b b)))
(FPCore (a b) :precision binary64 (fma a a (* b (- b))))
double code(double a, double b) {
	return (a * a) - (b * b);
}
double code(double a, double b) {
	return fma(a, a, (b * -b));
}
function code(a, b)
	return Float64(Float64(a * a) - Float64(b * b))
end
function code(a, b)
	return fma(a, a, Float64(b * Float64(-b)))
end
code[a_, b_] := N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]
code[a_, b_] := N[(a * a + N[(b * (-b)), $MachinePrecision]), $MachinePrecision]
a \cdot a - b \cdot b
\mathsf{fma}\left(a, a, b \cdot \left(-b\right)\right)

Local Percentage Accuracy?

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.

Target

Original93.8%
Target100.0%
Herbie96.9%
\[\left(a + b\right) \cdot \left(a - b\right) \]

Derivation?

  1. Initial program 92.6%

    \[a \cdot a - b \cdot b \]
  2. Simplified96.9%

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

    [Start]92.6

    \[ a \cdot a - b \cdot b \]

    fma-neg [=>]96.9

    \[ \color{blue}{\mathsf{fma}\left(a, a, -b \cdot b\right)} \]

    distribute-rgt-neg-in [=>]96.9

    \[ \mathsf{fma}\left(a, a, \color{blue}{b \cdot \left(-b\right)}\right) \]
  3. Final simplification96.9%

    \[\leadsto \mathsf{fma}\left(a, a, b \cdot \left(-b\right)\right) \]

Alternatives

Alternative 1
Accuracy76.7%
Cost786
\[\begin{array}{l} \mathbf{if}\;b \leq -3.8 \cdot 10^{+81} \lor \neg \left(b \leq -2.6 \cdot 10^{-21}\right) \land \left(b \leq -5.3 \cdot 10^{-89} \lor \neg \left(b \leq 2500000000000\right)\right):\\ \;\;\;\;b \cdot \left(-b\right)\\ \mathbf{else}:\\ \;\;\;\;a \cdot a\\ \end{array} \]
Alternative 2
Accuracy96.8%
Cost708
\[\begin{array}{l} \mathbf{if}\;b \cdot b \leq 4 \cdot 10^{+305}:\\ \;\;\;\;a \cdot a - b \cdot b\\ \mathbf{else}:\\ \;\;\;\;b \cdot \left(-b\right)\\ \end{array} \]
Alternative 3
Accuracy54.1%
Cost192
\[a \cdot a \]

Error

Reproduce?

herbie shell --seed 2023159 
(FPCore (a b)
  :name "Difference of squares"
  :precision binary64

  :herbie-target
  (* (+ a b) (- a b))

  (- (* a a) (* b b)))