Migdal et al, Equation (64)

Percentage Accurate: 99.5% → 99.6%
Time: 10.9s
Alternatives: 17
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 17 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 99.5% accurate, 1.0× speedup?

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

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

Alternative 1: 99.6% accurate, 2.0× speedup?

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

\\
\left(\sqrt{0.5} \cdot \cos th\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)
\end{array}
Derivation
  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.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 inf 99.7%

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

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

Alternative 2: 79.0% accurate, 1.3× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\cos th \leq 0.01:\\
\;\;\;\;a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}\\

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


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

    1. Initial program 99.4%

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

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

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

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

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

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

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

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

    if 0.0100000000000000002 < (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.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 87.7%

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

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

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

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

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

Alternative 3: 79.3% accurate, 2.0× speedup?

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

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

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


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

    1. Initial program 99.4%

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

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

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

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

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

    if -0.0100000000000000002 < (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.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 87.4%

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

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

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

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

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

Alternative 4: 79.5% accurate, 2.0× speedup?

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

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

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


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

    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.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. Applied egg-rr52.2%

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

    if 0.70999999999999996 < (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.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 91.7%

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

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

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

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

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

Alternative 5: 67.3% accurate, 2.0× speedup?

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

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

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


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

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

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

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

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

    if -2.89999999999999986e-102 < a1

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

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

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

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

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

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

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

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

Alternative 6: 67.3% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;a1 \leq -7 \cdot 10^{-101}:\\
\;\;\;\;\sqrt{0.5} \cdot \left(\cos th \cdot \left(a1 \cdot a1\right)\right)\\

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


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

    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.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 a1 around inf 73.3%

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

        \[\leadsto \sqrt{0.5} \cdot \left(\color{blue}{\left(a1 \cdot a1\right)} \cdot \cos th\right) \]
    8. Simplified73.3%

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

    if -6.99999999999999989e-101 < a1

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

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

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

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

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

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

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

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

Alternative 7: 67.3% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;a1 \leq -7 \cdot 10^{-101}:\\
\;\;\;\;\sqrt{0.5} \cdot \left(\cos th \cdot \left(a1 \cdot a1\right)\right)\\

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


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

    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.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 a1 around inf 73.3%

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

        \[\leadsto \sqrt{0.5} \cdot \left(\color{blue}{\left(a1 \cdot a1\right)} \cdot \cos th\right) \]
    8. Simplified73.3%

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

    if -6.99999999999999989e-101 < a1

    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.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 a1 around 0 63.6%

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

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

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

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

Alternative 8: 67.2% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;a1 \leq -7 \cdot 10^{-101}:\\
\;\;\;\;\sqrt{0.5} \cdot \left(\cos th \cdot \left(a1 \cdot a1\right)\right)\\

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


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

    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.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 a1 around inf 73.3%

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

        \[\leadsto \sqrt{0.5} \cdot \left(\color{blue}{\left(a1 \cdot a1\right)} \cdot \cos th\right) \]
    8. Simplified73.3%

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

    if -6.99999999999999989e-101 < a1

    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.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. Step-by-step derivation
      1. fma-def99.6%

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

        \[\leadsto \cos th \cdot \color{blue}{\frac{1}{\frac{\sqrt{2}}{a1 \cdot a1 + a2 \cdot a2}}} \]
      3. un-div-inv99.6%

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

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

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

        \[\leadsto \frac{\cos th}{\frac{\sqrt{2}}{{\color{blue}{\left(\mathsf{hypot}\left(a1, a2\right)\right)}}^{2}}} \]
    5. Applied egg-rr99.6%

      \[\leadsto \color{blue}{\frac{\cos th}{\frac{\sqrt{2}}{{\left(\mathsf{hypot}\left(a1, a2\right)\right)}^{2}}}} \]
    6. Taylor expanded in a1 around 0 63.6%

      \[\leadsto \frac{\cos th}{\color{blue}{\frac{\sqrt{2}}{{a2}^{2}}}} \]
    7. Step-by-step derivation
      1. unpow263.6%

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

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

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

Alternative 9: 59.8% accurate, 3.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := a1 \cdot a1 + a2 \cdot a2\\ \mathbf{if}\;\cos th \leq -0.01:\\ \;\;\;\;t_1 \cdot -0.5\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot t_1\\ \end{array} \end{array} \]
(FPCore (a1 a2 th)
 :precision binary64
 (let* ((t_1 (+ (* a1 a1) (* a2 a2))))
   (if (<= (cos th) -0.01) (* t_1 -0.5) (* 0.5 t_1))))
double code(double a1, double a2, double th) {
	double t_1 = (a1 * a1) + (a2 * a2);
	double tmp;
	if (cos(th) <= -0.01) {
		tmp = t_1 * -0.5;
	} else {
		tmp = 0.5 * t_1;
	}
	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.01d0)) then
        tmp = t_1 * (-0.5d0)
    else
        tmp = 0.5d0 * t_1
    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.01) {
		tmp = t_1 * -0.5;
	} else {
		tmp = 0.5 * t_1;
	}
	return tmp;
}
def code(a1, a2, th):
	t_1 = (a1 * a1) + (a2 * a2)
	tmp = 0
	if math.cos(th) <= -0.01:
		tmp = t_1 * -0.5
	else:
		tmp = 0.5 * t_1
	return tmp
function code(a1, a2, th)
	t_1 = Float64(Float64(a1 * a1) + Float64(a2 * a2))
	tmp = 0.0
	if (cos(th) <= -0.01)
		tmp = Float64(t_1 * -0.5);
	else
		tmp = Float64(0.5 * t_1);
	end
	return tmp
end
function tmp_2 = code(a1, a2, th)
	t_1 = (a1 * a1) + (a2 * a2);
	tmp = 0.0;
	if (cos(th) <= -0.01)
		tmp = t_1 * -0.5;
	else
		tmp = 0.5 * t_1;
	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.01], N[(t$95$1 * -0.5), $MachinePrecision], N[(0.5 * t$95$1), $MachinePrecision]]]
\begin{array}{l}

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

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


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

    1. Initial program 99.4%

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

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

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

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

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

    if -0.0100000000000000002 < (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 87.3%

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

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

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

Alternative 10: 45.8% accurate, 3.8× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;a1 \leq -7.6 \cdot 10^{+124}:\\
\;\;\;\;\cos th \cdot \left(a1 \cdot a1\right)\\

\mathbf{elif}\;a1 \leq -1.95 \cdot 10^{-121}:\\
\;\;\;\;\frac{a1 \cdot a1}{\sqrt{2}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if a1 < -7.5999999999999997e124

    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)} \]
      2. associate-*l/99.9%

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

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

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

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

      \[\leadsto \color{blue}{\frac{a1 \cdot a1}{\frac{\sqrt{2}}{\cos th}}} \]
    7. Applied egg-rr91.1%

      \[\leadsto \color{blue}{a1 \cdot \left(\cos th \cdot a1\right)} \]
    8. Step-by-step derivation
      1. *-commutative91.1%

        \[\leadsto a1 \cdot \color{blue}{\left(a1 \cdot \cos th\right)} \]
      2. associate-*r*91.1%

        \[\leadsto \color{blue}{\left(a1 \cdot a1\right) \cdot \cos th} \]
    9. Simplified91.1%

      \[\leadsto \color{blue}{\left(a1 \cdot a1\right) \cdot \cos th} \]

    if -7.5999999999999997e124 < a1 < -1.95e-121

    1. Initial program 99.2%

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

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

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

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

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

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

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

    if -1.95e-121 < a1

    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.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. Applied egg-rr51.1%

      \[\leadsto \color{blue}{\left(0 + \cos th\right)} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    7. Taylor expanded in a1 around 0 36.0%

      \[\leadsto \color{blue}{\cos th \cdot {a2}^{2}} \]
    8. Step-by-step derivation
      1. unpow236.0%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;a1 \leq -7.6 \cdot 10^{+124}:\\ \;\;\;\;\cos th \cdot \left(a1 \cdot a1\right)\\ \mathbf{elif}\;a1 \leq -1.95 \cdot 10^{-121}:\\ \;\;\;\;\frac{a1 \cdot a1}{\sqrt{2}}\\ \mathbf{else}:\\ \;\;\;\;\cos th \cdot \left(a2 \cdot a2\right)\\ \end{array} \]

Alternative 11: 48.4% accurate, 3.8× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;a1 \leq -7.6 \cdot 10^{+124}:\\
\;\;\;\;\cos th \cdot \left(a1 \cdot a1\right)\\

\mathbf{elif}\;a1 \leq -1.35 \cdot 10^{-91}:\\
\;\;\;\;\frac{a1 \cdot a1}{\sqrt{2}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if a1 < -7.5999999999999997e124

    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)} \]
      2. associate-*l/99.9%

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

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

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

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

      \[\leadsto \color{blue}{\frac{a1 \cdot a1}{\frac{\sqrt{2}}{\cos th}}} \]
    7. Applied egg-rr91.1%

      \[\leadsto \color{blue}{a1 \cdot \left(\cos th \cdot a1\right)} \]
    8. Step-by-step derivation
      1. *-commutative91.1%

        \[\leadsto a1 \cdot \color{blue}{\left(a1 \cdot \cos th\right)} \]
      2. associate-*r*91.1%

        \[\leadsto \color{blue}{\left(a1 \cdot a1\right) \cdot \cos th} \]
    9. Simplified91.1%

      \[\leadsto \color{blue}{\left(a1 \cdot a1\right) \cdot \cos th} \]

    if -7.5999999999999997e124 < a1 < -1.3499999999999999e-91

    1. Initial program 99.2%

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

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

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

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

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

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

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

    if -1.3499999999999999e-91 < a1

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

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

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

        \[\leadsto \frac{\color{blue}{a2 \cdot a2}}{\sqrt{2}} \]
    7. Simplified43.5%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;a1 \leq -7.6 \cdot 10^{+124}:\\ \;\;\;\;\cos th \cdot \left(a1 \cdot a1\right)\\ \mathbf{elif}\;a1 \leq -1.35 \cdot 10^{-91}:\\ \;\;\;\;\frac{a1 \cdot a1}{\sqrt{2}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a2 \cdot a2}{\sqrt{2}}\\ \end{array} \]

Alternative 12: 31.1% accurate, 58.8× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;a2 \leq 1.8 \cdot 10^{+151}:\\
\;\;\;\;a1 \cdot a1\\

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


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

    1. Initial program 99.4%

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

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

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

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

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

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

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

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

    if 1.8e151 < 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)} \]
    3. Simplified100.0%

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

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

      \[\leadsto \color{blue}{a1 + \left(-a2\right) \cdot a2} \]
    6. Step-by-step derivation
      1. cancel-sign-sub-inv36.7%

        \[\leadsto \color{blue}{a1 - a2 \cdot a2} \]
    7. Simplified36.7%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;a2 \leq 1.8 \cdot 10^{+151}:\\ \;\;\;\;a1 \cdot a1\\ \mathbf{else}:\\ \;\;\;\;a1 - a2 \cdot a2\\ \end{array} \]

Alternative 13: 46.3% 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.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 63.6%

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

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

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

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

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

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

Alternative 14: 3.6% 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.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 63.6%

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

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

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

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

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

    \[\leadsto a1 \cdot -2 \]

Alternative 15: 30.3% 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.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 63.6%

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

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

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

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

    \[\leadsto \color{blue}{a1 \cdot a1} \]
  9. Final simplification28.1%

    \[\leadsto a1 \cdot a1 \]

Alternative 16: 3.6% 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.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 63.6%

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

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

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

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

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

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

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

    \[\leadsto -a1 \]

Alternative 17: 3.5% accurate, 415.0× speedup?

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

\\
1
\end{array}
Derivation
  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.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. Applied egg-rr3.6%

    \[\leadsto \color{blue}{\frac{\cos th \cdot a1 - \cos th \cdot a2}{\cos th \cdot a1 - \cos th \cdot a2}} \]
  7. Step-by-step derivation
    1. *-inverses3.6%

      \[\leadsto \color{blue}{1} \]
  8. Simplified3.6%

    \[\leadsto \color{blue}{1} \]
  9. Final simplification3.6%

    \[\leadsto 1 \]

Reproduce

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