Migdal et al, Equation (64)

?

Percentage Accurate: 99.6% → 99.6%
Time: 10.8s
Precision: binary64
Cost: 19776

?

\[ \begin{array}{c}[a1, a2] = \mathsf{sort}([a1, a2])\\ \end{array} \]
\[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
\[\frac{\mathsf{fma}\left(a2, a2, a1 \cdot a1\right)}{\frac{\sqrt{2}}{\cos th}} \]
(FPCore (a1 a2 th)
 :precision binary64
 (+
  (* (/ (cos th) (sqrt 2.0)) (* a1 a1))
  (* (/ (cos th) (sqrt 2.0)) (* a2 a2))))
(FPCore (a1 a2 th)
 :precision binary64
 (/ (fma a2 a2 (* a1 a1)) (/ (sqrt 2.0) (cos th))))
double code(double a1, double a2, double th) {
	return ((cos(th) / sqrt(2.0)) * (a1 * a1)) + ((cos(th) / sqrt(2.0)) * (a2 * a2));
}
double code(double a1, double a2, double th) {
	return fma(a2, a2, (a1 * a1)) / (sqrt(2.0) / cos(th));
}
function code(a1, a2, th)
	return Float64(Float64(Float64(cos(th) / sqrt(2.0)) * Float64(a1 * a1)) + Float64(Float64(cos(th) / sqrt(2.0)) * Float64(a2 * a2)))
end
function code(a1, a2, th)
	return Float64(fma(a2, a2, Float64(a1 * a1)) / Float64(sqrt(2.0) / cos(th)))
end
code[a1_, a2_, th_] := N[(N[(N[(N[Cos[th], $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision] * N[(a1 * a1), $MachinePrecision]), $MachinePrecision] + N[(N[(N[Cos[th], $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision] * N[(a2 * a2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[a1_, a2_, th_] := N[(N[(a2 * a2 + N[(a1 * a1), $MachinePrecision]), $MachinePrecision] / N[(N[Sqrt[2.0], $MachinePrecision] / N[Cos[th], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right)
\frac{\mathsf{fma}\left(a2, a2, a1 \cdot a1\right)}{\frac{\sqrt{2}}{\cos th}}

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 13 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. Initial program 99.6%

    \[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]
  2. Simplified99.7%

    \[\leadsto \color{blue}{\cos th \cdot \frac{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}{\sqrt{2}}} \]
    Step-by-step derivation

    [Start]99.6%

    \[ \frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1\right) + \frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right) \]

    distribute-lft-out [=>]99.6%

    \[ \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]

    associate-*l/ [=>]99.7%

    \[ \color{blue}{\frac{\cos th \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]

    associate-*r/ [<=]99.6%

    \[ \color{blue}{\cos th \cdot \frac{a1 \cdot a1 + a2 \cdot a2}{\sqrt{2}}} \]

    fma-def [=>]99.7%

    \[ \cos th \cdot \frac{\color{blue}{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}}{\sqrt{2}} \]
  3. Taylor expanded in th around inf 99.7%

    \[\leadsto \color{blue}{\frac{\left({a2}^{2} + {a1}^{2}\right) \cdot \cos th}{\sqrt{2}}} \]
  4. Simplified99.7%

    \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a2, a2, a1 \cdot a1\right)}{\frac{\sqrt{2}}{\cos th}}} \]
    Step-by-step derivation

    [Start]99.7%

    \[ \frac{\left({a2}^{2} + {a1}^{2}\right) \cdot \cos th}{\sqrt{2}} \]

    associate-/l* [=>]99.7%

    \[ \color{blue}{\frac{{a2}^{2} + {a1}^{2}}{\frac{\sqrt{2}}{\cos th}}} \]

    unpow2 [=>]99.7%

    \[ \frac{\color{blue}{a2 \cdot a2} + {a1}^{2}}{\frac{\sqrt{2}}{\cos th}} \]

    unpow2 [=>]99.7%

    \[ \frac{a2 \cdot a2 + \color{blue}{a1 \cdot a1}}{\frac{\sqrt{2}}{\cos th}} \]

    fma-def [=>]99.7%

    \[ \frac{\color{blue}{\mathsf{fma}\left(a2, a2, a1 \cdot a1\right)}}{\frac{\sqrt{2}}{\cos th}} \]
  5. Final simplification99.7%

    \[\leadsto \frac{\mathsf{fma}\left(a2, a2, a1 \cdot a1\right)}{\frac{\sqrt{2}}{\cos th}} \]

Alternatives

Alternative 1
Accuracy99.6%
Cost19776
\[\frac{\mathsf{fma}\left(a2, a2, a1 \cdot a1\right)}{\frac{\sqrt{2}}{\cos th}} \]
Alternative 2
Accuracy78.7%
Cost19780
\[\begin{array}{l} \mathbf{if}\;\cos th \leq 0.065:\\ \;\;\;\;a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a1 \cdot a1 + a2 \cdot a2}{\sqrt{2}}\\ \end{array} \]
Alternative 3
Accuracy99.6%
Cost19776
\[\cos th \cdot \frac{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}{\sqrt{2}} \]
Alternative 4
Accuracy99.6%
Cost13504
\[\left(\cos th \cdot \sqrt{0.5}\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
Alternative 5
Accuracy87.8%
Cost13380
\[\begin{array}{l} \mathbf{if}\;a2 \leq 7.2 \cdot 10^{-112}:\\ \;\;\;\;\cos th \cdot \left(a1 \cdot \left(a1 \cdot \sqrt{0.5}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}\\ \end{array} \]
Alternative 6
Accuracy87.7%
Cost13380
\[\begin{array}{l} \mathbf{if}\;a2 \leq 7.2 \cdot 10^{-112}:\\ \;\;\;\;\cos th \cdot \left(\left(a1 \cdot a1\right) \cdot \sqrt{0.5}\right)\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}\\ \end{array} \]
Alternative 7
Accuracy87.7%
Cost13380
\[\begin{array}{l} \mathbf{if}\;a2 \leq 7.2 \cdot 10^{-112}:\\ \;\;\;\;\cos th \cdot \left(\left(a1 \cdot a1\right) \cdot \sqrt{0.5}\right)\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \frac{\cos th}{\frac{\sqrt{2}}{a2}}\\ \end{array} \]
Alternative 8
Accuracy65.8%
Cost6976
\[\sqrt{0.5} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
Alternative 9
Accuracy65.8%
Cost6976
\[\frac{a1 \cdot a1 + a2 \cdot a2}{\sqrt{2}} \]
Alternative 10
Accuracy58.7%
Cost6852
\[\begin{array}{l} \mathbf{if}\;a2 \leq 1.2 \cdot 10^{-114}:\\ \;\;\;\;\left(a1 \cdot a1\right) \cdot \sqrt{0.5}\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(a2 \cdot \sqrt{0.5}\right)\\ \end{array} \]
Alternative 11
Accuracy58.7%
Cost6852
\[\begin{array}{l} \mathbf{if}\;a2 \leq 1.12 \cdot 10^{-114}:\\ \;\;\;\;\frac{a1 \cdot a1}{\sqrt{2}}\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(a2 \cdot \sqrt{0.5}\right)\\ \end{array} \]
Alternative 12
Accuracy58.7%
Cost6852
\[\begin{array}{l} \mathbf{if}\;a2 \leq 1.2 \cdot 10^{-114}:\\ \;\;\;\;\frac{a1 \cdot a1}{\sqrt{2}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a2 \cdot a2}{\sqrt{2}}\\ \end{array} \]
Alternative 13
Accuracy40.0%
Cost6720
\[a2 \cdot \left(a2 \cdot \sqrt{0.5}\right) \]

Reproduce?

herbie shell --seed 2023165 
(FPCore (a1 a2 th)
  :name "Migdal et al, Equation (64)"
  :precision binary64
  (+ (* (/ (cos th) (sqrt 2.0)) (* a1 a1)) (* (/ (cos th) (sqrt 2.0)) (* a2 a2))))