Examples.Basics.BasicTests:f2 from sbv-4.4

Percentage Accurate: 93.4% → 99.8%
Time: 3.4s
Alternatives: 4
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 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.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.8% accurate, 0.1× 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 4 \cdot 10^{+260}:\\ \;\;\;\;\mathsf{fma}\left(x_m, x_m, y_m \cdot \left(-y_m\right)\right)\\ \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) 4e+260)
   (fma 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) <= 4e+260) {
		tmp = fma(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) <= 4e+260)
		tmp = fma(x_m, x_m, Float64(y_m * Float64(-y_m)));
	else
		tmp = Float64(x_m * Float64(x_m + Float64(y_m * -2.0)));
	end
	return 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], 4e+260], N[(x$95$m * x$95$m + 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 4 \cdot 10^{+260}:\\
\;\;\;\;\mathsf{fma}\left(x_m, x_m, y_m \cdot \left(-y_m\right)\right)\\

\mathbf{else}:\\
\;\;\;\;x_m \cdot \left(x_m + y_m \cdot -2\right)\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 2: 99.8% accurate, 0.6× 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 4 \cdot 10^{+260}:\\ \;\;\;\;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) 4e+260)
   (- (* 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) <= 4e+260) {
		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) <= 4d+260) 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) <= 4e+260) {
		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) <= 4e+260:
		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) <= 4e+260)
		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) <= 4e+260)
		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], 4e+260], 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 4 \cdot 10^{+260}:\\
\;\;\;\;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
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 3: 61.7% 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
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 4: 15.8% accurate, 1.4× speedup?

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

\\
-2 \cdot \left(x_m \cdot y_m\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Reproduce

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