Average Error: 0.5 → 0.5
Time: 14.5s
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) \]
\[\mathsf{fma}\left(a2, a2, a1 \cdot a1\right) \cdot \frac{\cos th}{\sqrt{2}} \]
(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)) (/ (cos th) (sqrt 2.0))))
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)) * (cos(th) / sqrt(2.0));
}
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(cos(th) / sqrt(2.0)))
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[Cos[th], $MachinePrecision] / N[Sqrt[2.0], $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)
\mathsf{fma}\left(a2, a2, a1 \cdot a1\right) \cdot \frac{\cos th}{\sqrt{2}}

Error

Derivation

  1. Initial program 0.5

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

    \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \mathsf{fma}\left(a1, a1, a2 \cdot a2\right)} \]
    Proof
    (*.f64 (/.f64 (cos.f64 th) (sqrt.f64 2)) (fma.f64 a1 a1 (*.f64 a2 a2))): 0 points increase in error, 0 points decrease in error
    (*.f64 (/.f64 (cos.f64 th) (sqrt.f64 2)) (Rewrite<= fma-def_binary64 (+.f64 (*.f64 a1 a1) (*.f64 a2 a2)))): 1 points increase in error, 0 points decrease in error
    (Rewrite<= distribute-lft-out_binary64 (+.f64 (*.f64 (/.f64 (cos.f64 th) (sqrt.f64 2)) (*.f64 a1 a1)) (*.f64 (/.f64 (cos.f64 th) (sqrt.f64 2)) (*.f64 a2 a2)))): 0 points increase in error, 1 points decrease in error
  3. Taylor expanded in th around inf 0.5

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

    \[\leadsto \color{blue}{\mathsf{fma}\left(a2, a2, a1 \cdot a1\right) \cdot \frac{\cos th}{\sqrt{2}}} \]
    Proof
    (*.f64 (fma.f64 a2 a2 (*.f64 a1 a1)) (/.f64 (cos.f64 th) (sqrt.f64 2))): 0 points increase in error, 0 points decrease in error
    (*.f64 (fma.f64 a2 a2 (Rewrite<= unpow2_binary64 (pow.f64 a1 2))) (/.f64 (cos.f64 th) (sqrt.f64 2))): 0 points increase in error, 0 points decrease in error
    (*.f64 (Rewrite<= fma-def_binary64 (+.f64 (*.f64 a2 a2) (pow.f64 a1 2))) (/.f64 (cos.f64 th) (sqrt.f64 2))): 2 points increase in error, 0 points decrease in error
    (*.f64 (+.f64 (Rewrite<= unpow2_binary64 (pow.f64 a2 2)) (pow.f64 a1 2)) (/.f64 (cos.f64 th) (sqrt.f64 2))): 0 points increase in error, 0 points decrease in error
    (Rewrite<= *-commutative_binary64 (*.f64 (/.f64 (cos.f64 th) (sqrt.f64 2)) (+.f64 (pow.f64 a2 2) (pow.f64 a1 2)))): 0 points increase in error, 0 points decrease in error
    (Rewrite=> associate-*l/_binary64 (/.f64 (*.f64 (cos.f64 th) (+.f64 (pow.f64 a2 2) (pow.f64 a1 2))) (sqrt.f64 2))): 24 points increase in error, 29 points decrease in error
    (/.f64 (Rewrite<= *-commutative_binary64 (*.f64 (+.f64 (pow.f64 a2 2) (pow.f64 a1 2)) (cos.f64 th))) (sqrt.f64 2)): 0 points increase in error, 0 points decrease in error
  5. Final simplification0.5

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

Alternatives

Alternative 1
Error14.3
Cost13512
\[\begin{array}{l} t_1 := \sqrt{0.5} \cdot \left(a2 \cdot \left(a2 \cdot \cos th\right)\right)\\ \mathbf{if}\;th \leq -2.513921369752175:\\ \;\;\;\;t_1\\ \mathbf{elif}\;th \leq 62.54464064129498:\\ \;\;\;\;\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \sqrt{0.5}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 2
Error0.5
Cost13504
\[\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
Alternative 3
Error7.7
Cost13380
\[\begin{array}{l} \mathbf{if}\;a2 \leq 7.629577286700069 \cdot 10^{-153}:\\ \;\;\;\;\left(a1 \cdot a1\right) \cdot \left(\cos th \cdot \sqrt{0.5}\right)\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5} \cdot \left(a2 \cdot \left(a2 \cdot \cos th\right)\right)\\ \end{array} \]
Alternative 4
Error7.7
Cost13380
\[\begin{array}{l} \mathbf{if}\;a2 \leq 7.629577286700069 \cdot 10^{-153}:\\ \;\;\;\;\left(a1 \cdot a1\right) \cdot \left(\cos th \cdot \sqrt{0.5}\right)\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(\sqrt{0.5} \cdot \left(a2 \cdot \cos th\right)\right)\\ \end{array} \]
Alternative 5
Error7.7
Cost13380
\[\begin{array}{l} \mathbf{if}\;a2 \leq 7.629577286700069 \cdot 10^{-153}:\\ \;\;\;\;a1 \cdot \left(a1 \cdot \left(\cos th \cdot \sqrt{0.5}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(\sqrt{0.5} \cdot \left(a2 \cdot \cos th\right)\right)\\ \end{array} \]
Alternative 6
Error25.8
Cost6976
\[\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \sqrt{0.5} \]
Alternative 7
Error29.8
Cost6852
\[\begin{array}{l} \mathbf{if}\;a2 \leq 7.629577286700069 \cdot 10^{-153}:\\ \;\;\;\;\left(a1 \cdot a1\right) \cdot \sqrt{0.5}\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(a2 \cdot \sqrt{0.5}\right)\\ \end{array} \]
Alternative 8
Error40.5
Cost6720
\[a2 \cdot \left(a2 \cdot \sqrt{0.5}\right) \]

Error

Reproduce

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