Migdal et al, Equation (64)

Percentage Accurate: 99.5% → 99.6%
Time: 10.9s
Alternatives: 14
Speedup: 2.0×

Specification

?
\[\begin{array}{l} \\ \begin{array}{l} t_1 := \frac{\cos th}{\sqrt{2}}\\ t_1 \cdot \left(a1 \cdot a1\right) + t_1 \cdot \left(a2 \cdot a2\right) \end{array} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (let* ((t_1 (/ (cos th) (sqrt 2.0))))
   (+ (* t_1 (* a1 a1)) (* t_1 (* a2 a2)))))
double code(double a1, double a2, double th) {
	double t_1 = cos(th) / sqrt(2.0);
	return (t_1 * (a1 * a1)) + (t_1 * (a2 * a2));
}
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    real(8) :: t_1
    t_1 = cos(th) / sqrt(2.0d0)
    code = (t_1 * (a1 * a1)) + (t_1 * (a2 * a2))
end function
public static double code(double a1, double a2, double th) {
	double t_1 = Math.cos(th) / Math.sqrt(2.0);
	return (t_1 * (a1 * a1)) + (t_1 * (a2 * a2));
}
def code(a1, a2, th):
	t_1 = math.cos(th) / math.sqrt(2.0)
	return (t_1 * (a1 * a1)) + (t_1 * (a2 * a2))
function code(a1, a2, th)
	t_1 = Float64(cos(th) / sqrt(2.0))
	return Float64(Float64(t_1 * Float64(a1 * a1)) + Float64(t_1 * Float64(a2 * a2)))
end
function tmp = code(a1, a2, th)
	t_1 = cos(th) / sqrt(2.0);
	tmp = (t_1 * (a1 * a1)) + (t_1 * (a2 * a2));
end
code[a1_, a2_, th_] := Block[{t$95$1 = N[(N[Cos[th], $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]}, N[(N[(t$95$1 * N[(a1 * a1), $MachinePrecision]), $MachinePrecision] + N[(t$95$1 * N[(a2 * a2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := \frac{\cos th}{\sqrt{2}}\\
t_1 \cdot \left(a1 \cdot a1\right) + t_1 \cdot \left(a2 \cdot a2\right)
\end{array}
\end{array}

Sampling outcomes in binary64 precision:

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.

Accuracy vs Speed?

Herbie found 14 alternatives:

AlternativeAccuracySpeedup
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.

Initial Program: 99.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := \frac{\cos th}{\sqrt{2}}\\ t_1 \cdot \left(a1 \cdot a1\right) + t_1 \cdot \left(a2 \cdot a2\right) \end{array} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (let* ((t_1 (/ (cos th) (sqrt 2.0))))
   (+ (* t_1 (* a1 a1)) (* t_1 (* a2 a2)))))
double code(double a1, double a2, double th) {
	double t_1 = cos(th) / sqrt(2.0);
	return (t_1 * (a1 * a1)) + (t_1 * (a2 * a2));
}
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    real(8) :: t_1
    t_1 = cos(th) / sqrt(2.0d0)
    code = (t_1 * (a1 * a1)) + (t_1 * (a2 * a2))
end function
public static double code(double a1, double a2, double th) {
	double t_1 = Math.cos(th) / Math.sqrt(2.0);
	return (t_1 * (a1 * a1)) + (t_1 * (a2 * a2));
}
def code(a1, a2, th):
	t_1 = math.cos(th) / math.sqrt(2.0)
	return (t_1 * (a1 * a1)) + (t_1 * (a2 * a2))
function code(a1, a2, th)
	t_1 = Float64(cos(th) / sqrt(2.0))
	return Float64(Float64(t_1 * Float64(a1 * a1)) + Float64(t_1 * Float64(a2 * a2)))
end
function tmp = code(a1, a2, th)
	t_1 = cos(th) / sqrt(2.0);
	tmp = (t_1 * (a1 * a1)) + (t_1 * (a2 * a2));
end
code[a1_, a2_, th_] := Block[{t$95$1 = N[(N[Cos[th], $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]}, N[(N[(t$95$1 * N[(a1 * a1), $MachinePrecision]), $MachinePrecision] + N[(t$95$1 * N[(a2 * a2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := \frac{\cos th}{\sqrt{2}}\\
t_1 \cdot \left(a1 \cdot a1\right) + t_1 \cdot \left(a2 \cdot a2\right)
\end{array}
\end{array}

Alternative 1: 99.6% accurate, 1.3× speedup?

\[\begin{array}{l} [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ \cos th \cdot \frac{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}{\sqrt{2}} \end{array} \]
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th)
 :precision binary64
 (* (cos th) (/ (fma a1 a1 (* a2 a2)) (sqrt 2.0))))
assert(a1 < a2);
double code(double a1, double a2, double th) {
	return cos(th) * (fma(a1, a1, (a2 * a2)) / sqrt(2.0));
}
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	return Float64(cos(th) * Float64(fma(a1, a1, Float64(a2 * a2)) / sqrt(2.0)))
end
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := N[(N[Cos[th], $MachinePrecision] * N[(N[(a1 * a1 + N[(a2 * a2), $MachinePrecision]), $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
\cos th \cdot \frac{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}{\sqrt{2}}
\end{array}
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. Step-by-step derivation
    1. distribute-lft-out99.6%

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
    2. associate-*l/99.6%

      \[\leadsto \color{blue}{\frac{\cos th \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
    3. associate-*r/99.7%

      \[\leadsto \color{blue}{\cos th \cdot \frac{a1 \cdot a1 + a2 \cdot a2}{\sqrt{2}}} \]
    4. fma-def99.7%

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

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

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

Alternative 2: 78.9% accurate, 1.3× speedup?

\[\begin{array}{l} [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ \begin{array}{l} \mathbf{if}\;\cos th \leq 0.115:\\ \;\;\;\;a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)\\ \end{array} \end{array} \]
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th)
 :precision binary64
 (if (<= (cos th) 0.115)
   (* a2 (/ a2 (/ (sqrt 2.0) (cos th))))
   (* (sqrt 0.5) (+ (* a2 a2) (* a1 a1)))))
assert(a1 < a2);
double code(double a1, double a2, double th) {
	double tmp;
	if (cos(th) <= 0.115) {
		tmp = a2 * (a2 / (sqrt(2.0) / cos(th)));
	} else {
		tmp = sqrt(0.5) * ((a2 * a2) + (a1 * a1));
	}
	return tmp;
}
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    real(8) :: tmp
    if (cos(th) <= 0.115d0) then
        tmp = a2 * (a2 / (sqrt(2.0d0) / cos(th)))
    else
        tmp = sqrt(0.5d0) * ((a2 * a2) + (a1 * a1))
    end if
    code = tmp
end function
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	double tmp;
	if (Math.cos(th) <= 0.115) {
		tmp = a2 * (a2 / (Math.sqrt(2.0) / Math.cos(th)));
	} else {
		tmp = Math.sqrt(0.5) * ((a2 * a2) + (a1 * a1));
	}
	return tmp;
}
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	tmp = 0
	if math.cos(th) <= 0.115:
		tmp = a2 * (a2 / (math.sqrt(2.0) / math.cos(th)))
	else:
		tmp = math.sqrt(0.5) * ((a2 * a2) + (a1 * a1))
	return tmp
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	tmp = 0.0
	if (cos(th) <= 0.115)
		tmp = Float64(a2 * Float64(a2 / Float64(sqrt(2.0) / cos(th))));
	else
		tmp = Float64(sqrt(0.5) * Float64(Float64(a2 * a2) + Float64(a1 * a1)));
	end
	return tmp
end
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp_2 = code(a1, a2, th)
	tmp = 0.0;
	if (cos(th) <= 0.115)
		tmp = a2 * (a2 / (sqrt(2.0) / cos(th)));
	else
		tmp = sqrt(0.5) * ((a2 * a2) + (a1 * a1));
	end
	tmp_2 = tmp;
end
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := If[LessEqual[N[Cos[th], $MachinePrecision], 0.115], N[(a2 * N[(a2 / N[(N[Sqrt[2.0], $MachinePrecision] / N[Cos[th], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Sqrt[0.5], $MachinePrecision] * N[(N[(a2 * a2), $MachinePrecision] + N[(a1 * a1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
\begin{array}{l}
\mathbf{if}\;\cos th \leq 0.115:\\
\;\;\;\;a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}\\

\mathbf{else}:\\
\;\;\;\;\sqrt{0.5} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (cos.f64 th) < 0.115000000000000005

    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. Step-by-step derivation
      1. distribute-lft-out99.6%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
      2. associate-*l/99.6%

        \[\leadsto \color{blue}{\frac{\cos th \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
      3. associate-*r/99.7%

        \[\leadsto \color{blue}{\cos th \cdot \frac{a1 \cdot a1 + a2 \cdot a2}{\sqrt{2}}} \]
      4. fma-def99.7%

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

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

      \[\leadsto \color{blue}{\frac{{a2}^{2} \cdot \cos th}{\sqrt{2}}} \]
    5. Step-by-step derivation
      1. unpow261.2%

        \[\leadsto \frac{\color{blue}{\left(a2 \cdot a2\right)} \cdot \cos th}{\sqrt{2}} \]
      2. associate-*l*61.3%

        \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
      3. associate-*r/61.2%

        \[\leadsto \color{blue}{a2 \cdot \frac{a2 \cdot \cos th}{\sqrt{2}}} \]
      4. associate-/l*61.2%

        \[\leadsto a2 \cdot \color{blue}{\frac{a2}{\frac{\sqrt{2}}{\cos th}}} \]
    6. Simplified61.2%

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

    if 0.115000000000000005 < (cos.f64 th)

    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. Step-by-step derivation
      1. distribute-lft-out99.6%

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

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
    4. Step-by-step derivation
      1. clear-num99.6%

        \[\leadsto \color{blue}{\frac{1}{\frac{\sqrt{2}}{\cos th}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
      2. associate-/r/99.5%

        \[\leadsto \color{blue}{\left(\frac{1}{\sqrt{2}} \cdot \cos th\right)} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
      3. pow1/299.5%

        \[\leadsto \left(\frac{1}{\color{blue}{{2}^{0.5}}} \cdot \cos th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
      4. pow-flip99.6%

        \[\leadsto \left(\color{blue}{{2}^{\left(-0.5\right)}} \cdot \cos th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
      5. metadata-eval99.6%

        \[\leadsto \left({2}^{\color{blue}{-0.5}} \cdot \cos th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. Applied egg-rr99.6%

      \[\leadsto \color{blue}{\left({2}^{-0.5} \cdot \cos th\right)} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    6. Taylor expanded in th around 0 83.7%

      \[\leadsto \color{blue}{\sqrt{0.5} \cdot \left({a2}^{2} + {a1}^{2}\right)} \]
    7. Step-by-step derivation
      1. unpow283.7%

        \[\leadsto \sqrt{0.5} \cdot \left(\color{blue}{a2 \cdot a2} + {a1}^{2}\right) \]
      2. unpow283.7%

        \[\leadsto \sqrt{0.5} \cdot \left(a2 \cdot a2 + \color{blue}{a1 \cdot a1}\right) \]
    8. Simplified83.7%

      \[\leadsto \color{blue}{\sqrt{0.5} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification78.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\cos th \leq 0.115:\\ \;\;\;\;a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{0.5} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)\\ \end{array} \]

Alternative 3: 99.6% accurate, 2.0× speedup?

\[\begin{array}{l} [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ \left(\cos th \cdot {2}^{-0.5}\right) \cdot \left(a2 \cdot a2 + a1 \cdot a1\right) \end{array} \]
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th)
 :precision binary64
 (* (* (cos th) (pow 2.0 -0.5)) (+ (* a2 a2) (* a1 a1))))
assert(a1 < a2);
double code(double a1, double a2, double th) {
	return (cos(th) * pow(2.0, -0.5)) * ((a2 * a2) + (a1 * a1));
}
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    code = (cos(th) * (2.0d0 ** (-0.5d0))) * ((a2 * a2) + (a1 * a1))
end function
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	return (Math.cos(th) * Math.pow(2.0, -0.5)) * ((a2 * a2) + (a1 * a1));
}
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	return (math.cos(th) * math.pow(2.0, -0.5)) * ((a2 * a2) + (a1 * a1))
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	return Float64(Float64(cos(th) * (2.0 ^ -0.5)) * Float64(Float64(a2 * a2) + Float64(a1 * a1)))
end
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp = code(a1, a2, th)
	tmp = (cos(th) * (2.0 ^ -0.5)) * ((a2 * a2) + (a1 * a1));
end
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := N[(N[(N[Cos[th], $MachinePrecision] * N[Power[2.0, -0.5], $MachinePrecision]), $MachinePrecision] * N[(N[(a2 * a2), $MachinePrecision] + N[(a1 * a1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
\left(\cos th \cdot {2}^{-0.5}\right) \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)
\end{array}
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. Step-by-step derivation
    1. distribute-lft-out99.6%

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

    \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
  4. Step-by-step derivation
    1. clear-num99.6%

      \[\leadsto \color{blue}{\frac{1}{\frac{\sqrt{2}}{\cos th}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    2. associate-/r/99.5%

      \[\leadsto \color{blue}{\left(\frac{1}{\sqrt{2}} \cdot \cos th\right)} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    3. pow1/299.5%

      \[\leadsto \left(\frac{1}{\color{blue}{{2}^{0.5}}} \cdot \cos th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    4. pow-flip99.6%

      \[\leadsto \left(\color{blue}{{2}^{\left(-0.5\right)}} \cdot \cos th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. metadata-eval99.6%

      \[\leadsto \left({2}^{\color{blue}{-0.5}} \cdot \cos th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
  5. Applied egg-rr99.6%

    \[\leadsto \color{blue}{\left({2}^{-0.5} \cdot \cos th\right)} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
  6. Final simplification99.6%

    \[\leadsto \left(\cos th \cdot {2}^{-0.5}\right) \cdot \left(a2 \cdot a2 + a1 \cdot a1\right) \]

Alternative 4: 99.6% accurate, 2.0× speedup?

\[\begin{array}{l} [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ \sqrt{0.5} \cdot \left(\cos th \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)\right) \end{array} \]
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th)
 :precision binary64
 (* (sqrt 0.5) (* (cos th) (+ (* a2 a2) (* a1 a1)))))
assert(a1 < a2);
double code(double a1, double a2, double th) {
	return sqrt(0.5) * (cos(th) * ((a2 * a2) + (a1 * a1)));
}
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    code = sqrt(0.5d0) * (cos(th) * ((a2 * a2) + (a1 * a1)))
end function
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	return Math.sqrt(0.5) * (Math.cos(th) * ((a2 * a2) + (a1 * a1)));
}
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	return math.sqrt(0.5) * (math.cos(th) * ((a2 * a2) + (a1 * a1)))
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	return Float64(sqrt(0.5) * Float64(cos(th) * Float64(Float64(a2 * a2) + Float64(a1 * a1))))
end
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp = code(a1, a2, th)
	tmp = sqrt(0.5) * (cos(th) * ((a2 * a2) + (a1 * a1)));
end
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := N[(N[Sqrt[0.5], $MachinePrecision] * N[(N[Cos[th], $MachinePrecision] * N[(N[(a2 * a2), $MachinePrecision] + N[(a1 * a1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
\sqrt{0.5} \cdot \left(\cos th \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)\right)
\end{array}
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. Step-by-step derivation
    1. distribute-lft-out99.6%

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

    \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
  4. Step-by-step derivation
    1. clear-num99.6%

      \[\leadsto \color{blue}{\frac{1}{\frac{\sqrt{2}}{\cos th}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    2. associate-/r/99.5%

      \[\leadsto \color{blue}{\left(\frac{1}{\sqrt{2}} \cdot \cos th\right)} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    3. pow1/299.5%

      \[\leadsto \left(\frac{1}{\color{blue}{{2}^{0.5}}} \cdot \cos th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    4. pow-flip99.6%

      \[\leadsto \left(\color{blue}{{2}^{\left(-0.5\right)}} \cdot \cos th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. metadata-eval99.6%

      \[\leadsto \left({2}^{\color{blue}{-0.5}} \cdot \cos th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
  5. Applied egg-rr99.6%

    \[\leadsto \color{blue}{\left({2}^{-0.5} \cdot \cos th\right)} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
  6. Taylor expanded in th around inf 99.6%

    \[\leadsto \color{blue}{\sqrt{0.5} \cdot \left(\left({a2}^{2} + {a1}^{2}\right) \cdot \cos th\right)} \]
  7. Step-by-step derivation
    1. unpow299.6%

      \[\leadsto \sqrt{0.5} \cdot \left(\left(\color{blue}{a2 \cdot a2} + {a1}^{2}\right) \cdot \cos th\right) \]
    2. unpow299.6%

      \[\leadsto \sqrt{0.5} \cdot \left(\left(a2 \cdot a2 + \color{blue}{a1 \cdot a1}\right) \cdot \cos th\right) \]
  8. Simplified99.6%

    \[\leadsto \color{blue}{\sqrt{0.5} \cdot \left(\left(a2 \cdot a2 + a1 \cdot a1\right) \cdot \cos th\right)} \]
  9. Final simplification99.6%

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

Alternative 5: 87.6% accurate, 2.0× speedup?

\[\begin{array}{l} [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ \begin{array}{l} \mathbf{if}\;a2 \leq 3.3 \cdot 10^{-127}:\\ \;\;\;\;\cos th \cdot \frac{a1}{\frac{\sqrt{2}}{a1}}\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}\\ \end{array} \end{array} \]
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th)
 :precision binary64
 (if (<= a2 3.3e-127)
   (* (cos th) (/ a1 (/ (sqrt 2.0) a1)))
   (* a2 (/ a2 (/ (sqrt 2.0) (cos th))))))
assert(a1 < a2);
double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 3.3e-127) {
		tmp = cos(th) * (a1 / (sqrt(2.0) / a1));
	} else {
		tmp = a2 * (a2 / (sqrt(2.0) / cos(th)));
	}
	return tmp;
}
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    real(8) :: tmp
    if (a2 <= 3.3d-127) then
        tmp = cos(th) * (a1 / (sqrt(2.0d0) / a1))
    else
        tmp = a2 * (a2 / (sqrt(2.0d0) / cos(th)))
    end if
    code = tmp
end function
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 3.3e-127) {
		tmp = Math.cos(th) * (a1 / (Math.sqrt(2.0) / a1));
	} else {
		tmp = a2 * (a2 / (Math.sqrt(2.0) / Math.cos(th)));
	}
	return tmp;
}
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	tmp = 0
	if a2 <= 3.3e-127:
		tmp = math.cos(th) * (a1 / (math.sqrt(2.0) / a1))
	else:
		tmp = a2 * (a2 / (math.sqrt(2.0) / math.cos(th)))
	return tmp
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	tmp = 0.0
	if (a2 <= 3.3e-127)
		tmp = Float64(cos(th) * Float64(a1 / Float64(sqrt(2.0) / a1)));
	else
		tmp = Float64(a2 * Float64(a2 / Float64(sqrt(2.0) / cos(th))));
	end
	return tmp
end
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp_2 = code(a1, a2, th)
	tmp = 0.0;
	if (a2 <= 3.3e-127)
		tmp = cos(th) * (a1 / (sqrt(2.0) / a1));
	else
		tmp = a2 * (a2 / (sqrt(2.0) / cos(th)));
	end
	tmp_2 = tmp;
end
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := If[LessEqual[a2, 3.3e-127], N[(N[Cos[th], $MachinePrecision] * N[(a1 / N[(N[Sqrt[2.0], $MachinePrecision] / a1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(a2 * N[(a2 / N[(N[Sqrt[2.0], $MachinePrecision] / N[Cos[th], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
\begin{array}{l}
\mathbf{if}\;a2 \leq 3.3 \cdot 10^{-127}:\\
\;\;\;\;\cos th \cdot \frac{a1}{\frac{\sqrt{2}}{a1}}\\

\mathbf{else}:\\
\;\;\;\;a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if a2 < 3.29999999999999981e-127

    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. Step-by-step derivation
      1. distribute-lft-out99.6%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
      2. associate-*l/99.7%

        \[\leadsto \color{blue}{\frac{\cos th \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
      3. associate-*r/99.7%

        \[\leadsto \color{blue}{\cos th \cdot \frac{a1 \cdot a1 + a2 \cdot a2}{\sqrt{2}}} \]
      4. fma-def99.7%

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

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

      \[\leadsto \cos th \cdot \color{blue}{\frac{{a1}^{2}}{\sqrt{2}}} \]
    5. Step-by-step derivation
      1. unpow264.6%

        \[\leadsto \cos th \cdot \frac{\color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
      2. associate-/l*64.6%

        \[\leadsto \cos th \cdot \color{blue}{\frac{a1}{\frac{\sqrt{2}}{a1}}} \]
    6. Simplified64.6%

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

    if 3.29999999999999981e-127 < a2

    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. Step-by-step derivation
      1. distribute-lft-out99.6%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
      2. associate-*l/99.6%

        \[\leadsto \color{blue}{\frac{\cos th \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
      3. associate-*r/99.6%

        \[\leadsto \color{blue}{\cos th \cdot \frac{a1 \cdot a1 + a2 \cdot a2}{\sqrt{2}}} \]
      4. fma-def99.6%

        \[\leadsto \cos th \cdot \frac{\color{blue}{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}}{\sqrt{2}} \]
    3. Simplified99.6%

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

      \[\leadsto \color{blue}{\frac{{a2}^{2} \cdot \cos th}{\sqrt{2}}} \]
    5. Step-by-step derivation
      1. unpow275.9%

        \[\leadsto \frac{\color{blue}{\left(a2 \cdot a2\right)} \cdot \cos th}{\sqrt{2}} \]
      2. associate-*l*75.9%

        \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
      3. associate-*r/75.8%

        \[\leadsto \color{blue}{a2 \cdot \frac{a2 \cdot \cos th}{\sqrt{2}}} \]
      4. associate-/l*75.9%

        \[\leadsto a2 \cdot \color{blue}{\frac{a2}{\frac{\sqrt{2}}{\cos th}}} \]
    6. Simplified75.9%

      \[\leadsto \color{blue}{a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification68.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;a2 \leq 3.3 \cdot 10^{-127}:\\ \;\;\;\;\cos th \cdot \frac{a1}{\frac{\sqrt{2}}{a1}}\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}\\ \end{array} \]

Alternative 6: 87.5% accurate, 2.0× speedup?

\[\begin{array}{l} [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ \begin{array}{l} \mathbf{if}\;a2 \leq 6 \cdot 10^{-130}:\\ \;\;\;\;\cos th \cdot \frac{a1 \cdot a1}{\sqrt{2}}\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}\\ \end{array} \end{array} \]
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th)
 :precision binary64
 (if (<= a2 6e-130)
   (* (cos th) (/ (* a1 a1) (sqrt 2.0)))
   (* a2 (/ a2 (/ (sqrt 2.0) (cos th))))))
assert(a1 < a2);
double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 6e-130) {
		tmp = cos(th) * ((a1 * a1) / sqrt(2.0));
	} else {
		tmp = a2 * (a2 / (sqrt(2.0) / cos(th)));
	}
	return tmp;
}
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    real(8) :: tmp
    if (a2 <= 6d-130) then
        tmp = cos(th) * ((a1 * a1) / sqrt(2.0d0))
    else
        tmp = a2 * (a2 / (sqrt(2.0d0) / cos(th)))
    end if
    code = tmp
end function
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 6e-130) {
		tmp = Math.cos(th) * ((a1 * a1) / Math.sqrt(2.0));
	} else {
		tmp = a2 * (a2 / (Math.sqrt(2.0) / Math.cos(th)));
	}
	return tmp;
}
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	tmp = 0
	if a2 <= 6e-130:
		tmp = math.cos(th) * ((a1 * a1) / math.sqrt(2.0))
	else:
		tmp = a2 * (a2 / (math.sqrt(2.0) / math.cos(th)))
	return tmp
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	tmp = 0.0
	if (a2 <= 6e-130)
		tmp = Float64(cos(th) * Float64(Float64(a1 * a1) / sqrt(2.0)));
	else
		tmp = Float64(a2 * Float64(a2 / Float64(sqrt(2.0) / cos(th))));
	end
	return tmp
end
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp_2 = code(a1, a2, th)
	tmp = 0.0;
	if (a2 <= 6e-130)
		tmp = cos(th) * ((a1 * a1) / sqrt(2.0));
	else
		tmp = a2 * (a2 / (sqrt(2.0) / cos(th)));
	end
	tmp_2 = tmp;
end
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := If[LessEqual[a2, 6e-130], N[(N[Cos[th], $MachinePrecision] * N[(N[(a1 * a1), $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(a2 * N[(a2 / N[(N[Sqrt[2.0], $MachinePrecision] / N[Cos[th], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
\begin{array}{l}
\mathbf{if}\;a2 \leq 6 \cdot 10^{-130}:\\
\;\;\;\;\cos th \cdot \frac{a1 \cdot a1}{\sqrt{2}}\\

\mathbf{else}:\\
\;\;\;\;a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if a2 < 5.99999999999999972e-130

    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. Step-by-step derivation
      1. distribute-lft-out99.6%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
      2. associate-*l/99.7%

        \[\leadsto \color{blue}{\frac{\cos th \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
      3. associate-*r/99.7%

        \[\leadsto \color{blue}{\cos th \cdot \frac{a1 \cdot a1 + a2 \cdot a2}{\sqrt{2}}} \]
      4. fma-def99.7%

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

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

      \[\leadsto \cos th \cdot \color{blue}{\frac{{a1}^{2}}{\sqrt{2}}} \]
    5. Step-by-step derivation
      1. unpow264.4%

        \[\leadsto \cos th \cdot \frac{\color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
    6. Simplified64.4%

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

    if 5.99999999999999972e-130 < a2

    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. Step-by-step derivation
      1. distribute-lft-out99.6%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
      2. associate-*l/99.6%

        \[\leadsto \color{blue}{\frac{\cos th \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
      3. associate-*r/99.6%

        \[\leadsto \color{blue}{\cos th \cdot \frac{a1 \cdot a1 + a2 \cdot a2}{\sqrt{2}}} \]
      4. fma-def99.6%

        \[\leadsto \cos th \cdot \frac{\color{blue}{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}}{\sqrt{2}} \]
    3. Simplified99.6%

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

      \[\leadsto \color{blue}{\frac{{a2}^{2} \cdot \cos th}{\sqrt{2}}} \]
    5. Step-by-step derivation
      1. unpow275.0%

        \[\leadsto \frac{\color{blue}{\left(a2 \cdot a2\right)} \cdot \cos th}{\sqrt{2}} \]
      2. associate-*l*75.0%

        \[\leadsto \frac{\color{blue}{a2 \cdot \left(a2 \cdot \cos th\right)}}{\sqrt{2}} \]
      3. associate-*r/75.0%

        \[\leadsto \color{blue}{a2 \cdot \frac{a2 \cdot \cos th}{\sqrt{2}}} \]
      4. associate-/l*75.0%

        \[\leadsto a2 \cdot \color{blue}{\frac{a2}{\frac{\sqrt{2}}{\cos th}}} \]
    6. Simplified75.0%

      \[\leadsto \color{blue}{a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification67.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;a2 \leq 6 \cdot 10^{-130}:\\ \;\;\;\;\cos th \cdot \frac{a1 \cdot a1}{\sqrt{2}}\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}\\ \end{array} \]

Alternative 7: 87.5% accurate, 2.0× speedup?

\[\begin{array}{l} [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ \begin{array}{l} \mathbf{if}\;a2 \leq 3.3 \cdot 10^{-127}:\\ \;\;\;\;\cos th \cdot \frac{a1 \cdot a1}{\sqrt{2}}\\ \mathbf{else}:\\ \;\;\;\;\cos th \cdot \frac{a2 \cdot a2}{\sqrt{2}}\\ \end{array} \end{array} \]
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th)
 :precision binary64
 (if (<= a2 3.3e-127)
   (* (cos th) (/ (* a1 a1) (sqrt 2.0)))
   (* (cos th) (/ (* a2 a2) (sqrt 2.0)))))
assert(a1 < a2);
double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 3.3e-127) {
		tmp = cos(th) * ((a1 * a1) / sqrt(2.0));
	} else {
		tmp = cos(th) * ((a2 * a2) / sqrt(2.0));
	}
	return tmp;
}
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    real(8) :: tmp
    if (a2 <= 3.3d-127) then
        tmp = cos(th) * ((a1 * a1) / sqrt(2.0d0))
    else
        tmp = cos(th) * ((a2 * a2) / sqrt(2.0d0))
    end if
    code = tmp
end function
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 3.3e-127) {
		tmp = Math.cos(th) * ((a1 * a1) / Math.sqrt(2.0));
	} else {
		tmp = Math.cos(th) * ((a2 * a2) / Math.sqrt(2.0));
	}
	return tmp;
}
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	tmp = 0
	if a2 <= 3.3e-127:
		tmp = math.cos(th) * ((a1 * a1) / math.sqrt(2.0))
	else:
		tmp = math.cos(th) * ((a2 * a2) / math.sqrt(2.0))
	return tmp
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	tmp = 0.0
	if (a2 <= 3.3e-127)
		tmp = Float64(cos(th) * Float64(Float64(a1 * a1) / sqrt(2.0)));
	else
		tmp = Float64(cos(th) * Float64(Float64(a2 * a2) / sqrt(2.0)));
	end
	return tmp
end
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp_2 = code(a1, a2, th)
	tmp = 0.0;
	if (a2 <= 3.3e-127)
		tmp = cos(th) * ((a1 * a1) / sqrt(2.0));
	else
		tmp = cos(th) * ((a2 * a2) / sqrt(2.0));
	end
	tmp_2 = tmp;
end
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := If[LessEqual[a2, 3.3e-127], N[(N[Cos[th], $MachinePrecision] * N[(N[(a1 * a1), $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Cos[th], $MachinePrecision] * N[(N[(a2 * a2), $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
\begin{array}{l}
\mathbf{if}\;a2 \leq 3.3 \cdot 10^{-127}:\\
\;\;\;\;\cos th \cdot \frac{a1 \cdot a1}{\sqrt{2}}\\

\mathbf{else}:\\
\;\;\;\;\cos th \cdot \frac{a2 \cdot a2}{\sqrt{2}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if a2 < 3.29999999999999981e-127

    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. Step-by-step derivation
      1. distribute-lft-out99.6%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
      2. associate-*l/99.7%

        \[\leadsto \color{blue}{\frac{\cos th \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
      3. associate-*r/99.7%

        \[\leadsto \color{blue}{\cos th \cdot \frac{a1 \cdot a1 + a2 \cdot a2}{\sqrt{2}}} \]
      4. fma-def99.7%

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

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

      \[\leadsto \cos th \cdot \color{blue}{\frac{{a1}^{2}}{\sqrt{2}}} \]
    5. Step-by-step derivation
      1. unpow264.6%

        \[\leadsto \cos th \cdot \frac{\color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
    6. Simplified64.6%

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

    if 3.29999999999999981e-127 < a2

    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. Step-by-step derivation
      1. distribute-lft-out99.6%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
      2. associate-*l/99.6%

        \[\leadsto \color{blue}{\frac{\cos th \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
      3. associate-*r/99.6%

        \[\leadsto \color{blue}{\cos th \cdot \frac{a1 \cdot a1 + a2 \cdot a2}{\sqrt{2}}} \]
      4. fma-def99.6%

        \[\leadsto \cos th \cdot \frac{\color{blue}{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}}{\sqrt{2}} \]
    3. Simplified99.6%

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

      \[\leadsto \cos th \cdot \color{blue}{\frac{{a2}^{2}}{\sqrt{2}}} \]
    5. Step-by-step derivation
      1. unpow275.9%

        \[\leadsto \cos th \cdot \frac{\color{blue}{a2 \cdot a2}}{\sqrt{2}} \]
    6. Simplified75.9%

      \[\leadsto \cos th \cdot \color{blue}{\frac{a2 \cdot a2}{\sqrt{2}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification68.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;a2 \leq 3.3 \cdot 10^{-127}:\\ \;\;\;\;\cos th \cdot \frac{a1 \cdot a1}{\sqrt{2}}\\ \mathbf{else}:\\ \;\;\;\;\cos th \cdot \frac{a2 \cdot a2}{\sqrt{2}}\\ \end{array} \]

Alternative 8: 87.6% accurate, 2.0× speedup?

\[\begin{array}{l} [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ \begin{array}{l} \mathbf{if}\;a2 \leq 3.3 \cdot 10^{-127}:\\ \;\;\;\;\cos th \cdot \frac{a1 \cdot a1}{\sqrt{2}}\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(a2 \cdot \left(\cos th \cdot \sqrt{0.5}\right)\right)\\ \end{array} \end{array} \]
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th)
 :precision binary64
 (if (<= a2 3.3e-127)
   (* (cos th) (/ (* a1 a1) (sqrt 2.0)))
   (* a2 (* a2 (* (cos th) (sqrt 0.5))))))
assert(a1 < a2);
double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 3.3e-127) {
		tmp = cos(th) * ((a1 * a1) / sqrt(2.0));
	} else {
		tmp = a2 * (a2 * (cos(th) * sqrt(0.5)));
	}
	return tmp;
}
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    real(8) :: tmp
    if (a2 <= 3.3d-127) then
        tmp = cos(th) * ((a1 * a1) / sqrt(2.0d0))
    else
        tmp = a2 * (a2 * (cos(th) * sqrt(0.5d0)))
    end if
    code = tmp
end function
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 3.3e-127) {
		tmp = Math.cos(th) * ((a1 * a1) / Math.sqrt(2.0));
	} else {
		tmp = a2 * (a2 * (Math.cos(th) * Math.sqrt(0.5)));
	}
	return tmp;
}
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	tmp = 0
	if a2 <= 3.3e-127:
		tmp = math.cos(th) * ((a1 * a1) / math.sqrt(2.0))
	else:
		tmp = a2 * (a2 * (math.cos(th) * math.sqrt(0.5)))
	return tmp
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	tmp = 0.0
	if (a2 <= 3.3e-127)
		tmp = Float64(cos(th) * Float64(Float64(a1 * a1) / sqrt(2.0)));
	else
		tmp = Float64(a2 * Float64(a2 * Float64(cos(th) * sqrt(0.5))));
	end
	return tmp
end
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp_2 = code(a1, a2, th)
	tmp = 0.0;
	if (a2 <= 3.3e-127)
		tmp = cos(th) * ((a1 * a1) / sqrt(2.0));
	else
		tmp = a2 * (a2 * (cos(th) * sqrt(0.5)));
	end
	tmp_2 = tmp;
end
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := If[LessEqual[a2, 3.3e-127], N[(N[Cos[th], $MachinePrecision] * N[(N[(a1 * a1), $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(a2 * N[(a2 * N[(N[Cos[th], $MachinePrecision] * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
\begin{array}{l}
\mathbf{if}\;a2 \leq 3.3 \cdot 10^{-127}:\\
\;\;\;\;\cos th \cdot \frac{a1 \cdot a1}{\sqrt{2}}\\

\mathbf{else}:\\
\;\;\;\;a2 \cdot \left(a2 \cdot \left(\cos th \cdot \sqrt{0.5}\right)\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if a2 < 3.29999999999999981e-127

    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. Step-by-step derivation
      1. distribute-lft-out99.6%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
      2. associate-*l/99.7%

        \[\leadsto \color{blue}{\frac{\cos th \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
      3. associate-*r/99.7%

        \[\leadsto \color{blue}{\cos th \cdot \frac{a1 \cdot a1 + a2 \cdot a2}{\sqrt{2}}} \]
      4. fma-def99.7%

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

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

      \[\leadsto \cos th \cdot \color{blue}{\frac{{a1}^{2}}{\sqrt{2}}} \]
    5. Step-by-step derivation
      1. unpow264.6%

        \[\leadsto \cos th \cdot \frac{\color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
    6. Simplified64.6%

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

    if 3.29999999999999981e-127 < a2

    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. Step-by-step derivation
      1. distribute-lft-out99.6%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
      2. associate-*l/99.6%

        \[\leadsto \color{blue}{\frac{\cos th \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
      3. associate-*r/99.6%

        \[\leadsto \color{blue}{\cos th \cdot \frac{a1 \cdot a1 + a2 \cdot a2}{\sqrt{2}}} \]
      4. fma-def99.6%

        \[\leadsto \cos th \cdot \frac{\color{blue}{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}}{\sqrt{2}} \]
    3. Simplified99.6%

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

      \[\leadsto \color{blue}{\frac{{a2}^{2} \cdot \cos th}{\sqrt{2}}} \]
    5. Step-by-step derivation
      1. unpow275.9%

        \[\leadsto \frac{\color{blue}{\left(a2 \cdot a2\right)} \cdot \cos th}{\sqrt{2}} \]
      2. *-commutative75.9%

        \[\leadsto \frac{\color{blue}{\cos th \cdot \left(a2 \cdot a2\right)}}{\sqrt{2}} \]
      3. associate-/l*75.9%

        \[\leadsto \color{blue}{\frac{\cos th}{\frac{\sqrt{2}}{a2 \cdot a2}}} \]
    6. Simplified75.9%

      \[\leadsto \color{blue}{\frac{\cos th}{\frac{\sqrt{2}}{a2 \cdot a2}}} \]
    7. Step-by-step derivation
      1. associate-/r/75.9%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right)} \]
      2. div-inv75.8%

        \[\leadsto \color{blue}{\left(\cos th \cdot \frac{1}{\sqrt{2}}\right)} \cdot \left(a2 \cdot a2\right) \]
      3. *-commutative75.8%

        \[\leadsto \color{blue}{\left(\frac{1}{\sqrt{2}} \cdot \cos th\right)} \cdot \left(a2 \cdot a2\right) \]
      4. pow1/275.8%

        \[\leadsto \left(\frac{1}{\color{blue}{{2}^{0.5}}} \cdot \cos th\right) \cdot \left(a2 \cdot a2\right) \]
      5. pow-flip75.9%

        \[\leadsto \left(\color{blue}{{2}^{\left(-0.5\right)}} \cdot \cos th\right) \cdot \left(a2 \cdot a2\right) \]
      6. metadata-eval75.9%

        \[\leadsto \left({2}^{\color{blue}{-0.5}} \cdot \cos th\right) \cdot \left(a2 \cdot a2\right) \]
      7. associate-*r*75.9%

        \[\leadsto \color{blue}{\left(\left({2}^{-0.5} \cdot \cos th\right) \cdot a2\right) \cdot a2} \]
      8. *-commutative75.9%

        \[\leadsto \left(\color{blue}{\left(\cos th \cdot {2}^{-0.5}\right)} \cdot a2\right) \cdot a2 \]
      9. metadata-eval75.9%

        \[\leadsto \left(\left(\cos th \cdot {2}^{\color{blue}{\left(-0.5\right)}}\right) \cdot a2\right) \cdot a2 \]
      10. pow-flip75.8%

        \[\leadsto \left(\left(\cos th \cdot \color{blue}{\frac{1}{{2}^{0.5}}}\right) \cdot a2\right) \cdot a2 \]
      11. pow1/275.8%

        \[\leadsto \left(\left(\cos th \cdot \frac{1}{\color{blue}{\sqrt{2}}}\right) \cdot a2\right) \cdot a2 \]
      12. add-sqr-sqrt75.8%

        \[\leadsto \left(\left(\cos th \cdot \color{blue}{\left(\sqrt{\frac{1}{\sqrt{2}}} \cdot \sqrt{\frac{1}{\sqrt{2}}}\right)}\right) \cdot a2\right) \cdot a2 \]
      13. sqrt-unprod75.8%

        \[\leadsto \left(\left(\cos th \cdot \color{blue}{\sqrt{\frac{1}{\sqrt{2}} \cdot \frac{1}{\sqrt{2}}}}\right) \cdot a2\right) \cdot a2 \]
      14. frac-times75.8%

        \[\leadsto \left(\left(\cos th \cdot \sqrt{\color{blue}{\frac{1 \cdot 1}{\sqrt{2} \cdot \sqrt{2}}}}\right) \cdot a2\right) \cdot a2 \]
      15. metadata-eval75.8%

        \[\leadsto \left(\left(\cos th \cdot \sqrt{\frac{\color{blue}{1}}{\sqrt{2} \cdot \sqrt{2}}}\right) \cdot a2\right) \cdot a2 \]
      16. add-sqr-sqrt75.9%

        \[\leadsto \left(\left(\cos th \cdot \sqrt{\frac{1}{\color{blue}{2}}}\right) \cdot a2\right) \cdot a2 \]
      17. metadata-eval75.9%

        \[\leadsto \left(\left(\cos th \cdot \sqrt{\color{blue}{0.5}}\right) \cdot a2\right) \cdot a2 \]
    8. Applied egg-rr75.9%

      \[\leadsto \color{blue}{\left(\left(\cos th \cdot \sqrt{0.5}\right) \cdot a2\right) \cdot a2} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification68.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;a2 \leq 3.3 \cdot 10^{-127}:\\ \;\;\;\;\cos th \cdot \frac{a1 \cdot a1}{\sqrt{2}}\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(a2 \cdot \left(\cos th \cdot \sqrt{0.5}\right)\right)\\ \end{array} \]

Alternative 9: 58.8% accurate, 3.8× speedup?

\[\begin{array}{l} [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ \begin{array}{l} \mathbf{if}\;a2 \leq 8.8 \cdot 10^{-91}:\\ \;\;\;\;a1 \cdot \left(a1 \cdot \sqrt{0.5}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{\sqrt{2}}{a2 \cdot a2}}\\ \end{array} \end{array} \]
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th)
 :precision binary64
 (if (<= a2 8.8e-91)
   (* a1 (* a1 (sqrt 0.5)))
   (/ 1.0 (/ (sqrt 2.0) (* a2 a2)))))
assert(a1 < a2);
double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 8.8e-91) {
		tmp = a1 * (a1 * sqrt(0.5));
	} else {
		tmp = 1.0 / (sqrt(2.0) / (a2 * a2));
	}
	return tmp;
}
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    real(8) :: tmp
    if (a2 <= 8.8d-91) then
        tmp = a1 * (a1 * sqrt(0.5d0))
    else
        tmp = 1.0d0 / (sqrt(2.0d0) / (a2 * a2))
    end if
    code = tmp
end function
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 8.8e-91) {
		tmp = a1 * (a1 * Math.sqrt(0.5));
	} else {
		tmp = 1.0 / (Math.sqrt(2.0) / (a2 * a2));
	}
	return tmp;
}
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	tmp = 0
	if a2 <= 8.8e-91:
		tmp = a1 * (a1 * math.sqrt(0.5))
	else:
		tmp = 1.0 / (math.sqrt(2.0) / (a2 * a2))
	return tmp
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	tmp = 0.0
	if (a2 <= 8.8e-91)
		tmp = Float64(a1 * Float64(a1 * sqrt(0.5)));
	else
		tmp = Float64(1.0 / Float64(sqrt(2.0) / Float64(a2 * a2)));
	end
	return tmp
end
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp_2 = code(a1, a2, th)
	tmp = 0.0;
	if (a2 <= 8.8e-91)
		tmp = a1 * (a1 * sqrt(0.5));
	else
		tmp = 1.0 / (sqrt(2.0) / (a2 * a2));
	end
	tmp_2 = tmp;
end
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := If[LessEqual[a2, 8.8e-91], N[(a1 * N[(a1 * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(N[Sqrt[2.0], $MachinePrecision] / N[(a2 * a2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
\begin{array}{l}
\mathbf{if}\;a2 \leq 8.8 \cdot 10^{-91}:\\
\;\;\;\;a1 \cdot \left(a1 \cdot \sqrt{0.5}\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{\sqrt{2}}{a2 \cdot a2}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if a2 < 8.8000000000000003e-91

    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. Step-by-step derivation
      1. distribute-lft-out99.6%

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

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
    4. Taylor expanded in th around 0 66.8%

      \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. Taylor expanded in a1 around inf 45.3%

      \[\leadsto \color{blue}{\frac{{a1}^{2}}{\sqrt{2}}} \]
    6. Step-by-step derivation
      1. unpow245.3%

        \[\leadsto \frac{\color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
      2. associate-*r/45.2%

        \[\leadsto \color{blue}{a1 \cdot \frac{a1}{\sqrt{2}}} \]
    7. Simplified45.2%

      \[\leadsto \color{blue}{a1 \cdot \frac{a1}{\sqrt{2}}} \]
    8. Step-by-step derivation
      1. clear-num45.2%

        \[\leadsto a1 \cdot \color{blue}{\frac{1}{\frac{\sqrt{2}}{a1}}} \]
      2. associate-/r/45.2%

        \[\leadsto a1 \cdot \color{blue}{\left(\frac{1}{\sqrt{2}} \cdot a1\right)} \]
      3. add-sqr-sqrt45.2%

        \[\leadsto a1 \cdot \left(\color{blue}{\left(\sqrt{\frac{1}{\sqrt{2}}} \cdot \sqrt{\frac{1}{\sqrt{2}}}\right)} \cdot a1\right) \]
      4. sqrt-unprod45.2%

        \[\leadsto a1 \cdot \left(\color{blue}{\sqrt{\frac{1}{\sqrt{2}} \cdot \frac{1}{\sqrt{2}}}} \cdot a1\right) \]
      5. frac-times45.2%

        \[\leadsto a1 \cdot \left(\sqrt{\color{blue}{\frac{1 \cdot 1}{\sqrt{2} \cdot \sqrt{2}}}} \cdot a1\right) \]
      6. metadata-eval45.2%

        \[\leadsto a1 \cdot \left(\sqrt{\frac{\color{blue}{1}}{\sqrt{2} \cdot \sqrt{2}}} \cdot a1\right) \]
      7. add-sqr-sqrt45.3%

        \[\leadsto a1 \cdot \left(\sqrt{\frac{1}{\color{blue}{2}}} \cdot a1\right) \]
      8. metadata-eval45.3%

        \[\leadsto a1 \cdot \left(\sqrt{\color{blue}{0.5}} \cdot a1\right) \]
    9. Applied egg-rr45.3%

      \[\leadsto a1 \cdot \color{blue}{\left(\sqrt{0.5} \cdot a1\right)} \]

    if 8.8000000000000003e-91 < a2

    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. Step-by-step derivation
      1. distribute-lft-out99.6%

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

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
    4. Taylor expanded in th around 0 61.6%

      \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. Taylor expanded in a1 around 0 47.4%

      \[\leadsto \frac{1}{\sqrt{2}} \cdot \color{blue}{{a2}^{2}} \]
    6. Step-by-step derivation
      1. unpow247.4%

        \[\leadsto \frac{1}{\sqrt{2}} \cdot \color{blue}{\left(a2 \cdot a2\right)} \]
    7. Simplified47.4%

      \[\leadsto \frac{1}{\sqrt{2}} \cdot \color{blue}{\left(a2 \cdot a2\right)} \]
    8. Step-by-step derivation
      1. associate-*l/47.5%

        \[\leadsto \color{blue}{\frac{1 \cdot \left(a2 \cdot a2\right)}{\sqrt{2}}} \]
      2. associate-/l*47.5%

        \[\leadsto \color{blue}{\frac{1}{\frac{\sqrt{2}}{a2 \cdot a2}}} \]
    9. Applied egg-rr47.5%

      \[\leadsto \color{blue}{\frac{1}{\frac{\sqrt{2}}{a2 \cdot a2}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification45.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;a2 \leq 8.8 \cdot 10^{-91}:\\ \;\;\;\;a1 \cdot \left(a1 \cdot \sqrt{0.5}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{\sqrt{2}}{a2 \cdot a2}}\\ \end{array} \]

Alternative 10: 66.3% accurate, 3.8× speedup?

\[\begin{array}{l} [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ \sqrt{0.5} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right) \end{array} \]
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th) :precision binary64 (* (sqrt 0.5) (+ (* a2 a2) (* a1 a1))))
assert(a1 < a2);
double code(double a1, double a2, double th) {
	return sqrt(0.5) * ((a2 * a2) + (a1 * a1));
}
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    code = sqrt(0.5d0) * ((a2 * a2) + (a1 * a1))
end function
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	return Math.sqrt(0.5) * ((a2 * a2) + (a1 * a1));
}
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	return math.sqrt(0.5) * ((a2 * a2) + (a1 * a1))
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	return Float64(sqrt(0.5) * Float64(Float64(a2 * a2) + Float64(a1 * a1)))
end
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp = code(a1, a2, th)
	tmp = sqrt(0.5) * ((a2 * a2) + (a1 * a1));
end
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := N[(N[Sqrt[0.5], $MachinePrecision] * N[(N[(a2 * a2), $MachinePrecision] + N[(a1 * a1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
\sqrt{0.5} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)
\end{array}
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. Step-by-step derivation
    1. distribute-lft-out99.6%

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

    \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
  4. Step-by-step derivation
    1. clear-num99.6%

      \[\leadsto \color{blue}{\frac{1}{\frac{\sqrt{2}}{\cos th}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    2. associate-/r/99.5%

      \[\leadsto \color{blue}{\left(\frac{1}{\sqrt{2}} \cdot \cos th\right)} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    3. pow1/299.5%

      \[\leadsto \left(\frac{1}{\color{blue}{{2}^{0.5}}} \cdot \cos th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    4. pow-flip99.6%

      \[\leadsto \left(\color{blue}{{2}^{\left(-0.5\right)}} \cdot \cos th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. metadata-eval99.6%

      \[\leadsto \left({2}^{\color{blue}{-0.5}} \cdot \cos th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
  5. Applied egg-rr99.6%

    \[\leadsto \color{blue}{\left({2}^{-0.5} \cdot \cos th\right)} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
  6. Taylor expanded in th around 0 65.3%

    \[\leadsto \color{blue}{\sqrt{0.5} \cdot \left({a2}^{2} + {a1}^{2}\right)} \]
  7. Step-by-step derivation
    1. unpow265.3%

      \[\leadsto \sqrt{0.5} \cdot \left(\color{blue}{a2 \cdot a2} + {a1}^{2}\right) \]
    2. unpow265.3%

      \[\leadsto \sqrt{0.5} \cdot \left(a2 \cdot a2 + \color{blue}{a1 \cdot a1}\right) \]
  8. Simplified65.3%

    \[\leadsto \color{blue}{\sqrt{0.5} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right)} \]
  9. Final simplification65.3%

    \[\leadsto \sqrt{0.5} \cdot \left(a2 \cdot a2 + a1 \cdot a1\right) \]

Alternative 11: 58.8% accurate, 3.9× speedup?

\[\begin{array}{l} [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ \begin{array}{l} \mathbf{if}\;a2 \leq 8.5 \cdot 10^{-92}:\\ \;\;\;\;a1 \cdot \left(a1 \cdot \sqrt{0.5}\right)\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \frac{a2}{\sqrt{2}}\\ \end{array} \end{array} \]
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th)
 :precision binary64
 (if (<= a2 8.5e-92) (* a1 (* a1 (sqrt 0.5))) (* a2 (/ a2 (sqrt 2.0)))))
assert(a1 < a2);
double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 8.5e-92) {
		tmp = a1 * (a1 * sqrt(0.5));
	} else {
		tmp = a2 * (a2 / sqrt(2.0));
	}
	return tmp;
}
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    real(8) :: tmp
    if (a2 <= 8.5d-92) then
        tmp = a1 * (a1 * sqrt(0.5d0))
    else
        tmp = a2 * (a2 / sqrt(2.0d0))
    end if
    code = tmp
end function
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 8.5e-92) {
		tmp = a1 * (a1 * Math.sqrt(0.5));
	} else {
		tmp = a2 * (a2 / Math.sqrt(2.0));
	}
	return tmp;
}
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	tmp = 0
	if a2 <= 8.5e-92:
		tmp = a1 * (a1 * math.sqrt(0.5))
	else:
		tmp = a2 * (a2 / math.sqrt(2.0))
	return tmp
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	tmp = 0.0
	if (a2 <= 8.5e-92)
		tmp = Float64(a1 * Float64(a1 * sqrt(0.5)));
	else
		tmp = Float64(a2 * Float64(a2 / sqrt(2.0)));
	end
	return tmp
end
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp_2 = code(a1, a2, th)
	tmp = 0.0;
	if (a2 <= 8.5e-92)
		tmp = a1 * (a1 * sqrt(0.5));
	else
		tmp = a2 * (a2 / sqrt(2.0));
	end
	tmp_2 = tmp;
end
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := If[LessEqual[a2, 8.5e-92], N[(a1 * N[(a1 * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(a2 * N[(a2 / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
\begin{array}{l}
\mathbf{if}\;a2 \leq 8.5 \cdot 10^{-92}:\\
\;\;\;\;a1 \cdot \left(a1 \cdot \sqrt{0.5}\right)\\

\mathbf{else}:\\
\;\;\;\;a2 \cdot \frac{a2}{\sqrt{2}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if a2 < 8.50000000000000067e-92

    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. Step-by-step derivation
      1. distribute-lft-out99.6%

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

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
    4. Taylor expanded in th around 0 66.8%

      \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. Taylor expanded in a1 around inf 45.3%

      \[\leadsto \color{blue}{\frac{{a1}^{2}}{\sqrt{2}}} \]
    6. Step-by-step derivation
      1. unpow245.3%

        \[\leadsto \frac{\color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
      2. associate-*r/45.2%

        \[\leadsto \color{blue}{a1 \cdot \frac{a1}{\sqrt{2}}} \]
    7. Simplified45.2%

      \[\leadsto \color{blue}{a1 \cdot \frac{a1}{\sqrt{2}}} \]
    8. Step-by-step derivation
      1. clear-num45.2%

        \[\leadsto a1 \cdot \color{blue}{\frac{1}{\frac{\sqrt{2}}{a1}}} \]
      2. associate-/r/45.2%

        \[\leadsto a1 \cdot \color{blue}{\left(\frac{1}{\sqrt{2}} \cdot a1\right)} \]
      3. add-sqr-sqrt45.2%

        \[\leadsto a1 \cdot \left(\color{blue}{\left(\sqrt{\frac{1}{\sqrt{2}}} \cdot \sqrt{\frac{1}{\sqrt{2}}}\right)} \cdot a1\right) \]
      4. sqrt-unprod45.2%

        \[\leadsto a1 \cdot \left(\color{blue}{\sqrt{\frac{1}{\sqrt{2}} \cdot \frac{1}{\sqrt{2}}}} \cdot a1\right) \]
      5. frac-times45.2%

        \[\leadsto a1 \cdot \left(\sqrt{\color{blue}{\frac{1 \cdot 1}{\sqrt{2} \cdot \sqrt{2}}}} \cdot a1\right) \]
      6. metadata-eval45.2%

        \[\leadsto a1 \cdot \left(\sqrt{\frac{\color{blue}{1}}{\sqrt{2} \cdot \sqrt{2}}} \cdot a1\right) \]
      7. add-sqr-sqrt45.3%

        \[\leadsto a1 \cdot \left(\sqrt{\frac{1}{\color{blue}{2}}} \cdot a1\right) \]
      8. metadata-eval45.3%

        \[\leadsto a1 \cdot \left(\sqrt{\color{blue}{0.5}} \cdot a1\right) \]
    9. Applied egg-rr45.3%

      \[\leadsto a1 \cdot \color{blue}{\left(\sqrt{0.5} \cdot a1\right)} \]

    if 8.50000000000000067e-92 < a2

    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. Step-by-step derivation
      1. distribute-lft-out99.6%

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

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
    4. Taylor expanded in th around 0 61.6%

      \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. Taylor expanded in a1 around 0 47.4%

      \[\leadsto \frac{1}{\sqrt{2}} \cdot \color{blue}{{a2}^{2}} \]
    6. Step-by-step derivation
      1. unpow247.4%

        \[\leadsto \frac{1}{\sqrt{2}} \cdot \color{blue}{\left(a2 \cdot a2\right)} \]
    7. Simplified47.4%

      \[\leadsto \frac{1}{\sqrt{2}} \cdot \color{blue}{\left(a2 \cdot a2\right)} \]
    8. Taylor expanded in a2 around 0 47.5%

      \[\leadsto \color{blue}{\frac{{a2}^{2}}{\sqrt{2}}} \]
    9. Step-by-step derivation
      1. unpow247.5%

        \[\leadsto \frac{\color{blue}{a2 \cdot a2}}{\sqrt{2}} \]
      2. associate-*l/47.4%

        \[\leadsto \color{blue}{\frac{a2}{\sqrt{2}} \cdot a2} \]
      3. *-commutative47.4%

        \[\leadsto \color{blue}{a2 \cdot \frac{a2}{\sqrt{2}}} \]
    10. Simplified47.4%

      \[\leadsto \color{blue}{a2 \cdot \frac{a2}{\sqrt{2}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification45.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;a2 \leq 8.5 \cdot 10^{-92}:\\ \;\;\;\;a1 \cdot \left(a1 \cdot \sqrt{0.5}\right)\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \frac{a2}{\sqrt{2}}\\ \end{array} \]

Alternative 12: 58.9% accurate, 3.9× speedup?

\[\begin{array}{l} [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ \begin{array}{l} \mathbf{if}\;a2 \leq 1.1 \cdot 10^{-90}:\\ \;\;\;\;a1 \cdot \left(a1 \cdot \sqrt{0.5}\right)\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(a2 \cdot \sqrt{0.5}\right)\\ \end{array} \end{array} \]
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th)
 :precision binary64
 (if (<= a2 1.1e-90) (* a1 (* a1 (sqrt 0.5))) (* a2 (* a2 (sqrt 0.5)))))
assert(a1 < a2);
double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 1.1e-90) {
		tmp = a1 * (a1 * sqrt(0.5));
	} else {
		tmp = a2 * (a2 * sqrt(0.5));
	}
	return tmp;
}
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    real(8) :: tmp
    if (a2 <= 1.1d-90) then
        tmp = a1 * (a1 * sqrt(0.5d0))
    else
        tmp = a2 * (a2 * sqrt(0.5d0))
    end if
    code = tmp
end function
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 1.1e-90) {
		tmp = a1 * (a1 * Math.sqrt(0.5));
	} else {
		tmp = a2 * (a2 * Math.sqrt(0.5));
	}
	return tmp;
}
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	tmp = 0
	if a2 <= 1.1e-90:
		tmp = a1 * (a1 * math.sqrt(0.5))
	else:
		tmp = a2 * (a2 * math.sqrt(0.5))
	return tmp
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	tmp = 0.0
	if (a2 <= 1.1e-90)
		tmp = Float64(a1 * Float64(a1 * sqrt(0.5)));
	else
		tmp = Float64(a2 * Float64(a2 * sqrt(0.5)));
	end
	return tmp
end
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp_2 = code(a1, a2, th)
	tmp = 0.0;
	if (a2 <= 1.1e-90)
		tmp = a1 * (a1 * sqrt(0.5));
	else
		tmp = a2 * (a2 * sqrt(0.5));
	end
	tmp_2 = tmp;
end
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := If[LessEqual[a2, 1.1e-90], N[(a1 * N[(a1 * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(a2 * N[(a2 * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
\begin{array}{l}
\mathbf{if}\;a2 \leq 1.1 \cdot 10^{-90}:\\
\;\;\;\;a1 \cdot \left(a1 \cdot \sqrt{0.5}\right)\\

\mathbf{else}:\\
\;\;\;\;a2 \cdot \left(a2 \cdot \sqrt{0.5}\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if a2 < 1.09999999999999993e-90

    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. Step-by-step derivation
      1. distribute-lft-out99.6%

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

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
    4. Taylor expanded in th around 0 66.8%

      \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. Taylor expanded in a1 around inf 45.3%

      \[\leadsto \color{blue}{\frac{{a1}^{2}}{\sqrt{2}}} \]
    6. Step-by-step derivation
      1. unpow245.3%

        \[\leadsto \frac{\color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
      2. associate-*r/45.2%

        \[\leadsto \color{blue}{a1 \cdot \frac{a1}{\sqrt{2}}} \]
    7. Simplified45.2%

      \[\leadsto \color{blue}{a1 \cdot \frac{a1}{\sqrt{2}}} \]
    8. Step-by-step derivation
      1. clear-num45.2%

        \[\leadsto a1 \cdot \color{blue}{\frac{1}{\frac{\sqrt{2}}{a1}}} \]
      2. associate-/r/45.2%

        \[\leadsto a1 \cdot \color{blue}{\left(\frac{1}{\sqrt{2}} \cdot a1\right)} \]
      3. add-sqr-sqrt45.2%

        \[\leadsto a1 \cdot \left(\color{blue}{\left(\sqrt{\frac{1}{\sqrt{2}}} \cdot \sqrt{\frac{1}{\sqrt{2}}}\right)} \cdot a1\right) \]
      4. sqrt-unprod45.2%

        \[\leadsto a1 \cdot \left(\color{blue}{\sqrt{\frac{1}{\sqrt{2}} \cdot \frac{1}{\sqrt{2}}}} \cdot a1\right) \]
      5. frac-times45.2%

        \[\leadsto a1 \cdot \left(\sqrt{\color{blue}{\frac{1 \cdot 1}{\sqrt{2} \cdot \sqrt{2}}}} \cdot a1\right) \]
      6. metadata-eval45.2%

        \[\leadsto a1 \cdot \left(\sqrt{\frac{\color{blue}{1}}{\sqrt{2} \cdot \sqrt{2}}} \cdot a1\right) \]
      7. add-sqr-sqrt45.3%

        \[\leadsto a1 \cdot \left(\sqrt{\frac{1}{\color{blue}{2}}} \cdot a1\right) \]
      8. metadata-eval45.3%

        \[\leadsto a1 \cdot \left(\sqrt{\color{blue}{0.5}} \cdot a1\right) \]
    9. Applied egg-rr45.3%

      \[\leadsto a1 \cdot \color{blue}{\left(\sqrt{0.5} \cdot a1\right)} \]

    if 1.09999999999999993e-90 < a2

    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. Step-by-step derivation
      1. distribute-lft-out99.6%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
      2. associate-*l/99.6%

        \[\leadsto \color{blue}{\frac{\cos th \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
      3. associate-*r/99.7%

        \[\leadsto \color{blue}{\cos th \cdot \frac{a1 \cdot a1 + a2 \cdot a2}{\sqrt{2}}} \]
      4. fma-def99.7%

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

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

      \[\leadsto \color{blue}{\frac{{a2}^{2} \cdot \cos th}{\sqrt{2}}} \]
    5. Step-by-step derivation
      1. unpow278.2%

        \[\leadsto \frac{\color{blue}{\left(a2 \cdot a2\right)} \cdot \cos th}{\sqrt{2}} \]
      2. *-commutative78.2%

        \[\leadsto \frac{\color{blue}{\cos th \cdot \left(a2 \cdot a2\right)}}{\sqrt{2}} \]
      3. associate-/l*78.2%

        \[\leadsto \color{blue}{\frac{\cos th}{\frac{\sqrt{2}}{a2 \cdot a2}}} \]
    6. Simplified78.2%

      \[\leadsto \color{blue}{\frac{\cos th}{\frac{\sqrt{2}}{a2 \cdot a2}}} \]
    7. Step-by-step derivation
      1. associate-/r/78.1%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a2 \cdot a2\right)} \]
      2. div-inv78.1%

        \[\leadsto \color{blue}{\left(\cos th \cdot \frac{1}{\sqrt{2}}\right)} \cdot \left(a2 \cdot a2\right) \]
      3. *-commutative78.1%

        \[\leadsto \color{blue}{\left(\frac{1}{\sqrt{2}} \cdot \cos th\right)} \cdot \left(a2 \cdot a2\right) \]
      4. pow1/278.1%

        \[\leadsto \left(\frac{1}{\color{blue}{{2}^{0.5}}} \cdot \cos th\right) \cdot \left(a2 \cdot a2\right) \]
      5. pow-flip78.1%

        \[\leadsto \left(\color{blue}{{2}^{\left(-0.5\right)}} \cdot \cos th\right) \cdot \left(a2 \cdot a2\right) \]
      6. metadata-eval78.1%

        \[\leadsto \left({2}^{\color{blue}{-0.5}} \cdot \cos th\right) \cdot \left(a2 \cdot a2\right) \]
      7. associate-*r*78.2%

        \[\leadsto \color{blue}{\left(\left({2}^{-0.5} \cdot \cos th\right) \cdot a2\right) \cdot a2} \]
      8. *-commutative78.2%

        \[\leadsto \left(\color{blue}{\left(\cos th \cdot {2}^{-0.5}\right)} \cdot a2\right) \cdot a2 \]
      9. metadata-eval78.2%

        \[\leadsto \left(\left(\cos th \cdot {2}^{\color{blue}{\left(-0.5\right)}}\right) \cdot a2\right) \cdot a2 \]
      10. pow-flip78.1%

        \[\leadsto \left(\left(\cos th \cdot \color{blue}{\frac{1}{{2}^{0.5}}}\right) \cdot a2\right) \cdot a2 \]
      11. pow1/278.1%

        \[\leadsto \left(\left(\cos th \cdot \frac{1}{\color{blue}{\sqrt{2}}}\right) \cdot a2\right) \cdot a2 \]
      12. add-sqr-sqrt78.1%

        \[\leadsto \left(\left(\cos th \cdot \color{blue}{\left(\sqrt{\frac{1}{\sqrt{2}}} \cdot \sqrt{\frac{1}{\sqrt{2}}}\right)}\right) \cdot a2\right) \cdot a2 \]
      13. sqrt-unprod78.1%

        \[\leadsto \left(\left(\cos th \cdot \color{blue}{\sqrt{\frac{1}{\sqrt{2}} \cdot \frac{1}{\sqrt{2}}}}\right) \cdot a2\right) \cdot a2 \]
      14. frac-times78.1%

        \[\leadsto \left(\left(\cos th \cdot \sqrt{\color{blue}{\frac{1 \cdot 1}{\sqrt{2} \cdot \sqrt{2}}}}\right) \cdot a2\right) \cdot a2 \]
      15. metadata-eval78.1%

        \[\leadsto \left(\left(\cos th \cdot \sqrt{\frac{\color{blue}{1}}{\sqrt{2} \cdot \sqrt{2}}}\right) \cdot a2\right) \cdot a2 \]
      16. add-sqr-sqrt78.2%

        \[\leadsto \left(\left(\cos th \cdot \sqrt{\frac{1}{\color{blue}{2}}}\right) \cdot a2\right) \cdot a2 \]
      17. metadata-eval78.2%

        \[\leadsto \left(\left(\cos th \cdot \sqrt{\color{blue}{0.5}}\right) \cdot a2\right) \cdot a2 \]
    8. Applied egg-rr78.2%

      \[\leadsto \color{blue}{\left(\left(\cos th \cdot \sqrt{0.5}\right) \cdot a2\right) \cdot a2} \]
    9. Taylor expanded in th around 0 47.4%

      \[\leadsto \color{blue}{\left(\sqrt{0.5} \cdot a2\right)} \cdot a2 \]
  3. Recombined 2 regimes into one program.
  4. Final simplification45.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;a2 \leq 1.1 \cdot 10^{-90}:\\ \;\;\;\;a1 \cdot \left(a1 \cdot \sqrt{0.5}\right)\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(a2 \cdot \sqrt{0.5}\right)\\ \end{array} \]

Alternative 13: 58.9% accurate, 3.9× speedup?

\[\begin{array}{l} [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ \begin{array}{l} \mathbf{if}\;a2 \leq 1.7 \cdot 10^{-90}:\\ \;\;\;\;a1 \cdot \left(a1 \cdot \sqrt{0.5}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{a2}{\frac{\sqrt{2}}{a2}}\\ \end{array} \end{array} \]
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th)
 :precision binary64
 (if (<= a2 1.7e-90) (* a1 (* a1 (sqrt 0.5))) (/ a2 (/ (sqrt 2.0) a2))))
assert(a1 < a2);
double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 1.7e-90) {
		tmp = a1 * (a1 * sqrt(0.5));
	} else {
		tmp = a2 / (sqrt(2.0) / a2);
	}
	return tmp;
}
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    real(8) :: tmp
    if (a2 <= 1.7d-90) then
        tmp = a1 * (a1 * sqrt(0.5d0))
    else
        tmp = a2 / (sqrt(2.0d0) / a2)
    end if
    code = tmp
end function
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 1.7e-90) {
		tmp = a1 * (a1 * Math.sqrt(0.5));
	} else {
		tmp = a2 / (Math.sqrt(2.0) / a2);
	}
	return tmp;
}
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	tmp = 0
	if a2 <= 1.7e-90:
		tmp = a1 * (a1 * math.sqrt(0.5))
	else:
		tmp = a2 / (math.sqrt(2.0) / a2)
	return tmp
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	tmp = 0.0
	if (a2 <= 1.7e-90)
		tmp = Float64(a1 * Float64(a1 * sqrt(0.5)));
	else
		tmp = Float64(a2 / Float64(sqrt(2.0) / a2));
	end
	return tmp
end
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp_2 = code(a1, a2, th)
	tmp = 0.0;
	if (a2 <= 1.7e-90)
		tmp = a1 * (a1 * sqrt(0.5));
	else
		tmp = a2 / (sqrt(2.0) / a2);
	end
	tmp_2 = tmp;
end
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := If[LessEqual[a2, 1.7e-90], N[(a1 * N[(a1 * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(a2 / N[(N[Sqrt[2.0], $MachinePrecision] / a2), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
\begin{array}{l}
\mathbf{if}\;a2 \leq 1.7 \cdot 10^{-90}:\\
\;\;\;\;a1 \cdot \left(a1 \cdot \sqrt{0.5}\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{a2}{\frac{\sqrt{2}}{a2}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if a2 < 1.69999999999999997e-90

    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. Step-by-step derivation
      1. distribute-lft-out99.6%

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

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
    4. Taylor expanded in th around 0 66.8%

      \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. Taylor expanded in a1 around inf 45.3%

      \[\leadsto \color{blue}{\frac{{a1}^{2}}{\sqrt{2}}} \]
    6. Step-by-step derivation
      1. unpow245.3%

        \[\leadsto \frac{\color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
      2. associate-*r/45.2%

        \[\leadsto \color{blue}{a1 \cdot \frac{a1}{\sqrt{2}}} \]
    7. Simplified45.2%

      \[\leadsto \color{blue}{a1 \cdot \frac{a1}{\sqrt{2}}} \]
    8. Step-by-step derivation
      1. clear-num45.2%

        \[\leadsto a1 \cdot \color{blue}{\frac{1}{\frac{\sqrt{2}}{a1}}} \]
      2. associate-/r/45.2%

        \[\leadsto a1 \cdot \color{blue}{\left(\frac{1}{\sqrt{2}} \cdot a1\right)} \]
      3. add-sqr-sqrt45.2%

        \[\leadsto a1 \cdot \left(\color{blue}{\left(\sqrt{\frac{1}{\sqrt{2}}} \cdot \sqrt{\frac{1}{\sqrt{2}}}\right)} \cdot a1\right) \]
      4. sqrt-unprod45.2%

        \[\leadsto a1 \cdot \left(\color{blue}{\sqrt{\frac{1}{\sqrt{2}} \cdot \frac{1}{\sqrt{2}}}} \cdot a1\right) \]
      5. frac-times45.2%

        \[\leadsto a1 \cdot \left(\sqrt{\color{blue}{\frac{1 \cdot 1}{\sqrt{2} \cdot \sqrt{2}}}} \cdot a1\right) \]
      6. metadata-eval45.2%

        \[\leadsto a1 \cdot \left(\sqrt{\frac{\color{blue}{1}}{\sqrt{2} \cdot \sqrt{2}}} \cdot a1\right) \]
      7. add-sqr-sqrt45.3%

        \[\leadsto a1 \cdot \left(\sqrt{\frac{1}{\color{blue}{2}}} \cdot a1\right) \]
      8. metadata-eval45.3%

        \[\leadsto a1 \cdot \left(\sqrt{\color{blue}{0.5}} \cdot a1\right) \]
    9. Applied egg-rr45.3%

      \[\leadsto a1 \cdot \color{blue}{\left(\sqrt{0.5} \cdot a1\right)} \]

    if 1.69999999999999997e-90 < a2

    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. Step-by-step derivation
      1. distribute-lft-out99.6%

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

      \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
    4. Taylor expanded in th around 0 61.6%

      \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. Taylor expanded in a1 around 0 47.4%

      \[\leadsto \frac{1}{\sqrt{2}} \cdot \color{blue}{{a2}^{2}} \]
    6. Step-by-step derivation
      1. unpow247.4%

        \[\leadsto \frac{1}{\sqrt{2}} \cdot \color{blue}{\left(a2 \cdot a2\right)} \]
    7. Simplified47.4%

      \[\leadsto \frac{1}{\sqrt{2}} \cdot \color{blue}{\left(a2 \cdot a2\right)} \]
    8. Step-by-step derivation
      1. associate-*l/47.5%

        \[\leadsto \color{blue}{\frac{1 \cdot \left(a2 \cdot a2\right)}{\sqrt{2}}} \]
      2. *-un-lft-identity47.5%

        \[\leadsto \frac{\color{blue}{a2 \cdot a2}}{\sqrt{2}} \]
      3. associate-/l*47.5%

        \[\leadsto \color{blue}{\frac{a2}{\frac{\sqrt{2}}{a2}}} \]
    9. Applied egg-rr47.5%

      \[\leadsto \color{blue}{\frac{a2}{\frac{\sqrt{2}}{a2}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification45.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;a2 \leq 1.7 \cdot 10^{-90}:\\ \;\;\;\;a1 \cdot \left(a1 \cdot \sqrt{0.5}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{a2}{\frac{\sqrt{2}}{a2}}\\ \end{array} \]

Alternative 14: 40.1% accurate, 4.0× speedup?

\[\begin{array}{l} [a1, a2] = \mathsf{sort}([a1, a2])\\ \\ a1 \cdot \left(a1 \cdot \sqrt{0.5}\right) \end{array} \]
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
(FPCore (a1 a2 th) :precision binary64 (* a1 (* a1 (sqrt 0.5))))
assert(a1 < a2);
double code(double a1, double a2, double th) {
	return a1 * (a1 * sqrt(0.5));
}
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    code = a1 * (a1 * sqrt(0.5d0))
end function
assert a1 < a2;
public static double code(double a1, double a2, double th) {
	return a1 * (a1 * Math.sqrt(0.5));
}
[a1, a2] = sort([a1, a2])
def code(a1, a2, th):
	return a1 * (a1 * math.sqrt(0.5))
a1, a2 = sort([a1, a2])
function code(a1, a2, th)
	return Float64(a1 * Float64(a1 * sqrt(0.5)))
end
a1, a2 = num2cell(sort([a1, a2])){:}
function tmp = code(a1, a2, th)
	tmp = a1 * (a1 * sqrt(0.5));
end
NOTE: a1 and a2 should be sorted in increasing order before calling this function.
code[a1_, a2_, th_] := N[(a1 * N[(a1 * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[a1, a2] = \mathsf{sort}([a1, a2])\\
\\
a1 \cdot \left(a1 \cdot \sqrt{0.5}\right)
\end{array}
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. Step-by-step derivation
    1. distribute-lft-out99.6%

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

    \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
  4. Taylor expanded in th around 0 65.3%

    \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
  5. Taylor expanded in a1 around inf 39.0%

    \[\leadsto \color{blue}{\frac{{a1}^{2}}{\sqrt{2}}} \]
  6. Step-by-step derivation
    1. unpow239.0%

      \[\leadsto \frac{\color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
    2. associate-*r/39.0%

      \[\leadsto \color{blue}{a1 \cdot \frac{a1}{\sqrt{2}}} \]
  7. Simplified39.0%

    \[\leadsto \color{blue}{a1 \cdot \frac{a1}{\sqrt{2}}} \]
  8. Step-by-step derivation
    1. clear-num39.0%

      \[\leadsto a1 \cdot \color{blue}{\frac{1}{\frac{\sqrt{2}}{a1}}} \]
    2. associate-/r/39.0%

      \[\leadsto a1 \cdot \color{blue}{\left(\frac{1}{\sqrt{2}} \cdot a1\right)} \]
    3. add-sqr-sqrt39.0%

      \[\leadsto a1 \cdot \left(\color{blue}{\left(\sqrt{\frac{1}{\sqrt{2}}} \cdot \sqrt{\frac{1}{\sqrt{2}}}\right)} \cdot a1\right) \]
    4. sqrt-unprod39.0%

      \[\leadsto a1 \cdot \left(\color{blue}{\sqrt{\frac{1}{\sqrt{2}} \cdot \frac{1}{\sqrt{2}}}} \cdot a1\right) \]
    5. frac-times39.0%

      \[\leadsto a1 \cdot \left(\sqrt{\color{blue}{\frac{1 \cdot 1}{\sqrt{2} \cdot \sqrt{2}}}} \cdot a1\right) \]
    6. metadata-eval39.0%

      \[\leadsto a1 \cdot \left(\sqrt{\frac{\color{blue}{1}}{\sqrt{2} \cdot \sqrt{2}}} \cdot a1\right) \]
    7. add-sqr-sqrt39.0%

      \[\leadsto a1 \cdot \left(\sqrt{\frac{1}{\color{blue}{2}}} \cdot a1\right) \]
    8. metadata-eval39.0%

      \[\leadsto a1 \cdot \left(\sqrt{\color{blue}{0.5}} \cdot a1\right) \]
  9. Applied egg-rr39.0%

    \[\leadsto a1 \cdot \color{blue}{\left(\sqrt{0.5} \cdot a1\right)} \]
  10. Final simplification39.0%

    \[\leadsto a1 \cdot \left(a1 \cdot \sqrt{0.5}\right) \]

Reproduce

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