Examples.Basics.BasicTests:f2 from sbv-4.4

Percentage Accurate: 93.2% → 98.0%
Time: 2.9s
Alternatives: 3
Speedup: 0.6×

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: 93.2% 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.0% accurate, 0.1× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;x\_m \leq 10^{+220}:\\ \;\;\;\;\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+220) (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+220) {
		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+220)
		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+220], 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^{+220}:\\
\;\;\;\;\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 < 1e220

    1. Initial program 94.9%

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

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

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

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

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

    if 1e220 < x

    1. Initial program 73.7%

      \[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-sqrt36.8%

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

        \[\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)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification96.9%

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

Alternative 2: 96.2% accurate, 0.6× speedup?

\[\begin{array}{l} x_m = \left|x\right| \\ \begin{array}{l} \mathbf{if}\;x\_m \leq 2.3 \cdot 10^{+150}:\\ \;\;\;\;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 2.3e+150) (- (* 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 <= 2.3e+150) {
		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 <= 2.3d+150) 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 <= 2.3e+150) {
		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 <= 2.3e+150:
		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 <= 2.3e+150)
		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 <= 2.3e+150)
		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, 2.3e+150], 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 2.3 \cdot 10^{+150}:\\
\;\;\;\;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 < 2.30000000000000001e150

    1. Initial program 95.6%

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

    if 2.30000000000000001e150 < x

    1. Initial program 77.4%

      \[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-sqrt51.6%

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

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

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

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

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

Alternative 3: 52.8% accurate, 1.0× speedup?

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

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

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

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

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

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

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

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

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

    \[\leadsto \left(x + y\right) \cdot \left(x + y\right) \]
  6. Add Preprocessing

Reproduce

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