Bouland and Aaronson, Equation (25)

?

Percentage Accurate: 73.2% → 98.1%
Time: 6.7s
Precision: binary64
Cost: 47748

?

\[\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} \\ \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:\\ \;\;\;\;\mathsf{fma}\left(4, \mathsf{fma}\left(a, \mathsf{fma}\left(a, a, a\right), b \cdot \left(b \cdot \mathsf{fma}\left(a, -3, 1\right)\right)\right), {\left(\mathsf{hypot}\left(a, b\right)\right)}^{4}\right) + -1\\ \mathbf{else}:\\ \;\;\;\;\left(a \cdot a\right) \cdot \left(a \cdot \left(a + 4\right)\right)\\ \end{array} \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
 (if (<=
      (+
       (pow (+ (* a a) (* b b)) 2.0)
       (* 4.0 (+ (* (* a a) (+ a 1.0)) (* (* b b) (- 1.0 (* a 3.0))))))
      INFINITY)
   (+
    (fma
     4.0
     (fma a (fma a a a) (* b (* b (fma a -3.0 1.0))))
     (pow (hypot a b) 4.0))
    -1.0)
   (* (* a a) (* 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 tmp;
	if ((pow(((a * a) + (b * b)), 2.0) + (4.0 * (((a * a) * (a + 1.0)) + ((b * b) * (1.0 - (a * 3.0)))))) <= ((double) INFINITY)) {
		tmp = fma(4.0, fma(a, fma(a, a, a), (b * (b * fma(a, -3.0, 1.0)))), pow(hypot(a, b), 4.0)) + -1.0;
	} else {
		tmp = (a * a) * (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)
	tmp = 0.0
	if (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)))))) <= Inf)
		tmp = Float64(fma(4.0, fma(a, fma(a, a, a), Float64(b * Float64(b * fma(a, -3.0, 1.0)))), (hypot(a, b) ^ 4.0)) + -1.0);
	else
		tmp = Float64(Float64(a * a) * 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_] := If[LessEqual[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], Infinity], N[(N[(4.0 * N[(a * N[(a * a + a), $MachinePrecision] + N[(b * N[(b * N[(a * -3.0 + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[Power[N[Sqrt[a ^ 2 + b ^ 2], $MachinePrecision], 4.0], $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], N[(N[(a * a), $MachinePrecision] * N[(a * N[(a + 4.0), $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}

\\
\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:\\
\;\;\;\;\mathsf{fma}\left(4, \mathsf{fma}\left(a, \mathsf{fma}\left(a, a, a\right), b \cdot \left(b \cdot \mathsf{fma}\left(a, -3, 1\right)\right)\right), {\left(\mathsf{hypot}\left(a, b\right)\right)}^{4}\right) + -1\\

\mathbf{else}:\\
\;\;\;\;\left(a \cdot a\right) \cdot \left(a \cdot \left(a + 4\right)\right)\\


\end{array}
\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 10 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.9%

      \[\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. Simplified100.0%

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

      [Start]99.9%

      \[ \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 \]

      sub-neg [=>]99.9%

      \[ \color{blue}{\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) + \left(-1\right)} \]

    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. Simplified2.5%

      \[\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 b around 0 25.5%

      \[\leadsto \color{blue}{\left({a}^{4} + 4 \cdot \left({a}^{2} \cdot \left(1 + a\right)\right)\right) - 1} \]
    4. Simplified25.5%

      \[\leadsto \color{blue}{{a}^{4} + \left(\left(4 \cdot \left(a \cdot a\right)\right) \cdot \left(1 + a\right) - 1\right)} \]
      Step-by-step derivation

      [Start]25.5%

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

      associate--l+ [=>]25.5%

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

      associate-*r* [=>]25.5%

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

      unpow2 [=>]25.5%

      \[ {a}^{4} + \left(\left(4 \cdot \color{blue}{\left(a \cdot a\right)}\right) \cdot \left(1 + a\right) - 1\right) \]
    5. Taylor expanded in a around inf 25.5%

      \[\leadsto \color{blue}{4 \cdot {a}^{3} + {a}^{4}} \]
    6. Simplified91.7%

      \[\leadsto \color{blue}{{a}^{3} \cdot \left(4 + a\right)} \]
      Step-by-step derivation

      [Start]25.5%

      \[ 4 \cdot {a}^{3} + {a}^{4} \]

      *-commutative [=>]25.5%

      \[ \color{blue}{{a}^{3} \cdot 4} + {a}^{4} \]

      metadata-eval [<=]25.5%

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

      pow-plus [<=]25.5%

      \[ {a}^{3} \cdot 4 + \color{blue}{{a}^{3} \cdot a} \]

      distribute-lft-out [=>]91.7%

      \[ \color{blue}{{a}^{3} \cdot \left(4 + a\right)} \]
    7. Applied egg-rr92.9%

      \[\leadsto \color{blue}{\frac{{a}^{3} \cdot \left(16 - a \cdot a\right)}{4 - a}} \]
      Step-by-step derivation

      [Start]91.7%

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

      flip-+ [=>]91.7%

      \[ {a}^{3} \cdot \color{blue}{\frac{4 \cdot 4 - a \cdot a}{4 - a}} \]

      associate-*r/ [=>]92.9%

      \[ \color{blue}{\frac{{a}^{3} \cdot \left(4 \cdot 4 - a \cdot a\right)}{4 - a}} \]

      metadata-eval [=>]92.9%

      \[ \frac{{a}^{3} \cdot \left(\color{blue}{16} - a \cdot a\right)}{4 - a} \]
    8. Simplified91.7%

      \[\leadsto \color{blue}{\frac{16 - a \cdot a}{\frac{4 - a}{{a}^{3}}}} \]
      Step-by-step derivation

      [Start]92.9%

      \[ \frac{{a}^{3} \cdot \left(16 - a \cdot a\right)}{4 - a} \]

      *-commutative [<=]92.9%

      \[ \frac{\color{blue}{\left(16 - a \cdot a\right) \cdot {a}^{3}}}{4 - a} \]

      associate-/l* [=>]91.7%

      \[ \color{blue}{\frac{16 - a \cdot a}{\frac{4 - a}{{a}^{3}}}} \]
    9. Applied egg-rr91.7%

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

      [Start]91.7%

      \[ \frac{16 - a \cdot a}{\frac{4 - a}{{a}^{3}}} \]

      associate-/r/ [=>]91.7%

      \[ \color{blue}{\frac{16 - a \cdot a}{4 - a} \cdot {a}^{3}} \]

      metadata-eval [<=]91.7%

      \[ \frac{\color{blue}{4 \cdot 4} - a \cdot a}{4 - a} \cdot {a}^{3} \]

      flip-+ [<=]91.7%

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

      cube-mult [=>]91.7%

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

      associate-*r* [=>]91.7%

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

      +-commutative [=>]91.7%

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

    \[\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:\\ \;\;\;\;\mathsf{fma}\left(4, \mathsf{fma}\left(a, \mathsf{fma}\left(a, a, a\right), b \cdot \left(b \cdot \mathsf{fma}\left(a, -3, 1\right)\right)\right), {\left(\mathsf{hypot}\left(a, b\right)\right)}^{4}\right) + -1\\ \mathbf{else}:\\ \;\;\;\;\left(a \cdot a\right) \cdot \left(a \cdot \left(a + 4\right)\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy98.1%
Cost47748
\[\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:\\ \;\;\;\;\mathsf{fma}\left(4, \mathsf{fma}\left(a, \mathsf{fma}\left(a, a, a\right), b \cdot \left(b \cdot \mathsf{fma}\left(a, -3, 1\right)\right)\right), {\left(\mathsf{hypot}\left(a, b\right)\right)}^{4}\right) + -1\\ \mathbf{else}:\\ \;\;\;\;\left(a \cdot a\right) \cdot \left(a \cdot \left(a + 4\right)\right)\\ \end{array} \]
Alternative 2
Accuracy98.0%
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(a \cdot \left(a + 4\right)\right)\\ \end{array} \]
Alternative 3
Accuracy93.2%
Cost7305
\[\begin{array}{l} \mathbf{if}\;b \leq -1.08 \cdot 10^{+21} \lor \neg \left(b \leq 120000000\right):\\ \;\;\;\;{b}^{4}\\ \mathbf{else}:\\ \;\;\;\;{a}^{4} + \left(\left(a \cdot a\right) \cdot 4 + -1\right)\\ \end{array} \]
Alternative 4
Accuracy93.2%
Cost7241
\[\begin{array}{l} \mathbf{if}\;b \leq -1.7 \cdot 10^{+21} \lor \neg \left(b \leq 370000000\right):\\ \;\;\;\;{b}^{4}\\ \mathbf{else}:\\ \;\;\;\;\left(a \cdot a\right) \cdot \mathsf{fma}\left(a, a, 4\right) + -1\\ \end{array} \]
Alternative 5
Accuracy82.1%
Cost6792
\[\begin{array}{l} \mathbf{if}\;b \leq -5.6 \cdot 10^{+20}:\\ \;\;\;\;{b}^{4}\\ \mathbf{elif}\;b \leq 800000:\\ \;\;\;\;a \cdot \left(a \cdot 4\right) + -1\\ \mathbf{else}:\\ \;\;\;\;{b}^{4}\\ \end{array} \]
Alternative 6
Accuracy68.7%
Cost1352
\[\begin{array}{l} \mathbf{if}\;a \leq -1.3 \cdot 10^{-11}:\\ \;\;\;\;a \cdot \left(\left(a \cdot a\right) \cdot \left(a + 4\right)\right)\\ \mathbf{elif}\;a \leq 0.42:\\ \;\;\;\;a \cdot \left(a \cdot 4\right) + -1\\ \mathbf{else}:\\ \;\;\;\;\frac{16 - a \cdot a}{\frac{1}{a} \cdot \frac{4 - a}{a \cdot a}}\\ \end{array} \]
Alternative 7
Accuracy68.7%
Cost841
\[\begin{array}{l} \mathbf{if}\;a \leq -1.3 \cdot 10^{-11} \lor \neg \left(a \leq 0.42\right):\\ \;\;\;\;\left(a \cdot a\right) \cdot \left(a \cdot \left(a + 4\right)\right)\\ \mathbf{else}:\\ \;\;\;\;a \cdot \left(a \cdot 4\right) + -1\\ \end{array} \]
Alternative 8
Accuracy68.7%
Cost841
\[\begin{array}{l} \mathbf{if}\;a \leq -1.3 \cdot 10^{-11} \lor \neg \left(a \leq 0.42\right):\\ \;\;\;\;a \cdot \left(\left(a \cdot a\right) \cdot \left(a + 4\right)\right)\\ \mathbf{else}:\\ \;\;\;\;a \cdot \left(a \cdot 4\right) + -1\\ \end{array} \]
Alternative 9
Accuracy51.4%
Cost448
\[a \cdot \left(a \cdot 4\right) + -1 \]
Alternative 10
Accuracy24.6%
Cost64
\[-1 \]

Reproduce?

herbie shell --seed 2023167 
(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))