Examples.Basics.BasicTests:f2 from sbv-4.4

Percentage Accurate: 93.6% → 98.3%
Time: 3.4s
Alternatives: 4
Speedup: 0.8×

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 4 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: 93.6% 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: 98.3% accurate, 0.1× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;x_m \leq 10^{+206}:\\ \;\;\;\;\mathsf{fma}\left(x_m, x_m, y \cdot \left(-y\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x_m - y\right) \cdot \left(x_m - y\right)\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m y)
 :precision binary64
 (if (<= x_m 1e+206) (fma x_m x_m (* y (- y))) (* (- x_m y) (- x_m y))))
x_m = fabs(x);
double code(double x_m, double y) {
	double tmp;
	if (x_m <= 1e+206) {
		tmp = fma(x_m, x_m, (y * -y));
	} else {
		tmp = (x_m - y) * (x_m - y);
	}
	return tmp;
}
x_m = abs(x)
function code(x_m, y)
	tmp = 0.0
	if (x_m <= 1e+206)
		tmp = fma(x_m, x_m, Float64(y * Float64(-y)));
	else
		tmp = Float64(Float64(x_m - y) * Float64(x_m - y));
	end
	return tmp
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_, y_] := If[LessEqual[x$95$m, 1e+206], N[(x$95$m * x$95$m + N[(y * (-y)), $MachinePrecision]), $MachinePrecision], N[(N[(x$95$m - y), $MachinePrecision] * N[(x$95$m - y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|

\\
\begin{array}{l}
\mathbf{if}\;x_m \leq 10^{+206}:\\
\;\;\;\;\mathsf{fma}\left(x_m, x_m, y \cdot \left(-y\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\left(x_m - y\right) \cdot \left(x_m - y\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 1e206

    1. Initial program 93.7%

      \[x \cdot x - y \cdot y \]
    2. Step-by-step derivation
      1. sqr-neg93.7%

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

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, x, y \cdot \left(-y\right)\right)} \]
    3. Simplified97.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, x, y \cdot \left(-y\right)\right)} \]

    if 1e206 < x

    1. Initial program 78.9%

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{{\left(x - y\right)}^{\left(1 + 1\right)}} \]
      11. metadata-eval94.7%

        \[\leadsto {\left(x - y\right)}^{\color{blue}{2}} \]
      12. add-sqr-sqrt94.7%

        \[\leadsto {\left(\color{blue}{\sqrt{x} \cdot \sqrt{x}} - y\right)}^{2} \]
      13. add-sqr-sqrt68.4%

        \[\leadsto {\left(\sqrt{x} \cdot \sqrt{x} - \color{blue}{\sqrt{y} \cdot \sqrt{y}}\right)}^{2} \]
      14. difference-of-squares68.4%

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

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

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

        \[\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. unpow268.4%

        \[\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-sqr68.4%

        \[\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-squares68.4%

        \[\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/268.4%

        \[\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/268.4%

        \[\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-sqr68.4%

        \[\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-eval68.4%

        \[\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. unpow168.4%

        \[\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/268.4%

        \[\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/268.4%

        \[\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-sqr68.4%

        \[\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-eval68.4%

        \[\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. unpow168.4%

        \[\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-squares68.4%

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

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

        \[\leadsto \left(x - y\right) \cdot \left({x}^{0.5} \cdot \color{blue}{{x}^{0.5}} - \sqrt{y} \cdot \sqrt{y}\right) \]
      18. pow-sqr68.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-eval68.4%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 10^{+206}:\\ \;\;\;\;\mathsf{fma}\left(x, x, y \cdot \left(-y\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x - y\right) \cdot \left(x - y\right)\\ \end{array} \]

Alternative 2: 76.7% accurate, 0.8× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;x_m \leq 1.9 \cdot 10^{-57}:\\
\;\;\;\;y \cdot \left(-y\right)\\

\mathbf{else}:\\
\;\;\;\;\left(x_m - y\right) \cdot \left(x_m - y\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 1.8999999999999999e-57

    1. Initial program 93.5%

      \[x \cdot x - y \cdot y \]
    2. Taylor expanded in x around 0 62.0%

      \[\leadsto \color{blue}{-1 \cdot {y}^{2}} \]
    3. Step-by-step derivation
      1. mul-1-neg62.0%

        \[\leadsto \color{blue}{-{y}^{2}} \]
    4. Simplified62.0%

      \[\leadsto \color{blue}{-{y}^{2}} \]
    5. Step-by-step derivation
      1. unpow262.0%

        \[\leadsto -\color{blue}{y \cdot y} \]
    6. Applied egg-rr62.0%

      \[\leadsto -\color{blue}{y \cdot y} \]

    if 1.8999999999999999e-57 < x

    1. Initial program 90.1%

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{{\left(x - y\right)}^{\left(1 + 1\right)}} \]
      11. metadata-eval74.8%

        \[\leadsto {\left(x - y\right)}^{\color{blue}{2}} \]
      12. add-sqr-sqrt74.4%

        \[\leadsto {\left(\color{blue}{\sqrt{x} \cdot \sqrt{x}} - y\right)}^{2} \]
      13. add-sqr-sqrt38.2%

        \[\leadsto {\left(\sqrt{x} \cdot \sqrt{x} - \color{blue}{\sqrt{y} \cdot \sqrt{y}}\right)}^{2} \]
      14. difference-of-squares38.2%

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

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

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

        \[\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. unpow238.2%

        \[\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-sqr38.2%

        \[\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-squares38.2%

        \[\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/238.2%

        \[\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/238.2%

        \[\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-sqr38.3%

        \[\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-eval38.3%

        \[\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. unpow138.3%

        \[\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/238.3%

        \[\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/238.3%

        \[\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-sqr38.3%

        \[\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-eval38.3%

        \[\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. unpow138.3%

        \[\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-squares38.3%

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

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

        \[\leadsto \left(x - y\right) \cdot \left({x}^{0.5} \cdot \color{blue}{{x}^{0.5}} - \sqrt{y} \cdot \sqrt{y}\right) \]
      18. pow-sqr38.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-eval38.4%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 1.9 \cdot 10^{-57}:\\ \;\;\;\;y \cdot \left(-y\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x - y\right) \cdot \left(x - y\right)\\ \end{array} \]

Alternative 3: 96.7% accurate, 0.8× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;x_m \leq 1.18 \cdot 10^{+147}:\\
\;\;\;\;x_m \cdot x_m - y \cdot y\\

\mathbf{else}:\\
\;\;\;\;\left(x_m - y\right) \cdot \left(x_m - y\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 1.18000000000000006e147

    1. Initial program 94.6%

      \[x \cdot x - y \cdot y \]

    if 1.18000000000000006e147 < x

    1. Initial program 78.8%

      \[x \cdot x - y \cdot y \]
    2. 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.5%

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

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

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

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

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

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

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

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

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

        \[\leadsto {\left(x - y\right)}^{\color{blue}{2}} \]
      12. add-sqr-sqrt87.9%

        \[\leadsto {\left(\color{blue}{\sqrt{x} \cdot \sqrt{x}} - y\right)}^{2} \]
      13. add-sqr-sqrt48.5%

        \[\leadsto {\left(\sqrt{x} \cdot \sqrt{x} - \color{blue}{\sqrt{y} \cdot \sqrt{y}}\right)}^{2} \]
      14. difference-of-squares48.5%

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

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

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

        \[\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. unpow248.5%

        \[\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-sqr48.5%

        \[\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-squares48.5%

        \[\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/248.5%

        \[\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/248.5%

        \[\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-sqr48.5%

        \[\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-eval48.5%

        \[\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. unpow148.5%

        \[\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/248.5%

        \[\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/248.5%

        \[\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-sqr48.5%

        \[\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-eval48.5%

        \[\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. unpow148.5%

        \[\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-squares48.5%

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 1.18 \cdot 10^{+147}:\\ \;\;\;\;x \cdot x - y \cdot y\\ \mathbf{else}:\\ \;\;\;\;\left(x - y\right) \cdot \left(x - y\right)\\ \end{array} \]

Alternative 4: 52.8% accurate, 1.8× speedup?

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

\\
y \cdot \left(-y\right)
\end{array}
Derivation
  1. Initial program 92.6%

    \[x \cdot x - y \cdot y \]
  2. Taylor expanded in x around 0 51.8%

    \[\leadsto \color{blue}{-1 \cdot {y}^{2}} \]
  3. Step-by-step derivation
    1. mul-1-neg51.8%

      \[\leadsto \color{blue}{-{y}^{2}} \]
  4. Simplified51.8%

    \[\leadsto \color{blue}{-{y}^{2}} \]
  5. Step-by-step derivation
    1. unpow251.8%

      \[\leadsto -\color{blue}{y \cdot y} \]
  6. Applied egg-rr51.8%

    \[\leadsto -\color{blue}{y \cdot y} \]
  7. Final simplification51.8%

    \[\leadsto y \cdot \left(-y\right) \]

Reproduce

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