Migdal et al, Equation (64)

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

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

Initial Program: 99.5% accurate, 1.0× speedup?

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

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

Alternative 1: 99.6% accurate, 1.0× speedup?

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

\\
\frac{\mathsf{hypot}\left(a1, a2\right) \cdot \cos th}{\frac{\sqrt{2}}{\mathsf{hypot}\left(a1, a2\right)}}
\end{array}
Derivation
  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)} \]
    2. associate-*l/99.3%

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

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

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

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

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

      \[\leadsto \cos th \cdot \frac{\color{blue}{\sqrt{a1 \cdot a1 + a2 \cdot a2} \cdot \sqrt{a1 \cdot a1 + a2 \cdot a2}}}{\sqrt{2}} \]
    3. *-un-lft-identity99.3%

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

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

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

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

    \[\leadsto \cos th \cdot \color{blue}{\left(\frac{\mathsf{hypot}\left(a1, a2\right)}{1} \cdot \frac{\mathsf{hypot}\left(a1, a2\right)}{\sqrt{2}}\right)} \]
  6. Step-by-step derivation
    1. /-rgt-identity99.2%

      \[\leadsto \cos th \cdot \left(\color{blue}{\mathsf{hypot}\left(a1, a2\right)} \cdot \frac{\mathsf{hypot}\left(a1, a2\right)}{\sqrt{2}}\right) \]
    2. associate-*r*99.5%

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

      \[\leadsto \left(\cos th \cdot \mathsf{hypot}\left(a1, a2\right)\right) \cdot \color{blue}{\frac{1}{\frac{\sqrt{2}}{\mathsf{hypot}\left(a1, a2\right)}}} \]
    4. un-div-inv99.6%

      \[\leadsto \color{blue}{\frac{\cos th \cdot \mathsf{hypot}\left(a1, a2\right)}{\frac{\sqrt{2}}{\mathsf{hypot}\left(a1, a2\right)}}} \]
    5. *-commutative99.6%

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

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

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

Alternative 2: 99.6% accurate, 1.3× speedup?

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

\\
\cos th \cdot \frac{\mathsf{fma}\left(a1, a1, a2 \cdot a2\right)}{\sqrt{2}}
\end{array}
Derivation
  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)} \]
    2. associate-*l/99.3%

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

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

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

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

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

Alternative 3: 99.6% accurate, 1.3× speedup?

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

\\
\frac{\mathsf{fma}\left(a2, a2, a1 \cdot a1\right)}{\frac{\sqrt{2}}{\cos th}}
\end{array}
Derivation
  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)} \]
    2. associate-*l/99.3%

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

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

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

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

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

      \[\leadsto \cos th \cdot \frac{\color{blue}{\sqrt{a1 \cdot a1 + a2 \cdot a2} \cdot \sqrt{a1 \cdot a1 + a2 \cdot a2}}}{\sqrt{2}} \]
    3. *-un-lft-identity99.3%

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

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

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

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

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

    \[\leadsto \color{blue}{\frac{\left({a2}^{2} + {a1}^{2}\right) \cdot \cos th}{\sqrt{2}}} \]
  7. Step-by-step derivation
    1. associate-/l*99.3%

      \[\leadsto \color{blue}{\frac{{a2}^{2} + {a1}^{2}}{\frac{\sqrt{2}}{\cos th}}} \]
    2. unpow299.3%

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

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

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

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

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

Alternative 4: 99.5% accurate, 1.9× speedup?

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

\\
\left(\cos th \cdot \frac{-1}{-\sqrt{2}}\right) \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)
\end{array}
Derivation
  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. Step-by-step derivation
    1. frac-2neg99.2%

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

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

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

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

Alternative 5: 72.0% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\cos th \leq 0.65:\\
\;\;\;\;\cos th \cdot \left(a2 \cdot a2\right)\\

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


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

    1. Initial program 98.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-out98.8%

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

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

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

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

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

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

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

      \[\leadsto \cos th \cdot \color{blue}{\frac{a2 \cdot a2}{\sqrt{2}}} \]
    7. Applied egg-rr35.3%

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

    if 0.650000000000000022 < (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. frac-2neg99.5%

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

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

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

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

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

        \[\leadsto \frac{a2 \cdot a2 + \color{blue}{a1 \cdot a1}}{\sqrt{2}} \]
    8. Simplified91.2%

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

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

Alternative 6: 99.5% 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.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. Final simplification99.2%

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

Alternative 7: 52.8% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\cos th \leq -0.001:\\
\;\;\;\;\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot -0.5\\

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


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

    1. Initial program 98.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-out98.6%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
    3. Simplified98.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 4.9%

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

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

    if -1e-3 < (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)} \]
      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.6%

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

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

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

        \[\leadsto \color{blue}{a2 \cdot \left(a2 \cdot \frac{\cos th}{\sqrt{2}}\right)} \]
    6. Simplified60.5%

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

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

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

Alternative 8: 47.7% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\cos th \leq 0.65:\\
\;\;\;\;\cos th \cdot \left(a2 \cdot a2\right)\\

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


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

    1. Initial program 98.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-out98.8%

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

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

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

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

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

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

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

      \[\leadsto \cos th \cdot \color{blue}{\frac{a2 \cdot a2}{\sqrt{2}}} \]
    7. Applied egg-rr35.3%

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

    if 0.650000000000000022 < (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)} \]
      2. associate-*l/99.7%

        \[\leadsto \color{blue}{\frac{\cos th \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
      3. associate-*r/99.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 \color{blue}{\frac{{a2}^{2} \cdot \cos th}{\sqrt{2}}} \]
    5. Step-by-step derivation
      1. unpow260.4%

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

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

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

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

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

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

Alternative 9: 47.7% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\cos th \leq 0.65:\\
\;\;\;\;\cos th \cdot \left(a2 \cdot a2\right)\\

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


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

    1. Initial program 98.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-out98.8%

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

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

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

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

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

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

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

      \[\leadsto \cos th \cdot \color{blue}{\frac{a2 \cdot a2}{\sqrt{2}}} \]
    7. Applied egg-rr35.3%

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

    if 0.650000000000000022 < (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)} \]
      2. associate-*l/99.7%

        \[\leadsto \color{blue}{\frac{\cos th \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)}{\sqrt{2}}} \]
      3. associate-*r/99.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 \color{blue}{\frac{{a2}^{2} \cdot \cos th}{\sqrt{2}}} \]
    5. Step-by-step derivation
      1. unpow260.4%

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

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

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

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

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

        \[\leadsto \frac{\color{blue}{a2 \cdot a2}}{\sqrt{2}} \]
    9. Simplified56.2%

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

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

Alternative 10: 57.4% accurate, 2.0× speedup?

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

\\
a2 \cdot \left(a2 \cdot \frac{\cos th}{\sqrt{2}}\right)
\end{array}
Derivation
  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)} \]
    2. associate-*l/99.3%

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{a2 \cdot \left(a2 \cdot \frac{\cos th}{\sqrt{2}}\right)} \]
  7. Final simplification60.8%

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

Alternative 11: 57.4% accurate, 2.0× speedup?

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

\\
a2 \cdot \frac{a2}{\frac{\sqrt{2}}{\cos th}}
\end{array}
Derivation
  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)} \]
    2. associate-*l/99.3%

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{a2 \cdot \left(a2 \cdot \frac{\cos th}{\sqrt{2}}\right)} \]
  7. Step-by-step derivation
    1. clear-num60.8%

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

      \[\leadsto a2 \cdot \color{blue}{\frac{a2}{\frac{\sqrt{2}}{\cos th}}} \]
  8. Applied egg-rr60.8%

    \[\leadsto a2 \cdot \color{blue}{\frac{a2}{\frac{\sqrt{2}}{\cos th}}} \]
  9. Final simplification60.8%

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

Alternative 12: 57.4% accurate, 2.0× speedup?

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

\\
\frac{a2}{\frac{\frac{\sqrt{2}}{\cos th}}{a2}}
\end{array}
Derivation
  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)} \]
    2. associate-*l/99.3%

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

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

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

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

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

      \[\leadsto \cos th \cdot \frac{\color{blue}{\sqrt{a1 \cdot a1 + a2 \cdot a2} \cdot \sqrt{a1 \cdot a1 + a2 \cdot a2}}}{\sqrt{2}} \]
    3. *-un-lft-identity99.3%

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

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\frac{a2}{\frac{\frac{\sqrt{2}}{\cos th}}{a2}}} \]
  9. Final simplification60.8%

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

Alternative 13: 59.5% accurate, 3.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := a1 \cdot a1 + a2 \cdot a2\\ \mathbf{if}\;\cos th \leq -0.001:\\ \;\;\;\;t_1 \cdot -0.5\\ \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) -0.001) (* t_1 -0.5) (* t_1 0.5))))
double code(double a1, double a2, double th) {
	double t_1 = (a1 * a1) + (a2 * a2);
	double tmp;
	if (cos(th) <= -0.001) {
		tmp = t_1 * -0.5;
	} 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) <= (-0.001d0)) then
        tmp = t_1 * (-0.5d0)
    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) <= -0.001) {
		tmp = t_1 * -0.5;
	} 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) <= -0.001:
		tmp = t_1 * -0.5
	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) <= -0.001)
		tmp = Float64(t_1 * -0.5);
	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) <= -0.001)
		tmp = t_1 * -0.5;
	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], -0.001], N[(t$95$1 * -0.5), $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 -0.001:\\
\;\;\;\;t_1 \cdot -0.5\\

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


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

    1. Initial program 98.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-out98.6%

        \[\leadsto \color{blue}{\frac{\cos th}{\sqrt{2}} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right)} \]
    3. Simplified98.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 4.9%

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

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

    if -1e-3 < (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 85.0%

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

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

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

Alternative 14: 32.1% accurate, 27.3× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;th \leq 6 \cdot 10^{+43} \lor \neg \left(th \leq 1.72 \cdot 10^{+75}\right) \land th \leq 1.5 \cdot 10^{+129}:\\
\;\;\;\;a2 \cdot a2\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if th < 6.00000000000000033e43 or 1.72e75 < th < 1.50000000000000015e129

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

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

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

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

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

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

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

      \[\leadsto \cos th \cdot \color{blue}{\frac{a2 \cdot a2}{\sqrt{2}}} \]
    7. Applied egg-rr39.0%

      \[\leadsto \cos th \cdot \color{blue}{\left(a2 \cdot a2\right)} \]
    8. Taylor expanded in th around 0 30.2%

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

        \[\leadsto \color{blue}{a2 \cdot a2} \]
    10. Simplified30.2%

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

    if 6.00000000000000033e43 < th < 1.72e75 or 1.50000000000000015e129 < 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-rr30.3%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;th \leq 6 \cdot 10^{+43} \lor \neg \left(th \leq 1.72 \cdot 10^{+75}\right) \land th \leq 1.5 \cdot 10^{+129}:\\ \;\;\;\;a2 \cdot a2\\ \mathbf{else}:\\ \;\;\;\;\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot -0.5\\ \end{array} \]

Alternative 15: 32.5% accurate, 27.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := a1 \cdot a1 + a2 \cdot a2\\ \mathbf{if}\;th \leq 6 \cdot 10^{+43}:\\ \;\;\;\;a2 \cdot a2\\ \mathbf{elif}\;th \leq 1.72 \cdot 10^{+75} \lor \neg \left(th \leq 4.3 \cdot 10^{+130}\right):\\ \;\;\;\;t_1 \cdot -0.5\\ \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 6e+43)
     (* a2 a2)
     (if (or (<= th 1.72e+75) (not (<= th 4.3e+130)))
       (* t_1 -0.5)
       (* t_1 0.125)))))
double code(double a1, double a2, double th) {
	double t_1 = (a1 * a1) + (a2 * a2);
	double tmp;
	if (th <= 6e+43) {
		tmp = a2 * a2;
	} else if ((th <= 1.72e+75) || !(th <= 4.3e+130)) {
		tmp = t_1 * -0.5;
	} 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 <= 6d+43) then
        tmp = a2 * a2
    else if ((th <= 1.72d+75) .or. (.not. (th <= 4.3d+130))) then
        tmp = t_1 * (-0.5d0)
    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 <= 6e+43) {
		tmp = a2 * a2;
	} else if ((th <= 1.72e+75) || !(th <= 4.3e+130)) {
		tmp = t_1 * -0.5;
	} else {
		tmp = t_1 * 0.125;
	}
	return tmp;
}
def code(a1, a2, th):
	t_1 = (a1 * a1) + (a2 * a2)
	tmp = 0
	if th <= 6e+43:
		tmp = a2 * a2
	elif (th <= 1.72e+75) or not (th <= 4.3e+130):
		tmp = t_1 * -0.5
	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 <= 6e+43)
		tmp = Float64(a2 * a2);
	elseif ((th <= 1.72e+75) || !(th <= 4.3e+130))
		tmp = Float64(t_1 * -0.5);
	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 <= 6e+43)
		tmp = a2 * a2;
	elseif ((th <= 1.72e+75) || ~((th <= 4.3e+130)))
		tmp = t_1 * -0.5;
	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, 6e+43], N[(a2 * a2), $MachinePrecision], If[Or[LessEqual[th, 1.72e+75], N[Not[LessEqual[th, 4.3e+130]], $MachinePrecision]], N[(t$95$1 * -0.5), $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 6 \cdot 10^{+43}:\\
\;\;\;\;a2 \cdot a2\\

\mathbf{elif}\;th \leq 1.72 \cdot 10^{+75} \lor \neg \left(th \leq 4.3 \cdot 10^{+130}\right):\\
\;\;\;\;t_1 \cdot -0.5\\

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


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

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

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

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

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

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

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

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

      \[\leadsto \cos th \cdot \color{blue}{\frac{a2 \cdot a2}{\sqrt{2}}} \]
    7. Applied egg-rr38.9%

      \[\leadsto \cos th \cdot \color{blue}{\left(a2 \cdot a2\right)} \]
    8. Taylor expanded in th around 0 30.1%

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

        \[\leadsto \color{blue}{a2 \cdot a2} \]
    10. Simplified30.1%

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

    if 6.00000000000000033e43 < th < 1.72e75 or 4.29999999999999984e130 < 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 21.4%

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

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

    if 1.72e75 < th < 4.29999999999999984e130

    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)} \]
    3. Simplified99.7%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;th \leq 6 \cdot 10^{+43}:\\ \;\;\;\;a2 \cdot a2\\ \mathbf{elif}\;th \leq 1.72 \cdot 10^{+75} \lor \neg \left(th \leq 4.3 \cdot 10^{+130}\right):\\ \;\;\;\;\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot -0.5\\ \mathbf{else}:\\ \;\;\;\;\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot 0.125\\ \end{array} \]

Alternative 16: 32.5% accurate, 27.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := a1 \cdot a1 + a2 \cdot a2\\ \mathbf{if}\;th \leq 6 \cdot 10^{+43}:\\ \;\;\;\;a2 \cdot a2\\ \mathbf{elif}\;th \leq 1.72 \cdot 10^{+75} \lor \neg \left(th \leq 1.5 \cdot 10^{+129}\right):\\ \;\;\;\;t_1 \cdot -0.5\\ \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 6e+43)
     (* a2 a2)
     (if (or (<= th 1.72e+75) (not (<= th 1.5e+129)))
       (* t_1 -0.5)
       (* t_1 0.25)))))
double code(double a1, double a2, double th) {
	double t_1 = (a1 * a1) + (a2 * a2);
	double tmp;
	if (th <= 6e+43) {
		tmp = a2 * a2;
	} else if ((th <= 1.72e+75) || !(th <= 1.5e+129)) {
		tmp = t_1 * -0.5;
	} 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 <= 6d+43) then
        tmp = a2 * a2
    else if ((th <= 1.72d+75) .or. (.not. (th <= 1.5d+129))) then
        tmp = t_1 * (-0.5d0)
    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 <= 6e+43) {
		tmp = a2 * a2;
	} else if ((th <= 1.72e+75) || !(th <= 1.5e+129)) {
		tmp = t_1 * -0.5;
	} else {
		tmp = t_1 * 0.25;
	}
	return tmp;
}
def code(a1, a2, th):
	t_1 = (a1 * a1) + (a2 * a2)
	tmp = 0
	if th <= 6e+43:
		tmp = a2 * a2
	elif (th <= 1.72e+75) or not (th <= 1.5e+129):
		tmp = t_1 * -0.5
	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 <= 6e+43)
		tmp = Float64(a2 * a2);
	elseif ((th <= 1.72e+75) || !(th <= 1.5e+129))
		tmp = Float64(t_1 * -0.5);
	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 <= 6e+43)
		tmp = a2 * a2;
	elseif ((th <= 1.72e+75) || ~((th <= 1.5e+129)))
		tmp = t_1 * -0.5;
	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, 6e+43], N[(a2 * a2), $MachinePrecision], If[Or[LessEqual[th, 1.72e+75], N[Not[LessEqual[th, 1.5e+129]], $MachinePrecision]], N[(t$95$1 * -0.5), $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 6 \cdot 10^{+43}:\\
\;\;\;\;a2 \cdot a2\\

\mathbf{elif}\;th \leq 1.72 \cdot 10^{+75} \lor \neg \left(th \leq 1.5 \cdot 10^{+129}\right):\\
\;\;\;\;t_1 \cdot -0.5\\

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


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

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

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

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

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

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

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

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

      \[\leadsto \cos th \cdot \color{blue}{\frac{a2 \cdot a2}{\sqrt{2}}} \]
    7. Applied egg-rr38.9%

      \[\leadsto \cos th \cdot \color{blue}{\left(a2 \cdot a2\right)} \]
    8. Taylor expanded in th around 0 30.1%

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

        \[\leadsto \color{blue}{a2 \cdot a2} \]
    10. Simplified30.1%

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

    if 6.00000000000000033e43 < th < 1.72e75 or 1.50000000000000015e129 < 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-rr30.3%

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

    if 1.72e75 < th < 1.50000000000000015e129

    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)} \]
    3. Simplified99.7%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;th \leq 6 \cdot 10^{+43}:\\ \;\;\;\;a2 \cdot a2\\ \mathbf{elif}\;th \leq 1.72 \cdot 10^{+75} \lor \neg \left(th \leq 1.5 \cdot 10^{+129}\right):\\ \;\;\;\;\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot -0.5\\ \mathbf{else}:\\ \;\;\;\;\left(a1 \cdot a1 + a2 \cdot a2\right) \cdot 0.25\\ \end{array} \]

Alternative 17: 30.1% accurate, 37.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;th \leq 6 \cdot 10^{+43} \lor \neg \left(th \leq 1.72 \cdot 10^{+75}\right) \land th \leq 4.3 \cdot 10^{+130}:\\
\;\;\;\;a2 \cdot a2\\

\mathbf{else}:\\
\;\;\;\;\left(a2 \cdot a2\right) \cdot -0.25\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if th < 6.00000000000000033e43 or 1.72e75 < th < 4.29999999999999984e130

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

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

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

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

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

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

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

      \[\leadsto \cos th \cdot \color{blue}{\frac{a2 \cdot a2}{\sqrt{2}}} \]
    7. Applied egg-rr39.3%

      \[\leadsto \cos th \cdot \color{blue}{\left(a2 \cdot a2\right)} \]
    8. Taylor expanded in th around 0 30.1%

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

        \[\leadsto \color{blue}{a2 \cdot a2} \]
    10. Simplified30.1%

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

    if 6.00000000000000033e43 < th < 1.72e75 or 4.29999999999999984e130 < 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 21.4%

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

      \[\leadsto \color{blue}{-0.25} \cdot \left(a1 \cdot a1 + a2 \cdot a2\right) \]
    6. Taylor expanded in a1 around 0 14.0%

      \[\leadsto \color{blue}{-0.25 \cdot {a2}^{2}} \]
    7. Step-by-step derivation
      1. unpow214.0%

        \[\leadsto -0.25 \cdot \color{blue}{\left(a2 \cdot a2\right)} \]
    8. Simplified14.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;th \leq 6 \cdot 10^{+43} \lor \neg \left(th \leq 1.72 \cdot 10^{+75}\right) \land th \leq 4.3 \cdot 10^{+130}:\\ \;\;\;\;a2 \cdot a2\\ \mathbf{else}:\\ \;\;\;\;\left(a2 \cdot a2\right) \cdot -0.25\\ \end{array} \]

Alternative 18: 30.1% accurate, 138.3× speedup?

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

\\
a2 \cdot a2
\end{array}
Derivation
  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)} \]
    2. associate-*l/99.3%

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

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

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

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

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

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

    \[\leadsto \cos th \cdot \color{blue}{\frac{a2 \cdot a2}{\sqrt{2}}} \]
  7. Applied egg-rr37.4%

    \[\leadsto \cos th \cdot \color{blue}{\left(a2 \cdot a2\right)} \]
  8. Taylor expanded in th around 0 28.0%

    \[\leadsto \color{blue}{{a2}^{2}} \]
  9. Step-by-step derivation
    1. unpow228.0%

      \[\leadsto \color{blue}{a2 \cdot a2} \]
  10. Simplified28.0%

    \[\leadsto \color{blue}{a2 \cdot a2} \]
  11. Final simplification28.0%

    \[\leadsto a2 \cdot a2 \]

Reproduce

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