Bouland and Aaronson, Equation (25)

?

Percentage Accurate: 74.1% → 99.9%
Time: 9.9s
Precision: binary64
Cost: 16580

?

\[\left({\left(a \cdot a + b \cdot b\right)}^{2} + 4 \cdot \left(\left(a \cdot a\right) \cdot \left(1 + a\right) + \left(b \cdot b\right) \cdot \left(1 - 3 \cdot a\right)\right)\right) - 1 \]
\[\begin{array}{l} t_0 := {\left(a \cdot a + b \cdot b\right)}^{2} + 4 \cdot \left(\left(a \cdot a\right) \cdot \left(a + 1\right) + \left(b \cdot b\right) \cdot \left(1 - a \cdot 3\right)\right)\\ \mathbf{if}\;t_0 \leq \infty:\\ \;\;\;\;t_0 + -1\\ \mathbf{else}:\\ \;\;\;\;\left(a \cdot a\right) \cdot \left(\mathsf{fma}\left(2, b \cdot b, 4\right) + a \cdot \left(a + 4\right)\right)\\ \end{array} \]
(FPCore (a b)
 :precision binary64
 (-
  (+
   (pow (+ (* a a) (* b b)) 2.0)
   (* 4.0 (+ (* (* a a) (+ 1.0 a)) (* (* b b) (- 1.0 (* 3.0 a))))))
  1.0))
(FPCore (a b)
 :precision binary64
 (let* ((t_0
         (+
          (pow (+ (* a a) (* b b)) 2.0)
          (* 4.0 (+ (* (* a a) (+ a 1.0)) (* (* b b) (- 1.0 (* a 3.0))))))))
   (if (<= t_0 INFINITY)
     (+ t_0 -1.0)
     (* (* a a) (+ (fma 2.0 (* b b) 4.0) (* a (+ a 4.0)))))))
double code(double a, double b) {
	return (pow(((a * a) + (b * b)), 2.0) + (4.0 * (((a * a) * (1.0 + a)) + ((b * b) * (1.0 - (3.0 * a)))))) - 1.0;
}
double code(double a, double b) {
	double t_0 = pow(((a * a) + (b * b)), 2.0) + (4.0 * (((a * a) * (a + 1.0)) + ((b * b) * (1.0 - (a * 3.0)))));
	double tmp;
	if (t_0 <= ((double) INFINITY)) {
		tmp = t_0 + -1.0;
	} else {
		tmp = (a * a) * (fma(2.0, (b * b), 4.0) + (a * (a + 4.0)));
	}
	return tmp;
}
function code(a, b)
	return Float64(Float64((Float64(Float64(a * a) + Float64(b * b)) ^ 2.0) + Float64(4.0 * Float64(Float64(Float64(a * a) * Float64(1.0 + a)) + Float64(Float64(b * b) * Float64(1.0 - Float64(3.0 * a)))))) - 1.0)
end
function code(a, b)
	t_0 = Float64((Float64(Float64(a * a) + Float64(b * b)) ^ 2.0) + Float64(4.0 * Float64(Float64(Float64(a * a) * Float64(a + 1.0)) + Float64(Float64(b * b) * Float64(1.0 - Float64(a * 3.0))))))
	tmp = 0.0
	if (t_0 <= Inf)
		tmp = Float64(t_0 + -1.0);
	else
		tmp = Float64(Float64(a * a) * Float64(fma(2.0, Float64(b * b), 4.0) + Float64(a * Float64(a + 4.0))));
	end
	return tmp
end
code[a_, b_] := N[(N[(N[Power[N[(N[(a * a), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision] + N[(4.0 * N[(N[(N[(a * a), $MachinePrecision] * N[(1.0 + a), $MachinePrecision]), $MachinePrecision] + N[(N[(b * b), $MachinePrecision] * N[(1.0 - N[(3.0 * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
code[a_, b_] := Block[{t$95$0 = N[(N[Power[N[(N[(a * a), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision] + N[(4.0 * N[(N[(N[(a * a), $MachinePrecision] * N[(a + 1.0), $MachinePrecision]), $MachinePrecision] + N[(N[(b * b), $MachinePrecision] * N[(1.0 - N[(a * 3.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, Infinity], N[(t$95$0 + -1.0), $MachinePrecision], N[(N[(a * a), $MachinePrecision] * N[(N[(2.0 * N[(b * b), $MachinePrecision] + 4.0), $MachinePrecision] + N[(a * N[(a + 4.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\left({\left(a \cdot a + b \cdot b\right)}^{2} + 4 \cdot \left(\left(a \cdot a\right) \cdot \left(1 + a\right) + \left(b \cdot b\right) \cdot \left(1 - 3 \cdot a\right)\right)\right) - 1
\begin{array}{l}
t_0 := {\left(a \cdot a + b \cdot b\right)}^{2} + 4 \cdot \left(\left(a \cdot a\right) \cdot \left(a + 1\right) + \left(b \cdot b\right) \cdot \left(1 - a \cdot 3\right)\right)\\
\mathbf{if}\;t_0 \leq \infty:\\
\;\;\;\;t_0 + -1\\

\mathbf{else}:\\
\;\;\;\;\left(a \cdot a\right) \cdot \left(\mathsf{fma}\left(2, b \cdot b, 4\right) + a \cdot \left(a + 4\right)\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 11 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

Derivation?

  1. Split input into 2 regimes
  2. if (+.f64 (pow.f64 (+.f64 (*.f64 a a) (*.f64 b b)) 2) (*.f64 4 (+.f64 (*.f64 (*.f64 a a) (+.f64 1 a)) (*.f64 (*.f64 b b) (-.f64 1 (*.f64 3 a)))))) < +inf.0

    1. Initial program 99.8%

      \[\left({\left(a \cdot a + b \cdot b\right)}^{2} + 4 \cdot \left(\left(a \cdot a\right) \cdot \left(1 + a\right) + \left(b \cdot b\right) \cdot \left(1 - 3 \cdot a\right)\right)\right) - 1 \]

    if +inf.0 < (+.f64 (pow.f64 (+.f64 (*.f64 a a) (*.f64 b b)) 2) (*.f64 4 (+.f64 (*.f64 (*.f64 a a) (+.f64 1 a)) (*.f64 (*.f64 b b) (-.f64 1 (*.f64 3 a))))))

    1. Initial program 0.0%

      \[\left({\left(a \cdot a + b \cdot b\right)}^{2} + 4 \cdot \left(\left(a \cdot a\right) \cdot \left(1 + a\right) + \left(b \cdot b\right) \cdot \left(1 - 3 \cdot a\right)\right)\right) - 1 \]
    2. Simplified4.4%

      \[\leadsto \color{blue}{{\left(\mathsf{fma}\left(a, a, b \cdot b\right)\right)}^{2} + \left(4 \cdot \mathsf{fma}\left(a \cdot a, a + 1, \left(b \cdot b\right) \cdot \left(1 + -3 \cdot a\right)\right) - 1\right)} \]
      Step-by-step derivation

      [Start]0.0%

      \[ \left({\left(a \cdot a + b \cdot b\right)}^{2} + 4 \cdot \left(\left(a \cdot a\right) \cdot \left(1 + a\right) + \left(b \cdot b\right) \cdot \left(1 - 3 \cdot a\right)\right)\right) - 1 \]

      associate--l+ [=>]0.0%

      \[ \color{blue}{{\left(a \cdot a + b \cdot b\right)}^{2} + \left(4 \cdot \left(\left(a \cdot a\right) \cdot \left(1 + a\right) + \left(b \cdot b\right) \cdot \left(1 - 3 \cdot a\right)\right) - 1\right)} \]

      fma-def [=>]0.0%

      \[ {\color{blue}{\left(\mathsf{fma}\left(a, a, b \cdot b\right)\right)}}^{2} + \left(4 \cdot \left(\left(a \cdot a\right) \cdot \left(1 + a\right) + \left(b \cdot b\right) \cdot \left(1 - 3 \cdot a\right)\right) - 1\right) \]
    3. Taylor expanded in a around 0 4.4%

      \[\leadsto \color{blue}{\left(2 \cdot \left({a}^{2} \cdot {b}^{2}\right) + \left({a}^{4} + {b}^{4}\right)\right)} + \left(4 \cdot \mathsf{fma}\left(a \cdot a, a + 1, \left(b \cdot b\right) \cdot \left(1 + -3 \cdot a\right)\right) - 1\right) \]
    4. Simplified4.4%

      \[\leadsto \color{blue}{\mathsf{fma}\left(2, b \cdot \left(b \cdot \left(a \cdot a\right)\right), {b}^{4} + {a}^{4}\right)} + \left(4 \cdot \mathsf{fma}\left(a \cdot a, a + 1, \left(b \cdot b\right) \cdot \left(1 + -3 \cdot a\right)\right) - 1\right) \]
      Step-by-step derivation

      [Start]4.4%

      \[ \left(2 \cdot \left({a}^{2} \cdot {b}^{2}\right) + \left({a}^{4} + {b}^{4}\right)\right) + \left(4 \cdot \mathsf{fma}\left(a \cdot a, a + 1, \left(b \cdot b\right) \cdot \left(1 + -3 \cdot a\right)\right) - 1\right) \]

      fma-def [=>]4.4%

      \[ \color{blue}{\mathsf{fma}\left(2, {a}^{2} \cdot {b}^{2}, {a}^{4} + {b}^{4}\right)} + \left(4 \cdot \mathsf{fma}\left(a \cdot a, a + 1, \left(b \cdot b\right) \cdot \left(1 + -3 \cdot a\right)\right) - 1\right) \]

      +-commutative [<=]4.4%

      \[ \mathsf{fma}\left(2, {a}^{2} \cdot {b}^{2}, \color{blue}{{b}^{4} + {a}^{4}}\right) + \left(4 \cdot \mathsf{fma}\left(a \cdot a, a + 1, \left(b \cdot b\right) \cdot \left(1 + -3 \cdot a\right)\right) - 1\right) \]

      *-commutative [=>]4.4%

      \[ \mathsf{fma}\left(2, \color{blue}{{b}^{2} \cdot {a}^{2}}, {b}^{4} + {a}^{4}\right) + \left(4 \cdot \mathsf{fma}\left(a \cdot a, a + 1, \left(b \cdot b\right) \cdot \left(1 + -3 \cdot a\right)\right) - 1\right) \]

      unpow2 [=>]4.4%

      \[ \mathsf{fma}\left(2, \color{blue}{\left(b \cdot b\right)} \cdot {a}^{2}, {b}^{4} + {a}^{4}\right) + \left(4 \cdot \mathsf{fma}\left(a \cdot a, a + 1, \left(b \cdot b\right) \cdot \left(1 + -3 \cdot a\right)\right) - 1\right) \]

      associate-*l* [=>]4.4%

      \[ \mathsf{fma}\left(2, \color{blue}{b \cdot \left(b \cdot {a}^{2}\right)}, {b}^{4} + {a}^{4}\right) + \left(4 \cdot \mathsf{fma}\left(a \cdot a, a + 1, \left(b \cdot b\right) \cdot \left(1 + -3 \cdot a\right)\right) - 1\right) \]

      unpow2 [=>]4.4%

      \[ \mathsf{fma}\left(2, b \cdot \left(b \cdot \color{blue}{\left(a \cdot a\right)}\right), {b}^{4} + {a}^{4}\right) + \left(4 \cdot \mathsf{fma}\left(a \cdot a, a + 1, \left(b \cdot b\right) \cdot \left(1 + -3 \cdot a\right)\right) - 1\right) \]
    5. Taylor expanded in a around inf 33.8%

      \[\leadsto \color{blue}{4 \cdot {a}^{3} + \left(\left(2 \cdot {b}^{2} + 4\right) \cdot {a}^{2} + {a}^{4}\right)} \]
    6. Simplified100.0%

      \[\leadsto \color{blue}{\left(a \cdot a\right) \cdot \left(\mathsf{fma}\left(2, b \cdot b, 4\right) + a \cdot \left(4 + a\right)\right)} \]
      Step-by-step derivation

      [Start]33.8%

      \[ 4 \cdot {a}^{3} + \left(\left(2 \cdot {b}^{2} + 4\right) \cdot {a}^{2} + {a}^{4}\right) \]

      +-commutative [=>]33.8%

      \[ \color{blue}{\left(\left(2 \cdot {b}^{2} + 4\right) \cdot {a}^{2} + {a}^{4}\right) + 4 \cdot {a}^{3}} \]

      *-commutative [=>]33.8%

      \[ \left(\left(2 \cdot {b}^{2} + 4\right) \cdot {a}^{2} + {a}^{4}\right) + \color{blue}{{a}^{3} \cdot 4} \]

      cube-mult [=>]33.8%

      \[ \left(\left(2 \cdot {b}^{2} + 4\right) \cdot {a}^{2} + {a}^{4}\right) + \color{blue}{\left(a \cdot \left(a \cdot a\right)\right)} \cdot 4 \]

      associate-*r* [<=]33.8%

      \[ \left(\left(2 \cdot {b}^{2} + 4\right) \cdot {a}^{2} + {a}^{4}\right) + \color{blue}{a \cdot \left(\left(a \cdot a\right) \cdot 4\right)} \]

      associate-+l+ [=>]33.8%

      \[ \color{blue}{\left(2 \cdot {b}^{2} + 4\right) \cdot {a}^{2} + \left({a}^{4} + a \cdot \left(\left(a \cdot a\right) \cdot 4\right)\right)} \]

      unpow2 [=>]33.8%

      \[ \left(2 \cdot {b}^{2} + 4\right) \cdot \color{blue}{\left(a \cdot a\right)} + \left({a}^{4} + a \cdot \left(\left(a \cdot a\right) \cdot 4\right)\right) \]

      *-commutative [=>]33.8%

      \[ \color{blue}{\left(a \cdot a\right) \cdot \left(2 \cdot {b}^{2} + 4\right)} + \left({a}^{4} + a \cdot \left(\left(a \cdot a\right) \cdot 4\right)\right) \]

      metadata-eval [<=]33.8%

      \[ \left(a \cdot a\right) \cdot \left(2 \cdot {b}^{2} + 4\right) + \left({a}^{\color{blue}{\left(2 \cdot 2\right)}} + a \cdot \left(\left(a \cdot a\right) \cdot 4\right)\right) \]

      pow-sqr [<=]33.8%

      \[ \left(a \cdot a\right) \cdot \left(2 \cdot {b}^{2} + 4\right) + \left(\color{blue}{{a}^{2} \cdot {a}^{2}} + a \cdot \left(\left(a \cdot a\right) \cdot 4\right)\right) \]

      unpow2 [=>]33.8%

      \[ \left(a \cdot a\right) \cdot \left(2 \cdot {b}^{2} + 4\right) + \left(\color{blue}{\left(a \cdot a\right)} \cdot {a}^{2} + a \cdot \left(\left(a \cdot a\right) \cdot 4\right)\right) \]

      unpow2 [=>]33.8%

      \[ \left(a \cdot a\right) \cdot \left(2 \cdot {b}^{2} + 4\right) + \left(\left(a \cdot a\right) \cdot \color{blue}{\left(a \cdot a\right)} + a \cdot \left(\left(a \cdot a\right) \cdot 4\right)\right) \]

      *-commutative [<=]33.8%

      \[ \left(a \cdot a\right) \cdot \left(2 \cdot {b}^{2} + 4\right) + \left(\left(a \cdot a\right) \cdot \left(a \cdot a\right) + \color{blue}{\left(\left(a \cdot a\right) \cdot 4\right) \cdot a}\right) \]

      associate-*l* [=>]33.8%

      \[ \left(a \cdot a\right) \cdot \left(2 \cdot {b}^{2} + 4\right) + \left(\left(a \cdot a\right) \cdot \left(a \cdot a\right) + \color{blue}{\left(a \cdot a\right) \cdot \left(4 \cdot a\right)}\right) \]

      distribute-lft-out [=>]100.0%

      \[ \left(a \cdot a\right) \cdot \left(2 \cdot {b}^{2} + 4\right) + \color{blue}{\left(a \cdot a\right) \cdot \left(a \cdot a + 4 \cdot a\right)} \]

      distribute-lft-out [=>]100.0%

      \[ \color{blue}{\left(a \cdot a\right) \cdot \left(\left(2 \cdot {b}^{2} + 4\right) + \left(a \cdot a + 4 \cdot a\right)\right)} \]

      fma-def [=>]100.0%

      \[ \left(a \cdot a\right) \cdot \left(\color{blue}{\mathsf{fma}\left(2, {b}^{2}, 4\right)} + \left(a \cdot a + 4 \cdot a\right)\right) \]

      unpow2 [=>]100.0%

      \[ \left(a \cdot a\right) \cdot \left(\mathsf{fma}\left(2, \color{blue}{b \cdot b}, 4\right) + \left(a \cdot a + 4 \cdot a\right)\right) \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;{\left(a \cdot a + b \cdot b\right)}^{2} + 4 \cdot \left(\left(a \cdot a\right) \cdot \left(a + 1\right) + \left(b \cdot b\right) \cdot \left(1 - a \cdot 3\right)\right) \leq \infty:\\ \;\;\;\;\left({\left(a \cdot a + b \cdot b\right)}^{2} + 4 \cdot \left(\left(a \cdot a\right) \cdot \left(a + 1\right) + \left(b \cdot b\right) \cdot \left(1 - a \cdot 3\right)\right)\right) + -1\\ \mathbf{else}:\\ \;\;\;\;\left(a \cdot a\right) \cdot \left(\mathsf{fma}\left(2, b \cdot b, 4\right) + a \cdot \left(a + 4\right)\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy99.9%
Cost16580
\[\begin{array}{l} t_0 := {\left(a \cdot a + b \cdot b\right)}^{2} + 4 \cdot \left(\left(a \cdot a\right) \cdot \left(a + 1\right) + \left(b \cdot b\right) \cdot \left(1 - a \cdot 3\right)\right)\\ \mathbf{if}\;t_0 \leq \infty:\\ \;\;\;\;t_0 + -1\\ \mathbf{else}:\\ \;\;\;\;\left(a \cdot a\right) \cdot \left(\mathsf{fma}\left(2, b \cdot b, 4\right) + a \cdot \left(a + 4\right)\right)\\ \end{array} \]
Alternative 2
Accuracy99.1%
Cost20544
\[\mathsf{fma}\left(2, b \cdot \left(b \cdot \left(a \cdot a\right)\right), {b}^{4} + {a}^{4}\right) + \left(4 \cdot \left(b \cdot b\right) + -1\right) \]
Alternative 3
Accuracy97.7%
Cost7625
\[\begin{array}{l} \mathbf{if}\;a \leq -2.4 \cdot 10^{+18} \lor \neg \left(a \leq 106000\right):\\ \;\;\;\;\left(a \cdot a\right) \cdot \left(\mathsf{fma}\left(2, b \cdot b, 4\right) + a \cdot \left(a + 4\right)\right)\\ \mathbf{else}:\\ \;\;\;\;4 \cdot \left(b \cdot b\right) + \left({b}^{4} + -1\right)\\ \end{array} \]
Alternative 4
Accuracy93.5%
Cost7304
\[\begin{array}{l} \mathbf{if}\;a \leq -8 \cdot 10^{+33}:\\ \;\;\;\;{a}^{4}\\ \mathbf{elif}\;a \leq 3.7 \cdot 10^{+59}:\\ \;\;\;\;4 \cdot \left(b \cdot b\right) + \left({b}^{4} + -1\right)\\ \mathbf{else}:\\ \;\;\;\;{a}^{4}\\ \end{array} \]
Alternative 5
Accuracy94.0%
Cost6788
\[\begin{array}{l} \mathbf{if}\;b \cdot b \leq 10^{+98}:\\ \;\;\;\;\left(a \cdot a\right) \cdot \left(a \cdot a + \left(4 + a \cdot 4\right)\right) + -1\\ \mathbf{else}:\\ \;\;\;\;{b}^{4}\\ \end{array} \]
Alternative 6
Accuracy79.8%
Cost1220
\[\begin{array}{l} \mathbf{if}\;b \cdot b \leq 7.2 \cdot 10^{+114}:\\ \;\;\;\;\left(a \cdot a\right) \cdot \left(a \cdot a + \left(4 + a \cdot 4\right)\right) + -1\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \left(\left(a \cdot a\right) \cdot \left(b \cdot b\right)\right)\\ \end{array} \]
Alternative 7
Accuracy79.2%
Cost964
\[\begin{array}{l} \mathbf{if}\;b \cdot b \leq 1.55 \cdot 10^{+115}:\\ \;\;\;\;\left(a \cdot a\right) \cdot \left(a \cdot \left(a + 4\right)\right) + -1\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \left(\left(a \cdot a\right) \cdot \left(b \cdot b\right)\right)\\ \end{array} \]
Alternative 8
Accuracy66.7%
Cost836
\[\begin{array}{l} \mathbf{if}\;b \cdot b \leq 0.031:\\ \;\;\;\;a \cdot \left(a \cdot 4\right) + -1\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \left(\left(a \cdot a\right) \cdot \left(b \cdot b\right)\right)\\ \end{array} \]
Alternative 9
Accuracy78.9%
Cost836
\[\begin{array}{l} \mathbf{if}\;b \cdot b \leq 1.7 \cdot 10^{+116}:\\ \;\;\;\;\left(a \cdot a\right) \cdot \left(a \cdot a\right) + -1\\ \mathbf{else}:\\ \;\;\;\;2 \cdot \left(\left(a \cdot a\right) \cdot \left(b \cdot b\right)\right)\\ \end{array} \]
Alternative 10
Accuracy50.9%
Cost448
\[a \cdot \left(a \cdot 4\right) + -1 \]
Alternative 11
Accuracy25.1%
Cost64
\[-1 \]

Reproduce?

herbie shell --seed 2023271 
(FPCore (a b)
  :name "Bouland and Aaronson, Equation (25)"
  :precision binary64
  (- (+ (pow (+ (* a a) (* b b)) 2.0) (* 4.0 (+ (* (* a a) (+ 1.0 a)) (* (* b b) (- 1.0 (* 3.0 a)))))) 1.0))