Difference of squares

?

Percentage Accurate: 93.4% → 96.6%
Time: 1.9s
Precision: binary64
Cost: 708

?

\[a \cdot a - b \cdot b \]
\[\begin{array}{l} \mathbf{if}\;b \cdot b \leq 10^{+304}:\\ \;\;\;\;a \cdot a - b \cdot b\\ \mathbf{else}:\\ \;\;\;\;b \cdot \left(-b\right)\\ \end{array} \]
(FPCore (a b) :precision binary64 (- (* a a) (* b b)))
(FPCore (a b)
 :precision binary64
 (if (<= (* b b) 1e+304) (- (* a a) (* b b)) (* b (- b))))
double code(double a, double b) {
	return (a * a) - (b * b);
}
double code(double a, double b) {
	double tmp;
	if ((b * b) <= 1e+304) {
		tmp = (a * a) - (b * b);
	} else {
		tmp = b * -b;
	}
	return tmp;
}
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    code = (a * a) - (b * b)
end function
real(8) function code(a, b)
    real(8), intent (in) :: a
    real(8), intent (in) :: b
    real(8) :: tmp
    if ((b * b) <= 1d+304) then
        tmp = (a * a) - (b * b)
    else
        tmp = b * -b
    end if
    code = tmp
end function
public static double code(double a, double b) {
	return (a * a) - (b * b);
}
public static double code(double a, double b) {
	double tmp;
	if ((b * b) <= 1e+304) {
		tmp = (a * a) - (b * b);
	} else {
		tmp = b * -b;
	}
	return tmp;
}
def code(a, b):
	return (a * a) - (b * b)
def code(a, b):
	tmp = 0
	if (b * b) <= 1e+304:
		tmp = (a * a) - (b * b)
	else:
		tmp = b * -b
	return tmp
function code(a, b)
	return Float64(Float64(a * a) - Float64(b * b))
end
function code(a, b)
	tmp = 0.0
	if (Float64(b * b) <= 1e+304)
		tmp = Float64(Float64(a * a) - Float64(b * b));
	else
		tmp = Float64(b * Float64(-b));
	end
	return tmp
end
function tmp = code(a, b)
	tmp = (a * a) - (b * b);
end
function tmp_2 = code(a, b)
	tmp = 0.0;
	if ((b * b) <= 1e+304)
		tmp = (a * a) - (b * b);
	else
		tmp = b * -b;
	end
	tmp_2 = tmp;
end
code[a_, b_] := N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision]
code[a_, b_] := If[LessEqual[N[(b * b), $MachinePrecision], 1e+304], N[(N[(a * a), $MachinePrecision] - N[(b * b), $MachinePrecision]), $MachinePrecision], N[(b * (-b)), $MachinePrecision]]
a \cdot a - b \cdot b
\begin{array}{l}
\mathbf{if}\;b \cdot b \leq 10^{+304}:\\
\;\;\;\;a \cdot a - b \cdot b\\

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


\end{array}

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.

Herbie found 4 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

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.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original93.4%
Target100.0%
Herbie96.6%
\[\left(a + b\right) \cdot \left(a - b\right) \]

Derivation?

  1. Split input into 2 regimes
  2. if (*.f64 b b) < 9.9999999999999994e303

    1. Initial program 100.0%

      \[a \cdot a - b \cdot b \]

    if 9.9999999999999994e303 < (*.f64 b b)

    1. Initial program 79.0%

      \[a \cdot a - b \cdot b \]
    2. Taylor expanded in a around 0 90.3%

      \[\leadsto \color{blue}{-1 \cdot {b}^{2}} \]
    3. Simplified90.3%

      \[\leadsto \color{blue}{b \cdot \left(-b\right)} \]
      Step-by-step derivation

      [Start]90.3%

      \[ -1 \cdot {b}^{2} \]

      unpow2 [=>]90.3%

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

      mul-1-neg [=>]90.3%

      \[ \color{blue}{-b \cdot b} \]

      distribute-rgt-neg-in [=>]90.3%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;b \cdot b \leq 10^{+304}:\\ \;\;\;\;a \cdot a - b \cdot b\\ \mathbf{else}:\\ \;\;\;\;b \cdot \left(-b\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy96.6%
Cost708
\[\begin{array}{l} \mathbf{if}\;b \cdot b \leq 10^{+304}:\\ \;\;\;\;a \cdot a - b \cdot b\\ \mathbf{else}:\\ \;\;\;\;b \cdot \left(-b\right)\\ \end{array} \]
Alternative 2
Accuracy96.7%
Cost6784
\[\mathsf{fma}\left(a, a, b \cdot \left(-b\right)\right) \]
Alternative 3
Accuracy75.4%
Cost521
\[\begin{array}{l} \mathbf{if}\;b \leq -8 \cdot 10^{+109} \lor \neg \left(b \leq 1.4 \cdot 10^{-57}\right):\\ \;\;\;\;b \cdot \left(-b\right)\\ \mathbf{else}:\\ \;\;\;\;a \cdot a\\ \end{array} \]
Alternative 4
Accuracy53.8%
Cost192
\[a \cdot a \]

Reproduce?

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

  :herbie-target
  (* (+ a b) (- a b))

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