Difference of squares

Percentage Accurate: 94.0% → 99.9%
Time: 2.7s
Alternatives: 3
Speedup: 0.6×

Specification

?
\[\begin{array}{l} \\ a \cdot a - b \cdot b \end{array} \]
(FPCore (a b) :precision binary64 (- (* a a) (* b b)))
double code(double a, double b) {
	return (a * a) - (b * b);
}
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = (a * a) - (b * b)
end function
public static double code(double a, double b) {
	return (a * a) - (b * b);
}
def code(a, b):
	return (a * a) - (b * b)
function code(a, b)
	return Float64(Float64(a * a) - Float64(b * b))
end
function tmp = code(a, b)
	tmp = (a * a) - (b * b);
end
code[a_, b_] := N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
a \cdot a - b \cdot b
\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 3 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: 94.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ a \cdot a - b \cdot b \end{array} \]
(FPCore (a b) :precision binary64 (- (* a a) (* b b)))
double code(double a, double b) {
	return (a * a) - (b * b);
}
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = (a * a) - (b * b)
end function
public static double code(double a, double b) {
	return (a * a) - (b * b);
}
def code(a, b):
	return (a * a) - (b * b)
function code(a, b)
	return Float64(Float64(a * a) - Float64(b * b))
end
function tmp = code(a, b)
	tmp = (a * a) - (b * b);
end
code[a_, b_] := N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
a \cdot a - b \cdot b
\end{array}

Alternative 1: 99.9% accurate, 0.6× speedup?

\[\begin{array}{l} a_m = \left|a\right| \\ b_m = \left|b\right| \\ \begin{array}{l} \mathbf{if}\;a_m \cdot a_m \leq 4 \cdot 10^{+283}:\\ \;\;\;\;a_m \cdot a_m - b_m \cdot b_m\\ \mathbf{else}:\\ \;\;\;\;a_m \cdot \left(a_m + b_m \cdot -2\right)\\ \end{array} \end{array} \]
a_m = (fabs.f64 a)
b_m = (fabs.f64 b)
(FPCore (a_m b_m)
 :precision binary64
 (if (<= (* a_m a_m) 4e+283)
   (- (* a_m a_m) (* b_m b_m))
   (* a_m (+ a_m (* b_m -2.0)))))
a_m = fabs(a);
b_m = fabs(b);
double code(double a_m, double b_m) {
	double tmp;
	if ((a_m * a_m) <= 4e+283) {
		tmp = (a_m * a_m) - (b_m * b_m);
	} else {
		tmp = a_m * (a_m + (b_m * -2.0));
	}
	return tmp;
}
a_m = abs(a)
b_m = abs(b)
real(8) function code(a_m, b_m)
    real(8), intent (in) :: a_m
    real(8), intent (in) :: b_m
    real(8) :: tmp
    if ((a_m * a_m) <= 4d+283) then
        tmp = (a_m * a_m) - (b_m * b_m)
    else
        tmp = a_m * (a_m + (b_m * (-2.0d0)))
    end if
    code = tmp
end function
a_m = Math.abs(a);
b_m = Math.abs(b);
public static double code(double a_m, double b_m) {
	double tmp;
	if ((a_m * a_m) <= 4e+283) {
		tmp = (a_m * a_m) - (b_m * b_m);
	} else {
		tmp = a_m * (a_m + (b_m * -2.0));
	}
	return tmp;
}
a_m = math.fabs(a)
b_m = math.fabs(b)
def code(a_m, b_m):
	tmp = 0
	if (a_m * a_m) <= 4e+283:
		tmp = (a_m * a_m) - (b_m * b_m)
	else:
		tmp = a_m * (a_m + (b_m * -2.0))
	return tmp
a_m = abs(a)
b_m = abs(b)
function code(a_m, b_m)
	tmp = 0.0
	if (Float64(a_m * a_m) <= 4e+283)
		tmp = Float64(Float64(a_m * a_m) - Float64(b_m * b_m));
	else
		tmp = Float64(a_m * Float64(a_m + Float64(b_m * -2.0)));
	end
	return tmp
end
a_m = abs(a);
b_m = abs(b);
function tmp_2 = code(a_m, b_m)
	tmp = 0.0;
	if ((a_m * a_m) <= 4e+283)
		tmp = (a_m * a_m) - (b_m * b_m);
	else
		tmp = a_m * (a_m + (b_m * -2.0));
	end
	tmp_2 = tmp;
end
a_m = N[Abs[a], $MachinePrecision]
b_m = N[Abs[b], $MachinePrecision]
code[a$95$m_, b$95$m_] := If[LessEqual[N[(a$95$m * a$95$m), $MachinePrecision], 4e+283], N[(N[(a$95$m * a$95$m), $MachinePrecision] - N[(b$95$m * b$95$m), $MachinePrecision]), $MachinePrecision], N[(a$95$m * N[(a$95$m + N[(b$95$m * -2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
a_m = \left|a\right|
\\
b_m = \left|b\right|

\\
\begin{array}{l}
\mathbf{if}\;a_m \cdot a_m \leq 4 \cdot 10^{+283}:\\
\;\;\;\;a_m \cdot a_m - b_m \cdot b_m\\

\mathbf{else}:\\
\;\;\;\;a_m \cdot \left(a_m + b_m \cdot -2\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 a a) < 3.99999999999999982e283

    1. Initial program 100.0%

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

    if 3.99999999999999982e283 < (*.f64 a a)

    1. Initial program 78.6%

      \[a \cdot a - b \cdot b \]
    2. Step-by-step derivation
      1. difference-of-squares100.0%

        \[\leadsto \color{blue}{\left(a + b\right) \cdot \left(a - b\right)} \]
      2. add-sqr-sqrt40.0%

        \[\leadsto \left(a + \color{blue}{\sqrt{b} \cdot \sqrt{b}}\right) \cdot \left(a - b\right) \]
      3. sqrt-prod84.3%

        \[\leadsto \left(a + \color{blue}{\sqrt{b \cdot b}}\right) \cdot \left(a - b\right) \]
      4. sqr-neg84.3%

        \[\leadsto \left(a + \sqrt{\color{blue}{\left(-b\right) \cdot \left(-b\right)}}\right) \cdot \left(a - b\right) \]
      5. sqrt-unprod50.0%

        \[\leadsto \left(a + \color{blue}{\sqrt{-b} \cdot \sqrt{-b}}\right) \cdot \left(a - b\right) \]
      6. add-sqr-sqrt84.3%

        \[\leadsto \left(a + \color{blue}{\left(-b\right)}\right) \cdot \left(a - b\right) \]
      7. sub-neg84.3%

        \[\leadsto \color{blue}{\left(a - b\right)} \cdot \left(a - b\right) \]
      8. pow184.3%

        \[\leadsto \color{blue}{{\left(a - b\right)}^{1}} \cdot \left(a - b\right) \]
      9. pow184.3%

        \[\leadsto {\left(a - b\right)}^{1} \cdot \color{blue}{{\left(a - b\right)}^{1}} \]
      10. pow-prod-up84.3%

        \[\leadsto \color{blue}{{\left(a - b\right)}^{\left(1 + 1\right)}} \]
      11. metadata-eval84.3%

        \[\leadsto {\left(a - b\right)}^{\color{blue}{2}} \]
      12. add-sqr-sqrt38.5%

        \[\leadsto {\left(\color{blue}{\sqrt{a} \cdot \sqrt{a}} - b\right)}^{2} \]
      13. add-sqr-sqrt10.0%

        \[\leadsto {\left(\sqrt{a} \cdot \sqrt{a} - \color{blue}{\sqrt{b} \cdot \sqrt{b}}\right)}^{2} \]
      14. difference-of-squares10.0%

        \[\leadsto {\color{blue}{\left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right)}}^{2} \]
      15. unpow-prod-down10.0%

        \[\leadsto \color{blue}{{\left(\sqrt{a} + \sqrt{b}\right)}^{2} \cdot {\left(\sqrt{a} - \sqrt{b}\right)}^{2}} \]
    3. Applied egg-rr10.0%

      \[\leadsto \color{blue}{{\left(\sqrt{a} + \sqrt{b}\right)}^{2} \cdot {\left(\sqrt{a} - \sqrt{b}\right)}^{2}} \]
    4. Step-by-step derivation
      1. unpow210.0%

        \[\leadsto \color{blue}{\left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} + \sqrt{b}\right)\right)} \cdot {\left(\sqrt{a} - \sqrt{b}\right)}^{2} \]
      2. unpow210.0%

        \[\leadsto \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} + \sqrt{b}\right)\right) \cdot \color{blue}{\left(\left(\sqrt{a} - \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right)} \]
      3. unswap-sqr10.0%

        \[\leadsto \color{blue}{\left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right)} \]
      4. difference-of-squares10.0%

        \[\leadsto \color{blue}{\left(\sqrt{a} \cdot \sqrt{a} - \sqrt{b} \cdot \sqrt{b}\right)} \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
      5. unpow1/210.0%

        \[\leadsto \left(\color{blue}{{a}^{0.5}} \cdot \sqrt{a} - \sqrt{b} \cdot \sqrt{b}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
      6. unpow1/210.0%

        \[\leadsto \left({a}^{0.5} \cdot \color{blue}{{a}^{0.5}} - \sqrt{b} \cdot \sqrt{b}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
      7. pow-sqr10.0%

        \[\leadsto \left(\color{blue}{{a}^{\left(2 \cdot 0.5\right)}} - \sqrt{b} \cdot \sqrt{b}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
      8. metadata-eval10.0%

        \[\leadsto \left({a}^{\color{blue}{1}} - \sqrt{b} \cdot \sqrt{b}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
      9. unpow110.0%

        \[\leadsto \left(\color{blue}{a} - \sqrt{b} \cdot \sqrt{b}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
      10. unpow1/210.0%

        \[\leadsto \left(a - \color{blue}{{b}^{0.5}} \cdot \sqrt{b}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
      11. unpow1/210.0%

        \[\leadsto \left(a - {b}^{0.5} \cdot \color{blue}{{b}^{0.5}}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
      12. pow-sqr10.0%

        \[\leadsto \left(a - \color{blue}{{b}^{\left(2 \cdot 0.5\right)}}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
      13. metadata-eval10.0%

        \[\leadsto \left(a - {b}^{\color{blue}{1}}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
      14. unpow110.0%

        \[\leadsto \left(a - \color{blue}{b}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
      15. difference-of-squares10.0%

        \[\leadsto \left(a - b\right) \cdot \color{blue}{\left(\sqrt{a} \cdot \sqrt{a} - \sqrt{b} \cdot \sqrt{b}\right)} \]
      16. unpow1/210.0%

        \[\leadsto \left(a - b\right) \cdot \left(\color{blue}{{a}^{0.5}} \cdot \sqrt{a} - \sqrt{b} \cdot \sqrt{b}\right) \]
      17. unpow1/210.0%

        \[\leadsto \left(a - b\right) \cdot \left({a}^{0.5} \cdot \color{blue}{{a}^{0.5}} - \sqrt{b} \cdot \sqrt{b}\right) \]
      18. pow-sqr34.3%

        \[\leadsto \left(a - b\right) \cdot \left(\color{blue}{{a}^{\left(2 \cdot 0.5\right)}} - \sqrt{b} \cdot \sqrt{b}\right) \]
      19. metadata-eval34.3%

        \[\leadsto \left(a - b\right) \cdot \left({a}^{\color{blue}{1}} - \sqrt{b} \cdot \sqrt{b}\right) \]
      20. unpow134.3%

        \[\leadsto \left(a - b\right) \cdot \left(\color{blue}{a} - \sqrt{b} \cdot \sqrt{b}\right) \]
    5. Simplified84.3%

      \[\leadsto \color{blue}{\left(a - b\right) \cdot \left(a - b\right)} \]
    6. Taylor expanded in a around inf 80.0%

      \[\leadsto \color{blue}{-2 \cdot \left(a \cdot b\right) + {a}^{2}} \]
    7. Step-by-step derivation
      1. *-commutative80.0%

        \[\leadsto \color{blue}{\left(a \cdot b\right) \cdot -2} + {a}^{2} \]
      2. associate-*l*80.0%

        \[\leadsto \color{blue}{a \cdot \left(b \cdot -2\right)} + {a}^{2} \]
      3. unpow280.0%

        \[\leadsto a \cdot \left(b \cdot -2\right) + \color{blue}{a \cdot a} \]
      4. distribute-lft-out88.6%

        \[\leadsto \color{blue}{a \cdot \left(b \cdot -2 + a\right)} \]
    8. Simplified88.6%

      \[\leadsto \color{blue}{a \cdot \left(b \cdot -2 + a\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification96.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;a \cdot a \leq 4 \cdot 10^{+283}:\\ \;\;\;\;a \cdot a - b \cdot b\\ \mathbf{else}:\\ \;\;\;\;a \cdot \left(a + b \cdot -2\right)\\ \end{array} \]

Alternative 2: 60.8% accurate, 1.0× speedup?

\[\begin{array}{l} a_m = \left|a\right| \\ b_m = \left|b\right| \\ a_m \cdot \left(a_m + b_m \cdot -2\right) \end{array} \]
a_m = (fabs.f64 a)
b_m = (fabs.f64 b)
(FPCore (a_m b_m) :precision binary64 (* a_m (+ a_m (* b_m -2.0))))
a_m = fabs(a);
b_m = fabs(b);
double code(double a_m, double b_m) {
	return a_m * (a_m + (b_m * -2.0));
}
a_m = abs(a)
b_m = abs(b)
real(8) function code(a_m, b_m)
    real(8), intent (in) :: a_m
    real(8), intent (in) :: b_m
    code = a_m * (a_m + (b_m * (-2.0d0)))
end function
a_m = Math.abs(a);
b_m = Math.abs(b);
public static double code(double a_m, double b_m) {
	return a_m * (a_m + (b_m * -2.0));
}
a_m = math.fabs(a)
b_m = math.fabs(b)
def code(a_m, b_m):
	return a_m * (a_m + (b_m * -2.0))
a_m = abs(a)
b_m = abs(b)
function code(a_m, b_m)
	return Float64(a_m * Float64(a_m + Float64(b_m * -2.0)))
end
a_m = abs(a);
b_m = abs(b);
function tmp = code(a_m, b_m)
	tmp = a_m * (a_m + (b_m * -2.0));
end
a_m = N[Abs[a], $MachinePrecision]
b_m = N[Abs[b], $MachinePrecision]
code[a$95$m_, b$95$m_] := N[(a$95$m * N[(a$95$m + N[(b$95$m * -2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
a_m = \left|a\right|
\\
b_m = \left|b\right|

\\
a_m \cdot \left(a_m + b_m \cdot -2\right)
\end{array}
Derivation
  1. Initial program 94.1%

    \[a \cdot a - b \cdot b \]
  2. Step-by-step derivation
    1. difference-of-squares100.0%

      \[\leadsto \color{blue}{\left(a + b\right) \cdot \left(a - b\right)} \]
    2. add-sqr-sqrt48.3%

      \[\leadsto \left(a + \color{blue}{\sqrt{b} \cdot \sqrt{b}}\right) \cdot \left(a - b\right) \]
    3. sqrt-prod75.3%

      \[\leadsto \left(a + \color{blue}{\sqrt{b \cdot b}}\right) \cdot \left(a - b\right) \]
    4. sqr-neg75.3%

      \[\leadsto \left(a + \sqrt{\color{blue}{\left(-b\right) \cdot \left(-b\right)}}\right) \cdot \left(a - b\right) \]
    5. sqrt-unprod28.5%

      \[\leadsto \left(a + \color{blue}{\sqrt{-b} \cdot \sqrt{-b}}\right) \cdot \left(a - b\right) \]
    6. add-sqr-sqrt51.4%

      \[\leadsto \left(a + \color{blue}{\left(-b\right)}\right) \cdot \left(a - b\right) \]
    7. sub-neg51.4%

      \[\leadsto \color{blue}{\left(a - b\right)} \cdot \left(a - b\right) \]
    8. pow151.4%

      \[\leadsto \color{blue}{{\left(a - b\right)}^{1}} \cdot \left(a - b\right) \]
    9. pow151.4%

      \[\leadsto {\left(a - b\right)}^{1} \cdot \color{blue}{{\left(a - b\right)}^{1}} \]
    10. pow-prod-up51.4%

      \[\leadsto \color{blue}{{\left(a - b\right)}^{\left(1 + 1\right)}} \]
    11. metadata-eval51.4%

      \[\leadsto {\left(a - b\right)}^{\color{blue}{2}} \]
    12. add-sqr-sqrt25.5%

      \[\leadsto {\left(\color{blue}{\sqrt{a} \cdot \sqrt{a}} - b\right)}^{2} \]
    13. add-sqr-sqrt10.0%

      \[\leadsto {\left(\sqrt{a} \cdot \sqrt{a} - \color{blue}{\sqrt{b} \cdot \sqrt{b}}\right)}^{2} \]
    14. difference-of-squares10.0%

      \[\leadsto {\color{blue}{\left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right)}}^{2} \]
    15. unpow-prod-down10.0%

      \[\leadsto \color{blue}{{\left(\sqrt{a} + \sqrt{b}\right)}^{2} \cdot {\left(\sqrt{a} - \sqrt{b}\right)}^{2}} \]
  3. Applied egg-rr10.0%

    \[\leadsto \color{blue}{{\left(\sqrt{a} + \sqrt{b}\right)}^{2} \cdot {\left(\sqrt{a} - \sqrt{b}\right)}^{2}} \]
  4. Step-by-step derivation
    1. unpow210.0%

      \[\leadsto \color{blue}{\left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} + \sqrt{b}\right)\right)} \cdot {\left(\sqrt{a} - \sqrt{b}\right)}^{2} \]
    2. unpow210.0%

      \[\leadsto \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} + \sqrt{b}\right)\right) \cdot \color{blue}{\left(\left(\sqrt{a} - \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right)} \]
    3. unswap-sqr10.0%

      \[\leadsto \color{blue}{\left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right)} \]
    4. difference-of-squares10.0%

      \[\leadsto \color{blue}{\left(\sqrt{a} \cdot \sqrt{a} - \sqrt{b} \cdot \sqrt{b}\right)} \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    5. unpow1/210.0%

      \[\leadsto \left(\color{blue}{{a}^{0.5}} \cdot \sqrt{a} - \sqrt{b} \cdot \sqrt{b}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    6. unpow1/210.0%

      \[\leadsto \left({a}^{0.5} \cdot \color{blue}{{a}^{0.5}} - \sqrt{b} \cdot \sqrt{b}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    7. pow-sqr10.0%

      \[\leadsto \left(\color{blue}{{a}^{\left(2 \cdot 0.5\right)}} - \sqrt{b} \cdot \sqrt{b}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    8. metadata-eval10.0%

      \[\leadsto \left({a}^{\color{blue}{1}} - \sqrt{b} \cdot \sqrt{b}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    9. unpow110.0%

      \[\leadsto \left(\color{blue}{a} - \sqrt{b} \cdot \sqrt{b}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    10. unpow1/210.0%

      \[\leadsto \left(a - \color{blue}{{b}^{0.5}} \cdot \sqrt{b}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    11. unpow1/210.0%

      \[\leadsto \left(a - {b}^{0.5} \cdot \color{blue}{{b}^{0.5}}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    12. pow-sqr10.0%

      \[\leadsto \left(a - \color{blue}{{b}^{\left(2 \cdot 0.5\right)}}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    13. metadata-eval10.0%

      \[\leadsto \left(a - {b}^{\color{blue}{1}}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    14. unpow110.0%

      \[\leadsto \left(a - \color{blue}{b}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    15. difference-of-squares10.0%

      \[\leadsto \left(a - b\right) \cdot \color{blue}{\left(\sqrt{a} \cdot \sqrt{a} - \sqrt{b} \cdot \sqrt{b}\right)} \]
    16. unpow1/210.0%

      \[\leadsto \left(a - b\right) \cdot \left(\color{blue}{{a}^{0.5}} \cdot \sqrt{a} - \sqrt{b} \cdot \sqrt{b}\right) \]
    17. unpow1/210.0%

      \[\leadsto \left(a - b\right) \cdot \left({a}^{0.5} \cdot \color{blue}{{a}^{0.5}} - \sqrt{b} \cdot \sqrt{b}\right) \]
    18. pow-sqr22.9%

      \[\leadsto \left(a - b\right) \cdot \left(\color{blue}{{a}^{\left(2 \cdot 0.5\right)}} - \sqrt{b} \cdot \sqrt{b}\right) \]
    19. metadata-eval22.9%

      \[\leadsto \left(a - b\right) \cdot \left({a}^{\color{blue}{1}} - \sqrt{b} \cdot \sqrt{b}\right) \]
    20. unpow122.9%

      \[\leadsto \left(a - b\right) \cdot \left(\color{blue}{a} - \sqrt{b} \cdot \sqrt{b}\right) \]
  5. Simplified51.4%

    \[\leadsto \color{blue}{\left(a - b\right) \cdot \left(a - b\right)} \]
  6. Taylor expanded in a around inf 52.5%

    \[\leadsto \color{blue}{-2 \cdot \left(a \cdot b\right) + {a}^{2}} \]
  7. Step-by-step derivation
    1. *-commutative52.5%

      \[\leadsto \color{blue}{\left(a \cdot b\right) \cdot -2} + {a}^{2} \]
    2. associate-*l*52.5%

      \[\leadsto \color{blue}{a \cdot \left(b \cdot -2\right)} + {a}^{2} \]
    3. unpow252.5%

      \[\leadsto a \cdot \left(b \cdot -2\right) + \color{blue}{a \cdot a} \]
    4. distribute-lft-out54.8%

      \[\leadsto \color{blue}{a \cdot \left(b \cdot -2 + a\right)} \]
  8. Simplified54.8%

    \[\leadsto \color{blue}{a \cdot \left(b \cdot -2 + a\right)} \]
  9. Final simplification54.8%

    \[\leadsto a \cdot \left(a + b \cdot -2\right) \]

Alternative 3: 14.6% accurate, 1.4× speedup?

\[\begin{array}{l} a_m = \left|a\right| \\ b_m = \left|b\right| \\ b_m \cdot \left(a_m \cdot -2\right) \end{array} \]
a_m = (fabs.f64 a)
b_m = (fabs.f64 b)
(FPCore (a_m b_m) :precision binary64 (* b_m (* a_m -2.0)))
a_m = fabs(a);
b_m = fabs(b);
double code(double a_m, double b_m) {
	return b_m * (a_m * -2.0);
}
a_m = abs(a)
b_m = abs(b)
real(8) function code(a_m, b_m)
    real(8), intent (in) :: a_m
    real(8), intent (in) :: b_m
    code = b_m * (a_m * (-2.0d0))
end function
a_m = Math.abs(a);
b_m = Math.abs(b);
public static double code(double a_m, double b_m) {
	return b_m * (a_m * -2.0);
}
a_m = math.fabs(a)
b_m = math.fabs(b)
def code(a_m, b_m):
	return b_m * (a_m * -2.0)
a_m = abs(a)
b_m = abs(b)
function code(a_m, b_m)
	return Float64(b_m * Float64(a_m * -2.0))
end
a_m = abs(a);
b_m = abs(b);
function tmp = code(a_m, b_m)
	tmp = b_m * (a_m * -2.0);
end
a_m = N[Abs[a], $MachinePrecision]
b_m = N[Abs[b], $MachinePrecision]
code[a$95$m_, b$95$m_] := N[(b$95$m * N[(a$95$m * -2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
a_m = \left|a\right|
\\
b_m = \left|b\right|

\\
b_m \cdot \left(a_m \cdot -2\right)
\end{array}
Derivation
  1. Initial program 94.1%

    \[a \cdot a - b \cdot b \]
  2. Step-by-step derivation
    1. difference-of-squares100.0%

      \[\leadsto \color{blue}{\left(a + b\right) \cdot \left(a - b\right)} \]
    2. add-sqr-sqrt48.3%

      \[\leadsto \left(a + \color{blue}{\sqrt{b} \cdot \sqrt{b}}\right) \cdot \left(a - b\right) \]
    3. sqrt-prod75.3%

      \[\leadsto \left(a + \color{blue}{\sqrt{b \cdot b}}\right) \cdot \left(a - b\right) \]
    4. sqr-neg75.3%

      \[\leadsto \left(a + \sqrt{\color{blue}{\left(-b\right) \cdot \left(-b\right)}}\right) \cdot \left(a - b\right) \]
    5. sqrt-unprod28.5%

      \[\leadsto \left(a + \color{blue}{\sqrt{-b} \cdot \sqrt{-b}}\right) \cdot \left(a - b\right) \]
    6. add-sqr-sqrt51.4%

      \[\leadsto \left(a + \color{blue}{\left(-b\right)}\right) \cdot \left(a - b\right) \]
    7. sub-neg51.4%

      \[\leadsto \color{blue}{\left(a - b\right)} \cdot \left(a - b\right) \]
    8. pow151.4%

      \[\leadsto \color{blue}{{\left(a - b\right)}^{1}} \cdot \left(a - b\right) \]
    9. pow151.4%

      \[\leadsto {\left(a - b\right)}^{1} \cdot \color{blue}{{\left(a - b\right)}^{1}} \]
    10. pow-prod-up51.4%

      \[\leadsto \color{blue}{{\left(a - b\right)}^{\left(1 + 1\right)}} \]
    11. metadata-eval51.4%

      \[\leadsto {\left(a - b\right)}^{\color{blue}{2}} \]
    12. add-sqr-sqrt25.5%

      \[\leadsto {\left(\color{blue}{\sqrt{a} \cdot \sqrt{a}} - b\right)}^{2} \]
    13. add-sqr-sqrt10.0%

      \[\leadsto {\left(\sqrt{a} \cdot \sqrt{a} - \color{blue}{\sqrt{b} \cdot \sqrt{b}}\right)}^{2} \]
    14. difference-of-squares10.0%

      \[\leadsto {\color{blue}{\left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right)}}^{2} \]
    15. unpow-prod-down10.0%

      \[\leadsto \color{blue}{{\left(\sqrt{a} + \sqrt{b}\right)}^{2} \cdot {\left(\sqrt{a} - \sqrt{b}\right)}^{2}} \]
  3. Applied egg-rr10.0%

    \[\leadsto \color{blue}{{\left(\sqrt{a} + \sqrt{b}\right)}^{2} \cdot {\left(\sqrt{a} - \sqrt{b}\right)}^{2}} \]
  4. Step-by-step derivation
    1. unpow210.0%

      \[\leadsto \color{blue}{\left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} + \sqrt{b}\right)\right)} \cdot {\left(\sqrt{a} - \sqrt{b}\right)}^{2} \]
    2. unpow210.0%

      \[\leadsto \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} + \sqrt{b}\right)\right) \cdot \color{blue}{\left(\left(\sqrt{a} - \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right)} \]
    3. unswap-sqr10.0%

      \[\leadsto \color{blue}{\left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right)} \]
    4. difference-of-squares10.0%

      \[\leadsto \color{blue}{\left(\sqrt{a} \cdot \sqrt{a} - \sqrt{b} \cdot \sqrt{b}\right)} \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    5. unpow1/210.0%

      \[\leadsto \left(\color{blue}{{a}^{0.5}} \cdot \sqrt{a} - \sqrt{b} \cdot \sqrt{b}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    6. unpow1/210.0%

      \[\leadsto \left({a}^{0.5} \cdot \color{blue}{{a}^{0.5}} - \sqrt{b} \cdot \sqrt{b}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    7. pow-sqr10.0%

      \[\leadsto \left(\color{blue}{{a}^{\left(2 \cdot 0.5\right)}} - \sqrt{b} \cdot \sqrt{b}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    8. metadata-eval10.0%

      \[\leadsto \left({a}^{\color{blue}{1}} - \sqrt{b} \cdot \sqrt{b}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    9. unpow110.0%

      \[\leadsto \left(\color{blue}{a} - \sqrt{b} \cdot \sqrt{b}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    10. unpow1/210.0%

      \[\leadsto \left(a - \color{blue}{{b}^{0.5}} \cdot \sqrt{b}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    11. unpow1/210.0%

      \[\leadsto \left(a - {b}^{0.5} \cdot \color{blue}{{b}^{0.5}}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    12. pow-sqr10.0%

      \[\leadsto \left(a - \color{blue}{{b}^{\left(2 \cdot 0.5\right)}}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    13. metadata-eval10.0%

      \[\leadsto \left(a - {b}^{\color{blue}{1}}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    14. unpow110.0%

      \[\leadsto \left(a - \color{blue}{b}\right) \cdot \left(\left(\sqrt{a} + \sqrt{b}\right) \cdot \left(\sqrt{a} - \sqrt{b}\right)\right) \]
    15. difference-of-squares10.0%

      \[\leadsto \left(a - b\right) \cdot \color{blue}{\left(\sqrt{a} \cdot \sqrt{a} - \sqrt{b} \cdot \sqrt{b}\right)} \]
    16. unpow1/210.0%

      \[\leadsto \left(a - b\right) \cdot \left(\color{blue}{{a}^{0.5}} \cdot \sqrt{a} - \sqrt{b} \cdot \sqrt{b}\right) \]
    17. unpow1/210.0%

      \[\leadsto \left(a - b\right) \cdot \left({a}^{0.5} \cdot \color{blue}{{a}^{0.5}} - \sqrt{b} \cdot \sqrt{b}\right) \]
    18. pow-sqr22.9%

      \[\leadsto \left(a - b\right) \cdot \left(\color{blue}{{a}^{\left(2 \cdot 0.5\right)}} - \sqrt{b} \cdot \sqrt{b}\right) \]
    19. metadata-eval22.9%

      \[\leadsto \left(a - b\right) \cdot \left({a}^{\color{blue}{1}} - \sqrt{b} \cdot \sqrt{b}\right) \]
    20. unpow122.9%

      \[\leadsto \left(a - b\right) \cdot \left(\color{blue}{a} - \sqrt{b} \cdot \sqrt{b}\right) \]
  5. Simplified51.4%

    \[\leadsto \color{blue}{\left(a - b\right) \cdot \left(a - b\right)} \]
  6. Taylor expanded in a around inf 52.5%

    \[\leadsto \color{blue}{-2 \cdot \left(a \cdot b\right) + {a}^{2}} \]
  7. Step-by-step derivation
    1. *-commutative52.5%

      \[\leadsto \color{blue}{\left(a \cdot b\right) \cdot -2} + {a}^{2} \]
    2. associate-*l*52.5%

      \[\leadsto \color{blue}{a \cdot \left(b \cdot -2\right)} + {a}^{2} \]
    3. unpow252.5%

      \[\leadsto a \cdot \left(b \cdot -2\right) + \color{blue}{a \cdot a} \]
    4. distribute-lft-out54.8%

      \[\leadsto \color{blue}{a \cdot \left(b \cdot -2 + a\right)} \]
  8. Simplified54.8%

    \[\leadsto \color{blue}{a \cdot \left(b \cdot -2 + a\right)} \]
  9. Taylor expanded in a around 0 15.4%

    \[\leadsto \color{blue}{-2 \cdot \left(a \cdot b\right)} \]
  10. Step-by-step derivation
    1. associate-*r*15.4%

      \[\leadsto \color{blue}{\left(-2 \cdot a\right) \cdot b} \]
    2. *-commutative15.4%

      \[\leadsto \color{blue}{b \cdot \left(-2 \cdot a\right)} \]
  11. Simplified15.4%

    \[\leadsto \color{blue}{b \cdot \left(-2 \cdot a\right)} \]
  12. Final simplification15.4%

    \[\leadsto b \cdot \left(a \cdot -2\right) \]

Developer target: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(a + b\right) \cdot \left(a - b\right) \end{array} \]
(FPCore (a b) :precision binary64 (* (+ a b) (- a b)))
double code(double a, double b) {
	return (a + b) * (a - b);
}
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = (a + b) * (a - b)
end function
public static double code(double a, double b) {
	return (a + b) * (a - b);
}
def code(a, b):
	return (a + b) * (a - b)
function code(a, b)
	return Float64(Float64(a + b) * Float64(a - b))
end
function tmp = code(a, b)
	tmp = (a + b) * (a - b);
end
code[a_, b_] := N[(N[(a + b), $MachinePrecision] * N[(a - b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(a + b\right) \cdot \left(a - b\right)
\end{array}

Reproduce

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

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

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