Migdal et al, Equation (64)

Percentage Accurate: 99.6% → 99.6%
Time: 12.3s
Alternatives: 20
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 20 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.6% 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, 2.0× speedup?

\[\begin{array}{l} \\ \left({2}^{-0.5} \cdot \cos th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (* (* (pow 2.0 -0.5) (cos th)) (+ (* a1 a1) (* a2 a2))))
double code(double a1, double a2, double th) {
	return (pow(2.0, -0.5) * cos(th)) * ((a1 * a1) + (a2 * a2));
}
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    code = ((2.0d0 ** (-0.5d0)) * cos(th)) * ((a1 * a1) + (a2 * a2))
end function
public static double code(double a1, double a2, double th) {
	return (Math.pow(2.0, -0.5) * Math.cos(th)) * ((a1 * a1) + (a2 * a2));
}
def code(a1, a2, th):
	return (math.pow(2.0, -0.5) * math.cos(th)) * ((a1 * a1) + (a2 * a2))
function code(a1, a2, th)
	return Float64(Float64((2.0 ^ -0.5) * cos(th)) * Float64(Float64(a1 * a1) + Float64(a2 * a2)))
end
function tmp = code(a1, a2, th)
	tmp = ((2.0 ^ -0.5) * cos(th)) * ((a1 * a1) + (a2 * a2));
end
code[a1_, a2_, th_] := N[(N[(N[Power[2.0, -0.5], $MachinePrecision] * N[Cos[th], $MachinePrecision]), $MachinePrecision] * N[(N[(a1 * a1), $MachinePrecision] + N[(a2 * a2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left({2}^{-0.5} \cdot \cos th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\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.6%

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

      \[\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({2}^{-0.5} \cdot \cos th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]

Alternative 2: 79.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := a1 \cdot a1 + a2 \cdot a2\\ \mathbf{if}\;\cos th \leq -0.4:\\ \;\;\;\;-0.5 \cdot t_1\\ \mathbf{elif}\;\cos th \leq 0.97:\\ \;\;\;\;a2 \cdot \left(a2 \cdot \left(\cos th \cdot \sqrt{0.5}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_1 \cdot \sqrt{0.5}\\ \end{array} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (let* ((t_1 (+ (* a1 a1) (* a2 a2))))
   (if (<= (cos th) -0.4)
     (* -0.5 t_1)
     (if (<= (cos th) 0.97)
       (* a2 (* a2 (* (cos th) (sqrt 0.5))))
       (* t_1 (sqrt 0.5))))))
double code(double a1, double a2, double th) {
	double t_1 = (a1 * a1) + (a2 * a2);
	double tmp;
	if (cos(th) <= -0.4) {
		tmp = -0.5 * t_1;
	} else if (cos(th) <= 0.97) {
		tmp = a2 * (a2 * (cos(th) * sqrt(0.5)));
	} else {
		tmp = t_1 * sqrt(0.5);
	}
	return tmp;
}
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
    real(8) :: tmp
    t_1 = (a1 * a1) + (a2 * a2)
    if (cos(th) <= (-0.4d0)) then
        tmp = (-0.5d0) * t_1
    else if (cos(th) <= 0.97d0) then
        tmp = a2 * (a2 * (cos(th) * sqrt(0.5d0)))
    else
        tmp = t_1 * sqrt(0.5d0)
    end if
    code = tmp
end function
public static double code(double a1, double a2, double th) {
	double t_1 = (a1 * a1) + (a2 * a2);
	double tmp;
	if (Math.cos(th) <= -0.4) {
		tmp = -0.5 * t_1;
	} else if (Math.cos(th) <= 0.97) {
		tmp = a2 * (a2 * (Math.cos(th) * Math.sqrt(0.5)));
	} else {
		tmp = t_1 * Math.sqrt(0.5);
	}
	return tmp;
}
def code(a1, a2, th):
	t_1 = (a1 * a1) + (a2 * a2)
	tmp = 0
	if math.cos(th) <= -0.4:
		tmp = -0.5 * t_1
	elif math.cos(th) <= 0.97:
		tmp = a2 * (a2 * (math.cos(th) * math.sqrt(0.5)))
	else:
		tmp = t_1 * math.sqrt(0.5)
	return tmp
function code(a1, a2, th)
	t_1 = Float64(Float64(a1 * a1) + Float64(a2 * a2))
	tmp = 0.0
	if (cos(th) <= -0.4)
		tmp = Float64(-0.5 * t_1);
	elseif (cos(th) <= 0.97)
		tmp = Float64(a2 * Float64(a2 * Float64(cos(th) * sqrt(0.5))));
	else
		tmp = Float64(t_1 * sqrt(0.5));
	end
	return tmp
end
function tmp_2 = code(a1, a2, th)
	t_1 = (a1 * a1) + (a2 * a2);
	tmp = 0.0;
	if (cos(th) <= -0.4)
		tmp = -0.5 * t_1;
	elseif (cos(th) <= 0.97)
		tmp = a2 * (a2 * (cos(th) * sqrt(0.5)));
	else
		tmp = t_1 * sqrt(0.5);
	end
	tmp_2 = tmp;
end
code[a1_, a2_, th_] := Block[{t$95$1 = N[(N[(a1 * a1), $MachinePrecision] + N[(a2 * a2), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[Cos[th], $MachinePrecision], -0.4], N[(-0.5 * t$95$1), $MachinePrecision], If[LessEqual[N[Cos[th], $MachinePrecision], 0.97], N[(a2 * N[(a2 * N[(N[Cos[th], $MachinePrecision] * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(t$95$1 * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := a1 \cdot a1 + a2 \cdot a2\\
\mathbf{if}\;\cos th \leq -0.4:\\
\;\;\;\;-0.5 \cdot t_1\\

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

\mathbf{else}:\\
\;\;\;\;t_1 \cdot \sqrt{0.5}\\


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

    1. Initial program 99.9%

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

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

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

      \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. Applied egg-rr75.7%

      \[\leadsto \color{blue}{-0.5} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]

    if -0.40000000000000002 < (cos.f64 th) < 0.96999999999999997

    1. Initial program 99.5%

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

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

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

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

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

      \[\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 54.3%

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

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

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

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

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

      \[\leadsto \color{blue}{a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}} \]
    7. Step-by-step derivation
      1. *-un-lft-identity54.3%

        \[\leadsto a2 \cdot \frac{\color{blue}{1 \cdot a2}}{\frac{\sqrt{2}}{\cos th}} \]
      2. div-inv54.2%

        \[\leadsto a2 \cdot \frac{1 \cdot a2}{\color{blue}{\sqrt{2} \cdot \frac{1}{\cos th}}} \]
      3. times-frac54.2%

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

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

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

        \[\leadsto a2 \cdot \left({2}^{\color{blue}{-0.5}} \cdot \frac{a2}{\frac{1}{\cos th}}\right) \]
      7. add-sqr-sqrt54.0%

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

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

        \[\leadsto a2 \cdot \left(\sqrt{\color{blue}{{2}^{\left(-0.5 + -0.5\right)}}} \cdot \frac{a2}{\frac{1}{\cos th}}\right) \]
      10. metadata-eval54.3%

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

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

      \[\leadsto a2 \cdot \color{blue}{\left(\sqrt{0.5} \cdot \frac{a2}{\frac{1}{\cos th}}\right)} \]
    9. Step-by-step derivation
      1. associate-/r/54.3%

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

        \[\leadsto a2 \cdot \left(\sqrt{0.5} \cdot \left(\color{blue}{a2} \cdot \cos th\right)\right) \]
      3. *-commutative54.3%

        \[\leadsto a2 \cdot \left(\sqrt{0.5} \cdot \color{blue}{\left(\cos th \cdot a2\right)}\right) \]
      4. associate-*l*54.3%

        \[\leadsto a2 \cdot \color{blue}{\left(\left(\sqrt{0.5} \cdot \cos th\right) \cdot a2\right)} \]
    10. Simplified54.3%

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

    if 0.96999999999999997 < (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.6%

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

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

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

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

      \[\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 94.2%

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

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

Alternative 3: 79.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := a1 \cdot a1 + a2 \cdot a2\\ \mathbf{if}\;\cos th \leq -0.4:\\ \;\;\;\;-0.5 \cdot t_1\\ \mathbf{elif}\;\cos th \leq 0.97:\\ \;\;\;\;\cos th \cdot \left(a2 \cdot \left(a2 \cdot \sqrt{0.5}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_1 \cdot \sqrt{0.5}\\ \end{array} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (let* ((t_1 (+ (* a1 a1) (* a2 a2))))
   (if (<= (cos th) -0.4)
     (* -0.5 t_1)
     (if (<= (cos th) 0.97)
       (* (cos th) (* a2 (* a2 (sqrt 0.5))))
       (* t_1 (sqrt 0.5))))))
double code(double a1, double a2, double th) {
	double t_1 = (a1 * a1) + (a2 * a2);
	double tmp;
	if (cos(th) <= -0.4) {
		tmp = -0.5 * t_1;
	} else if (cos(th) <= 0.97) {
		tmp = cos(th) * (a2 * (a2 * sqrt(0.5)));
	} else {
		tmp = t_1 * sqrt(0.5);
	}
	return tmp;
}
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
    real(8) :: tmp
    t_1 = (a1 * a1) + (a2 * a2)
    if (cos(th) <= (-0.4d0)) then
        tmp = (-0.5d0) * t_1
    else if (cos(th) <= 0.97d0) then
        tmp = cos(th) * (a2 * (a2 * sqrt(0.5d0)))
    else
        tmp = t_1 * sqrt(0.5d0)
    end if
    code = tmp
end function
public static double code(double a1, double a2, double th) {
	double t_1 = (a1 * a1) + (a2 * a2);
	double tmp;
	if (Math.cos(th) <= -0.4) {
		tmp = -0.5 * t_1;
	} else if (Math.cos(th) <= 0.97) {
		tmp = Math.cos(th) * (a2 * (a2 * Math.sqrt(0.5)));
	} else {
		tmp = t_1 * Math.sqrt(0.5);
	}
	return tmp;
}
def code(a1, a2, th):
	t_1 = (a1 * a1) + (a2 * a2)
	tmp = 0
	if math.cos(th) <= -0.4:
		tmp = -0.5 * t_1
	elif math.cos(th) <= 0.97:
		tmp = math.cos(th) * (a2 * (a2 * math.sqrt(0.5)))
	else:
		tmp = t_1 * math.sqrt(0.5)
	return tmp
function code(a1, a2, th)
	t_1 = Float64(Float64(a1 * a1) + Float64(a2 * a2))
	tmp = 0.0
	if (cos(th) <= -0.4)
		tmp = Float64(-0.5 * t_1);
	elseif (cos(th) <= 0.97)
		tmp = Float64(cos(th) * Float64(a2 * Float64(a2 * sqrt(0.5))));
	else
		tmp = Float64(t_1 * sqrt(0.5));
	end
	return tmp
end
function tmp_2 = code(a1, a2, th)
	t_1 = (a1 * a1) + (a2 * a2);
	tmp = 0.0;
	if (cos(th) <= -0.4)
		tmp = -0.5 * t_1;
	elseif (cos(th) <= 0.97)
		tmp = cos(th) * (a2 * (a2 * sqrt(0.5)));
	else
		tmp = t_1 * sqrt(0.5);
	end
	tmp_2 = tmp;
end
code[a1_, a2_, th_] := Block[{t$95$1 = N[(N[(a1 * a1), $MachinePrecision] + N[(a2 * a2), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[Cos[th], $MachinePrecision], -0.4], N[(-0.5 * t$95$1), $MachinePrecision], If[LessEqual[N[Cos[th], $MachinePrecision], 0.97], N[(N[Cos[th], $MachinePrecision] * N[(a2 * N[(a2 * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(t$95$1 * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := a1 \cdot a1 + a2 \cdot a2\\
\mathbf{if}\;\cos th \leq -0.4:\\
\;\;\;\;-0.5 \cdot t_1\\

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

\mathbf{else}:\\
\;\;\;\;t_1 \cdot \sqrt{0.5}\\


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

    1. Initial program 99.9%

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

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

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

      \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. Applied egg-rr75.7%

      \[\leadsto \color{blue}{-0.5} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]

    if -0.40000000000000002 < (cos.f64 th) < 0.96999999999999997

    1. Initial program 99.5%

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

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

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

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

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

        \[\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 a1 around 0 54.4%

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

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

        \[\leadsto \color{blue}{\left(\sqrt{0.5} \cdot \left(a2 \cdot a2\right)\right) \cdot \cos th} \]
      3. *-commutative54.4%

        \[\leadsto \color{blue}{\cos th \cdot \left(\sqrt{0.5} \cdot \left(a2 \cdot a2\right)\right)} \]
      4. *-commutative54.4%

        \[\leadsto \cos th \cdot \color{blue}{\left(\left(a2 \cdot a2\right) \cdot \sqrt{0.5}\right)} \]
      5. associate-*l*54.4%

        \[\leadsto \cos th \cdot \color{blue}{\left(a2 \cdot \left(a2 \cdot \sqrt{0.5}\right)\right)} \]
    8. Simplified54.4%

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

    if 0.96999999999999997 < (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.6%

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

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

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

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

      \[\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 94.2%

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

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

Alternative 4: 79.7% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := a1 \cdot a1 + a2 \cdot a2\\ \mathbf{if}\;\cos th \leq -0.02:\\ \;\;\;\;-0.5 \cdot t_1\\ \mathbf{else}:\\ \;\;\;\;t_1 \cdot \sqrt{0.5}\\ \end{array} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (let* ((t_1 (+ (* a1 a1) (* a2 a2))))
   (if (<= (cos th) -0.02) (* -0.5 t_1) (* t_1 (sqrt 0.5)))))
double code(double a1, double a2, double th) {
	double t_1 = (a1 * a1) + (a2 * a2);
	double tmp;
	if (cos(th) <= -0.02) {
		tmp = -0.5 * t_1;
	} else {
		tmp = t_1 * sqrt(0.5);
	}
	return tmp;
}
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
    real(8) :: tmp
    t_1 = (a1 * a1) + (a2 * a2)
    if (cos(th) <= (-0.02d0)) then
        tmp = (-0.5d0) * t_1
    else
        tmp = t_1 * sqrt(0.5d0)
    end if
    code = tmp
end function
public static double code(double a1, double a2, double th) {
	double t_1 = (a1 * a1) + (a2 * a2);
	double tmp;
	if (Math.cos(th) <= -0.02) {
		tmp = -0.5 * t_1;
	} else {
		tmp = t_1 * Math.sqrt(0.5);
	}
	return tmp;
}
def code(a1, a2, th):
	t_1 = (a1 * a1) + (a2 * a2)
	tmp = 0
	if math.cos(th) <= -0.02:
		tmp = -0.5 * t_1
	else:
		tmp = t_1 * math.sqrt(0.5)
	return tmp
function code(a1, a2, th)
	t_1 = Float64(Float64(a1 * a1) + Float64(a2 * a2))
	tmp = 0.0
	if (cos(th) <= -0.02)
		tmp = Float64(-0.5 * t_1);
	else
		tmp = Float64(t_1 * sqrt(0.5));
	end
	return tmp
end
function tmp_2 = code(a1, a2, th)
	t_1 = (a1 * a1) + (a2 * a2);
	tmp = 0.0;
	if (cos(th) <= -0.02)
		tmp = -0.5 * t_1;
	else
		tmp = t_1 * sqrt(0.5);
	end
	tmp_2 = tmp;
end
code[a1_, a2_, th_] := Block[{t$95$1 = N[(N[(a1 * a1), $MachinePrecision] + N[(a2 * a2), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[Cos[th], $MachinePrecision], -0.02], N[(-0.5 * t$95$1), $MachinePrecision], N[(t$95$1 * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := a1 \cdot a1 + a2 \cdot a2\\
\mathbf{if}\;\cos th \leq -0.02:\\
\;\;\;\;-0.5 \cdot t_1\\

\mathbf{else}:\\
\;\;\;\;t_1 \cdot \sqrt{0.5}\\


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

    1. Initial program 99.8%

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

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

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

      \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. Applied egg-rr70.0%

      \[\leadsto \color{blue}{-0.5} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]

    if -0.0200000000000000004 < (cos.f64 th)

    1. Initial program 99.5%

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

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

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

        \[\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 81.4%

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

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

Alternative 5: 99.6% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \frac{\cos th}{\sqrt{2}} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (* (+ (* a1 a1) (* a2 a2)) (/ (cos th) (sqrt 2.0))))
double code(double a1, double a2, double th) {
	return ((a1 * a1) + (a2 * a2)) * (cos(th) / sqrt(2.0));
}
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) + (a2 * a2)) * (cos(th) / sqrt(2.0d0))
end function
public static double code(double a1, double a2, double th) {
	return ((a1 * a1) + (a2 * a2)) * (Math.cos(th) / Math.sqrt(2.0));
}
def code(a1, a2, th):
	return ((a1 * a1) + (a2 * a2)) * (math.cos(th) / math.sqrt(2.0))
function code(a1, a2, th)
	return Float64(Float64(Float64(a1 * a1) + Float64(a2 * a2)) * Float64(cos(th) / sqrt(2.0)))
end
function tmp = code(a1, a2, th)
	tmp = ((a1 * a1) + (a2 * a2)) * (cos(th) / sqrt(2.0));
end
code[a1_, a2_, th_] := N[(N[(N[(a1 * a1), $MachinePrecision] + N[(a2 * a2), $MachinePrecision]), $MachinePrecision] * N[(N[Cos[th], $MachinePrecision] / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot \frac{\cos th}{\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)} \]
  3. Simplified99.6%

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

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

Alternative 6: 68.8% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;a1 \leq -7.8 \cdot 10^{-59}:\\ \;\;\;\;\cos th \cdot \frac{a1}{\frac{\sqrt{2}}{a1}}\\ \mathbf{else}:\\ \;\;\;\;\cos th \cdot \left(a2 \cdot \left(a2 \cdot \sqrt{0.5}\right)\right)\\ \end{array} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (if (<= a1 -7.8e-59)
   (* (cos th) (/ a1 (/ (sqrt 2.0) a1)))
   (* (cos th) (* a2 (* a2 (sqrt 0.5))))))
double code(double a1, double a2, double th) {
	double tmp;
	if (a1 <= -7.8e-59) {
		tmp = cos(th) * (a1 / (sqrt(2.0) / a1));
	} else {
		tmp = cos(th) * (a2 * (a2 * sqrt(0.5)));
	}
	return tmp;
}
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 (a1 <= (-7.8d-59)) then
        tmp = cos(th) * (a1 / (sqrt(2.0d0) / a1))
    else
        tmp = cos(th) * (a2 * (a2 * sqrt(0.5d0)))
    end if
    code = tmp
end function
public static double code(double a1, double a2, double th) {
	double tmp;
	if (a1 <= -7.8e-59) {
		tmp = Math.cos(th) * (a1 / (Math.sqrt(2.0) / a1));
	} else {
		tmp = Math.cos(th) * (a2 * (a2 * Math.sqrt(0.5)));
	}
	return tmp;
}
def code(a1, a2, th):
	tmp = 0
	if a1 <= -7.8e-59:
		tmp = math.cos(th) * (a1 / (math.sqrt(2.0) / a1))
	else:
		tmp = math.cos(th) * (a2 * (a2 * math.sqrt(0.5)))
	return tmp
function code(a1, a2, th)
	tmp = 0.0
	if (a1 <= -7.8e-59)
		tmp = Float64(cos(th) * Float64(a1 / Float64(sqrt(2.0) / a1)));
	else
		tmp = Float64(cos(th) * Float64(a2 * Float64(a2 * sqrt(0.5))));
	end
	return tmp
end
function tmp_2 = code(a1, a2, th)
	tmp = 0.0;
	if (a1 <= -7.8e-59)
		tmp = cos(th) * (a1 / (sqrt(2.0) / a1));
	else
		tmp = cos(th) * (a2 * (a2 * sqrt(0.5)));
	end
	tmp_2 = tmp;
end
code[a1_, a2_, th_] := If[LessEqual[a1, -7.8e-59], N[(N[Cos[th], $MachinePrecision] * N[(a1 / N[(N[Sqrt[2.0], $MachinePrecision] / a1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Cos[th], $MachinePrecision] * N[(a2 * N[(a2 * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;a1 \leq -7.8 \cdot 10^{-59}:\\
\;\;\;\;\cos th \cdot \frac{a1}{\frac{\sqrt{2}}{a1}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if a1 < -7.80000000000000038e-59

    1. Initial program 99.7%

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

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

        \[\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 inf 80.9%

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

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

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

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

    if -7.80000000000000038e-59 < a1

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

        \[\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 a1 around 0 60.4%

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

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

        \[\leadsto \color{blue}{\left(\sqrt{0.5} \cdot \left(a2 \cdot a2\right)\right) \cdot \cos th} \]
      3. *-commutative60.4%

        \[\leadsto \color{blue}{\cos th \cdot \left(\sqrt{0.5} \cdot \left(a2 \cdot a2\right)\right)} \]
      4. *-commutative60.4%

        \[\leadsto \cos th \cdot \color{blue}{\left(\left(a2 \cdot a2\right) \cdot \sqrt{0.5}\right)} \]
      5. associate-*l*60.4%

        \[\leadsto \cos th \cdot \color{blue}{\left(a2 \cdot \left(a2 \cdot \sqrt{0.5}\right)\right)} \]
    8. Simplified60.4%

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

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

Alternative 7: 68.8% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;a1 \leq -8.2 \cdot 10^{-59}:\\ \;\;\;\;\cos th \cdot \frac{a1}{\frac{\sqrt{2}}{a1}}\\ \mathbf{else}:\\ \;\;\;\;\cos th \cdot \frac{a2}{\frac{\sqrt{2}}{a2}}\\ \end{array} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (if (<= a1 -8.2e-59)
   (* (cos th) (/ a1 (/ (sqrt 2.0) a1)))
   (* (cos th) (/ a2 (/ (sqrt 2.0) a2)))))
double code(double a1, double a2, double th) {
	double tmp;
	if (a1 <= -8.2e-59) {
		tmp = cos(th) * (a1 / (sqrt(2.0) / a1));
	} else {
		tmp = cos(th) * (a2 / (sqrt(2.0) / a2));
	}
	return tmp;
}
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 (a1 <= (-8.2d-59)) then
        tmp = cos(th) * (a1 / (sqrt(2.0d0) / a1))
    else
        tmp = cos(th) * (a2 / (sqrt(2.0d0) / a2))
    end if
    code = tmp
end function
public static double code(double a1, double a2, double th) {
	double tmp;
	if (a1 <= -8.2e-59) {
		tmp = Math.cos(th) * (a1 / (Math.sqrt(2.0) / a1));
	} else {
		tmp = Math.cos(th) * (a2 / (Math.sqrt(2.0) / a2));
	}
	return tmp;
}
def code(a1, a2, th):
	tmp = 0
	if a1 <= -8.2e-59:
		tmp = math.cos(th) * (a1 / (math.sqrt(2.0) / a1))
	else:
		tmp = math.cos(th) * (a2 / (math.sqrt(2.0) / a2))
	return tmp
function code(a1, a2, th)
	tmp = 0.0
	if (a1 <= -8.2e-59)
		tmp = Float64(cos(th) * Float64(a1 / Float64(sqrt(2.0) / a1)));
	else
		tmp = Float64(cos(th) * Float64(a2 / Float64(sqrt(2.0) / a2)));
	end
	return tmp
end
function tmp_2 = code(a1, a2, th)
	tmp = 0.0;
	if (a1 <= -8.2e-59)
		tmp = cos(th) * (a1 / (sqrt(2.0) / a1));
	else
		tmp = cos(th) * (a2 / (sqrt(2.0) / a2));
	end
	tmp_2 = tmp;
end
code[a1_, a2_, th_] := If[LessEqual[a1, -8.2e-59], N[(N[Cos[th], $MachinePrecision] * N[(a1 / N[(N[Sqrt[2.0], $MachinePrecision] / a1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Cos[th], $MachinePrecision] * N[(a2 / N[(N[Sqrt[2.0], $MachinePrecision] / a2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;a1 \leq -8.2 \cdot 10^{-59}:\\
\;\;\;\;\cos th \cdot \frac{a1}{\frac{\sqrt{2}}{a1}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if a1 < -8.1999999999999991e-59

    1. Initial program 99.7%

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

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

        \[\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 inf 80.9%

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

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

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

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

    if -8.1999999999999991e-59 < a1

    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 60.4%

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

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

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

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

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

Alternative 8: 68.8% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;a1 \leq -1.1 \cdot 10^{-59}:\\ \;\;\;\;\frac{a1 \cdot a1}{\frac{\sqrt{2}}{\cos th}}\\ \mathbf{else}:\\ \;\;\;\;\cos th \cdot \frac{a2}{\frac{\sqrt{2}}{a2}}\\ \end{array} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (if (<= a1 -1.1e-59)
   (/ (* a1 a1) (/ (sqrt 2.0) (cos th)))
   (* (cos th) (/ a2 (/ (sqrt 2.0) a2)))))
double code(double a1, double a2, double th) {
	double tmp;
	if (a1 <= -1.1e-59) {
		tmp = (a1 * a1) / (sqrt(2.0) / cos(th));
	} else {
		tmp = cos(th) * (a2 / (sqrt(2.0) / a2));
	}
	return tmp;
}
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 (a1 <= (-1.1d-59)) then
        tmp = (a1 * a1) / (sqrt(2.0d0) / cos(th))
    else
        tmp = cos(th) * (a2 / (sqrt(2.0d0) / a2))
    end if
    code = tmp
end function
public static double code(double a1, double a2, double th) {
	double tmp;
	if (a1 <= -1.1e-59) {
		tmp = (a1 * a1) / (Math.sqrt(2.0) / Math.cos(th));
	} else {
		tmp = Math.cos(th) * (a2 / (Math.sqrt(2.0) / a2));
	}
	return tmp;
}
def code(a1, a2, th):
	tmp = 0
	if a1 <= -1.1e-59:
		tmp = (a1 * a1) / (math.sqrt(2.0) / math.cos(th))
	else:
		tmp = math.cos(th) * (a2 / (math.sqrt(2.0) / a2))
	return tmp
function code(a1, a2, th)
	tmp = 0.0
	if (a1 <= -1.1e-59)
		tmp = Float64(Float64(a1 * a1) / Float64(sqrt(2.0) / cos(th)));
	else
		tmp = Float64(cos(th) * Float64(a2 / Float64(sqrt(2.0) / a2)));
	end
	return tmp
end
function tmp_2 = code(a1, a2, th)
	tmp = 0.0;
	if (a1 <= -1.1e-59)
		tmp = (a1 * a1) / (sqrt(2.0) / cos(th));
	else
		tmp = cos(th) * (a2 / (sqrt(2.0) / a2));
	end
	tmp_2 = tmp;
end
code[a1_, a2_, th_] := If[LessEqual[a1, -1.1e-59], N[(N[(a1 * a1), $MachinePrecision] / N[(N[Sqrt[2.0], $MachinePrecision] / N[Cos[th], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Cos[th], $MachinePrecision] * N[(a2 / N[(N[Sqrt[2.0], $MachinePrecision] / a2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;a1 \leq -1.1 \cdot 10^{-59}:\\
\;\;\;\;\frac{a1 \cdot a1}{\frac{\sqrt{2}}{\cos th}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if a1 < -1.0999999999999999e-59

    1. Initial program 99.7%

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

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

        \[\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 inf 80.9%

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

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

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

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

    if -1.0999999999999999e-59 < a1

    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 60.4%

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

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

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

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

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

Alternative 9: 51.8% accurate, 3.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;a2 \leq 1.45:\\ \;\;\;\;\left(a1 \cdot a1\right) \cdot \sqrt{0.5}\\ \mathbf{elif}\;a2 \leq 1.05 \cdot 10^{+150}:\\ \;\;\;\;a2 \cdot \sqrt{\frac{a2 \cdot a2}{2}}\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(\cos th \cdot a2\right)\\ \end{array} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (if (<= a2 1.45)
   (* (* a1 a1) (sqrt 0.5))
   (if (<= a2 1.05e+150)
     (* a2 (sqrt (/ (* a2 a2) 2.0)))
     (* a2 (* (cos th) a2)))))
double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 1.45) {
		tmp = (a1 * a1) * sqrt(0.5);
	} else if (a2 <= 1.05e+150) {
		tmp = a2 * sqrt(((a2 * a2) / 2.0));
	} else {
		tmp = a2 * (cos(th) * a2);
	}
	return tmp;
}
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.45d0) then
        tmp = (a1 * a1) * sqrt(0.5d0)
    else if (a2 <= 1.05d+150) then
        tmp = a2 * sqrt(((a2 * a2) / 2.0d0))
    else
        tmp = a2 * (cos(th) * a2)
    end if
    code = tmp
end function
public static double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 1.45) {
		tmp = (a1 * a1) * Math.sqrt(0.5);
	} else if (a2 <= 1.05e+150) {
		tmp = a2 * Math.sqrt(((a2 * a2) / 2.0));
	} else {
		tmp = a2 * (Math.cos(th) * a2);
	}
	return tmp;
}
def code(a1, a2, th):
	tmp = 0
	if a2 <= 1.45:
		tmp = (a1 * a1) * math.sqrt(0.5)
	elif a2 <= 1.05e+150:
		tmp = a2 * math.sqrt(((a2 * a2) / 2.0))
	else:
		tmp = a2 * (math.cos(th) * a2)
	return tmp
function code(a1, a2, th)
	tmp = 0.0
	if (a2 <= 1.45)
		tmp = Float64(Float64(a1 * a1) * sqrt(0.5));
	elseif (a2 <= 1.05e+150)
		tmp = Float64(a2 * sqrt(Float64(Float64(a2 * a2) / 2.0)));
	else
		tmp = Float64(a2 * Float64(cos(th) * a2));
	end
	return tmp
end
function tmp_2 = code(a1, a2, th)
	tmp = 0.0;
	if (a2 <= 1.45)
		tmp = (a1 * a1) * sqrt(0.5);
	elseif (a2 <= 1.05e+150)
		tmp = a2 * sqrt(((a2 * a2) / 2.0));
	else
		tmp = a2 * (cos(th) * a2);
	end
	tmp_2 = tmp;
end
code[a1_, a2_, th_] := If[LessEqual[a2, 1.45], N[(N[(a1 * a1), $MachinePrecision] * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision], If[LessEqual[a2, 1.05e+150], N[(a2 * N[Sqrt[N[(N[(a2 * a2), $MachinePrecision] / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(a2 * N[(N[Cos[th], $MachinePrecision] * a2), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;a2 \leq 1.45:\\
\;\;\;\;\left(a1 \cdot a1\right) \cdot \sqrt{0.5}\\

\mathbf{elif}\;a2 \leq 1.05 \cdot 10^{+150}:\\
\;\;\;\;a2 \cdot \sqrt{\frac{a2 \cdot a2}{2}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if a2 < 1.44999999999999996

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

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

      \[\leadsto \color{blue}{\sqrt{0.5}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    7. Taylor expanded in a1 around inf 46.3%

      \[\leadsto \color{blue}{\sqrt{0.5} \cdot {a1}^{2}} \]
    8. Step-by-step derivation
      1. unpow246.3%

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

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

    if 1.44999999999999996 < a2 < 1.04999999999999999e150

    1. Initial program 99.5%

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

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

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

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

      \[\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 55.2%

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

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

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

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

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

      \[\leadsto \color{blue}{a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}} \]
    7. Taylor expanded in th around 0 28.7%

      \[\leadsto a2 \cdot \color{blue}{\frac{a2}{\sqrt{2}}} \]
    8. Step-by-step derivation
      1. add-sqr-sqrt28.6%

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

        \[\leadsto a2 \cdot \color{blue}{\sqrt{\frac{a2}{\sqrt{2}} \cdot \frac{a2}{\sqrt{2}}}} \]
      3. frac-times28.6%

        \[\leadsto a2 \cdot \sqrt{\color{blue}{\frac{a2 \cdot a2}{\sqrt{2} \cdot \sqrt{2}}}} \]
      4. add-sqr-sqrt28.6%

        \[\leadsto a2 \cdot \sqrt{\frac{a2 \cdot a2}{\color{blue}{2}}} \]
    9. Applied egg-rr28.6%

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

    if 1.04999999999999999e150 < a2

    1. Initial program 100.0%

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

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

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

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

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

      \[\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 100.0%

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

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

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

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

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

      \[\leadsto \color{blue}{a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}} \]
    7. Step-by-step derivation
      1. *-un-lft-identity100.0%

        \[\leadsto a2 \cdot \frac{\color{blue}{1 \cdot a2}}{\frac{\sqrt{2}}{\cos th}} \]
      2. div-inv100.0%

        \[\leadsto a2 \cdot \frac{1 \cdot a2}{\color{blue}{\sqrt{2} \cdot \frac{1}{\cos th}}} \]
      3. times-frac100.0%

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

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

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

        \[\leadsto a2 \cdot \left({2}^{\color{blue}{-0.5}} \cdot \frac{a2}{\frac{1}{\cos th}}\right) \]
      7. add-sqr-sqrt100.0%

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

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

        \[\leadsto a2 \cdot \left(\sqrt{\color{blue}{{2}^{\left(-0.5 + -0.5\right)}}} \cdot \frac{a2}{\frac{1}{\cos th}}\right) \]
      10. metadata-eval100.0%

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

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

      \[\leadsto a2 \cdot \color{blue}{\left(\sqrt{0.5} \cdot \frac{a2}{\frac{1}{\cos th}}\right)} \]
    9. Step-by-step derivation
      1. associate-/r/100.0%

        \[\leadsto a2 \cdot \left(\sqrt{0.5} \cdot \color{blue}{\left(\frac{a2}{1} \cdot \cos th\right)}\right) \]
      2. /-rgt-identity100.0%

        \[\leadsto a2 \cdot \left(\sqrt{0.5} \cdot \left(\color{blue}{a2} \cdot \cos th\right)\right) \]
      3. *-commutative100.0%

        \[\leadsto a2 \cdot \left(\sqrt{0.5} \cdot \color{blue}{\left(\cos th \cdot a2\right)}\right) \]
      4. associate-*l*100.0%

        \[\leadsto a2 \cdot \color{blue}{\left(\left(\sqrt{0.5} \cdot \cos th\right) \cdot a2\right)} \]
    10. Simplified100.0%

      \[\leadsto a2 \cdot \color{blue}{\left(\left(\sqrt{0.5} \cdot \cos th\right) \cdot a2\right)} \]
    11. Applied egg-rr100.0%

      \[\leadsto a2 \cdot \left(\color{blue}{\log \left(e^{\cos th}\right)} \cdot a2\right) \]
    12. Step-by-step derivation
      1. rem-log-exp100.0%

        \[\leadsto a2 \cdot \left(\color{blue}{\cos th} \cdot a2\right) \]
    13. Simplified100.0%

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

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

Alternative 10: 60.3% accurate, 3.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := a1 \cdot a1 + a2 \cdot a2\\ \mathbf{if}\;\cos th \leq -1 \cdot 10^{-309}:\\ \;\;\;\;-0.5 \cdot t_1\\ \mathbf{else}:\\ \;\;\;\;t_1 \cdot 0.5\\ \end{array} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (let* ((t_1 (+ (* a1 a1) (* a2 a2))))
   (if (<= (cos th) -1e-309) (* -0.5 t_1) (* t_1 0.5))))
double code(double a1, double a2, double th) {
	double t_1 = (a1 * a1) + (a2 * a2);
	double tmp;
	if (cos(th) <= -1e-309) {
		tmp = -0.5 * t_1;
	} else {
		tmp = t_1 * 0.5;
	}
	return tmp;
}
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
    real(8) :: tmp
    t_1 = (a1 * a1) + (a2 * a2)
    if (cos(th) <= (-1d-309)) then
        tmp = (-0.5d0) * t_1
    else
        tmp = t_1 * 0.5d0
    end if
    code = tmp
end function
public static double code(double a1, double a2, double th) {
	double t_1 = (a1 * a1) + (a2 * a2);
	double tmp;
	if (Math.cos(th) <= -1e-309) {
		tmp = -0.5 * t_1;
	} else {
		tmp = t_1 * 0.5;
	}
	return tmp;
}
def code(a1, a2, th):
	t_1 = (a1 * a1) + (a2 * a2)
	tmp = 0
	if math.cos(th) <= -1e-309:
		tmp = -0.5 * t_1
	else:
		tmp = t_1 * 0.5
	return tmp
function code(a1, a2, th)
	t_1 = Float64(Float64(a1 * a1) + Float64(a2 * a2))
	tmp = 0.0
	if (cos(th) <= -1e-309)
		tmp = Float64(-0.5 * t_1);
	else
		tmp = Float64(t_1 * 0.5);
	end
	return tmp
end
function tmp_2 = code(a1, a2, th)
	t_1 = (a1 * a1) + (a2 * a2);
	tmp = 0.0;
	if (cos(th) <= -1e-309)
		tmp = -0.5 * t_1;
	else
		tmp = t_1 * 0.5;
	end
	tmp_2 = tmp;
end
code[a1_, a2_, th_] := Block[{t$95$1 = N[(N[(a1 * a1), $MachinePrecision] + N[(a2 * a2), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[Cos[th], $MachinePrecision], -1e-309], N[(-0.5 * t$95$1), $MachinePrecision], N[(t$95$1 * 0.5), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := a1 \cdot a1 + a2 \cdot a2\\
\mathbf{if}\;\cos th \leq -1 \cdot 10^{-309}:\\
\;\;\;\;-0.5 \cdot t_1\\

\mathbf{else}:\\
\;\;\;\;t_1 \cdot 0.5\\


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

    1. Initial program 99.8%

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

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

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

      \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. Applied egg-rr70.0%

      \[\leadsto \color{blue}{-0.5} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]

    if -1.000000000000002e-309 < (cos.f64 th)

    1. Initial program 99.5%

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

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

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

      \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. Applied egg-rr56.8%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\cos th \leq -1 \cdot 10^{-309}:\\ \;\;\;\;-0.5 \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)\\ \mathbf{else}:\\ \;\;\;\;\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot 0.5\\ \end{array} \]

Alternative 11: 51.9% accurate, 3.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;a2 \leq 0.00035:\\ \;\;\;\;\left(a1 \cdot a1\right) \cdot \sqrt{0.5}\\ \mathbf{elif}\;a2 \leq 10^{+150}:\\ \;\;\;\;a2 \cdot \left(a2 \cdot \sqrt{0.5}\right)\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(\cos th \cdot a2\right)\\ \end{array} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (if (<= a2 0.00035)
   (* (* a1 a1) (sqrt 0.5))
   (if (<= a2 1e+150) (* a2 (* a2 (sqrt 0.5))) (* a2 (* (cos th) a2)))))
double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 0.00035) {
		tmp = (a1 * a1) * sqrt(0.5);
	} else if (a2 <= 1e+150) {
		tmp = a2 * (a2 * sqrt(0.5));
	} else {
		tmp = a2 * (cos(th) * a2);
	}
	return tmp;
}
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 <= 0.00035d0) then
        tmp = (a1 * a1) * sqrt(0.5d0)
    else if (a2 <= 1d+150) then
        tmp = a2 * (a2 * sqrt(0.5d0))
    else
        tmp = a2 * (cos(th) * a2)
    end if
    code = tmp
end function
public static double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 0.00035) {
		tmp = (a1 * a1) * Math.sqrt(0.5);
	} else if (a2 <= 1e+150) {
		tmp = a2 * (a2 * Math.sqrt(0.5));
	} else {
		tmp = a2 * (Math.cos(th) * a2);
	}
	return tmp;
}
def code(a1, a2, th):
	tmp = 0
	if a2 <= 0.00035:
		tmp = (a1 * a1) * math.sqrt(0.5)
	elif a2 <= 1e+150:
		tmp = a2 * (a2 * math.sqrt(0.5))
	else:
		tmp = a2 * (math.cos(th) * a2)
	return tmp
function code(a1, a2, th)
	tmp = 0.0
	if (a2 <= 0.00035)
		tmp = Float64(Float64(a1 * a1) * sqrt(0.5));
	elseif (a2 <= 1e+150)
		tmp = Float64(a2 * Float64(a2 * sqrt(0.5)));
	else
		tmp = Float64(a2 * Float64(cos(th) * a2));
	end
	return tmp
end
function tmp_2 = code(a1, a2, th)
	tmp = 0.0;
	if (a2 <= 0.00035)
		tmp = (a1 * a1) * sqrt(0.5);
	elseif (a2 <= 1e+150)
		tmp = a2 * (a2 * sqrt(0.5));
	else
		tmp = a2 * (cos(th) * a2);
	end
	tmp_2 = tmp;
end
code[a1_, a2_, th_] := If[LessEqual[a2, 0.00035], N[(N[(a1 * a1), $MachinePrecision] * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision], If[LessEqual[a2, 1e+150], N[(a2 * N[(a2 * N[Sqrt[0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(a2 * N[(N[Cos[th], $MachinePrecision] * a2), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;a2 \leq 0.00035:\\
\;\;\;\;\left(a1 \cdot a1\right) \cdot \sqrt{0.5}\\

\mathbf{elif}\;a2 \leq 10^{+150}:\\
\;\;\;\;a2 \cdot \left(a2 \cdot \sqrt{0.5}\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if a2 < 3.49999999999999996e-4

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

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

      \[\leadsto \color{blue}{\sqrt{0.5}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    7. Taylor expanded in a1 around inf 46.3%

      \[\leadsto \color{blue}{\sqrt{0.5} \cdot {a1}^{2}} \]
    8. Step-by-step derivation
      1. unpow246.3%

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

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

    if 3.49999999999999996e-4 < a2 < 9.99999999999999981e149

    1. Initial program 99.5%

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

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

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

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

      \[\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 55.2%

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

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

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

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

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

      \[\leadsto \color{blue}{a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}} \]
    7. Step-by-step derivation
      1. *-un-lft-identity55.2%

        \[\leadsto a2 \cdot \frac{\color{blue}{1 \cdot a2}}{\frac{\sqrt{2}}{\cos th}} \]
      2. div-inv55.2%

        \[\leadsto a2 \cdot \frac{1 \cdot a2}{\color{blue}{\sqrt{2} \cdot \frac{1}{\cos th}}} \]
      3. times-frac55.1%

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

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

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

        \[\leadsto a2 \cdot \left({2}^{\color{blue}{-0.5}} \cdot \frac{a2}{\frac{1}{\cos th}}\right) \]
      7. add-sqr-sqrt54.6%

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

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

        \[\leadsto a2 \cdot \left(\sqrt{\color{blue}{{2}^{\left(-0.5 + -0.5\right)}}} \cdot \frac{a2}{\frac{1}{\cos th}}\right) \]
      10. metadata-eval55.2%

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

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

      \[\leadsto a2 \cdot \color{blue}{\left(\sqrt{0.5} \cdot \frac{a2}{\frac{1}{\cos th}}\right)} \]
    9. Step-by-step derivation
      1. associate-/r/55.3%

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

        \[\leadsto a2 \cdot \left(\sqrt{0.5} \cdot \left(\color{blue}{a2} \cdot \cos th\right)\right) \]
      3. *-commutative55.3%

        \[\leadsto a2 \cdot \left(\sqrt{0.5} \cdot \color{blue}{\left(\cos th \cdot a2\right)}\right) \]
      4. associate-*l*55.2%

        \[\leadsto a2 \cdot \color{blue}{\left(\left(\sqrt{0.5} \cdot \cos th\right) \cdot a2\right)} \]
    10. Simplified55.2%

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

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

    if 9.99999999999999981e149 < a2

    1. Initial program 100.0%

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

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

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

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

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

      \[\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 100.0%

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

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

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

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

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

      \[\leadsto \color{blue}{a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}} \]
    7. Step-by-step derivation
      1. *-un-lft-identity100.0%

        \[\leadsto a2 \cdot \frac{\color{blue}{1 \cdot a2}}{\frac{\sqrt{2}}{\cos th}} \]
      2. div-inv100.0%

        \[\leadsto a2 \cdot \frac{1 \cdot a2}{\color{blue}{\sqrt{2} \cdot \frac{1}{\cos th}}} \]
      3. times-frac100.0%

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

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

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

        \[\leadsto a2 \cdot \left({2}^{\color{blue}{-0.5}} \cdot \frac{a2}{\frac{1}{\cos th}}\right) \]
      7. add-sqr-sqrt100.0%

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

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

        \[\leadsto a2 \cdot \left(\sqrt{\color{blue}{{2}^{\left(-0.5 + -0.5\right)}}} \cdot \frac{a2}{\frac{1}{\cos th}}\right) \]
      10. metadata-eval100.0%

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

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

      \[\leadsto a2 \cdot \color{blue}{\left(\sqrt{0.5} \cdot \frac{a2}{\frac{1}{\cos th}}\right)} \]
    9. Step-by-step derivation
      1. associate-/r/100.0%

        \[\leadsto a2 \cdot \left(\sqrt{0.5} \cdot \color{blue}{\left(\frac{a2}{1} \cdot \cos th\right)}\right) \]
      2. /-rgt-identity100.0%

        \[\leadsto a2 \cdot \left(\sqrt{0.5} \cdot \left(\color{blue}{a2} \cdot \cos th\right)\right) \]
      3. *-commutative100.0%

        \[\leadsto a2 \cdot \left(\sqrt{0.5} \cdot \color{blue}{\left(\cos th \cdot a2\right)}\right) \]
      4. associate-*l*100.0%

        \[\leadsto a2 \cdot \color{blue}{\left(\left(\sqrt{0.5} \cdot \cos th\right) \cdot a2\right)} \]
    10. Simplified100.0%

      \[\leadsto a2 \cdot \color{blue}{\left(\left(\sqrt{0.5} \cdot \cos th\right) \cdot a2\right)} \]
    11. Applied egg-rr100.0%

      \[\leadsto a2 \cdot \left(\color{blue}{\log \left(e^{\cos th}\right)} \cdot a2\right) \]
    12. Step-by-step derivation
      1. rem-log-exp100.0%

        \[\leadsto a2 \cdot \left(\color{blue}{\cos th} \cdot a2\right) \]
    13. Simplified100.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;a2 \leq 0.00035:\\ \;\;\;\;\left(a1 \cdot a1\right) \cdot \sqrt{0.5}\\ \mathbf{elif}\;a2 \leq 10^{+150}:\\ \;\;\;\;a2 \cdot \left(a2 \cdot \sqrt{0.5}\right)\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot \left(\cos th \cdot a2\right)\\ \end{array} \]

Alternative 12: 47.2% accurate, 27.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;th \leq 1.5 \cdot 10^{+80} \lor \neg \left(th \leq 2.6 \cdot 10^{+254}\right) \land th \leq 6.8 \cdot 10^{+289}:\\ \;\;\;\;\left(a1 + a2\right) \cdot \left(a1 + a2\right)\\ \mathbf{else}:\\ \;\;\;\;-0.5 \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)\\ \end{array} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (if (or (<= th 1.5e+80) (and (not (<= th 2.6e+254)) (<= th 6.8e+289)))
   (* (+ a1 a2) (+ a1 a2))
   (* -0.5 (+ (* a1 a1) (* a2 a2)))))
double code(double a1, double a2, double th) {
	double tmp;
	if ((th <= 1.5e+80) || (!(th <= 2.6e+254) && (th <= 6.8e+289))) {
		tmp = (a1 + a2) * (a1 + a2);
	} else {
		tmp = -0.5 * ((a1 * a1) + (a2 * a2));
	}
	return tmp;
}
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 ((th <= 1.5d+80) .or. (.not. (th <= 2.6d+254)) .and. (th <= 6.8d+289)) then
        tmp = (a1 + a2) * (a1 + a2)
    else
        tmp = (-0.5d0) * ((a1 * a1) + (a2 * a2))
    end if
    code = tmp
end function
public static double code(double a1, double a2, double th) {
	double tmp;
	if ((th <= 1.5e+80) || (!(th <= 2.6e+254) && (th <= 6.8e+289))) {
		tmp = (a1 + a2) * (a1 + a2);
	} else {
		tmp = -0.5 * ((a1 * a1) + (a2 * a2));
	}
	return tmp;
}
def code(a1, a2, th):
	tmp = 0
	if (th <= 1.5e+80) or (not (th <= 2.6e+254) and (th <= 6.8e+289)):
		tmp = (a1 + a2) * (a1 + a2)
	else:
		tmp = -0.5 * ((a1 * a1) + (a2 * a2))
	return tmp
function code(a1, a2, th)
	tmp = 0.0
	if ((th <= 1.5e+80) || (!(th <= 2.6e+254) && (th <= 6.8e+289)))
		tmp = Float64(Float64(a1 + a2) * Float64(a1 + a2));
	else
		tmp = Float64(-0.5 * Float64(Float64(a1 * a1) + Float64(a2 * a2)));
	end
	return tmp
end
function tmp_2 = code(a1, a2, th)
	tmp = 0.0;
	if ((th <= 1.5e+80) || (~((th <= 2.6e+254)) && (th <= 6.8e+289)))
		tmp = (a1 + a2) * (a1 + a2);
	else
		tmp = -0.5 * ((a1 * a1) + (a2 * a2));
	end
	tmp_2 = tmp;
end
code[a1_, a2_, th_] := If[Or[LessEqual[th, 1.5e+80], And[N[Not[LessEqual[th, 2.6e+254]], $MachinePrecision], LessEqual[th, 6.8e+289]]], N[(N[(a1 + a2), $MachinePrecision] * N[(a1 + a2), $MachinePrecision]), $MachinePrecision], N[(-0.5 * N[(N[(a1 * a1), $MachinePrecision] + N[(a2 * a2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;th \leq 1.5 \cdot 10^{+80} \lor \neg \left(th \leq 2.6 \cdot 10^{+254}\right) \land th \leq 6.8 \cdot 10^{+289}:\\
\;\;\;\;\left(a1 + a2\right) \cdot \left(a1 + a2\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if th < 1.49999999999999993e80 or 2.6000000000000001e254 < th < 6.7999999999999997e289

    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 71.6%

      \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. Applied egg-rr45.7%

      \[\leadsto \color{blue}{\left(a1 + a2\right) \cdot a1 + \left(a1 + a2\right) \cdot a2} \]
    6. Step-by-step derivation
      1. distribute-lft-out49.4%

        \[\leadsto \color{blue}{\left(a1 + a2\right) \cdot \left(a1 + a2\right)} \]
      2. +-commutative49.4%

        \[\leadsto \color{blue}{\left(a2 + a1\right)} \cdot \left(a1 + a2\right) \]
      3. +-commutative49.4%

        \[\leadsto \left(a2 + a1\right) \cdot \color{blue}{\left(a2 + a1\right)} \]
    7. Simplified49.4%

      \[\leadsto \color{blue}{\left(a2 + a1\right) \cdot \left(a2 + a1\right)} \]

    if 1.49999999999999993e80 < th < 2.6000000000000001e254 or 6.7999999999999997e289 < 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. Taylor expanded in th around 0 20.8%

      \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. Applied egg-rr57.2%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;th \leq 1.5 \cdot 10^{+80} \lor \neg \left(th \leq 2.6 \cdot 10^{+254}\right) \land th \leq 6.8 \cdot 10^{+289}:\\ \;\;\;\;\left(a1 + a2\right) \cdot \left(a1 + a2\right)\\ \mathbf{else}:\\ \;\;\;\;-0.5 \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)\\ \end{array} \]

Alternative 13: 47.2% accurate, 27.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := a1 \cdot a1 + a2 \cdot a2\\ \mathbf{if}\;th \leq 1.5 \cdot 10^{+80}:\\ \;\;\;\;\left(a1 + a2\right) \cdot \left(a1 + a2\right)\\ \mathbf{elif}\;th \leq 2.6 \cdot 10^{+254} \lor \neg \left(th \leq 6.8 \cdot 10^{+289}\right):\\ \;\;\;\;-0.5 \cdot t_1\\ \mathbf{else}:\\ \;\;\;\;t_1 \cdot 0.125\\ \end{array} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (let* ((t_1 (+ (* a1 a1) (* a2 a2))))
   (if (<= th 1.5e+80)
     (* (+ a1 a2) (+ a1 a2))
     (if (or (<= th 2.6e+254) (not (<= th 6.8e+289)))
       (* -0.5 t_1)
       (* t_1 0.125)))))
double code(double a1, double a2, double th) {
	double t_1 = (a1 * a1) + (a2 * a2);
	double tmp;
	if (th <= 1.5e+80) {
		tmp = (a1 + a2) * (a1 + a2);
	} else if ((th <= 2.6e+254) || !(th <= 6.8e+289)) {
		tmp = -0.5 * t_1;
	} else {
		tmp = t_1 * 0.125;
	}
	return tmp;
}
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
    real(8) :: tmp
    t_1 = (a1 * a1) + (a2 * a2)
    if (th <= 1.5d+80) then
        tmp = (a1 + a2) * (a1 + a2)
    else if ((th <= 2.6d+254) .or. (.not. (th <= 6.8d+289))) then
        tmp = (-0.5d0) * t_1
    else
        tmp = t_1 * 0.125d0
    end if
    code = tmp
end function
public static double code(double a1, double a2, double th) {
	double t_1 = (a1 * a1) + (a2 * a2);
	double tmp;
	if (th <= 1.5e+80) {
		tmp = (a1 + a2) * (a1 + a2);
	} else if ((th <= 2.6e+254) || !(th <= 6.8e+289)) {
		tmp = -0.5 * t_1;
	} else {
		tmp = t_1 * 0.125;
	}
	return tmp;
}
def code(a1, a2, th):
	t_1 = (a1 * a1) + (a2 * a2)
	tmp = 0
	if th <= 1.5e+80:
		tmp = (a1 + a2) * (a1 + a2)
	elif (th <= 2.6e+254) or not (th <= 6.8e+289):
		tmp = -0.5 * t_1
	else:
		tmp = t_1 * 0.125
	return tmp
function code(a1, a2, th)
	t_1 = Float64(Float64(a1 * a1) + Float64(a2 * a2))
	tmp = 0.0
	if (th <= 1.5e+80)
		tmp = Float64(Float64(a1 + a2) * Float64(a1 + a2));
	elseif ((th <= 2.6e+254) || !(th <= 6.8e+289))
		tmp = Float64(-0.5 * t_1);
	else
		tmp = Float64(t_1 * 0.125);
	end
	return tmp
end
function tmp_2 = code(a1, a2, th)
	t_1 = (a1 * a1) + (a2 * a2);
	tmp = 0.0;
	if (th <= 1.5e+80)
		tmp = (a1 + a2) * (a1 + a2);
	elseif ((th <= 2.6e+254) || ~((th <= 6.8e+289)))
		tmp = -0.5 * t_1;
	else
		tmp = t_1 * 0.125;
	end
	tmp_2 = tmp;
end
code[a1_, a2_, th_] := Block[{t$95$1 = N[(N[(a1 * a1), $MachinePrecision] + N[(a2 * a2), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[th, 1.5e+80], N[(N[(a1 + a2), $MachinePrecision] * N[(a1 + a2), $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[th, 2.6e+254], N[Not[LessEqual[th, 6.8e+289]], $MachinePrecision]], N[(-0.5 * t$95$1), $MachinePrecision], N[(t$95$1 * 0.125), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := a1 \cdot a1 + a2 \cdot a2\\
\mathbf{if}\;th \leq 1.5 \cdot 10^{+80}:\\
\;\;\;\;\left(a1 + a2\right) \cdot \left(a1 + a2\right)\\

\mathbf{elif}\;th \leq 2.6 \cdot 10^{+254} \lor \neg \left(th \leq 6.8 \cdot 10^{+289}\right):\\
\;\;\;\;-0.5 \cdot t_1\\

\mathbf{else}:\\
\;\;\;\;t_1 \cdot 0.125\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if th < 1.49999999999999993e80

    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 72.9%

      \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. Applied egg-rr45.6%

      \[\leadsto \color{blue}{\left(a1 + a2\right) \cdot a1 + \left(a1 + a2\right) \cdot a2} \]
    6. Step-by-step derivation
      1. distribute-lft-out49.5%

        \[\leadsto \color{blue}{\left(a1 + a2\right) \cdot \left(a1 + a2\right)} \]
      2. +-commutative49.5%

        \[\leadsto \color{blue}{\left(a2 + a1\right)} \cdot \left(a1 + a2\right) \]
      3. +-commutative49.5%

        \[\leadsto \left(a2 + a1\right) \cdot \color{blue}{\left(a2 + a1\right)} \]
    7. Simplified49.5%

      \[\leadsto \color{blue}{\left(a2 + a1\right) \cdot \left(a2 + a1\right)} \]

    if 1.49999999999999993e80 < th < 2.6000000000000001e254 or 6.7999999999999997e289 < 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. Taylor expanded in th around 0 20.8%

      \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. Applied egg-rr57.2%

      \[\leadsto \color{blue}{-0.5} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]

    if 2.6000000000000001e254 < th < 6.7999999999999997e289

    1. Initial program 99.1%

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

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

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

      \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. Applied egg-rr48.0%

      \[\leadsto \color{blue}{0.125} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
  3. Recombined 3 regimes into one program.
  4. Final simplification50.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;th \leq 1.5 \cdot 10^{+80}:\\ \;\;\;\;\left(a1 + a2\right) \cdot \left(a1 + a2\right)\\ \mathbf{elif}\;th \leq 2.6 \cdot 10^{+254} \lor \neg \left(th \leq 6.8 \cdot 10^{+289}\right):\\ \;\;\;\;-0.5 \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)\\ \mathbf{else}:\\ \;\;\;\;\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot 0.125\\ \end{array} \]

Alternative 14: 47.2% accurate, 27.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := a1 \cdot a1 + a2 \cdot a2\\ \mathbf{if}\;th \leq 1.5 \cdot 10^{+80}:\\ \;\;\;\;\left(a1 + a2\right) \cdot \left(a1 + a2\right)\\ \mathbf{elif}\;th \leq 2.6 \cdot 10^{+254} \lor \neg \left(th \leq 6.8 \cdot 10^{+289}\right):\\ \;\;\;\;-0.5 \cdot t_1\\ \mathbf{else}:\\ \;\;\;\;t_1 \cdot 0.25\\ \end{array} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (let* ((t_1 (+ (* a1 a1) (* a2 a2))))
   (if (<= th 1.5e+80)
     (* (+ a1 a2) (+ a1 a2))
     (if (or (<= th 2.6e+254) (not (<= th 6.8e+289)))
       (* -0.5 t_1)
       (* t_1 0.25)))))
double code(double a1, double a2, double th) {
	double t_1 = (a1 * a1) + (a2 * a2);
	double tmp;
	if (th <= 1.5e+80) {
		tmp = (a1 + a2) * (a1 + a2);
	} else if ((th <= 2.6e+254) || !(th <= 6.8e+289)) {
		tmp = -0.5 * t_1;
	} else {
		tmp = t_1 * 0.25;
	}
	return tmp;
}
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
    real(8) :: tmp
    t_1 = (a1 * a1) + (a2 * a2)
    if (th <= 1.5d+80) then
        tmp = (a1 + a2) * (a1 + a2)
    else if ((th <= 2.6d+254) .or. (.not. (th <= 6.8d+289))) then
        tmp = (-0.5d0) * t_1
    else
        tmp = t_1 * 0.25d0
    end if
    code = tmp
end function
public static double code(double a1, double a2, double th) {
	double t_1 = (a1 * a1) + (a2 * a2);
	double tmp;
	if (th <= 1.5e+80) {
		tmp = (a1 + a2) * (a1 + a2);
	} else if ((th <= 2.6e+254) || !(th <= 6.8e+289)) {
		tmp = -0.5 * t_1;
	} else {
		tmp = t_1 * 0.25;
	}
	return tmp;
}
def code(a1, a2, th):
	t_1 = (a1 * a1) + (a2 * a2)
	tmp = 0
	if th <= 1.5e+80:
		tmp = (a1 + a2) * (a1 + a2)
	elif (th <= 2.6e+254) or not (th <= 6.8e+289):
		tmp = -0.5 * t_1
	else:
		tmp = t_1 * 0.25
	return tmp
function code(a1, a2, th)
	t_1 = Float64(Float64(a1 * a1) + Float64(a2 * a2))
	tmp = 0.0
	if (th <= 1.5e+80)
		tmp = Float64(Float64(a1 + a2) * Float64(a1 + a2));
	elseif ((th <= 2.6e+254) || !(th <= 6.8e+289))
		tmp = Float64(-0.5 * t_1);
	else
		tmp = Float64(t_1 * 0.25);
	end
	return tmp
end
function tmp_2 = code(a1, a2, th)
	t_1 = (a1 * a1) + (a2 * a2);
	tmp = 0.0;
	if (th <= 1.5e+80)
		tmp = (a1 + a2) * (a1 + a2);
	elseif ((th <= 2.6e+254) || ~((th <= 6.8e+289)))
		tmp = -0.5 * t_1;
	else
		tmp = t_1 * 0.25;
	end
	tmp_2 = tmp;
end
code[a1_, a2_, th_] := Block[{t$95$1 = N[(N[(a1 * a1), $MachinePrecision] + N[(a2 * a2), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[th, 1.5e+80], N[(N[(a1 + a2), $MachinePrecision] * N[(a1 + a2), $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[th, 2.6e+254], N[Not[LessEqual[th, 6.8e+289]], $MachinePrecision]], N[(-0.5 * t$95$1), $MachinePrecision], N[(t$95$1 * 0.25), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := a1 \cdot a1 + a2 \cdot a2\\
\mathbf{if}\;th \leq 1.5 \cdot 10^{+80}:\\
\;\;\;\;\left(a1 + a2\right) \cdot \left(a1 + a2\right)\\

\mathbf{elif}\;th \leq 2.6 \cdot 10^{+254} \lor \neg \left(th \leq 6.8 \cdot 10^{+289}\right):\\
\;\;\;\;-0.5 \cdot t_1\\

\mathbf{else}:\\
\;\;\;\;t_1 \cdot 0.25\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if th < 1.49999999999999993e80

    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 72.9%

      \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. Applied egg-rr45.6%

      \[\leadsto \color{blue}{\left(a1 + a2\right) \cdot a1 + \left(a1 + a2\right) \cdot a2} \]
    6. Step-by-step derivation
      1. distribute-lft-out49.5%

        \[\leadsto \color{blue}{\left(a1 + a2\right) \cdot \left(a1 + a2\right)} \]
      2. +-commutative49.5%

        \[\leadsto \color{blue}{\left(a2 + a1\right)} \cdot \left(a1 + a2\right) \]
      3. +-commutative49.5%

        \[\leadsto \left(a2 + a1\right) \cdot \color{blue}{\left(a2 + a1\right)} \]
    7. Simplified49.5%

      \[\leadsto \color{blue}{\left(a2 + a1\right) \cdot \left(a2 + a1\right)} \]

    if 1.49999999999999993e80 < th < 2.6000000000000001e254 or 6.7999999999999997e289 < 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. Taylor expanded in th around 0 20.8%

      \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. Applied egg-rr57.2%

      \[\leadsto \color{blue}{-0.5} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]

    if 2.6000000000000001e254 < th < 6.7999999999999997e289

    1. Initial program 99.1%

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

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

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

      \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    5. Applied egg-rr48.6%

      \[\leadsto \color{blue}{0.25} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
  3. Recombined 3 regimes into one program.
  4. Final simplification50.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;th \leq 1.5 \cdot 10^{+80}:\\ \;\;\;\;\left(a1 + a2\right) \cdot \left(a1 + a2\right)\\ \mathbf{elif}\;th \leq 2.6 \cdot 10^{+254} \lor \neg \left(th \leq 6.8 \cdot 10^{+289}\right):\\ \;\;\;\;-0.5 \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)\\ \mathbf{else}:\\ \;\;\;\;\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot 0.25\\ \end{array} \]

Alternative 15: 46.9% accurate, 59.3× speedup?

\[\begin{array}{l} \\ \left(a1 + a2\right) \cdot \left(a1 + a2\right) \end{array} \]
(FPCore (a1 a2 th) :precision binary64 (* (+ a1 a2) (+ a1 a2)))
double code(double a1, double a2, double th) {
	return (a1 + a2) * (a1 + a2);
}
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    code = (a1 + a2) * (a1 + a2)
end function
public static double code(double a1, double a2, double th) {
	return (a1 + a2) * (a1 + a2);
}
def code(a1, a2, th):
	return (a1 + a2) * (a1 + a2)
function code(a1, a2, th)
	return Float64(Float64(a1 + a2) * Float64(a1 + a2))
end
function tmp = code(a1, a2, th)
	tmp = (a1 + a2) * (a1 + a2);
end
code[a1_, a2_, th_] := N[(N[(a1 + a2), $MachinePrecision] * N[(a1 + a2), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(a1 + a2\right) \cdot \left(a1 + a2\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 63.1%

    \[\leadsto \color{blue}{\frac{1}{\sqrt{2}}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
  5. Applied egg-rr41.3%

    \[\leadsto \color{blue}{\left(a1 + a2\right) \cdot a1 + \left(a1 + a2\right) \cdot a2} \]
  6. Step-by-step derivation
    1. distribute-lft-out44.4%

      \[\leadsto \color{blue}{\left(a1 + a2\right) \cdot \left(a1 + a2\right)} \]
    2. +-commutative44.4%

      \[\leadsto \color{blue}{\left(a2 + a1\right)} \cdot \left(a1 + a2\right) \]
    3. +-commutative44.4%

      \[\leadsto \left(a2 + a1\right) \cdot \color{blue}{\left(a2 + a1\right)} \]
  7. Simplified44.4%

    \[\leadsto \color{blue}{\left(a2 + a1\right) \cdot \left(a2 + a1\right)} \]
  8. Final simplification44.4%

    \[\leadsto \left(a1 + a2\right) \cdot \left(a1 + a2\right) \]

Alternative 16: 36.4% accurate, 82.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;a2 \leq 0.03:\\ \;\;\;\;a1 \cdot a1\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot a2\\ \end{array} \end{array} \]
(FPCore (a1 a2 th) :precision binary64 (if (<= a2 0.03) (* a1 a1) (* a2 a2)))
double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 0.03) {
		tmp = a1 * a1;
	} else {
		tmp = a2 * a2;
	}
	return tmp;
}
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 <= 0.03d0) then
        tmp = a1 * a1
    else
        tmp = a2 * a2
    end if
    code = tmp
end function
public static double code(double a1, double a2, double th) {
	double tmp;
	if (a2 <= 0.03) {
		tmp = a1 * a1;
	} else {
		tmp = a2 * a2;
	}
	return tmp;
}
def code(a1, a2, th):
	tmp = 0
	if a2 <= 0.03:
		tmp = a1 * a1
	else:
		tmp = a2 * a2
	return tmp
function code(a1, a2, th)
	tmp = 0.0
	if (a2 <= 0.03)
		tmp = Float64(a1 * a1);
	else
		tmp = Float64(a2 * a2);
	end
	return tmp
end
function tmp_2 = code(a1, a2, th)
	tmp = 0.0;
	if (a2 <= 0.03)
		tmp = a1 * a1;
	else
		tmp = a2 * a2;
	end
	tmp_2 = tmp;
end
code[a1_, a2_, th_] := If[LessEqual[a2, 0.03], N[(a1 * a1), $MachinePrecision], N[(a2 * a2), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;a2 \leq 0.03:\\
\;\;\;\;a1 \cdot a1\\

\mathbf{else}:\\
\;\;\;\;a2 \cdot a2\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if a2 < 0.029999999999999999

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

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

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

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

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

        \[\leadsto \frac{\color{blue}{a2 \cdot a2} + {a1}^{2}}{\sqrt{2}} \]
      2. unpow265.5%

        \[\leadsto \frac{a2 \cdot a2 + \color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
      3. +-commutative65.5%

        \[\leadsto \frac{\color{blue}{a1 \cdot a1 + a2 \cdot a2}}{\sqrt{2}} \]
    6. Simplified65.5%

      \[\leadsto \color{blue}{\frac{a1 \cdot a1 + a2 \cdot a2}{\sqrt{2}}} \]
    7. Taylor expanded in a1 around inf 46.3%

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

        \[\leadsto \frac{\color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
    9. Simplified46.3%

      \[\leadsto \color{blue}{\frac{a1 \cdot a1}{\sqrt{2}}} \]
    10. Applied egg-rr35.1%

      \[\leadsto \color{blue}{a1 \cdot a1} \]

    if 0.029999999999999999 < a2

    1. Initial program 99.7%

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

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

        \[\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 74.3%

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

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

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

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

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

      \[\leadsto \color{blue}{a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}} \]
    7. Taylor expanded in th around 0 42.4%

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

        \[\leadsto a2 \cdot \color{blue}{\frac{-a2}{-\sqrt{2}}} \]
      2. div-inv42.4%

        \[\leadsto a2 \cdot \color{blue}{\left(\left(-a2\right) \cdot \frac{1}{-\sqrt{2}}\right)} \]
    9. Applied egg-rr42.4%

      \[\leadsto a2 \cdot \color{blue}{\left(\left(-a2\right) \cdot \frac{1}{-\sqrt{2}}\right)} \]
    10. Applied egg-rr31.2%

      \[\leadsto a2 \cdot \color{blue}{\left(0 + a2\right)} \]
    11. Step-by-step derivation
      1. +-lft-identity31.2%

        \[\leadsto a2 \cdot \color{blue}{a2} \]
    12. Simplified31.2%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;a2 \leq 0.03:\\ \;\;\;\;a1 \cdot a1\\ \mathbf{else}:\\ \;\;\;\;a2 \cdot a2\\ \end{array} \]

Alternative 17: 3.7% accurate, 138.3× speedup?

\[\begin{array}{l} \\ a1 \cdot -2 \end{array} \]
(FPCore (a1 a2 th) :precision binary64 (* a1 -2.0))
double code(double a1, double a2, double th) {
	return a1 * -2.0;
}
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    code = a1 * (-2.0d0)
end function
public static double code(double a1, double a2, double th) {
	return a1 * -2.0;
}
def code(a1, a2, th):
	return a1 * -2.0
function code(a1, a2, th)
	return Float64(a1 * -2.0)
end
function tmp = code(a1, a2, th)
	tmp = a1 * -2.0;
end
code[a1_, a2_, th_] := N[(a1 * -2.0), $MachinePrecision]
\begin{array}{l}

\\
a1 \cdot -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.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 th around 0 63.1%

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

      \[\leadsto \frac{\color{blue}{a2 \cdot a2} + {a1}^{2}}{\sqrt{2}} \]
    2. unpow263.1%

      \[\leadsto \frac{a2 \cdot a2 + \color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
    3. +-commutative63.1%

      \[\leadsto \frac{\color{blue}{a1 \cdot a1 + a2 \cdot a2}}{\sqrt{2}} \]
  6. Simplified63.1%

    \[\leadsto \color{blue}{\frac{a1 \cdot a1 + a2 \cdot a2}{\sqrt{2}}} \]
  7. Taylor expanded in a1 around inf 40.2%

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

      \[\leadsto \frac{\color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
  9. Simplified40.2%

    \[\leadsto \color{blue}{\frac{a1 \cdot a1}{\sqrt{2}}} \]
  10. Applied egg-rr3.3%

    \[\leadsto \color{blue}{-2 \cdot a1} \]
  11. Step-by-step derivation
    1. *-commutative3.3%

      \[\leadsto \color{blue}{a1 \cdot -2} \]
  12. Simplified3.3%

    \[\leadsto \color{blue}{a1 \cdot -2} \]
  13. Final simplification3.3%

    \[\leadsto a1 \cdot -2 \]

Alternative 18: 30.5% accurate, 138.3× speedup?

\[\begin{array}{l} \\ a1 \cdot a1 \end{array} \]
(FPCore (a1 a2 th) :precision binary64 (* a1 a1))
double code(double a1, double a2, double th) {
	return a1 * a1;
}
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
end function
public static double code(double a1, double a2, double th) {
	return a1 * a1;
}
def code(a1, a2, th):
	return a1 * a1
function code(a1, a2, th)
	return Float64(a1 * a1)
end
function tmp = code(a1, a2, th)
	tmp = a1 * a1;
end
code[a1_, a2_, th_] := N[(a1 * a1), $MachinePrecision]
\begin{array}{l}

\\
a1 \cdot a1
\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.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 th around 0 63.1%

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

      \[\leadsto \frac{\color{blue}{a2 \cdot a2} + {a1}^{2}}{\sqrt{2}} \]
    2. unpow263.1%

      \[\leadsto \frac{a2 \cdot a2 + \color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
    3. +-commutative63.1%

      \[\leadsto \frac{\color{blue}{a1 \cdot a1 + a2 \cdot a2}}{\sqrt{2}} \]
  6. Simplified63.1%

    \[\leadsto \color{blue}{\frac{a1 \cdot a1 + a2 \cdot a2}{\sqrt{2}}} \]
  7. Taylor expanded in a1 around inf 40.2%

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

      \[\leadsto \frac{\color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
  9. Simplified40.2%

    \[\leadsto \color{blue}{\frac{a1 \cdot a1}{\sqrt{2}}} \]
  10. Applied egg-rr31.0%

    \[\leadsto \color{blue}{a1 \cdot a1} \]
  11. Final simplification31.0%

    \[\leadsto a1 \cdot a1 \]

Alternative 19: 3.7% accurate, 207.5× speedup?

\[\begin{array}{l} \\ -a1 \end{array} \]
(FPCore (a1 a2 th) :precision binary64 (- a1))
double code(double a1, double a2, double th) {
	return -a1;
}
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    code = -a1
end function
public static double code(double a1, double a2, double th) {
	return -a1;
}
def code(a1, a2, th):
	return -a1
function code(a1, a2, th)
	return Float64(-a1)
end
function tmp = code(a1, a2, th)
	tmp = -a1;
end
code[a1_, a2_, th_] := (-a1)
\begin{array}{l}

\\
-a1
\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.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 th around 0 63.1%

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

      \[\leadsto \frac{\color{blue}{a2 \cdot a2} + {a1}^{2}}{\sqrt{2}} \]
    2. unpow263.1%

      \[\leadsto \frac{a2 \cdot a2 + \color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
    3. +-commutative63.1%

      \[\leadsto \frac{\color{blue}{a1 \cdot a1 + a2 \cdot a2}}{\sqrt{2}} \]
  6. Simplified63.1%

    \[\leadsto \color{blue}{\frac{a1 \cdot a1 + a2 \cdot a2}{\sqrt{2}}} \]
  7. Taylor expanded in a1 around inf 40.2%

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

      \[\leadsto \frac{\color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
  9. Simplified40.2%

    \[\leadsto \color{blue}{\frac{a1 \cdot a1}{\sqrt{2}}} \]
  10. Applied egg-rr3.3%

    \[\leadsto \color{blue}{0 - a1} \]
  11. Step-by-step derivation
    1. neg-sub03.3%

      \[\leadsto \color{blue}{-a1} \]
  12. Simplified3.3%

    \[\leadsto \color{blue}{-a1} \]
  13. Final simplification3.3%

    \[\leadsto -a1 \]

Alternative 20: 3.6% accurate, 415.0× speedup?

\[\begin{array}{l} \\ a1 \end{array} \]
(FPCore (a1 a2 th) :precision binary64 a1)
double code(double a1, double a2, double th) {
	return a1;
}
real(8) function code(a1, a2, th)
    real(8), intent (in) :: a1
    real(8), intent (in) :: a2
    real(8), intent (in) :: th
    code = a1
end function
public static double code(double a1, double a2, double th) {
	return a1;
}
def code(a1, a2, th):
	return a1
function code(a1, a2, th)
	return a1
end
function tmp = code(a1, a2, th)
	tmp = a1;
end
code[a1_, a2_, th_] := a1
\begin{array}{l}

\\
a1
\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.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 th around 0 63.1%

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

      \[\leadsto \frac{\color{blue}{a2 \cdot a2} + {a1}^{2}}{\sqrt{2}} \]
    2. unpow263.1%

      \[\leadsto \frac{a2 \cdot a2 + \color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
    3. +-commutative63.1%

      \[\leadsto \frac{\color{blue}{a1 \cdot a1 + a2 \cdot a2}}{\sqrt{2}} \]
  6. Simplified63.1%

    \[\leadsto \color{blue}{\frac{a1 \cdot a1 + a2 \cdot a2}{\sqrt{2}}} \]
  7. Taylor expanded in a1 around inf 40.2%

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

      \[\leadsto \frac{\color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
  9. Simplified40.2%

    \[\leadsto \color{blue}{\frac{a1 \cdot a1}{\sqrt{2}}} \]
  10. Applied egg-rr4.7%

    \[\leadsto \color{blue}{\left|a1\right|} \]
  11. Step-by-step derivation
    1. unpow14.7%

      \[\leadsto \left|\color{blue}{{a1}^{1}}\right| \]
    2. metadata-eval4.7%

      \[\leadsto \left|{a1}^{\color{blue}{\left(\frac{2}{2}\right)}}\right| \]
    3. sqr-pow2.9%

      \[\leadsto \left|\color{blue}{{a1}^{\left(\frac{\frac{2}{2}}{2}\right)} \cdot {a1}^{\left(\frac{\frac{2}{2}}{2}\right)}}\right| \]
    4. fabs-sqr2.9%

      \[\leadsto \color{blue}{{a1}^{\left(\frac{\frac{2}{2}}{2}\right)} \cdot {a1}^{\left(\frac{\frac{2}{2}}{2}\right)}} \]
    5. sqr-pow3.9%

      \[\leadsto \color{blue}{{a1}^{\left(\frac{2}{2}\right)}} \]
    6. metadata-eval3.9%

      \[\leadsto {a1}^{\color{blue}{1}} \]
    7. unpow13.9%

      \[\leadsto \color{blue}{a1} \]
  12. Simplified3.9%

    \[\leadsto \color{blue}{a1} \]
  13. Final simplification3.9%

    \[\leadsto a1 \]

Reproduce

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