Examples.Basics.BasicTests:f2 from sbv-4.4

Percentage Accurate: 93.6% → 98.1%
Time: 1.9s
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.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.1% accurate, 0.1× speedup?

\[\begin{array}{l} x = |x|\\ \\ \begin{array}{l} \mathbf{if}\;x \leq 4.3 \cdot 10^{+190}:\\ \;\;\;\;\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 4.3e+190) (fma x x (* y (- y))) (* x x)))
x = abs(x);
double code(double x, double y) {
	double tmp;
	if (x <= 4.3e+190) {
		tmp = fma(x, x, (y * -y));
	} else {
		tmp = x * x;
	}
	return tmp;
}
x = abs(x)
function code(x, y)
	tmp = 0.0
	if (x <= 4.3e+190)
		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, 4.3e+190], N[(x * x + N[(y * (-y)), $MachinePrecision]), $MachinePrecision], N[(x * x), $MachinePrecision]]
\begin{array}{l}
x = |x|\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq 4.3 \cdot 10^{+190}:\\
\;\;\;\;\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 < 4.3000000000000001e190

    1. Initial program 92.3%

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

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

        \[\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 4.3000000000000001e190 < x

    1. Initial program 69.6%

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

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

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

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

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

Alternative 2: 96.7% accurate, 0.6× speedup?

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

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


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

    1. Initial program 100.0%

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

    if 1.99999999999999997e307 < (*.f64 y y)

    1. Initial program 64.3%

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

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

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

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

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

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

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

Alternative 3: 67.1% accurate, 1.2× speedup?

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if y < 1.4799999999999999e23

    1. Initial program 94.0%

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

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

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

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

    if 1.4799999999999999e23 < y

    1. Initial program 76.8%

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

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

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

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

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

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

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

Alternative 4: 54.3% 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 90.2%

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

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

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

    \[\leadsto \color{blue}{x \cdot x} \]
  5. Final simplification55.3%

    \[\leadsto x \cdot x \]

Reproduce

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