Examples.Basics.BasicTests:f2 from sbv-4.4

Percentage Accurate: 93.0% → 98.2%
Time: 5.6s
Alternatives: 4
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 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.0% 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.2% accurate, 0.1× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;x\_m \cdot x\_m\\


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

    1. Initial program 92.7%

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

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

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

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

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

    if 2.0000000000000001e251 < x

    1. Initial program 80.0%

      \[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. sub-neg100.0%

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

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

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

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

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

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

      \[\leadsto \color{blue}{\left(x + y\right) \cdot \left(x + y\right)} \]
    5. Taylor expanded in x around inf 100.0%

      \[\leadsto \left(x + y\right) \cdot \color{blue}{x} \]
    6. Taylor expanded in x around inf 100.0%

      \[\leadsto \color{blue}{x} \cdot x \]
  3. Recombined 2 regimes into one program.
  4. Final simplification98.0%

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

Alternative 2: 93.0% accurate, 0.5× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;y \cdot y \leq \infty:\\ \;\;\;\;x\_m \cdot x\_m - y \cdot y\\ \mathbf{else}:\\ \;\;\;\;-y \cdot y\\ \end{array} \end{array} \]
x_m = (fabs.f64 x)
(FPCore (x_m y)
 :precision binary64
 (if (<= (* y y) INFINITY) (- (* x_m x_m) (* y y)) (- (* y y))))
x_m = fabs(x);
double code(double x_m, double y) {
	double tmp;
	if ((y * y) <= ((double) INFINITY)) {
		tmp = (x_m * x_m) - (y * y);
	} else {
		tmp = -(y * y);
	}
	return tmp;
}
x_m = Math.abs(x);
public static double code(double x_m, double y) {
	double tmp;
	if ((y * y) <= Double.POSITIVE_INFINITY) {
		tmp = (x_m * x_m) - (y * y);
	} else {
		tmp = -(y * y);
	}
	return tmp;
}
x_m = math.fabs(x)
def code(x_m, y):
	tmp = 0
	if (y * y) <= math.inf:
		tmp = (x_m * x_m) - (y * y)
	else:
		tmp = -(y * y)
	return tmp
x_m = abs(x)
function code(x_m, y)
	tmp = 0.0
	if (Float64(y * y) <= Inf)
		tmp = Float64(Float64(x_m * x_m) - Float64(y * y));
	else
		tmp = Float64(-Float64(y * y));
	end
	return tmp
end
x_m = abs(x);
function tmp_2 = code(x_m, y)
	tmp = 0.0;
	if ((y * y) <= Inf)
		tmp = (x_m * x_m) - (y * y);
	else
		tmp = -(y * y);
	end
	tmp_2 = tmp;
end
x_m = N[Abs[x], $MachinePrecision]
code[x$95$m_, y_] := If[LessEqual[N[(y * y), $MachinePrecision], Infinity], N[(N[(x$95$m * x$95$m), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision], (-N[(y * y), $MachinePrecision])]
\begin{array}{l}
x_m = \left|x\right|

\\
\begin{array}{l}
\mathbf{if}\;y \cdot y \leq \infty:\\
\;\;\;\;x\_m \cdot x\_m - y \cdot y\\

\mathbf{else}:\\
\;\;\;\;-y \cdot y\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 y y) < +inf.0

    1. Initial program 92.2%

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

    if +inf.0 < (*.f64 y y)

    1. Initial program 92.2%

      \[x \cdot x - y \cdot y \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0 54.9%

      \[\leadsto \color{blue}{-1 \cdot {y}^{2}} \]
    4. Step-by-step derivation
      1. neg-mul-154.9%

        \[\leadsto \color{blue}{-{y}^{2}} \]
    5. Simplified54.9%

      \[\leadsto \color{blue}{-{y}^{2}} \]
    6. Step-by-step derivation
      1. unpow254.9%

        \[\leadsto -\color{blue}{y \cdot y} \]
      2. distribute-lft-neg-in54.9%

        \[\leadsto \color{blue}{\left(-y\right) \cdot y} \]
    7. Applied egg-rr54.9%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \cdot y \leq \infty:\\ \;\;\;\;x \cdot x - y \cdot y\\ \mathbf{else}:\\ \;\;\;\;-y \cdot y\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 77.8% accurate, 0.6× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;-y \cdot y\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 y y) < 9.99999999999999953e-45

    1. Initial program 100.0%

      \[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. sub-neg100.0%

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

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

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

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

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

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

      \[\leadsto \color{blue}{\left(x + y\right) \cdot \left(x + y\right)} \]
    5. Taylor expanded in x around inf 84.3%

      \[\leadsto \left(x + y\right) \cdot \color{blue}{x} \]
    6. Taylor expanded in x around inf 84.2%

      \[\leadsto \color{blue}{x} \cdot x \]

    if 9.99999999999999953e-45 < (*.f64 y y)

    1. Initial program 84.4%

      \[x \cdot x - y \cdot y \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0 78.3%

      \[\leadsto \color{blue}{-1 \cdot {y}^{2}} \]
    4. Step-by-step derivation
      1. neg-mul-178.3%

        \[\leadsto \color{blue}{-{y}^{2}} \]
    5. Simplified78.3%

      \[\leadsto \color{blue}{-{y}^{2}} \]
    6. Step-by-step derivation
      1. unpow278.3%

        \[\leadsto -\color{blue}{y \cdot y} \]
      2. distribute-lft-neg-in78.3%

        \[\leadsto \color{blue}{\left(-y\right) \cdot y} \]
    7. Applied egg-rr78.3%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \cdot y \leq 10^{-44}:\\ \;\;\;\;x \cdot x\\ \mathbf{else}:\\ \;\;\;\;-y \cdot y\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 53.9% accurate, 2.3× speedup?

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

\\
x\_m \cdot x\_m
\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. sub-neg100.0%

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

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

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

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

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

      \[\leadsto \left(x + y\right) \cdot \left(x + \color{blue}{y}\right) \]
  4. Applied egg-rr52.6%

    \[\leadsto \color{blue}{\left(x + y\right) \cdot \left(x + y\right)} \]
  5. Taylor expanded in x around inf 57.1%

    \[\leadsto \left(x + y\right) \cdot \color{blue}{x} \]
  6. Taylor expanded in x around inf 53.1%

    \[\leadsto \color{blue}{x} \cdot x \]
  7. Add Preprocessing

Reproduce

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