Bouland and Aaronson, Equation (24)

?

Percentage Accurate: 73.5% → 98.9%
Time: 10.7s
Precision: binary64
Cost: 7744

?

\[\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(3 + a\right)\right)\right) - 1 \]
\[\left(\mathsf{fma}\left(b, b, a \cdot a\right) \cdot \left(a \cdot a + b \cdot b\right) + b \cdot \left(b \cdot 12\right)\right) + -1 \]
(FPCore (a b)
 :precision binary64
 (-
  (+
   (pow (+ (* a a) (* b b)) 2.0)
   (* 4.0 (+ (* (* a a) (- 1.0 a)) (* (* b b) (+ 3.0 a)))))
  1.0))
(FPCore (a b)
 :precision binary64
 (+ (+ (* (fma b b (* a a)) (+ (* a a) (* b b))) (* b (* b 12.0))) -1.0))
double code(double a, double b) {
	return (pow(((a * a) + (b * b)), 2.0) + (4.0 * (((a * a) * (1.0 - a)) + ((b * b) * (3.0 + a))))) - 1.0;
}
double code(double a, double b) {
	return ((fma(b, b, (a * a)) * ((a * a) + (b * b))) + (b * (b * 12.0))) + -1.0;
}
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(3.0 + a))))) - 1.0)
end
function code(a, b)
	return Float64(Float64(Float64(fma(b, b, Float64(a * a)) * Float64(Float64(a * a) + Float64(b * b))) + Float64(b * Float64(b * 12.0))) + -1.0)
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[(3.0 + a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
code[a_, b_] := N[(N[(N[(N[(b * b + N[(a * a), $MachinePrecision]), $MachinePrecision] * N[(N[(a * a), $MachinePrecision] + N[(b * b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(b * N[(b * 12.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $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(3 + a\right)\right)\right) - 1
\left(\mathsf{fma}\left(b, b, a \cdot a\right) \cdot \left(a \cdot a + b \cdot b\right) + b \cdot \left(b \cdot 12\right)\right) + -1

Local Percentage Accuracy?

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 6 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Bogosity?

Bogosity

Derivation?

  1. Initial program 71.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(3 + a\right)\right)\right) - 1 \]
  2. Taylor expanded in a around 0 99.2%

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

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

    [Start]99.2

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

    *-commutative [=>]99.2

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

    unpow2 [=>]99.2

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

    associate-*r* [<=]99.2

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

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

    [Start]99.2

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

    unpow2 [=>]99.2

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

    distribute-lft-in [=>]93.3

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

    add-sqr-sqrt [=>]93.3

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

    pow2 [=>]93.3

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

    hypot-def [=>]93.3

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

    add-sqr-sqrt [=>]93.3

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

    pow2 [=>]93.3

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

    hypot-def [=>]93.3

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

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

    [Start]93.3

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

    unpow2 [<=]93.3

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

    unpow2 [<=]93.3

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

    distribute-lft-out [=>]99.2

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

    unpow2 [=>]99.2

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

    unpow2 [=>]99.2

    \[ \left({\left(\mathsf{hypot}\left(a, b\right)\right)}^{2} \cdot \left(a \cdot a + \color{blue}{b \cdot b}\right) + 4 \cdot \left(b \cdot \left(b \cdot 3\right)\right)\right) - 1 \]
  6. Applied egg-rr99.2%

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

    [Start]99.2

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

    unpow2 [=>]99.2

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

    hypot-udef [=>]99.2

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

    hypot-udef [=>]99.2

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

    add-sqr-sqrt [<=]99.2

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

    +-commutative [=>]99.2

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

    fma-def [=>]99.2

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

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

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

    [Start]99.2

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

    unpow2 [=>]99.2

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

    *-commutative [<=]99.2

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

    associate-*l* [=>]99.2

    \[ \left(\mathsf{fma}\left(b, b, a \cdot a\right) \cdot \left(a \cdot a + b \cdot b\right) + \color{blue}{b \cdot \left(b \cdot 12\right)}\right) - 1 \]
  9. Final simplification99.2%

    \[\leadsto \left(\mathsf{fma}\left(b, b, a \cdot a\right) \cdot \left(a \cdot a + b \cdot b\right) + b \cdot \left(b \cdot 12\right)\right) + -1 \]

Alternatives

Alternative 1
Accuracy97.2%
Cost1604
\[\begin{array}{l} t_0 := 4 \cdot \left(b \cdot \left(b \cdot 3\right)\right)\\ \mathbf{if}\;b \cdot b \leq 0.1:\\ \;\;\;\;\left(t_0 + \left(a \cdot a\right) \cdot \left(a \cdot a\right)\right) + -1\\ \mathbf{else}:\\ \;\;\;\;\left(t_0 + \left(b \cdot b\right) \cdot \left(a \cdot a + b \cdot b\right)\right) + -1\\ \end{array} \]
Alternative 2
Accuracy98.9%
Cost1600
\[\begin{array}{l} t_0 := a \cdot a + b \cdot b\\ \left(t_0 \cdot t_0 + 4 \cdot \left(b \cdot \left(b \cdot 3\right)\right)\right) + -1 \end{array} \]
Alternative 3
Accuracy85.5%
Cost1088
\[\left(4 \cdot \left(b \cdot \left(b \cdot 3\right)\right) + \left(a \cdot a\right) \cdot \left(a \cdot a\right)\right) + -1 \]
Alternative 4
Accuracy66.7%
Cost841
\[\begin{array}{l} \mathbf{if}\;a \leq -0.41 \lor \neg \left(a \leq 2.4\right):\\ \;\;\;\;2 \cdot \left(\left(a \cdot a\right) \cdot \left(b \cdot b\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(b \cdot b\right) \cdot 12 + -1\\ \end{array} \]
Alternative 5
Accuracy50.7%
Cost448
\[\left(b \cdot b\right) \cdot 12 + -1 \]

Reproduce?

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