Difference of squares

Percentage Accurate: 93.4% → 100.0%
Time: 13.1s
Alternatives: 3
Speedup: 1.2×

Specification

?
\[\begin{array}{l} \\ a \cdot a - b \cdot b \end{array} \]
(FPCore (a b) :precision binary64 (- (* a a) (* b b)))
double code(double a, double b) {
	return (a * a) - (b * b);
}
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = (a * a) - (b * b)
end function
public static double code(double a, double b) {
	return (a * a) - (b * b);
}
def code(a, b):
	return (a * a) - (b * b)
function code(a, b)
	return Float64(Float64(a * a) - Float64(b * b))
end
function tmp = code(a, b)
	tmp = (a * a) - (b * b);
end
code[a_, b_] := N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
a \cdot a - b \cdot b
\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.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ a \cdot a - b \cdot b \end{array} \]
(FPCore (a b) :precision binary64 (- (* a a) (* b b)))
double code(double a, double b) {
	return (a * a) - (b * b);
}
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = (a * a) - (b * b)
end function
public static double code(double a, double b) {
	return (a * a) - (b * b);
}
def code(a, b):
	return (a * a) - (b * b)
function code(a, b)
	return Float64(Float64(a * a) - Float64(b * b))
end
function tmp = code(a, b)
	tmp = (a * a) - (b * b);
end
code[a_, b_] := N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
a \cdot a - b \cdot b
\end{array}

Alternative 1: 100.0% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \left(b + a\right) \cdot \left(a - b\right) \end{array} \]
(FPCore (a b) :precision binary64 (* (+ b a) (- a b)))
double code(double a, double b) {
	return (b + a) * (a - b);
}
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = (b + a) * (a - b)
end function
public static double code(double a, double b) {
	return (b + a) * (a - b);
}
def code(a, b):
	return (b + a) * (a - b)
function code(a, b)
	return Float64(Float64(b + a) * Float64(a - b))
end
function tmp = code(a, b)
	tmp = (b + a) * (a - b);
end
code[a_, b_] := N[(N[(b + a), $MachinePrecision] * N[(a - b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(b + a\right) \cdot \left(a - b\right)
\end{array}
Derivation
  1. Initial program 95.7%

    \[a \cdot a - b \cdot b \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto \color{blue}{a \cdot a - b \cdot b} \]
    2. lift-*.f64N/A

      \[\leadsto \color{blue}{a \cdot a} - b \cdot b \]
    3. lift-*.f64N/A

      \[\leadsto a \cdot a - \color{blue}{b \cdot b} \]
    4. difference-of-squaresN/A

      \[\leadsto \color{blue}{\left(a + b\right) \cdot \left(a - b\right)} \]
    5. *-commutativeN/A

      \[\leadsto \color{blue}{\left(a - b\right) \cdot \left(a + b\right)} \]
    6. lower-*.f64N/A

      \[\leadsto \color{blue}{\left(a - b\right) \cdot \left(a + b\right)} \]
    7. lower--.f64N/A

      \[\leadsto \color{blue}{\left(a - b\right)} \cdot \left(a + b\right) \]
    8. +-commutativeN/A

      \[\leadsto \left(a - b\right) \cdot \color{blue}{\left(b + a\right)} \]
    9. lower-+.f64100.0

      \[\leadsto \left(a - b\right) \cdot \color{blue}{\left(b + a\right)} \]
  4. Applied rewrites100.0%

    \[\leadsto \color{blue}{\left(a - b\right) \cdot \left(b + a\right)} \]
  5. Final simplification100.0%

    \[\leadsto \left(b + a\right) \cdot \left(a - b\right) \]
  6. Add Preprocessing

Alternative 2: 96.1% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := a \cdot a - b \cdot b\\ t_1 := \left(-b\right) \cdot b\\ \mathbf{if}\;t\_0 \leq -5 \cdot 10^{-312}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;t\_0 \leq \infty:\\ \;\;\;\;a \cdot a\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (a b)
 :precision binary64
 (let* ((t_0 (- (* a a) (* b b))) (t_1 (* (- b) b)))
   (if (<= t_0 -5e-312) t_1 (if (<= t_0 INFINITY) (* a a) t_1))))
double code(double a, double b) {
	double t_0 = (a * a) - (b * b);
	double t_1 = -b * b;
	double tmp;
	if (t_0 <= -5e-312) {
		tmp = t_1;
	} else if (t_0 <= ((double) INFINITY)) {
		tmp = a * a;
	} else {
		tmp = t_1;
	}
	return tmp;
}
public static double code(double a, double b) {
	double t_0 = (a * a) - (b * b);
	double t_1 = -b * b;
	double tmp;
	if (t_0 <= -5e-312) {
		tmp = t_1;
	} else if (t_0 <= Double.POSITIVE_INFINITY) {
		tmp = a * a;
	} else {
		tmp = t_1;
	}
	return tmp;
}
def code(a, b):
	t_0 = (a * a) - (b * b)
	t_1 = -b * b
	tmp = 0
	if t_0 <= -5e-312:
		tmp = t_1
	elif t_0 <= math.inf:
		tmp = a * a
	else:
		tmp = t_1
	return tmp
function code(a, b)
	t_0 = Float64(Float64(a * a) - Float64(b * b))
	t_1 = Float64(Float64(-b) * b)
	tmp = 0.0
	if (t_0 <= -5e-312)
		tmp = t_1;
	elseif (t_0 <= Inf)
		tmp = Float64(a * a);
	else
		tmp = t_1;
	end
	return tmp
end
function tmp_2 = code(a, b)
	t_0 = (a * a) - (b * b);
	t_1 = -b * b;
	tmp = 0.0;
	if (t_0 <= -5e-312)
		tmp = t_1;
	elseif (t_0 <= Inf)
		tmp = a * a;
	else
		tmp = t_1;
	end
	tmp_2 = tmp;
end
code[a_, b_] := Block[{t$95$0 = N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[((-b) * b), $MachinePrecision]}, If[LessEqual[t$95$0, -5e-312], t$95$1, If[LessEqual[t$95$0, Infinity], N[(a * a), $MachinePrecision], t$95$1]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := a \cdot a - b \cdot b\\
t_1 := \left(-b\right) \cdot b\\
\mathbf{if}\;t\_0 \leq -5 \cdot 10^{-312}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;t\_0 \leq \infty:\\
\;\;\;\;a \cdot a\\

\mathbf{else}:\\
\;\;\;\;t\_1\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (*.f64 a a) (*.f64 b b)) < -5.0000000000022e-312 or +inf.0 < (-.f64 (*.f64 a a) (*.f64 b b))

    1. Initial program 91.1%

      \[a \cdot a - b \cdot b \]
    2. Add Preprocessing
    3. Taylor expanded in a around 0

      \[\leadsto \color{blue}{-1 \cdot {b}^{2}} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \color{blue}{\mathsf{neg}\left({b}^{2}\right)} \]
      2. unpow2N/A

        \[\leadsto \mathsf{neg}\left(\color{blue}{b \cdot b}\right) \]
      3. distribute-lft-neg-inN/A

        \[\leadsto \color{blue}{\left(\mathsf{neg}\left(b\right)\right) \cdot b} \]
      4. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(\mathsf{neg}\left(b\right)\right) \cdot b} \]
      5. lower-neg.f6497.3

        \[\leadsto \color{blue}{\left(-b\right)} \cdot b \]
    5. Applied rewrites97.3%

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

    if -5.0000000000022e-312 < (-.f64 (*.f64 a a) (*.f64 b b)) < +inf.0

    1. Initial program 100.0%

      \[a \cdot a - b \cdot b \]
    2. Add Preprocessing
    3. Taylor expanded in a around 0

      \[\leadsto \color{blue}{-1 \cdot {b}^{2}} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \color{blue}{\mathsf{neg}\left({b}^{2}\right)} \]
      2. unpow2N/A

        \[\leadsto \mathsf{neg}\left(\color{blue}{b \cdot b}\right) \]
      3. distribute-lft-neg-inN/A

        \[\leadsto \color{blue}{\left(\mathsf{neg}\left(b\right)\right) \cdot b} \]
      4. lower-*.f64N/A

        \[\leadsto \color{blue}{\left(\mathsf{neg}\left(b\right)\right) \cdot b} \]
      5. lower-neg.f6411.0

        \[\leadsto \color{blue}{\left(-b\right)} \cdot b \]
    5. Applied rewrites11.0%

      \[\leadsto \color{blue}{\left(-b\right) \cdot b} \]
    6. Taylor expanded in a around inf

      \[\leadsto \color{blue}{{a}^{2}} \]
    7. Step-by-step derivation
      1. unpow2N/A

        \[\leadsto \color{blue}{a \cdot a} \]
      2. lower-*.f64100.0

        \[\leadsto \color{blue}{a \cdot a} \]
    8. Applied rewrites100.0%

      \[\leadsto \color{blue}{a \cdot a} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 54.5% accurate, 2.3× speedup?

\[\begin{array}{l} \\ a \cdot a \end{array} \]
(FPCore (a b) :precision binary64 (* a a))
double code(double a, double b) {
	return a * a;
}
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = a * a
end function
public static double code(double a, double b) {
	return a * a;
}
def code(a, b):
	return a * a
function code(a, b)
	return Float64(a * a)
end
function tmp = code(a, b)
	tmp = a * a;
end
code[a_, b_] := N[(a * a), $MachinePrecision]
\begin{array}{l}

\\
a \cdot a
\end{array}
Derivation
  1. Initial program 95.7%

    \[a \cdot a - b \cdot b \]
  2. Add Preprocessing
  3. Taylor expanded in a around 0

    \[\leadsto \color{blue}{-1 \cdot {b}^{2}} \]
  4. Step-by-step derivation
    1. mul-1-negN/A

      \[\leadsto \color{blue}{\mathsf{neg}\left({b}^{2}\right)} \]
    2. unpow2N/A

      \[\leadsto \mathsf{neg}\left(\color{blue}{b \cdot b}\right) \]
    3. distribute-lft-neg-inN/A

      \[\leadsto \color{blue}{\left(\mathsf{neg}\left(b\right)\right) \cdot b} \]
    4. lower-*.f64N/A

      \[\leadsto \color{blue}{\left(\mathsf{neg}\left(b\right)\right) \cdot b} \]
    5. lower-neg.f6452.8

      \[\leadsto \color{blue}{\left(-b\right)} \cdot b \]
  5. Applied rewrites52.8%

    \[\leadsto \color{blue}{\left(-b\right) \cdot b} \]
  6. Taylor expanded in a around inf

    \[\leadsto \color{blue}{{a}^{2}} \]
  7. Step-by-step derivation
    1. unpow2N/A

      \[\leadsto \color{blue}{a \cdot a} \]
    2. lower-*.f6453.8

      \[\leadsto \color{blue}{a \cdot a} \]
  8. Applied rewrites53.8%

    \[\leadsto \color{blue}{a \cdot a} \]
  9. Add Preprocessing

Developer Target 1: 100.0% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \left(a + b\right) \cdot \left(a - b\right) \end{array} \]
(FPCore (a b) :precision binary64 (* (+ a b) (- a b)))
double code(double a, double b) {
	return (a + b) * (a - b);
}
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = (a + b) * (a - b)
end function
public static double code(double a, double b) {
	return (a + b) * (a - b);
}
def code(a, b):
	return (a + b) * (a - b)
function code(a, b)
	return Float64(Float64(a + b) * Float64(a - b))
end
function tmp = code(a, b)
	tmp = (a + b) * (a - b);
end
code[a_, b_] := N[(N[(a + b), $MachinePrecision] * N[(a - b), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(a + b\right) \cdot \left(a - b\right)
\end{array}

Reproduce

?
herbie shell --seed 2024288 
(FPCore (a b)
  :name "Difference of squares"
  :precision binary64

  :alt
  (! :herbie-platform default (* (+ a b) (- a b)))

  (- (* a a) (* b b)))