Examples.Basics.BasicTests:f2 from sbv-4.4

Percentage Accurate: 94.4% → 99.2%
Time: 3.0s
Alternatives: 3
Speedup: 0.5×

Specification

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

\\
x \cdot x - y \cdot y
\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.4% accurate, 1.0× speedup?

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

\\
x \cdot x - y \cdot y
\end{array}

Alternative 1: 99.2% accurate, 0.5× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;x\_m \cdot x\_m \leq 10^{+212}:\\
\;\;\;\;x\_m \cdot x\_m - y\_m \cdot y\_m\\

\mathbf{else}:\\
\;\;\;\;x\_m \cdot \left(x\_m + y\_m \cdot -2\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 x x) < 9.9999999999999991e211

    1. Initial program 100.0%

      \[x \cdot x - y \cdot y \]
    2. Add Preprocessing

    if 9.9999999999999991e211 < (*.f64 x x)

    1. Initial program 76.5%

      \[x \cdot x - y \cdot y \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. difference-of-squares100.0%

        \[\leadsto \color{blue}{\left(x + y\right) \cdot \left(x - y\right)} \]
      2. add-sqr-sqrt51.8%

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

        \[\leadsto \left(x + \color{blue}{\sqrt{y \cdot y}}\right) \cdot \left(x - y\right) \]
      4. sqr-neg84.7%

        \[\leadsto \left(x + \sqrt{\color{blue}{\left(-y\right) \cdot \left(-y\right)}}\right) \cdot \left(x - y\right) \]
      5. sqrt-unprod37.6%

        \[\leadsto \left(x + \color{blue}{\sqrt{-y} \cdot \sqrt{-y}}\right) \cdot \left(x - y\right) \]
      6. add-sqr-sqrt82.4%

        \[\leadsto \left(x + \color{blue}{\left(-y\right)}\right) \cdot \left(x - y\right) \]
      7. sub-neg82.4%

        \[\leadsto \color{blue}{\left(x - y\right)} \cdot \left(x - y\right) \]
      8. pow182.4%

        \[\leadsto \color{blue}{{\left(x - y\right)}^{1}} \cdot \left(x - y\right) \]
      9. pow182.4%

        \[\leadsto {\left(x - y\right)}^{1} \cdot \color{blue}{{\left(x - y\right)}^{1}} \]
      10. pow-prod-up82.4%

        \[\leadsto \color{blue}{{\left(x - y\right)}^{\left(1 + 1\right)}} \]
      11. add-sqr-sqrt44.5%

        \[\leadsto {\left(\color{blue}{\sqrt{x} \cdot \sqrt{x}} - y\right)}^{\left(1 + 1\right)} \]
      12. add-sqr-sqrt25.8%

        \[\leadsto {\left(\sqrt{x} \cdot \sqrt{x} - \color{blue}{\sqrt{y} \cdot \sqrt{y}}\right)}^{\left(1 + 1\right)} \]
      13. difference-of-squares25.8%

        \[\leadsto {\color{blue}{\left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right)}}^{\left(1 + 1\right)} \]
      14. metadata-eval25.8%

        \[\leadsto {\left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right)}^{\color{blue}{2}} \]
      15. unpow-prod-down25.8%

        \[\leadsto \color{blue}{{\left(\sqrt{x} + \sqrt{y}\right)}^{2} \cdot {\left(\sqrt{x} - \sqrt{y}\right)}^{2}} \]
    4. Applied egg-rr25.8%

      \[\leadsto \color{blue}{{\left(\sqrt{x} + \sqrt{y}\right)}^{2} \cdot {\left(\sqrt{x} - \sqrt{y}\right)}^{2}} \]
    5. Step-by-step derivation
      1. unpow225.8%

        \[\leadsto \color{blue}{\left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} + \sqrt{y}\right)\right)} \cdot {\left(\sqrt{x} - \sqrt{y}\right)}^{2} \]
      2. unpow225.8%

        \[\leadsto \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} + \sqrt{y}\right)\right) \cdot \color{blue}{\left(\left(\sqrt{x} - \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right)} \]
      3. unswap-sqr25.8%

        \[\leadsto \color{blue}{\left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right)} \]
      4. difference-of-squares25.8%

        \[\leadsto \color{blue}{\left(\sqrt{x} \cdot \sqrt{x} - \sqrt{y} \cdot \sqrt{y}\right)} \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
      5. unpow1/225.8%

        \[\leadsto \left(\color{blue}{{x}^{0.5}} \cdot \sqrt{x} - \sqrt{y} \cdot \sqrt{y}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
      6. unpow1/225.8%

        \[\leadsto \left({x}^{0.5} \cdot \color{blue}{{x}^{0.5}} - \sqrt{y} \cdot \sqrt{y}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
      7. pow-sqr25.8%

        \[\leadsto \left(\color{blue}{{x}^{\left(2 \cdot 0.5\right)}} - \sqrt{y} \cdot \sqrt{y}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
      8. metadata-eval25.8%

        \[\leadsto \left({x}^{\color{blue}{1}} - \sqrt{y} \cdot \sqrt{y}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
      9. unpow125.8%

        \[\leadsto \left(\color{blue}{x} - \sqrt{y} \cdot \sqrt{y}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
      10. unpow1/225.8%

        \[\leadsto \left(x - \color{blue}{{y}^{0.5}} \cdot \sqrt{y}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
      11. unpow1/225.8%

        \[\leadsto \left(x - {y}^{0.5} \cdot \color{blue}{{y}^{0.5}}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
      12. pow-sqr25.8%

        \[\leadsto \left(x - \color{blue}{{y}^{\left(2 \cdot 0.5\right)}}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
      13. metadata-eval25.8%

        \[\leadsto \left(x - {y}^{\color{blue}{1}}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
      14. unpow125.8%

        \[\leadsto \left(x - \color{blue}{y}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
      15. difference-of-squares25.8%

        \[\leadsto \left(x - y\right) \cdot \color{blue}{\left(\sqrt{x} \cdot \sqrt{x} - \sqrt{y} \cdot \sqrt{y}\right)} \]
      16. unpow1/225.8%

        \[\leadsto \left(x - y\right) \cdot \left(\color{blue}{{x}^{0.5}} \cdot \sqrt{x} - \sqrt{y} \cdot \sqrt{y}\right) \]
      17. unpow1/225.8%

        \[\leadsto \left(x - y\right) \cdot \left({x}^{0.5} \cdot \color{blue}{{x}^{0.5}} - \sqrt{y} \cdot \sqrt{y}\right) \]
      18. pow-sqr44.7%

        \[\leadsto \left(x - y\right) \cdot \left(\color{blue}{{x}^{\left(2 \cdot 0.5\right)}} - \sqrt{y} \cdot \sqrt{y}\right) \]
      19. metadata-eval44.7%

        \[\leadsto \left(x - y\right) \cdot \left({x}^{\color{blue}{1}} - \sqrt{y} \cdot \sqrt{y}\right) \]
      20. unpow144.7%

        \[\leadsto \left(x - y\right) \cdot \left(\color{blue}{x} - \sqrt{y} \cdot \sqrt{y}\right) \]
    6. Simplified82.4%

      \[\leadsto \color{blue}{\left(x - y\right) \cdot \left(x - y\right)} \]
    7. Taylor expanded in x around inf 75.3%

      \[\leadsto \color{blue}{-2 \cdot \left(x \cdot y\right) + {x}^{2}} \]
    8. Step-by-step derivation
      1. *-commutative75.3%

        \[\leadsto \color{blue}{\left(x \cdot y\right) \cdot -2} + {x}^{2} \]
      2. associate-*l*75.3%

        \[\leadsto \color{blue}{x \cdot \left(y \cdot -2\right)} + {x}^{2} \]
      3. unpow275.3%

        \[\leadsto x \cdot \left(y \cdot -2\right) + \color{blue}{x \cdot x} \]
      4. distribute-lft-out91.8%

        \[\leadsto \color{blue}{x \cdot \left(y \cdot -2 + x\right)} \]
    9. Simplified91.8%

      \[\leadsto \color{blue}{x \cdot \left(y \cdot -2 + x\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification97.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot x \leq 10^{+212}:\\ \;\;\;\;x \cdot x - y \cdot y\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(x + y \cdot -2\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 60.3% accurate, 1.0× speedup?

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

\\
x\_m \cdot \left(x\_m + y\_m \cdot -2\right)
\end{array}
Derivation
  1. Initial program 92.2%

    \[x \cdot x - y \cdot y \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. difference-of-squares100.0%

      \[\leadsto \color{blue}{\left(x + y\right) \cdot \left(x - y\right)} \]
    2. add-sqr-sqrt49.1%

      \[\leadsto \left(x + \color{blue}{\sqrt{y} \cdot \sqrt{y}}\right) \cdot \left(x - y\right) \]
    3. sqrt-prod73.2%

      \[\leadsto \left(x + \color{blue}{\sqrt{y \cdot y}}\right) \cdot \left(x - y\right) \]
    4. sqr-neg73.2%

      \[\leadsto \left(x + \sqrt{\color{blue}{\left(-y\right) \cdot \left(-y\right)}}\right) \cdot \left(x - y\right) \]
    5. sqrt-unprod25.7%

      \[\leadsto \left(x + \color{blue}{\sqrt{-y} \cdot \sqrt{-y}}\right) \cdot \left(x - y\right) \]
    6. add-sqr-sqrt55.0%

      \[\leadsto \left(x + \color{blue}{\left(-y\right)}\right) \cdot \left(x - y\right) \]
    7. sub-neg55.0%

      \[\leadsto \color{blue}{\left(x - y\right)} \cdot \left(x - y\right) \]
    8. pow155.0%

      \[\leadsto \color{blue}{{\left(x - y\right)}^{1}} \cdot \left(x - y\right) \]
    9. pow155.0%

      \[\leadsto {\left(x - y\right)}^{1} \cdot \color{blue}{{\left(x - y\right)}^{1}} \]
    10. pow-prod-up55.0%

      \[\leadsto \color{blue}{{\left(x - y\right)}^{\left(1 + 1\right)}} \]
    11. add-sqr-sqrt28.9%

      \[\leadsto {\left(\color{blue}{\sqrt{x} \cdot \sqrt{x}} - y\right)}^{\left(1 + 1\right)} \]
    12. add-sqr-sqrt16.0%

      \[\leadsto {\left(\sqrt{x} \cdot \sqrt{x} - \color{blue}{\sqrt{y} \cdot \sqrt{y}}\right)}^{\left(1 + 1\right)} \]
    13. difference-of-squares16.0%

      \[\leadsto {\color{blue}{\left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right)}}^{\left(1 + 1\right)} \]
    14. metadata-eval16.0%

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

      \[\leadsto \color{blue}{{\left(\sqrt{x} + \sqrt{y}\right)}^{2} \cdot {\left(\sqrt{x} - \sqrt{y}\right)}^{2}} \]
  4. Applied egg-rr16.0%

    \[\leadsto \color{blue}{{\left(\sqrt{x} + \sqrt{y}\right)}^{2} \cdot {\left(\sqrt{x} - \sqrt{y}\right)}^{2}} \]
  5. Step-by-step derivation
    1. unpow216.0%

      \[\leadsto \color{blue}{\left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} + \sqrt{y}\right)\right)} \cdot {\left(\sqrt{x} - \sqrt{y}\right)}^{2} \]
    2. unpow216.0%

      \[\leadsto \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} + \sqrt{y}\right)\right) \cdot \color{blue}{\left(\left(\sqrt{x} - \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right)} \]
    3. unswap-sqr16.0%

      \[\leadsto \color{blue}{\left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right)} \]
    4. difference-of-squares16.0%

      \[\leadsto \color{blue}{\left(\sqrt{x} \cdot \sqrt{x} - \sqrt{y} \cdot \sqrt{y}\right)} \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    5. unpow1/216.0%

      \[\leadsto \left(\color{blue}{{x}^{0.5}} \cdot \sqrt{x} - \sqrt{y} \cdot \sqrt{y}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    6. unpow1/216.0%

      \[\leadsto \left({x}^{0.5} \cdot \color{blue}{{x}^{0.5}} - \sqrt{y} \cdot \sqrt{y}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    7. pow-sqr16.1%

      \[\leadsto \left(\color{blue}{{x}^{\left(2 \cdot 0.5\right)}} - \sqrt{y} \cdot \sqrt{y}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    8. metadata-eval16.1%

      \[\leadsto \left({x}^{\color{blue}{1}} - \sqrt{y} \cdot \sqrt{y}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    9. unpow116.1%

      \[\leadsto \left(\color{blue}{x} - \sqrt{y} \cdot \sqrt{y}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    10. unpow1/216.1%

      \[\leadsto \left(x - \color{blue}{{y}^{0.5}} \cdot \sqrt{y}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    11. unpow1/216.1%

      \[\leadsto \left(x - {y}^{0.5} \cdot \color{blue}{{y}^{0.5}}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    12. pow-sqr16.1%

      \[\leadsto \left(x - \color{blue}{{y}^{\left(2 \cdot 0.5\right)}}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    13. metadata-eval16.1%

      \[\leadsto \left(x - {y}^{\color{blue}{1}}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    14. unpow116.1%

      \[\leadsto \left(x - \color{blue}{y}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    15. difference-of-squares16.1%

      \[\leadsto \left(x - y\right) \cdot \color{blue}{\left(\sqrt{x} \cdot \sqrt{x} - \sqrt{y} \cdot \sqrt{y}\right)} \]
    16. unpow1/216.1%

      \[\leadsto \left(x - y\right) \cdot \left(\color{blue}{{x}^{0.5}} \cdot \sqrt{x} - \sqrt{y} \cdot \sqrt{y}\right) \]
    17. unpow1/216.1%

      \[\leadsto \left(x - y\right) \cdot \left({x}^{0.5} \cdot \color{blue}{{x}^{0.5}} - \sqrt{y} \cdot \sqrt{y}\right) \]
    18. pow-sqr29.4%

      \[\leadsto \left(x - y\right) \cdot \left(\color{blue}{{x}^{\left(2 \cdot 0.5\right)}} - \sqrt{y} \cdot \sqrt{y}\right) \]
    19. metadata-eval29.4%

      \[\leadsto \left(x - y\right) \cdot \left({x}^{\color{blue}{1}} - \sqrt{y} \cdot \sqrt{y}\right) \]
    20. unpow129.4%

      \[\leadsto \left(x - y\right) \cdot \left(\color{blue}{x} - \sqrt{y} \cdot \sqrt{y}\right) \]
  6. Simplified55.0%

    \[\leadsto \color{blue}{\left(x - y\right) \cdot \left(x - y\right)} \]
  7. Taylor expanded in x around inf 53.4%

    \[\leadsto \color{blue}{-2 \cdot \left(x \cdot y\right) + {x}^{2}} \]
  8. Step-by-step derivation
    1. *-commutative53.4%

      \[\leadsto \color{blue}{\left(x \cdot y\right) \cdot -2} + {x}^{2} \]
    2. associate-*l*53.4%

      \[\leadsto \color{blue}{x \cdot \left(y \cdot -2\right)} + {x}^{2} \]
    3. unpow253.4%

      \[\leadsto x \cdot \left(y \cdot -2\right) + \color{blue}{x \cdot x} \]
    4. distribute-lft-out58.9%

      \[\leadsto \color{blue}{x \cdot \left(y \cdot -2 + x\right)} \]
  9. Simplified58.9%

    \[\leadsto \color{blue}{x \cdot \left(y \cdot -2 + x\right)} \]
  10. Final simplification58.9%

    \[\leadsto x \cdot \left(x + y \cdot -2\right) \]
  11. Add Preprocessing

Alternative 3: 15.4% accurate, 1.4× speedup?

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

\\
y\_m \cdot \left(x\_m \cdot -2\right)
\end{array}
Derivation
  1. Initial program 92.2%

    \[x \cdot x - y \cdot y \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. difference-of-squares100.0%

      \[\leadsto \color{blue}{\left(x + y\right) \cdot \left(x - y\right)} \]
    2. add-sqr-sqrt49.1%

      \[\leadsto \left(x + \color{blue}{\sqrt{y} \cdot \sqrt{y}}\right) \cdot \left(x - y\right) \]
    3. sqrt-prod73.2%

      \[\leadsto \left(x + \color{blue}{\sqrt{y \cdot y}}\right) \cdot \left(x - y\right) \]
    4. sqr-neg73.2%

      \[\leadsto \left(x + \sqrt{\color{blue}{\left(-y\right) \cdot \left(-y\right)}}\right) \cdot \left(x - y\right) \]
    5. sqrt-unprod25.7%

      \[\leadsto \left(x + \color{blue}{\sqrt{-y} \cdot \sqrt{-y}}\right) \cdot \left(x - y\right) \]
    6. add-sqr-sqrt55.0%

      \[\leadsto \left(x + \color{blue}{\left(-y\right)}\right) \cdot \left(x - y\right) \]
    7. sub-neg55.0%

      \[\leadsto \color{blue}{\left(x - y\right)} \cdot \left(x - y\right) \]
    8. pow155.0%

      \[\leadsto \color{blue}{{\left(x - y\right)}^{1}} \cdot \left(x - y\right) \]
    9. pow155.0%

      \[\leadsto {\left(x - y\right)}^{1} \cdot \color{blue}{{\left(x - y\right)}^{1}} \]
    10. pow-prod-up55.0%

      \[\leadsto \color{blue}{{\left(x - y\right)}^{\left(1 + 1\right)}} \]
    11. add-sqr-sqrt28.9%

      \[\leadsto {\left(\color{blue}{\sqrt{x} \cdot \sqrt{x}} - y\right)}^{\left(1 + 1\right)} \]
    12. add-sqr-sqrt16.0%

      \[\leadsto {\left(\sqrt{x} \cdot \sqrt{x} - \color{blue}{\sqrt{y} \cdot \sqrt{y}}\right)}^{\left(1 + 1\right)} \]
    13. difference-of-squares16.0%

      \[\leadsto {\color{blue}{\left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right)}}^{\left(1 + 1\right)} \]
    14. metadata-eval16.0%

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

      \[\leadsto \color{blue}{{\left(\sqrt{x} + \sqrt{y}\right)}^{2} \cdot {\left(\sqrt{x} - \sqrt{y}\right)}^{2}} \]
  4. Applied egg-rr16.0%

    \[\leadsto \color{blue}{{\left(\sqrt{x} + \sqrt{y}\right)}^{2} \cdot {\left(\sqrt{x} - \sqrt{y}\right)}^{2}} \]
  5. Step-by-step derivation
    1. unpow216.0%

      \[\leadsto \color{blue}{\left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} + \sqrt{y}\right)\right)} \cdot {\left(\sqrt{x} - \sqrt{y}\right)}^{2} \]
    2. unpow216.0%

      \[\leadsto \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} + \sqrt{y}\right)\right) \cdot \color{blue}{\left(\left(\sqrt{x} - \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right)} \]
    3. unswap-sqr16.0%

      \[\leadsto \color{blue}{\left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right)} \]
    4. difference-of-squares16.0%

      \[\leadsto \color{blue}{\left(\sqrt{x} \cdot \sqrt{x} - \sqrt{y} \cdot \sqrt{y}\right)} \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    5. unpow1/216.0%

      \[\leadsto \left(\color{blue}{{x}^{0.5}} \cdot \sqrt{x} - \sqrt{y} \cdot \sqrt{y}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    6. unpow1/216.0%

      \[\leadsto \left({x}^{0.5} \cdot \color{blue}{{x}^{0.5}} - \sqrt{y} \cdot \sqrt{y}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    7. pow-sqr16.1%

      \[\leadsto \left(\color{blue}{{x}^{\left(2 \cdot 0.5\right)}} - \sqrt{y} \cdot \sqrt{y}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    8. metadata-eval16.1%

      \[\leadsto \left({x}^{\color{blue}{1}} - \sqrt{y} \cdot \sqrt{y}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    9. unpow116.1%

      \[\leadsto \left(\color{blue}{x} - \sqrt{y} \cdot \sqrt{y}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    10. unpow1/216.1%

      \[\leadsto \left(x - \color{blue}{{y}^{0.5}} \cdot \sqrt{y}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    11. unpow1/216.1%

      \[\leadsto \left(x - {y}^{0.5} \cdot \color{blue}{{y}^{0.5}}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    12. pow-sqr16.1%

      \[\leadsto \left(x - \color{blue}{{y}^{\left(2 \cdot 0.5\right)}}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    13. metadata-eval16.1%

      \[\leadsto \left(x - {y}^{\color{blue}{1}}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    14. unpow116.1%

      \[\leadsto \left(x - \color{blue}{y}\right) \cdot \left(\left(\sqrt{x} + \sqrt{y}\right) \cdot \left(\sqrt{x} - \sqrt{y}\right)\right) \]
    15. difference-of-squares16.1%

      \[\leadsto \left(x - y\right) \cdot \color{blue}{\left(\sqrt{x} \cdot \sqrt{x} - \sqrt{y} \cdot \sqrt{y}\right)} \]
    16. unpow1/216.1%

      \[\leadsto \left(x - y\right) \cdot \left(\color{blue}{{x}^{0.5}} \cdot \sqrt{x} - \sqrt{y} \cdot \sqrt{y}\right) \]
    17. unpow1/216.1%

      \[\leadsto \left(x - y\right) \cdot \left({x}^{0.5} \cdot \color{blue}{{x}^{0.5}} - \sqrt{y} \cdot \sqrt{y}\right) \]
    18. pow-sqr29.4%

      \[\leadsto \left(x - y\right) \cdot \left(\color{blue}{{x}^{\left(2 \cdot 0.5\right)}} - \sqrt{y} \cdot \sqrt{y}\right) \]
    19. metadata-eval29.4%

      \[\leadsto \left(x - y\right) \cdot \left({x}^{\color{blue}{1}} - \sqrt{y} \cdot \sqrt{y}\right) \]
    20. unpow129.4%

      \[\leadsto \left(x - y\right) \cdot \left(\color{blue}{x} - \sqrt{y} \cdot \sqrt{y}\right) \]
  6. Simplified55.0%

    \[\leadsto \color{blue}{\left(x - y\right) \cdot \left(x - y\right)} \]
  7. Taylor expanded in x around inf 53.4%

    \[\leadsto \color{blue}{-2 \cdot \left(x \cdot y\right) + {x}^{2}} \]
  8. Step-by-step derivation
    1. *-commutative53.4%

      \[\leadsto \color{blue}{\left(x \cdot y\right) \cdot -2} + {x}^{2} \]
    2. associate-*l*53.4%

      \[\leadsto \color{blue}{x \cdot \left(y \cdot -2\right)} + {x}^{2} \]
    3. unpow253.4%

      \[\leadsto x \cdot \left(y \cdot -2\right) + \color{blue}{x \cdot x} \]
    4. distribute-lft-out58.9%

      \[\leadsto \color{blue}{x \cdot \left(y \cdot -2 + x\right)} \]
  9. Simplified58.9%

    \[\leadsto \color{blue}{x \cdot \left(y \cdot -2 + x\right)} \]
  10. Taylor expanded in x around 0 14.7%

    \[\leadsto \color{blue}{-2 \cdot \left(x \cdot y\right)} \]
  11. Step-by-step derivation
    1. associate-*r*14.7%

      \[\leadsto \color{blue}{\left(-2 \cdot x\right) \cdot y} \]
    2. *-commutative14.7%

      \[\leadsto \color{blue}{y \cdot \left(-2 \cdot x\right)} \]
  12. Simplified14.7%

    \[\leadsto \color{blue}{y \cdot \left(-2 \cdot x\right)} \]
  13. Final simplification14.7%

    \[\leadsto y \cdot \left(x \cdot -2\right) \]
  14. Add Preprocessing

Reproduce

?
herbie shell --seed 2024036 
(FPCore (x y)
  :name "Examples.Basics.BasicTests:f2 from sbv-4.4"
  :precision binary64
  (- (* x x) (* y y)))