Difference of squares

Percentage Accurate: 94.5% → 100.0%
Time: 3.0s
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: 94.5% 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(a - b\right) \cdot \left(b + a\right) \end{array} \]
(FPCore (a b) :precision binary64 (* (- a b) (+ b a)))
double code(double a, double b) {
	return (a - b) * (b + a);
}
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = (a - b) * (b + a)
end function
public static double code(double a, double b) {
	return (a - b) * (b + a);
}
def code(a, b):
	return (a - b) * (b + a)
function code(a, b)
	return Float64(Float64(a - b) * Float64(b + a))
end
function tmp = code(a, b)
	tmp = (a - b) * (b + a);
end
code[a_, b_] := N[(N[(a - b), $MachinePrecision] * N[(b + a), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

    \[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. Add Preprocessing

Alternative 2: 96.6% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := a \cdot a - b \cdot b\\ \mathbf{if}\;t\_0 \leq -4 \cdot 10^{-306} \lor \neg \left(t\_0 \leq \infty\right):\\ \;\;\;\;\left(-b\right) \cdot b\\ \mathbf{else}:\\ \;\;\;\;a \cdot a\\ \end{array} \end{array} \]
(FPCore (a b)
 :precision binary64
 (let* ((t_0 (- (* a a) (* b b))))
   (if (or (<= t_0 -4e-306) (not (<= t_0 INFINITY))) (* (- b) b) (* a a))))
double code(double a, double b) {
	double t_0 = (a * a) - (b * b);
	double tmp;
	if ((t_0 <= -4e-306) || !(t_0 <= ((double) INFINITY))) {
		tmp = -b * b;
	} else {
		tmp = a * a;
	}
	return tmp;
}
public static double code(double a, double b) {
	double t_0 = (a * a) - (b * b);
	double tmp;
	if ((t_0 <= -4e-306) || !(t_0 <= Double.POSITIVE_INFINITY)) {
		tmp = -b * b;
	} else {
		tmp = a * a;
	}
	return tmp;
}
def code(a, b):
	t_0 = (a * a) - (b * b)
	tmp = 0
	if (t_0 <= -4e-306) or not (t_0 <= math.inf):
		tmp = -b * b
	else:
		tmp = a * a
	return tmp
function code(a, b)
	t_0 = Float64(Float64(a * a) - Float64(b * b))
	tmp = 0.0
	if ((t_0 <= -4e-306) || !(t_0 <= Inf))
		tmp = Float64(Float64(-b) * b);
	else
		tmp = Float64(a * a);
	end
	return tmp
end
function tmp_2 = code(a, b)
	t_0 = (a * a) - (b * b);
	tmp = 0.0;
	if ((t_0 <= -4e-306) || ~((t_0 <= Inf)))
		tmp = -b * b;
	else
		tmp = a * a;
	end
	tmp_2 = tmp;
end
code[a_, b_] := Block[{t$95$0 = N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, -4e-306], N[Not[LessEqual[t$95$0, Infinity]], $MachinePrecision]], N[((-b) * b), $MachinePrecision], N[(a * a), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := a \cdot a - b \cdot b\\
\mathbf{if}\;t\_0 \leq -4 \cdot 10^{-306} \lor \neg \left(t\_0 \leq \infty\right):\\
\;\;\;\;\left(-b\right) \cdot b\\

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


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

    1. Initial program 87.6%

      \[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.f6495.9

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

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

    if -4.00000000000000011e-306 < (-.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.f6412.7

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

      \[\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-*.f6499.9

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;a \cdot a - b \cdot b \leq -4 \cdot 10^{-306} \lor \neg \left(a \cdot a - b \cdot b \leq \infty\right):\\ \;\;\;\;\left(-b\right) \cdot b\\ \mathbf{else}:\\ \;\;\;\;a \cdot a\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 53.9% 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 93.8%

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

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

    \[\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-*.f6452.4

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

    \[\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 2024337 
(FPCore (a b)
  :name "Difference of squares"
  :precision binary64

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

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