Examples.Basics.BasicTests:f2 from sbv-4.4

Percentage Accurate: 93.7% → 97.7%
Time: 2.2s
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.7% 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: 97.7% accurate, 0.1× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;x \leq 1.45 \cdot 10^{+176}:\\ \;\;\;\;\mathsf{fma}\left(x, x, y \cdot \left(-y\right)\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x y)
 :precision binary64
 (if (<= x 1.45e+176) (fma x x (* y (- y))) (* x x)))
x = abs(x);
double code(double x, double y) {
	double tmp;
	if (x <= 1.45e+176) {
		tmp = fma(x, x, (y * -y));
	} else {
		tmp = x * x;
	}
	return tmp;
}
x = abs(x)
function code(x, y)
	tmp = 0.0
	if (x <= 1.45e+176)
		tmp = fma(x, x, Float64(y * Float64(-y)));
	else
		tmp = Float64(x * x);
	end
	return tmp
end
NOTE: x should be positive before calling this function
code[x_, y_] := If[LessEqual[x, 1.45e+176], N[(x * x + N[(y * (-y)), $MachinePrecision]), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.45 \cdot 10^{+176}:\\
\;\;\;\;\mathsf{fma}\left(x, x, y \cdot \left(-y\right)\right)\\

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


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

    1. Initial program 93.6%

      \[x \cdot x - y \cdot y \]
    2. Step-by-step derivation
      1. fma-neg96.6%

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, x, -y \cdot y\right)} \]
      2. distribute-rgt-neg-in96.6%

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

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

    if 1.4500000000000001e176 < x

    1. Initial program 87.0%

      \[x \cdot x - y \cdot y \]
    2. Taylor expanded in x around inf 100.0%

      \[\leadsto \color{blue}{{x}^{2}} \]
    3. Step-by-step derivation
      1. unpow2100.0%

        \[\leadsto \color{blue}{x \cdot x} \]
    4. Simplified100.0%

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

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

Alternative 2: 77.0% accurate, 0.4× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;x \cdot x \leq 6.2 \cdot 10^{-62} \lor \neg \left(x \cdot x \leq 1.35 \cdot 10^{+166}\right) \land x \cdot x \leq 4.2 \cdot 10^{+219}:\\ \;\;\;\;y \cdot \left(-y\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x y)
 :precision binary64
 (if (or (<= (* x x) 6.2e-62)
         (and (not (<= (* x x) 1.35e+166)) (<= (* x x) 4.2e+219)))
   (* y (- y))
   (* x x)))
x = abs(x);
double code(double x, double y) {
	double tmp;
	if (((x * x) <= 6.2e-62) || (!((x * x) <= 1.35e+166) && ((x * x) <= 4.2e+219))) {
		tmp = y * -y;
	} else {
		tmp = x * x;
	}
	return tmp;
}
NOTE: x should be positive before calling this function
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if (((x * x) <= 6.2d-62) .or. (.not. ((x * x) <= 1.35d+166)) .and. ((x * x) <= 4.2d+219)) then
        tmp = y * -y
    else
        tmp = x * x
    end if
    code = tmp
end function
x = Math.abs(x);
public static double code(double x, double y) {
	double tmp;
	if (((x * x) <= 6.2e-62) || (!((x * x) <= 1.35e+166) && ((x * x) <= 4.2e+219))) {
		tmp = y * -y;
	} else {
		tmp = x * x;
	}
	return tmp;
}
x = abs(x)
def code(x, y):
	tmp = 0
	if ((x * x) <= 6.2e-62) or (not ((x * x) <= 1.35e+166) and ((x * x) <= 4.2e+219)):
		tmp = y * -y
	else:
		tmp = x * x
	return tmp
x = abs(x)
function code(x, y)
	tmp = 0.0
	if ((Float64(x * x) <= 6.2e-62) || (!(Float64(x * x) <= 1.35e+166) && (Float64(x * x) <= 4.2e+219)))
		tmp = Float64(y * Float64(-y));
	else
		tmp = Float64(x * x);
	end
	return tmp
end
x = abs(x)
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (((x * x) <= 6.2e-62) || (~(((x * x) <= 1.35e+166)) && ((x * x) <= 4.2e+219)))
		tmp = y * -y;
	else
		tmp = x * x;
	end
	tmp_2 = tmp;
end
NOTE: x should be positive before calling this function
code[x_, y_] := If[Or[LessEqual[N[(x * x), $MachinePrecision], 6.2e-62], And[N[Not[LessEqual[N[(x * x), $MachinePrecision], 1.35e+166]], $MachinePrecision], LessEqual[N[(x * x), $MachinePrecision], 4.2e+219]]], N[(y * (-y)), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \cdot x \leq 6.2 \cdot 10^{-62} \lor \neg \left(x \cdot x \leq 1.35 \cdot 10^{+166}\right) \land x \cdot x \leq 4.2 \cdot 10^{+219}:\\
\;\;\;\;y \cdot \left(-y\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 x x) < 6.1999999999999999e-62 or 1.35000000000000006e166 < (*.f64 x x) < 4.19999999999999976e219

    1. Initial program 100.0%

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

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

        \[\leadsto -1 \cdot \color{blue}{\left(y \cdot y\right)} \]
      2. mul-1-neg83.2%

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

        \[\leadsto \color{blue}{y \cdot \left(-y\right)} \]
    4. Simplified83.2%

      \[\leadsto \color{blue}{y \cdot \left(-y\right)} \]

    if 6.1999999999999999e-62 < (*.f64 x x) < 1.35000000000000006e166 or 4.19999999999999976e219 < (*.f64 x x)

    1. Initial program 85.2%

      \[x \cdot x - y \cdot y \]
    2. Taylor expanded in x around inf 81.4%

      \[\leadsto \color{blue}{{x}^{2}} \]
    3. Step-by-step derivation
      1. unpow281.4%

        \[\leadsto \color{blue}{x \cdot x} \]
    4. Simplified81.4%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot x \leq 6.2 \cdot 10^{-62} \lor \neg \left(x \cdot x \leq 1.35 \cdot 10^{+166}\right) \land x \cdot x \leq 4.2 \cdot 10^{+219}:\\ \;\;\;\;y \cdot \left(-y\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \]

Alternative 3: 96.5% accurate, 0.8× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;x \leq 1.42 \cdot 10^{+150}:\\ \;\;\;\;x \cdot x - y \cdot y\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x y)
 :precision binary64
 (if (<= x 1.42e+150) (- (* x x) (* y y)) (* x x)))
x = abs(x);
double code(double x, double y) {
	double tmp;
	if (x <= 1.42e+150) {
		tmp = (x * x) - (y * y);
	} else {
		tmp = x * x;
	}
	return tmp;
}
NOTE: x should be positive before calling this function
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if (x <= 1.42d+150) then
        tmp = (x * x) - (y * y)
    else
        tmp = x * x
    end if
    code = tmp
end function
x = Math.abs(x);
public static double code(double x, double y) {
	double tmp;
	if (x <= 1.42e+150) {
		tmp = (x * x) - (y * y);
	} else {
		tmp = x * x;
	}
	return tmp;
}
x = abs(x)
def code(x, y):
	tmp = 0
	if x <= 1.42e+150:
		tmp = (x * x) - (y * y)
	else:
		tmp = x * x
	return tmp
x = abs(x)
function code(x, y)
	tmp = 0.0
	if (x <= 1.42e+150)
		tmp = Float64(Float64(x * x) - Float64(y * y));
	else
		tmp = Float64(x * x);
	end
	return tmp
end
x = abs(x)
function tmp_2 = code(x, y)
	tmp = 0.0;
	if (x <= 1.42e+150)
		tmp = (x * x) - (y * y);
	else
		tmp = x * x;
	end
	tmp_2 = tmp;
end
NOTE: x should be positive before calling this function
code[x_, y_] := If[LessEqual[x, 1.42e+150], N[(N[(x * x), $MachinePrecision] - N[(y * y), $MachinePrecision]), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1.42 \cdot 10^{+150}:\\
\;\;\;\;x \cdot x - y \cdot y\\

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


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

    1. Initial program 94.2%

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

    if 1.42e150 < x

    1. Initial program 84.8%

      \[x \cdot x - y \cdot y \]
    2. Taylor expanded in x around inf 97.0%

      \[\leadsto \color{blue}{{x}^{2}} \]
    3. Step-by-step derivation
      1. unpow297.0%

        \[\leadsto \color{blue}{x \cdot x} \]
    4. Simplified97.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 1.42 \cdot 10^{+150}:\\ \;\;\;\;x \cdot x - y \cdot y\\ \mathbf{else}:\\ \;\;\;\;x \cdot x\\ \end{array} \]

Alternative 4: 53.2% accurate, 2.3× speedup?

\[\begin{array}{l} x = |x|\\ \\ x \cdot x \end{array} \]
NOTE: x should be positive before calling this function
(FPCore (x y) :precision binary64 (* x x))
x = abs(x);
double code(double x, double y) {
	return x * x;
}
NOTE: x should be positive before calling this function
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = x * x
end function
x = Math.abs(x);
public static double code(double x, double y) {
	return x * x;
}
x = abs(x)
def code(x, y):
	return x * x
x = abs(x)
function code(x, y)
	return Float64(x * x)
end
x = abs(x)
function tmp = code(x, y)
	tmp = x * x;
end
NOTE: x should be positive before calling this function
code[x_, y_] := N[(x * x), $MachinePrecision]
\begin{array}{l}
x = |x|\\
\\
x \cdot x
\end{array}
Derivation
  1. Initial program 93.0%

    \[x \cdot x - y \cdot y \]
  2. Taylor expanded in x around inf 52.9%

    \[\leadsto \color{blue}{{x}^{2}} \]
  3. Step-by-step derivation
    1. unpow252.9%

      \[\leadsto \color{blue}{x \cdot x} \]
  4. Simplified52.9%

    \[\leadsto \color{blue}{x \cdot x} \]
  5. Final simplification52.9%

    \[\leadsto x \cdot x \]

Reproduce

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