Toniolo and Linder, Equation (3b), real

Percentage Accurate: 94.1% → 99.7%
Time: 7.8s
Alternatives: 13
Speedup: 1.2×

Specification

?
\[\begin{array}{l} \\ \frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \end{array} \]
(FPCore (kx ky th)
 :precision binary64
 (* (/ (sin ky) (sqrt (+ (pow (sin kx) 2.0) (pow (sin ky) 2.0)))) (sin th)))
double code(double kx, double ky, double th) {
	return (sin(ky) / sqrt((pow(sin(kx), 2.0) + pow(sin(ky), 2.0)))) * sin(th);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(kx, ky, th)
use fmin_fmax_functions
    real(8), intent (in) :: kx
    real(8), intent (in) :: ky
    real(8), intent (in) :: th
    code = (sin(ky) / sqrt(((sin(kx) ** 2.0d0) + (sin(ky) ** 2.0d0)))) * sin(th)
end function
public static double code(double kx, double ky, double th) {
	return (Math.sin(ky) / Math.sqrt((Math.pow(Math.sin(kx), 2.0) + Math.pow(Math.sin(ky), 2.0)))) * Math.sin(th);
}
def code(kx, ky, th):
	return (math.sin(ky) / math.sqrt((math.pow(math.sin(kx), 2.0) + math.pow(math.sin(ky), 2.0)))) * math.sin(th)
function code(kx, ky, th)
	return Float64(Float64(sin(ky) / sqrt(Float64((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)))) * sin(th))
end
function tmp = code(kx, ky, th)
	tmp = (sin(ky) / sqrt(((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)))) * sin(th);
end
code[kx_, ky_, th_] := N[(N[(N[Sin[ky], $MachinePrecision] / N[Sqrt[N[(N[Power[N[Sin[kx], $MachinePrecision], 2.0], $MachinePrecision] + N[Power[N[Sin[ky], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Sin[th], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th
\end{array}

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 13 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: 94.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \end{array} \]
(FPCore (kx ky th)
 :precision binary64
 (* (/ (sin ky) (sqrt (+ (pow (sin kx) 2.0) (pow (sin ky) 2.0)))) (sin th)))
double code(double kx, double ky, double th) {
	return (sin(ky) / sqrt((pow(sin(kx), 2.0) + pow(sin(ky), 2.0)))) * sin(th);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(kx, ky, th)
use fmin_fmax_functions
    real(8), intent (in) :: kx
    real(8), intent (in) :: ky
    real(8), intent (in) :: th
    code = (sin(ky) / sqrt(((sin(kx) ** 2.0d0) + (sin(ky) ** 2.0d0)))) * sin(th)
end function
public static double code(double kx, double ky, double th) {
	return (Math.sin(ky) / Math.sqrt((Math.pow(Math.sin(kx), 2.0) + Math.pow(Math.sin(ky), 2.0)))) * Math.sin(th);
}
def code(kx, ky, th):
	return (math.sin(ky) / math.sqrt((math.pow(math.sin(kx), 2.0) + math.pow(math.sin(ky), 2.0)))) * math.sin(th)
function code(kx, ky, th)
	return Float64(Float64(sin(ky) / sqrt(Float64((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)))) * sin(th))
end
function tmp = code(kx, ky, th)
	tmp = (sin(ky) / sqrt(((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)))) * sin(th);
end
code[kx_, ky_, th_] := N[(N[(N[Sin[ky], $MachinePrecision] / N[Sqrt[N[(N[Power[N[Sin[kx], $MachinePrecision], 2.0], $MachinePrecision] + N[Power[N[Sin[ky], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Sin[th], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th
\end{array}

Alternative 1: 99.7% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \frac{\sin ky}{\mathsf{hypot}\left(\sin ky, \sin kx\right)} \cdot \sin th \end{array} \]
(FPCore (kx ky th)
 :precision binary64
 (* (/ (sin ky) (hypot (sin ky) (sin kx))) (sin th)))
double code(double kx, double ky, double th) {
	return (sin(ky) / hypot(sin(ky), sin(kx))) * sin(th);
}
public static double code(double kx, double ky, double th) {
	return (Math.sin(ky) / Math.hypot(Math.sin(ky), Math.sin(kx))) * Math.sin(th);
}
def code(kx, ky, th):
	return (math.sin(ky) / math.hypot(math.sin(ky), math.sin(kx))) * math.sin(th)
function code(kx, ky, th)
	return Float64(Float64(sin(ky) / hypot(sin(ky), sin(kx))) * sin(th))
end
function tmp = code(kx, ky, th)
	tmp = (sin(ky) / hypot(sin(ky), sin(kx))) * sin(th);
end
code[kx_, ky_, th_] := N[(N[(N[Sin[ky], $MachinePrecision] / N[Sqrt[N[Sin[ky], $MachinePrecision] ^ 2 + N[Sin[kx], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision] * N[Sin[th], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\sin ky}{\mathsf{hypot}\left(\sin ky, \sin kx\right)} \cdot \sin th
\end{array}
Derivation
  1. Initial program 94.1%

    \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
  2. Applied rewrites99.7%

    \[\leadsto \frac{\sin ky}{\color{blue}{\mathsf{hypot}\left(\sin ky, \sin kx\right)}} \cdot \sin th \]
  3. Add Preprocessing

Alternative 2: 80.8% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := \frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}}\\ t_2 := \frac{\sin ky \cdot th}{\mathsf{hypot}\left(\sin ky, \sin kx\right)}\\ t_3 := \frac{ky}{\mathsf{hypot}\left(ky, \sin kx\right)} \cdot \sin th\\ \mathbf{if}\;t\_1 \leq -0.9995:\\ \;\;\;\;\frac{\sin ky}{\mathsf{hypot}\left(\sin ky, kx\right)} \cdot \sin th\\ \mathbf{elif}\;t\_1 \leq -0.05:\\ \;\;\;\;t\_2\\ \mathbf{elif}\;t\_1 \leq 0.1:\\ \;\;\;\;t\_3\\ \mathbf{elif}\;t\_1 \leq 0.98:\\ \;\;\;\;t\_2\\ \mathbf{else}:\\ \;\;\;\;t\_3\\ \end{array} \end{array} \]
(FPCore (kx ky th)
 :precision binary64
 (let* ((t_1 (/ (sin ky) (sqrt (+ (pow (sin kx) 2.0) (pow (sin ky) 2.0)))))
        (t_2 (/ (* (sin ky) th) (hypot (sin ky) (sin kx))))
        (t_3 (* (/ ky (hypot ky (sin kx))) (sin th))))
   (if (<= t_1 -0.9995)
     (* (/ (sin ky) (hypot (sin ky) kx)) (sin th))
     (if (<= t_1 -0.05)
       t_2
       (if (<= t_1 0.1) t_3 (if (<= t_1 0.98) t_2 t_3))))))
double code(double kx, double ky, double th) {
	double t_1 = sin(ky) / sqrt((pow(sin(kx), 2.0) + pow(sin(ky), 2.0)));
	double t_2 = (sin(ky) * th) / hypot(sin(ky), sin(kx));
	double t_3 = (ky / hypot(ky, sin(kx))) * sin(th);
	double tmp;
	if (t_1 <= -0.9995) {
		tmp = (sin(ky) / hypot(sin(ky), kx)) * sin(th);
	} else if (t_1 <= -0.05) {
		tmp = t_2;
	} else if (t_1 <= 0.1) {
		tmp = t_3;
	} else if (t_1 <= 0.98) {
		tmp = t_2;
	} else {
		tmp = t_3;
	}
	return tmp;
}
public static double code(double kx, double ky, double th) {
	double t_1 = Math.sin(ky) / Math.sqrt((Math.pow(Math.sin(kx), 2.0) + Math.pow(Math.sin(ky), 2.0)));
	double t_2 = (Math.sin(ky) * th) / Math.hypot(Math.sin(ky), Math.sin(kx));
	double t_3 = (ky / Math.hypot(ky, Math.sin(kx))) * Math.sin(th);
	double tmp;
	if (t_1 <= -0.9995) {
		tmp = (Math.sin(ky) / Math.hypot(Math.sin(ky), kx)) * Math.sin(th);
	} else if (t_1 <= -0.05) {
		tmp = t_2;
	} else if (t_1 <= 0.1) {
		tmp = t_3;
	} else if (t_1 <= 0.98) {
		tmp = t_2;
	} else {
		tmp = t_3;
	}
	return tmp;
}
def code(kx, ky, th):
	t_1 = math.sin(ky) / math.sqrt((math.pow(math.sin(kx), 2.0) + math.pow(math.sin(ky), 2.0)))
	t_2 = (math.sin(ky) * th) / math.hypot(math.sin(ky), math.sin(kx))
	t_3 = (ky / math.hypot(ky, math.sin(kx))) * math.sin(th)
	tmp = 0
	if t_1 <= -0.9995:
		tmp = (math.sin(ky) / math.hypot(math.sin(ky), kx)) * math.sin(th)
	elif t_1 <= -0.05:
		tmp = t_2
	elif t_1 <= 0.1:
		tmp = t_3
	elif t_1 <= 0.98:
		tmp = t_2
	else:
		tmp = t_3
	return tmp
function code(kx, ky, th)
	t_1 = Float64(sin(ky) / sqrt(Float64((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0))))
	t_2 = Float64(Float64(sin(ky) * th) / hypot(sin(ky), sin(kx)))
	t_3 = Float64(Float64(ky / hypot(ky, sin(kx))) * sin(th))
	tmp = 0.0
	if (t_1 <= -0.9995)
		tmp = Float64(Float64(sin(ky) / hypot(sin(ky), kx)) * sin(th));
	elseif (t_1 <= -0.05)
		tmp = t_2;
	elseif (t_1 <= 0.1)
		tmp = t_3;
	elseif (t_1 <= 0.98)
		tmp = t_2;
	else
		tmp = t_3;
	end
	return tmp
end
function tmp_2 = code(kx, ky, th)
	t_1 = sin(ky) / sqrt(((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)));
	t_2 = (sin(ky) * th) / hypot(sin(ky), sin(kx));
	t_3 = (ky / hypot(ky, sin(kx))) * sin(th);
	tmp = 0.0;
	if (t_1 <= -0.9995)
		tmp = (sin(ky) / hypot(sin(ky), kx)) * sin(th);
	elseif (t_1 <= -0.05)
		tmp = t_2;
	elseif (t_1 <= 0.1)
		tmp = t_3;
	elseif (t_1 <= 0.98)
		tmp = t_2;
	else
		tmp = t_3;
	end
	tmp_2 = tmp;
end
code[kx_, ky_, th_] := Block[{t$95$1 = N[(N[Sin[ky], $MachinePrecision] / N[Sqrt[N[(N[Power[N[Sin[kx], $MachinePrecision], 2.0], $MachinePrecision] + N[Power[N[Sin[ky], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[(N[Sin[ky], $MachinePrecision] * th), $MachinePrecision] / N[Sqrt[N[Sin[ky], $MachinePrecision] ^ 2 + N[Sin[kx], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$3 = N[(N[(ky / N[Sqrt[ky ^ 2 + N[Sin[kx], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision] * N[Sin[th], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, -0.9995], N[(N[(N[Sin[ky], $MachinePrecision] / N[Sqrt[N[Sin[ky], $MachinePrecision] ^ 2 + kx ^ 2], $MachinePrecision]), $MachinePrecision] * N[Sin[th], $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, -0.05], t$95$2, If[LessEqual[t$95$1, 0.1], t$95$3, If[LessEqual[t$95$1, 0.98], t$95$2, t$95$3]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := \frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}}\\
t_2 := \frac{\sin ky \cdot th}{\mathsf{hypot}\left(\sin ky, \sin kx\right)}\\
t_3 := \frac{ky}{\mathsf{hypot}\left(ky, \sin kx\right)} \cdot \sin th\\
\mathbf{if}\;t\_1 \leq -0.9995:\\
\;\;\;\;\frac{\sin ky}{\mathsf{hypot}\left(\sin ky, kx\right)} \cdot \sin th\\

\mathbf{elif}\;t\_1 \leq -0.05:\\
\;\;\;\;t\_2\\

\mathbf{elif}\;t\_1 \leq 0.1:\\
\;\;\;\;t\_3\\

\mathbf{elif}\;t\_1 \leq 0.98:\\
\;\;\;\;t\_2\\

\mathbf{else}:\\
\;\;\;\;t\_3\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64))))) < -0.99950000000000006

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Applied rewrites99.7%

      \[\leadsto \frac{\sin ky}{\color{blue}{\mathsf{hypot}\left(\sin ky, \sin kx\right)}} \cdot \sin th \]
    3. Taylor expanded in kx around 0

      \[\leadsto \frac{\sin ky}{\mathsf{hypot}\left(\sin ky, \color{blue}{kx}\right)} \cdot \sin th \]
    4. Applied rewrites58.7%

      \[\leadsto \frac{\sin ky}{\mathsf{hypot}\left(\sin ky, \color{blue}{kx}\right)} \cdot \sin th \]

    if -0.99950000000000006 < (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64))))) < -0.050000000000000003 or 0.10000000000000001 < (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64))))) < 0.97999999999999998

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Applied rewrites99.7%

      \[\leadsto \frac{\sin ky}{\color{blue}{\mathsf{hypot}\left(\sin ky, \sin kx\right)}} \cdot \sin th \]
    3. Applied rewrites96.4%

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\mathsf{hypot}\left(\sin ky, \sin kx\right)}} \]
    4. Taylor expanded in th around 0

      \[\leadsto \frac{\sin ky \cdot \color{blue}{th}}{\mathsf{hypot}\left(\sin ky, \sin kx\right)} \]
    5. Applied rewrites48.2%

      \[\leadsto \frac{\sin ky \cdot \color{blue}{th}}{\mathsf{hypot}\left(\sin ky, \sin kx\right)} \]

    if -0.050000000000000003 < (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64))))) < 0.10000000000000001 or 0.97999999999999998 < (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64)))))

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Applied rewrites99.7%

      \[\leadsto \frac{\sin ky}{\color{blue}{\mathsf{hypot}\left(\sin ky, \sin kx\right)}} \cdot \sin th \]
    3. Taylor expanded in ky around 0

      \[\leadsto \frac{\color{blue}{ky}}{\mathsf{hypot}\left(\sin ky, \sin kx\right)} \cdot \sin th \]
    4. Applied rewrites51.6%

      \[\leadsto \frac{\color{blue}{ky}}{\mathsf{hypot}\left(\sin ky, \sin kx\right)} \cdot \sin th \]
    5. Taylor expanded in ky around 0

      \[\leadsto \frac{ky}{\mathsf{hypot}\left(\color{blue}{ky}, \sin kx\right)} \cdot \sin th \]
    6. Applied rewrites65.8%

      \[\leadsto \frac{ky}{\mathsf{hypot}\left(\color{blue}{ky}, \sin kx\right)} \cdot \sin th \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 3: 79.8% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := \frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}}\\ t_2 := \frac{\sin ky \cdot th}{\mathsf{hypot}\left(\sin ky, \sin kx\right)}\\ t_3 := \frac{ky}{\mathsf{hypot}\left(ky, \sin kx\right)} \cdot \sin th\\ \mathbf{if}\;t\_1 \leq -0.9995:\\ \;\;\;\;\frac{\sin ky \cdot \sin th}{\mathsf{hypot}\left(\sin ky, kx\right)}\\ \mathbf{elif}\;t\_1 \leq -0.05:\\ \;\;\;\;t\_2\\ \mathbf{elif}\;t\_1 \leq 0.1:\\ \;\;\;\;t\_3\\ \mathbf{elif}\;t\_1 \leq 0.98:\\ \;\;\;\;t\_2\\ \mathbf{else}:\\ \;\;\;\;t\_3\\ \end{array} \end{array} \]
(FPCore (kx ky th)
 :precision binary64
 (let* ((t_1 (/ (sin ky) (sqrt (+ (pow (sin kx) 2.0) (pow (sin ky) 2.0)))))
        (t_2 (/ (* (sin ky) th) (hypot (sin ky) (sin kx))))
        (t_3 (* (/ ky (hypot ky (sin kx))) (sin th))))
   (if (<= t_1 -0.9995)
     (/ (* (sin ky) (sin th)) (hypot (sin ky) kx))
     (if (<= t_1 -0.05)
       t_2
       (if (<= t_1 0.1) t_3 (if (<= t_1 0.98) t_2 t_3))))))
double code(double kx, double ky, double th) {
	double t_1 = sin(ky) / sqrt((pow(sin(kx), 2.0) + pow(sin(ky), 2.0)));
	double t_2 = (sin(ky) * th) / hypot(sin(ky), sin(kx));
	double t_3 = (ky / hypot(ky, sin(kx))) * sin(th);
	double tmp;
	if (t_1 <= -0.9995) {
		tmp = (sin(ky) * sin(th)) / hypot(sin(ky), kx);
	} else if (t_1 <= -0.05) {
		tmp = t_2;
	} else if (t_1 <= 0.1) {
		tmp = t_3;
	} else if (t_1 <= 0.98) {
		tmp = t_2;
	} else {
		tmp = t_3;
	}
	return tmp;
}
public static double code(double kx, double ky, double th) {
	double t_1 = Math.sin(ky) / Math.sqrt((Math.pow(Math.sin(kx), 2.0) + Math.pow(Math.sin(ky), 2.0)));
	double t_2 = (Math.sin(ky) * th) / Math.hypot(Math.sin(ky), Math.sin(kx));
	double t_3 = (ky / Math.hypot(ky, Math.sin(kx))) * Math.sin(th);
	double tmp;
	if (t_1 <= -0.9995) {
		tmp = (Math.sin(ky) * Math.sin(th)) / Math.hypot(Math.sin(ky), kx);
	} else if (t_1 <= -0.05) {
		tmp = t_2;
	} else if (t_1 <= 0.1) {
		tmp = t_3;
	} else if (t_1 <= 0.98) {
		tmp = t_2;
	} else {
		tmp = t_3;
	}
	return tmp;
}
def code(kx, ky, th):
	t_1 = math.sin(ky) / math.sqrt((math.pow(math.sin(kx), 2.0) + math.pow(math.sin(ky), 2.0)))
	t_2 = (math.sin(ky) * th) / math.hypot(math.sin(ky), math.sin(kx))
	t_3 = (ky / math.hypot(ky, math.sin(kx))) * math.sin(th)
	tmp = 0
	if t_1 <= -0.9995:
		tmp = (math.sin(ky) * math.sin(th)) / math.hypot(math.sin(ky), kx)
	elif t_1 <= -0.05:
		tmp = t_2
	elif t_1 <= 0.1:
		tmp = t_3
	elif t_1 <= 0.98:
		tmp = t_2
	else:
		tmp = t_3
	return tmp
function code(kx, ky, th)
	t_1 = Float64(sin(ky) / sqrt(Float64((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0))))
	t_2 = Float64(Float64(sin(ky) * th) / hypot(sin(ky), sin(kx)))
	t_3 = Float64(Float64(ky / hypot(ky, sin(kx))) * sin(th))
	tmp = 0.0
	if (t_1 <= -0.9995)
		tmp = Float64(Float64(sin(ky) * sin(th)) / hypot(sin(ky), kx));
	elseif (t_1 <= -0.05)
		tmp = t_2;
	elseif (t_1 <= 0.1)
		tmp = t_3;
	elseif (t_1 <= 0.98)
		tmp = t_2;
	else
		tmp = t_3;
	end
	return tmp
end
function tmp_2 = code(kx, ky, th)
	t_1 = sin(ky) / sqrt(((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)));
	t_2 = (sin(ky) * th) / hypot(sin(ky), sin(kx));
	t_3 = (ky / hypot(ky, sin(kx))) * sin(th);
	tmp = 0.0;
	if (t_1 <= -0.9995)
		tmp = (sin(ky) * sin(th)) / hypot(sin(ky), kx);
	elseif (t_1 <= -0.05)
		tmp = t_2;
	elseif (t_1 <= 0.1)
		tmp = t_3;
	elseif (t_1 <= 0.98)
		tmp = t_2;
	else
		tmp = t_3;
	end
	tmp_2 = tmp;
end
code[kx_, ky_, th_] := Block[{t$95$1 = N[(N[Sin[ky], $MachinePrecision] / N[Sqrt[N[(N[Power[N[Sin[kx], $MachinePrecision], 2.0], $MachinePrecision] + N[Power[N[Sin[ky], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[(N[Sin[ky], $MachinePrecision] * th), $MachinePrecision] / N[Sqrt[N[Sin[ky], $MachinePrecision] ^ 2 + N[Sin[kx], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$3 = N[(N[(ky / N[Sqrt[ky ^ 2 + N[Sin[kx], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision] * N[Sin[th], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, -0.9995], N[(N[(N[Sin[ky], $MachinePrecision] * N[Sin[th], $MachinePrecision]), $MachinePrecision] / N[Sqrt[N[Sin[ky], $MachinePrecision] ^ 2 + kx ^ 2], $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, -0.05], t$95$2, If[LessEqual[t$95$1, 0.1], t$95$3, If[LessEqual[t$95$1, 0.98], t$95$2, t$95$3]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := \frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}}\\
t_2 := \frac{\sin ky \cdot th}{\mathsf{hypot}\left(\sin ky, \sin kx\right)}\\
t_3 := \frac{ky}{\mathsf{hypot}\left(ky, \sin kx\right)} \cdot \sin th\\
\mathbf{if}\;t\_1 \leq -0.9995:\\
\;\;\;\;\frac{\sin ky \cdot \sin th}{\mathsf{hypot}\left(\sin ky, kx\right)}\\

\mathbf{elif}\;t\_1 \leq -0.05:\\
\;\;\;\;t\_2\\

\mathbf{elif}\;t\_1 \leq 0.1:\\
\;\;\;\;t\_3\\

\mathbf{elif}\;t\_1 \leq 0.98:\\
\;\;\;\;t\_2\\

\mathbf{else}:\\
\;\;\;\;t\_3\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64))))) < -0.99950000000000006

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Applied rewrites99.7%

      \[\leadsto \frac{\sin ky}{\color{blue}{\mathsf{hypot}\left(\sin ky, \sin kx\right)}} \cdot \sin th \]
    3. Applied rewrites96.4%

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\mathsf{hypot}\left(\sin ky, \sin kx\right)}} \]
    4. Taylor expanded in kx around 0

      \[\leadsto \frac{\sin ky \cdot \sin th}{\mathsf{hypot}\left(\sin ky, \color{blue}{kx}\right)} \]
    5. Applied rewrites55.4%

      \[\leadsto \frac{\sin ky \cdot \sin th}{\mathsf{hypot}\left(\sin ky, \color{blue}{kx}\right)} \]

    if -0.99950000000000006 < (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64))))) < -0.050000000000000003 or 0.10000000000000001 < (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64))))) < 0.97999999999999998

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Applied rewrites99.7%

      \[\leadsto \frac{\sin ky}{\color{blue}{\mathsf{hypot}\left(\sin ky, \sin kx\right)}} \cdot \sin th \]
    3. Applied rewrites96.4%

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\mathsf{hypot}\left(\sin ky, \sin kx\right)}} \]
    4. Taylor expanded in th around 0

      \[\leadsto \frac{\sin ky \cdot \color{blue}{th}}{\mathsf{hypot}\left(\sin ky, \sin kx\right)} \]
    5. Applied rewrites48.2%

      \[\leadsto \frac{\sin ky \cdot \color{blue}{th}}{\mathsf{hypot}\left(\sin ky, \sin kx\right)} \]

    if -0.050000000000000003 < (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64))))) < 0.10000000000000001 or 0.97999999999999998 < (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64)))))

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Applied rewrites99.7%

      \[\leadsto \frac{\sin ky}{\color{blue}{\mathsf{hypot}\left(\sin ky, \sin kx\right)}} \cdot \sin th \]
    3. Taylor expanded in ky around 0

      \[\leadsto \frac{\color{blue}{ky}}{\mathsf{hypot}\left(\sin ky, \sin kx\right)} \cdot \sin th \]
    4. Applied rewrites51.6%

      \[\leadsto \frac{\color{blue}{ky}}{\mathsf{hypot}\left(\sin ky, \sin kx\right)} \cdot \sin th \]
    5. Taylor expanded in ky around 0

      \[\leadsto \frac{ky}{\mathsf{hypot}\left(\color{blue}{ky}, \sin kx\right)} \cdot \sin th \]
    6. Applied rewrites65.8%

      \[\leadsto \frac{ky}{\mathsf{hypot}\left(\color{blue}{ky}, \sin kx\right)} \cdot \sin th \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 4: 79.5% accurate, 0.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := \frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}}\\ t_2 := \frac{\sin ky \cdot th}{\mathsf{hypot}\left(\sin ky, \sin kx\right)}\\ t_3 := \frac{ky}{\mathsf{hypot}\left(ky, \sin kx\right)} \cdot \sin th\\ \mathbf{if}\;t\_1 \leq -0.9995:\\ \;\;\;\;\mathsf{copysign}\left(1, \sin ky\right) \cdot \sin th\\ \mathbf{elif}\;t\_1 \leq -0.05:\\ \;\;\;\;t\_2\\ \mathbf{elif}\;t\_1 \leq 0.1:\\ \;\;\;\;t\_3\\ \mathbf{elif}\;t\_1 \leq 0.98:\\ \;\;\;\;t\_2\\ \mathbf{else}:\\ \;\;\;\;t\_3\\ \end{array} \end{array} \]
(FPCore (kx ky th)
 :precision binary64
 (let* ((t_1 (/ (sin ky) (sqrt (+ (pow (sin kx) 2.0) (pow (sin ky) 2.0)))))
        (t_2 (/ (* (sin ky) th) (hypot (sin ky) (sin kx))))
        (t_3 (* (/ ky (hypot ky (sin kx))) (sin th))))
   (if (<= t_1 -0.9995)
     (* (copysign 1.0 (sin ky)) (sin th))
     (if (<= t_1 -0.05)
       t_2
       (if (<= t_1 0.1) t_3 (if (<= t_1 0.98) t_2 t_3))))))
double code(double kx, double ky, double th) {
	double t_1 = sin(ky) / sqrt((pow(sin(kx), 2.0) + pow(sin(ky), 2.0)));
	double t_2 = (sin(ky) * th) / hypot(sin(ky), sin(kx));
	double t_3 = (ky / hypot(ky, sin(kx))) * sin(th);
	double tmp;
	if (t_1 <= -0.9995) {
		tmp = copysign(1.0, sin(ky)) * sin(th);
	} else if (t_1 <= -0.05) {
		tmp = t_2;
	} else if (t_1 <= 0.1) {
		tmp = t_3;
	} else if (t_1 <= 0.98) {
		tmp = t_2;
	} else {
		tmp = t_3;
	}
	return tmp;
}
public static double code(double kx, double ky, double th) {
	double t_1 = Math.sin(ky) / Math.sqrt((Math.pow(Math.sin(kx), 2.0) + Math.pow(Math.sin(ky), 2.0)));
	double t_2 = (Math.sin(ky) * th) / Math.hypot(Math.sin(ky), Math.sin(kx));
	double t_3 = (ky / Math.hypot(ky, Math.sin(kx))) * Math.sin(th);
	double tmp;
	if (t_1 <= -0.9995) {
		tmp = Math.copySign(1.0, Math.sin(ky)) * Math.sin(th);
	} else if (t_1 <= -0.05) {
		tmp = t_2;
	} else if (t_1 <= 0.1) {
		tmp = t_3;
	} else if (t_1 <= 0.98) {
		tmp = t_2;
	} else {
		tmp = t_3;
	}
	return tmp;
}
def code(kx, ky, th):
	t_1 = math.sin(ky) / math.sqrt((math.pow(math.sin(kx), 2.0) + math.pow(math.sin(ky), 2.0)))
	t_2 = (math.sin(ky) * th) / math.hypot(math.sin(ky), math.sin(kx))
	t_3 = (ky / math.hypot(ky, math.sin(kx))) * math.sin(th)
	tmp = 0
	if t_1 <= -0.9995:
		tmp = math.copysign(1.0, math.sin(ky)) * math.sin(th)
	elif t_1 <= -0.05:
		tmp = t_2
	elif t_1 <= 0.1:
		tmp = t_3
	elif t_1 <= 0.98:
		tmp = t_2
	else:
		tmp = t_3
	return tmp
function code(kx, ky, th)
	t_1 = Float64(sin(ky) / sqrt(Float64((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0))))
	t_2 = Float64(Float64(sin(ky) * th) / hypot(sin(ky), sin(kx)))
	t_3 = Float64(Float64(ky / hypot(ky, sin(kx))) * sin(th))
	tmp = 0.0
	if (t_1 <= -0.9995)
		tmp = Float64(copysign(1.0, sin(ky)) * sin(th));
	elseif (t_1 <= -0.05)
		tmp = t_2;
	elseif (t_1 <= 0.1)
		tmp = t_3;
	elseif (t_1 <= 0.98)
		tmp = t_2;
	else
		tmp = t_3;
	end
	return tmp
end
function tmp_2 = code(kx, ky, th)
	t_1 = sin(ky) / sqrt(((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)));
	t_2 = (sin(ky) * th) / hypot(sin(ky), sin(kx));
	t_3 = (ky / hypot(ky, sin(kx))) * sin(th);
	tmp = 0.0;
	if (t_1 <= -0.9995)
		tmp = (sign(sin(ky)) * abs(1.0)) * sin(th);
	elseif (t_1 <= -0.05)
		tmp = t_2;
	elseif (t_1 <= 0.1)
		tmp = t_3;
	elseif (t_1 <= 0.98)
		tmp = t_2;
	else
		tmp = t_3;
	end
	tmp_2 = tmp;
end
code[kx_, ky_, th_] := Block[{t$95$1 = N[(N[Sin[ky], $MachinePrecision] / N[Sqrt[N[(N[Power[N[Sin[kx], $MachinePrecision], 2.0], $MachinePrecision] + N[Power[N[Sin[ky], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[(N[Sin[ky], $MachinePrecision] * th), $MachinePrecision] / N[Sqrt[N[Sin[ky], $MachinePrecision] ^ 2 + N[Sin[kx], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$3 = N[(N[(ky / N[Sqrt[ky ^ 2 + N[Sin[kx], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision] * N[Sin[th], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, -0.9995], N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[N[Sin[ky], $MachinePrecision]]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * N[Sin[th], $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, -0.05], t$95$2, If[LessEqual[t$95$1, 0.1], t$95$3, If[LessEqual[t$95$1, 0.98], t$95$2, t$95$3]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := \frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}}\\
t_2 := \frac{\sin ky \cdot th}{\mathsf{hypot}\left(\sin ky, \sin kx\right)}\\
t_3 := \frac{ky}{\mathsf{hypot}\left(ky, \sin kx\right)} \cdot \sin th\\
\mathbf{if}\;t\_1 \leq -0.9995:\\
\;\;\;\;\mathsf{copysign}\left(1, \sin ky\right) \cdot \sin th\\

\mathbf{elif}\;t\_1 \leq -0.05:\\
\;\;\;\;t\_2\\

\mathbf{elif}\;t\_1 \leq 0.1:\\
\;\;\;\;t\_3\\

\mathbf{elif}\;t\_1 \leq 0.98:\\
\;\;\;\;t\_2\\

\mathbf{else}:\\
\;\;\;\;t\_3\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64))))) < -0.99950000000000006

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Taylor expanded in kx around 0

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
    3. Applied rewrites41.8%

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
    4. Applied rewrites31.2%

      \[\leadsto \left(\sin ky \cdot \sin th\right) \cdot \color{blue}{\frac{1}{\sqrt{0.5 - 0.5 \cdot \cos \left(2 \cdot ky\right)}}} \]
    5. Applied rewrites44.4%

      \[\leadsto \color{blue}{\mathsf{copysign}\left(1, \sin ky\right) \cdot \sin th} \]

    if -0.99950000000000006 < (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64))))) < -0.050000000000000003 or 0.10000000000000001 < (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64))))) < 0.97999999999999998

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Applied rewrites99.7%

      \[\leadsto \frac{\sin ky}{\color{blue}{\mathsf{hypot}\left(\sin ky, \sin kx\right)}} \cdot \sin th \]
    3. Applied rewrites96.4%

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\mathsf{hypot}\left(\sin ky, \sin kx\right)}} \]
    4. Taylor expanded in th around 0

      \[\leadsto \frac{\sin ky \cdot \color{blue}{th}}{\mathsf{hypot}\left(\sin ky, \sin kx\right)} \]
    5. Applied rewrites48.2%

      \[\leadsto \frac{\sin ky \cdot \color{blue}{th}}{\mathsf{hypot}\left(\sin ky, \sin kx\right)} \]

    if -0.050000000000000003 < (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64))))) < 0.10000000000000001 or 0.97999999999999998 < (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64)))))

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Applied rewrites99.7%

      \[\leadsto \frac{\sin ky}{\color{blue}{\mathsf{hypot}\left(\sin ky, \sin kx\right)}} \cdot \sin th \]
    3. Taylor expanded in ky around 0

      \[\leadsto \frac{\color{blue}{ky}}{\mathsf{hypot}\left(\sin ky, \sin kx\right)} \cdot \sin th \]
    4. Applied rewrites51.6%

      \[\leadsto \frac{\color{blue}{ky}}{\mathsf{hypot}\left(\sin ky, \sin kx\right)} \cdot \sin th \]
    5. Taylor expanded in ky around 0

      \[\leadsto \frac{ky}{\mathsf{hypot}\left(\color{blue}{ky}, \sin kx\right)} \cdot \sin th \]
    6. Applied rewrites65.8%

      \[\leadsto \frac{ky}{\mathsf{hypot}\left(\color{blue}{ky}, \sin kx\right)} \cdot \sin th \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 5: 77.0% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;th \leq 7.5 \cdot 10^{-5}:\\ \;\;\;\;\frac{\sin ky}{\mathsf{hypot}\left(\sin ky, \sin kx\right)} \cdot th\\ \mathbf{else}:\\ \;\;\;\;\frac{ky}{\mathsf{hypot}\left(ky, \sin kx\right)} \cdot \sin th\\ \end{array} \end{array} \]
(FPCore (kx ky th)
 :precision binary64
 (if (<= th 7.5e-5)
   (* (/ (sin ky) (hypot (sin ky) (sin kx))) th)
   (* (/ ky (hypot ky (sin kx))) (sin th))))
double code(double kx, double ky, double th) {
	double tmp;
	if (th <= 7.5e-5) {
		tmp = (sin(ky) / hypot(sin(ky), sin(kx))) * th;
	} else {
		tmp = (ky / hypot(ky, sin(kx))) * sin(th);
	}
	return tmp;
}
public static double code(double kx, double ky, double th) {
	double tmp;
	if (th <= 7.5e-5) {
		tmp = (Math.sin(ky) / Math.hypot(Math.sin(ky), Math.sin(kx))) * th;
	} else {
		tmp = (ky / Math.hypot(ky, Math.sin(kx))) * Math.sin(th);
	}
	return tmp;
}
def code(kx, ky, th):
	tmp = 0
	if th <= 7.5e-5:
		tmp = (math.sin(ky) / math.hypot(math.sin(ky), math.sin(kx))) * th
	else:
		tmp = (ky / math.hypot(ky, math.sin(kx))) * math.sin(th)
	return tmp
function code(kx, ky, th)
	tmp = 0.0
	if (th <= 7.5e-5)
		tmp = Float64(Float64(sin(ky) / hypot(sin(ky), sin(kx))) * th);
	else
		tmp = Float64(Float64(ky / hypot(ky, sin(kx))) * sin(th));
	end
	return tmp
end
function tmp_2 = code(kx, ky, th)
	tmp = 0.0;
	if (th <= 7.5e-5)
		tmp = (sin(ky) / hypot(sin(ky), sin(kx))) * th;
	else
		tmp = (ky / hypot(ky, sin(kx))) * sin(th);
	end
	tmp_2 = tmp;
end
code[kx_, ky_, th_] := If[LessEqual[th, 7.5e-5], N[(N[(N[Sin[ky], $MachinePrecision] / N[Sqrt[N[Sin[ky], $MachinePrecision] ^ 2 + N[Sin[kx], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision] * th), $MachinePrecision], N[(N[(ky / N[Sqrt[ky ^ 2 + N[Sin[kx], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision] * N[Sin[th], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;th \leq 7.5 \cdot 10^{-5}:\\
\;\;\;\;\frac{\sin ky}{\mathsf{hypot}\left(\sin ky, \sin kx\right)} \cdot th\\

\mathbf{else}:\\
\;\;\;\;\frac{ky}{\mathsf{hypot}\left(ky, \sin kx\right)} \cdot \sin th\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if th < 7.49999999999999934e-5

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Taylor expanded in th around 0

      \[\leadsto \frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \color{blue}{th} \]
    3. Applied rewrites48.9%

      \[\leadsto \frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \color{blue}{th} \]
    4. Applied rewrites51.5%

      \[\leadsto \color{blue}{\frac{\sin ky}{\mathsf{hypot}\left(\sin ky, \sin kx\right)} \cdot th} \]

    if 7.49999999999999934e-5 < th

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Applied rewrites99.7%

      \[\leadsto \frac{\sin ky}{\color{blue}{\mathsf{hypot}\left(\sin ky, \sin kx\right)}} \cdot \sin th \]
    3. Taylor expanded in ky around 0

      \[\leadsto \frac{\color{blue}{ky}}{\mathsf{hypot}\left(\sin ky, \sin kx\right)} \cdot \sin th \]
    4. Applied rewrites51.6%

      \[\leadsto \frac{\color{blue}{ky}}{\mathsf{hypot}\left(\sin ky, \sin kx\right)} \cdot \sin th \]
    5. Taylor expanded in ky around 0

      \[\leadsto \frac{ky}{\mathsf{hypot}\left(\color{blue}{ky}, \sin kx\right)} \cdot \sin th \]
    6. Applied rewrites65.8%

      \[\leadsto \frac{ky}{\mathsf{hypot}\left(\color{blue}{ky}, \sin kx\right)} \cdot \sin th \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 6: 71.7% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \leq -0.05:\\ \;\;\;\;\mathsf{copysign}\left(1, \sin ky\right) \cdot \sin th\\ \mathbf{else}:\\ \;\;\;\;\frac{ky}{\mathsf{hypot}\left(ky, \sin kx\right)} \cdot \sin th\\ \end{array} \end{array} \]
(FPCore (kx ky th)
 :precision binary64
 (if (<= (/ (sin ky) (sqrt (+ (pow (sin kx) 2.0) (pow (sin ky) 2.0)))) -0.05)
   (* (copysign 1.0 (sin ky)) (sin th))
   (* (/ ky (hypot ky (sin kx))) (sin th))))
double code(double kx, double ky, double th) {
	double tmp;
	if ((sin(ky) / sqrt((pow(sin(kx), 2.0) + pow(sin(ky), 2.0)))) <= -0.05) {
		tmp = copysign(1.0, sin(ky)) * sin(th);
	} else {
		tmp = (ky / hypot(ky, sin(kx))) * sin(th);
	}
	return tmp;
}
public static double code(double kx, double ky, double th) {
	double tmp;
	if ((Math.sin(ky) / Math.sqrt((Math.pow(Math.sin(kx), 2.0) + Math.pow(Math.sin(ky), 2.0)))) <= -0.05) {
		tmp = Math.copySign(1.0, Math.sin(ky)) * Math.sin(th);
	} else {
		tmp = (ky / Math.hypot(ky, Math.sin(kx))) * Math.sin(th);
	}
	return tmp;
}
def code(kx, ky, th):
	tmp = 0
	if (math.sin(ky) / math.sqrt((math.pow(math.sin(kx), 2.0) + math.pow(math.sin(ky), 2.0)))) <= -0.05:
		tmp = math.copysign(1.0, math.sin(ky)) * math.sin(th)
	else:
		tmp = (ky / math.hypot(ky, math.sin(kx))) * math.sin(th)
	return tmp
function code(kx, ky, th)
	tmp = 0.0
	if (Float64(sin(ky) / sqrt(Float64((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)))) <= -0.05)
		tmp = Float64(copysign(1.0, sin(ky)) * sin(th));
	else
		tmp = Float64(Float64(ky / hypot(ky, sin(kx))) * sin(th));
	end
	return tmp
end
function tmp_2 = code(kx, ky, th)
	tmp = 0.0;
	if ((sin(ky) / sqrt(((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)))) <= -0.05)
		tmp = (sign(sin(ky)) * abs(1.0)) * sin(th);
	else
		tmp = (ky / hypot(ky, sin(kx))) * sin(th);
	end
	tmp_2 = tmp;
end
code[kx_, ky_, th_] := If[LessEqual[N[(N[Sin[ky], $MachinePrecision] / N[Sqrt[N[(N[Power[N[Sin[kx], $MachinePrecision], 2.0], $MachinePrecision] + N[Power[N[Sin[ky], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -0.05], N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[N[Sin[ky], $MachinePrecision]]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * N[Sin[th], $MachinePrecision]), $MachinePrecision], N[(N[(ky / N[Sqrt[ky ^ 2 + N[Sin[kx], $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision] * N[Sin[th], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \leq -0.05:\\
\;\;\;\;\mathsf{copysign}\left(1, \sin ky\right) \cdot \sin th\\

\mathbf{else}:\\
\;\;\;\;\frac{ky}{\mathsf{hypot}\left(ky, \sin kx\right)} \cdot \sin th\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64))))) < -0.050000000000000003

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Taylor expanded in kx around 0

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
    3. Applied rewrites41.8%

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
    4. Applied rewrites31.2%

      \[\leadsto \left(\sin ky \cdot \sin th\right) \cdot \color{blue}{\frac{1}{\sqrt{0.5 - 0.5 \cdot \cos \left(2 \cdot ky\right)}}} \]
    5. Applied rewrites44.4%

      \[\leadsto \color{blue}{\mathsf{copysign}\left(1, \sin ky\right) \cdot \sin th} \]

    if -0.050000000000000003 < (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64)))))

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Applied rewrites99.7%

      \[\leadsto \frac{\sin ky}{\color{blue}{\mathsf{hypot}\left(\sin ky, \sin kx\right)}} \cdot \sin th \]
    3. Taylor expanded in ky around 0

      \[\leadsto \frac{\color{blue}{ky}}{\mathsf{hypot}\left(\sin ky, \sin kx\right)} \cdot \sin th \]
    4. Applied rewrites51.6%

      \[\leadsto \frac{\color{blue}{ky}}{\mathsf{hypot}\left(\sin ky, \sin kx\right)} \cdot \sin th \]
    5. Taylor expanded in ky around 0

      \[\leadsto \frac{ky}{\mathsf{hypot}\left(\color{blue}{ky}, \sin kx\right)} \cdot \sin th \]
    6. Applied rewrites65.8%

      \[\leadsto \frac{ky}{\mathsf{hypot}\left(\color{blue}{ky}, \sin kx\right)} \cdot \sin th \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 7: 66.9% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := \frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}}\\ \mathbf{if}\;t\_1 \leq -0.05:\\ \;\;\;\;\mathsf{copysign}\left(1, \sin ky\right) \cdot \sin th\\ \mathbf{elif}\;t\_1 \leq 0.1:\\ \;\;\;\;\frac{ky}{\left|\sin kx\right|} \cdot \sin th\\ \mathbf{else}:\\ \;\;\;\;\sin th\\ \end{array} \end{array} \]
(FPCore (kx ky th)
 :precision binary64
 (let* ((t_1 (/ (sin ky) (sqrt (+ (pow (sin kx) 2.0) (pow (sin ky) 2.0))))))
   (if (<= t_1 -0.05)
     (* (copysign 1.0 (sin ky)) (sin th))
     (if (<= t_1 0.1) (* (/ ky (fabs (sin kx))) (sin th)) (sin th)))))
double code(double kx, double ky, double th) {
	double t_1 = sin(ky) / sqrt((pow(sin(kx), 2.0) + pow(sin(ky), 2.0)));
	double tmp;
	if (t_1 <= -0.05) {
		tmp = copysign(1.0, sin(ky)) * sin(th);
	} else if (t_1 <= 0.1) {
		tmp = (ky / fabs(sin(kx))) * sin(th);
	} else {
		tmp = sin(th);
	}
	return tmp;
}
public static double code(double kx, double ky, double th) {
	double t_1 = Math.sin(ky) / Math.sqrt((Math.pow(Math.sin(kx), 2.0) + Math.pow(Math.sin(ky), 2.0)));
	double tmp;
	if (t_1 <= -0.05) {
		tmp = Math.copySign(1.0, Math.sin(ky)) * Math.sin(th);
	} else if (t_1 <= 0.1) {
		tmp = (ky / Math.abs(Math.sin(kx))) * Math.sin(th);
	} else {
		tmp = Math.sin(th);
	}
	return tmp;
}
def code(kx, ky, th):
	t_1 = math.sin(ky) / math.sqrt((math.pow(math.sin(kx), 2.0) + math.pow(math.sin(ky), 2.0)))
	tmp = 0
	if t_1 <= -0.05:
		tmp = math.copysign(1.0, math.sin(ky)) * math.sin(th)
	elif t_1 <= 0.1:
		tmp = (ky / math.fabs(math.sin(kx))) * math.sin(th)
	else:
		tmp = math.sin(th)
	return tmp
function code(kx, ky, th)
	t_1 = Float64(sin(ky) / sqrt(Float64((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0))))
	tmp = 0.0
	if (t_1 <= -0.05)
		tmp = Float64(copysign(1.0, sin(ky)) * sin(th));
	elseif (t_1 <= 0.1)
		tmp = Float64(Float64(ky / abs(sin(kx))) * sin(th));
	else
		tmp = sin(th);
	end
	return tmp
end
function tmp_2 = code(kx, ky, th)
	t_1 = sin(ky) / sqrt(((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)));
	tmp = 0.0;
	if (t_1 <= -0.05)
		tmp = (sign(sin(ky)) * abs(1.0)) * sin(th);
	elseif (t_1 <= 0.1)
		tmp = (ky / abs(sin(kx))) * sin(th);
	else
		tmp = sin(th);
	end
	tmp_2 = tmp;
end
code[kx_, ky_, th_] := Block[{t$95$1 = N[(N[Sin[ky], $MachinePrecision] / N[Sqrt[N[(N[Power[N[Sin[kx], $MachinePrecision], 2.0], $MachinePrecision] + N[Power[N[Sin[ky], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, -0.05], N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[N[Sin[ky], $MachinePrecision]]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * N[Sin[th], $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, 0.1], N[(N[(ky / N[Abs[N[Sin[kx], $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Sin[th], $MachinePrecision]), $MachinePrecision], N[Sin[th], $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := \frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}}\\
\mathbf{if}\;t\_1 \leq -0.05:\\
\;\;\;\;\mathsf{copysign}\left(1, \sin ky\right) \cdot \sin th\\

\mathbf{elif}\;t\_1 \leq 0.1:\\
\;\;\;\;\frac{ky}{\left|\sin kx\right|} \cdot \sin th\\

\mathbf{else}:\\
\;\;\;\;\sin th\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64))))) < -0.050000000000000003

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Taylor expanded in kx around 0

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
    3. Applied rewrites41.8%

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
    4. Applied rewrites31.2%

      \[\leadsto \left(\sin ky \cdot \sin th\right) \cdot \color{blue}{\frac{1}{\sqrt{0.5 - 0.5 \cdot \cos \left(2 \cdot ky\right)}}} \]
    5. Applied rewrites44.4%

      \[\leadsto \color{blue}{\mathsf{copysign}\left(1, \sin ky\right) \cdot \sin th} \]

    if -0.050000000000000003 < (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64))))) < 0.10000000000000001

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Taylor expanded in ky around 0

      \[\leadsto \color{blue}{\frac{ky}{\sqrt{{\sin kx}^{2}}}} \cdot \sin th \]
    3. Applied rewrites36.8%

      \[\leadsto \color{blue}{\frac{ky}{\sqrt{{\sin kx}^{2}}}} \cdot \sin th \]
    4. Applied rewrites27.6%

      \[\leadsto \color{blue}{\frac{ky}{\sqrt{0.5 - 0.5 \cdot \cos \left(2 \cdot kx\right)}} \cdot \sin th} \]
    5. Applied rewrites40.0%

      \[\leadsto \color{blue}{\frac{ky}{\left|\sin kx\right|} \cdot \sin th} \]

    if 0.10000000000000001 < (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64)))))

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Taylor expanded in kx around 0

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
    3. Applied rewrites41.8%

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
    4. Taylor expanded in ky around 0

      \[\leadsto \sin th \]
    5. Applied rewrites23.7%

      \[\leadsto \sin th \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 8: 60.1% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := {\sin kx}^{2}\\ t_2 := \frac{\sin ky}{\sqrt{t\_1 + {\sin ky}^{2}}}\\ \mathbf{if}\;t\_2 \leq -0.05:\\ \;\;\;\;\mathsf{copysign}\left(1, \sin ky\right) \cdot \sin th\\ \mathbf{elif}\;t\_2 \leq 2 \cdot 10^{-12}:\\ \;\;\;\;\frac{ky}{\sqrt{t\_1}} \cdot th\\ \mathbf{else}:\\ \;\;\;\;\sin th\\ \end{array} \end{array} \]
(FPCore (kx ky th)
 :precision binary64
 (let* ((t_1 (pow (sin kx) 2.0))
        (t_2 (/ (sin ky) (sqrt (+ t_1 (pow (sin ky) 2.0))))))
   (if (<= t_2 -0.05)
     (* (copysign 1.0 (sin ky)) (sin th))
     (if (<= t_2 2e-12) (* (/ ky (sqrt t_1)) th) (sin th)))))
double code(double kx, double ky, double th) {
	double t_1 = pow(sin(kx), 2.0);
	double t_2 = sin(ky) / sqrt((t_1 + pow(sin(ky), 2.0)));
	double tmp;
	if (t_2 <= -0.05) {
		tmp = copysign(1.0, sin(ky)) * sin(th);
	} else if (t_2 <= 2e-12) {
		tmp = (ky / sqrt(t_1)) * th;
	} else {
		tmp = sin(th);
	}
	return tmp;
}
public static double code(double kx, double ky, double th) {
	double t_1 = Math.pow(Math.sin(kx), 2.0);
	double t_2 = Math.sin(ky) / Math.sqrt((t_1 + Math.pow(Math.sin(ky), 2.0)));
	double tmp;
	if (t_2 <= -0.05) {
		tmp = Math.copySign(1.0, Math.sin(ky)) * Math.sin(th);
	} else if (t_2 <= 2e-12) {
		tmp = (ky / Math.sqrt(t_1)) * th;
	} else {
		tmp = Math.sin(th);
	}
	return tmp;
}
def code(kx, ky, th):
	t_1 = math.pow(math.sin(kx), 2.0)
	t_2 = math.sin(ky) / math.sqrt((t_1 + math.pow(math.sin(ky), 2.0)))
	tmp = 0
	if t_2 <= -0.05:
		tmp = math.copysign(1.0, math.sin(ky)) * math.sin(th)
	elif t_2 <= 2e-12:
		tmp = (ky / math.sqrt(t_1)) * th
	else:
		tmp = math.sin(th)
	return tmp
function code(kx, ky, th)
	t_1 = sin(kx) ^ 2.0
	t_2 = Float64(sin(ky) / sqrt(Float64(t_1 + (sin(ky) ^ 2.0))))
	tmp = 0.0
	if (t_2 <= -0.05)
		tmp = Float64(copysign(1.0, sin(ky)) * sin(th));
	elseif (t_2 <= 2e-12)
		tmp = Float64(Float64(ky / sqrt(t_1)) * th);
	else
		tmp = sin(th);
	end
	return tmp
end
function tmp_2 = code(kx, ky, th)
	t_1 = sin(kx) ^ 2.0;
	t_2 = sin(ky) / sqrt((t_1 + (sin(ky) ^ 2.0)));
	tmp = 0.0;
	if (t_2 <= -0.05)
		tmp = (sign(sin(ky)) * abs(1.0)) * sin(th);
	elseif (t_2 <= 2e-12)
		tmp = (ky / sqrt(t_1)) * th;
	else
		tmp = sin(th);
	end
	tmp_2 = tmp;
end
code[kx_, ky_, th_] := Block[{t$95$1 = N[Power[N[Sin[kx], $MachinePrecision], 2.0], $MachinePrecision]}, Block[{t$95$2 = N[(N[Sin[ky], $MachinePrecision] / N[Sqrt[N[(t$95$1 + N[Power[N[Sin[ky], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$2, -0.05], N[(N[With[{TMP1 = Abs[1.0], TMP2 = Sign[N[Sin[ky], $MachinePrecision]]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision] * N[Sin[th], $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$2, 2e-12], N[(N[(ky / N[Sqrt[t$95$1], $MachinePrecision]), $MachinePrecision] * th), $MachinePrecision], N[Sin[th], $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := {\sin kx}^{2}\\
t_2 := \frac{\sin ky}{\sqrt{t\_1 + {\sin ky}^{2}}}\\
\mathbf{if}\;t\_2 \leq -0.05:\\
\;\;\;\;\mathsf{copysign}\left(1, \sin ky\right) \cdot \sin th\\

\mathbf{elif}\;t\_2 \leq 2 \cdot 10^{-12}:\\
\;\;\;\;\frac{ky}{\sqrt{t\_1}} \cdot th\\

\mathbf{else}:\\
\;\;\;\;\sin th\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64))))) < -0.050000000000000003

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Taylor expanded in kx around 0

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
    3. Applied rewrites41.8%

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
    4. Applied rewrites31.2%

      \[\leadsto \left(\sin ky \cdot \sin th\right) \cdot \color{blue}{\frac{1}{\sqrt{0.5 - 0.5 \cdot \cos \left(2 \cdot ky\right)}}} \]
    5. Applied rewrites44.4%

      \[\leadsto \color{blue}{\mathsf{copysign}\left(1, \sin ky\right) \cdot \sin th} \]

    if -0.050000000000000003 < (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64))))) < 1.99999999999999996e-12

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Taylor expanded in th around 0

      \[\leadsto \frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \color{blue}{th} \]
    3. Applied rewrites48.9%

      \[\leadsto \frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \color{blue}{th} \]
    4. Taylor expanded in ky around 0

      \[\leadsto \frac{\color{blue}{ky}}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot th \]
    5. Applied rewrites24.5%

      \[\leadsto \frac{\color{blue}{ky}}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot th \]
    6. Taylor expanded in ky around 0

      \[\leadsto \frac{ky}{\sqrt{{\sin kx}^{2} + {\color{blue}{ky}}^{2}}} \cdot th \]
    7. Applied rewrites28.6%

      \[\leadsto \frac{ky}{\sqrt{{\sin kx}^{2} + {\color{blue}{ky}}^{2}}} \cdot th \]
    8. Taylor expanded in ky around 0

      \[\leadsto \color{blue}{\frac{ky}{\sqrt{{\sin kx}^{2}}}} \cdot th \]
    9. Applied rewrites19.8%

      \[\leadsto \color{blue}{\frac{ky}{\sqrt{{\sin kx}^{2}}}} \cdot th \]

    if 1.99999999999999996e-12 < (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64)))))

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Taylor expanded in kx around 0

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
    3. Applied rewrites41.8%

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
    4. Taylor expanded in ky around 0

      \[\leadsto \sin th \]
    5. Applied rewrites23.7%

      \[\leadsto \sin th \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 9: 39.8% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_1 := {\sin kx}^{2}\\ \mathbf{if}\;\frac{\sin ky}{\sqrt{t\_1 + {\sin ky}^{2}}} \leq 2 \cdot 10^{-12}:\\ \;\;\;\;\frac{ky}{\sqrt{t\_1}} \cdot th\\ \mathbf{else}:\\ \;\;\;\;\sin th\\ \end{array} \end{array} \]
(FPCore (kx ky th)
 :precision binary64
 (let* ((t_1 (pow (sin kx) 2.0)))
   (if (<= (/ (sin ky) (sqrt (+ t_1 (pow (sin ky) 2.0)))) 2e-12)
     (* (/ ky (sqrt t_1)) th)
     (sin th))))
double code(double kx, double ky, double th) {
	double t_1 = pow(sin(kx), 2.0);
	double tmp;
	if ((sin(ky) / sqrt((t_1 + pow(sin(ky), 2.0)))) <= 2e-12) {
		tmp = (ky / sqrt(t_1)) * th;
	} else {
		tmp = sin(th);
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(kx, ky, th)
use fmin_fmax_functions
    real(8), intent (in) :: kx
    real(8), intent (in) :: ky
    real(8), intent (in) :: th
    real(8) :: t_1
    real(8) :: tmp
    t_1 = sin(kx) ** 2.0d0
    if ((sin(ky) / sqrt((t_1 + (sin(ky) ** 2.0d0)))) <= 2d-12) then
        tmp = (ky / sqrt(t_1)) * th
    else
        tmp = sin(th)
    end if
    code = tmp
end function
public static double code(double kx, double ky, double th) {
	double t_1 = Math.pow(Math.sin(kx), 2.0);
	double tmp;
	if ((Math.sin(ky) / Math.sqrt((t_1 + Math.pow(Math.sin(ky), 2.0)))) <= 2e-12) {
		tmp = (ky / Math.sqrt(t_1)) * th;
	} else {
		tmp = Math.sin(th);
	}
	return tmp;
}
def code(kx, ky, th):
	t_1 = math.pow(math.sin(kx), 2.0)
	tmp = 0
	if (math.sin(ky) / math.sqrt((t_1 + math.pow(math.sin(ky), 2.0)))) <= 2e-12:
		tmp = (ky / math.sqrt(t_1)) * th
	else:
		tmp = math.sin(th)
	return tmp
function code(kx, ky, th)
	t_1 = sin(kx) ^ 2.0
	tmp = 0.0
	if (Float64(sin(ky) / sqrt(Float64(t_1 + (sin(ky) ^ 2.0)))) <= 2e-12)
		tmp = Float64(Float64(ky / sqrt(t_1)) * th);
	else
		tmp = sin(th);
	end
	return tmp
end
function tmp_2 = code(kx, ky, th)
	t_1 = sin(kx) ^ 2.0;
	tmp = 0.0;
	if ((sin(ky) / sqrt((t_1 + (sin(ky) ^ 2.0)))) <= 2e-12)
		tmp = (ky / sqrt(t_1)) * th;
	else
		tmp = sin(th);
	end
	tmp_2 = tmp;
end
code[kx_, ky_, th_] := Block[{t$95$1 = N[Power[N[Sin[kx], $MachinePrecision], 2.0], $MachinePrecision]}, If[LessEqual[N[(N[Sin[ky], $MachinePrecision] / N[Sqrt[N[(t$95$1 + N[Power[N[Sin[ky], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], 2e-12], N[(N[(ky / N[Sqrt[t$95$1], $MachinePrecision]), $MachinePrecision] * th), $MachinePrecision], N[Sin[th], $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_1 := {\sin kx}^{2}\\
\mathbf{if}\;\frac{\sin ky}{\sqrt{t\_1 + {\sin ky}^{2}}} \leq 2 \cdot 10^{-12}:\\
\;\;\;\;\frac{ky}{\sqrt{t\_1}} \cdot th\\

\mathbf{else}:\\
\;\;\;\;\sin th\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64))))) < 1.99999999999999996e-12

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Taylor expanded in th around 0

      \[\leadsto \frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \color{blue}{th} \]
    3. Applied rewrites48.9%

      \[\leadsto \frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \color{blue}{th} \]
    4. Taylor expanded in ky around 0

      \[\leadsto \frac{\color{blue}{ky}}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot th \]
    5. Applied rewrites24.5%

      \[\leadsto \frac{\color{blue}{ky}}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot th \]
    6. Taylor expanded in ky around 0

      \[\leadsto \frac{ky}{\sqrt{{\sin kx}^{2} + {\color{blue}{ky}}^{2}}} \cdot th \]
    7. Applied rewrites28.6%

      \[\leadsto \frac{ky}{\sqrt{{\sin kx}^{2} + {\color{blue}{ky}}^{2}}} \cdot th \]
    8. Taylor expanded in ky around 0

      \[\leadsto \color{blue}{\frac{ky}{\sqrt{{\sin kx}^{2}}}} \cdot th \]
    9. Applied rewrites19.8%

      \[\leadsto \color{blue}{\frac{ky}{\sqrt{{\sin kx}^{2}}}} \cdot th \]

    if 1.99999999999999996e-12 < (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64)))))

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Taylor expanded in kx around 0

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
    3. Applied rewrites41.8%

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
    4. Taylor expanded in ky around 0

      \[\leadsto \sin th \]
    5. Applied rewrites23.7%

      \[\leadsto \sin th \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 10: 39.1% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \leq 0.1:\\ \;\;\;\;\frac{ky}{\sqrt{{kx}^{2}}} \cdot \sin th\\ \mathbf{else}:\\ \;\;\;\;\sin th\\ \end{array} \end{array} \]
(FPCore (kx ky th)
 :precision binary64
 (if (<= (/ (sin ky) (sqrt (+ (pow (sin kx) 2.0) (pow (sin ky) 2.0)))) 0.1)
   (* (/ ky (sqrt (pow kx 2.0))) (sin th))
   (sin th)))
double code(double kx, double ky, double th) {
	double tmp;
	if ((sin(ky) / sqrt((pow(sin(kx), 2.0) + pow(sin(ky), 2.0)))) <= 0.1) {
		tmp = (ky / sqrt(pow(kx, 2.0))) * sin(th);
	} else {
		tmp = sin(th);
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(kx, ky, th)
use fmin_fmax_functions
    real(8), intent (in) :: kx
    real(8), intent (in) :: ky
    real(8), intent (in) :: th
    real(8) :: tmp
    if ((sin(ky) / sqrt(((sin(kx) ** 2.0d0) + (sin(ky) ** 2.0d0)))) <= 0.1d0) then
        tmp = (ky / sqrt((kx ** 2.0d0))) * sin(th)
    else
        tmp = sin(th)
    end if
    code = tmp
end function
public static double code(double kx, double ky, double th) {
	double tmp;
	if ((Math.sin(ky) / Math.sqrt((Math.pow(Math.sin(kx), 2.0) + Math.pow(Math.sin(ky), 2.0)))) <= 0.1) {
		tmp = (ky / Math.sqrt(Math.pow(kx, 2.0))) * Math.sin(th);
	} else {
		tmp = Math.sin(th);
	}
	return tmp;
}
def code(kx, ky, th):
	tmp = 0
	if (math.sin(ky) / math.sqrt((math.pow(math.sin(kx), 2.0) + math.pow(math.sin(ky), 2.0)))) <= 0.1:
		tmp = (ky / math.sqrt(math.pow(kx, 2.0))) * math.sin(th)
	else:
		tmp = math.sin(th)
	return tmp
function code(kx, ky, th)
	tmp = 0.0
	if (Float64(sin(ky) / sqrt(Float64((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)))) <= 0.1)
		tmp = Float64(Float64(ky / sqrt((kx ^ 2.0))) * sin(th));
	else
		tmp = sin(th);
	end
	return tmp
end
function tmp_2 = code(kx, ky, th)
	tmp = 0.0;
	if ((sin(ky) / sqrt(((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)))) <= 0.1)
		tmp = (ky / sqrt((kx ^ 2.0))) * sin(th);
	else
		tmp = sin(th);
	end
	tmp_2 = tmp;
end
code[kx_, ky_, th_] := If[LessEqual[N[(N[Sin[ky], $MachinePrecision] / N[Sqrt[N[(N[Power[N[Sin[kx], $MachinePrecision], 2.0], $MachinePrecision] + N[Power[N[Sin[ky], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], 0.1], N[(N[(ky / N[Sqrt[N[Power[kx, 2.0], $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Sin[th], $MachinePrecision]), $MachinePrecision], N[Sin[th], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \leq 0.1:\\
\;\;\;\;\frac{ky}{\sqrt{{kx}^{2}}} \cdot \sin th\\

\mathbf{else}:\\
\;\;\;\;\sin th\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64))))) < 0.10000000000000001

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Taylor expanded in ky around 0

      \[\leadsto \color{blue}{\frac{ky}{\sqrt{{\sin kx}^{2}}}} \cdot \sin th \]
    3. Applied rewrites36.8%

      \[\leadsto \color{blue}{\frac{ky}{\sqrt{{\sin kx}^{2}}}} \cdot \sin th \]
    4. Taylor expanded in kx around 0

      \[\leadsto \frac{ky}{\sqrt{{kx}^{2}}} \cdot \sin th \]
    5. Applied rewrites19.1%

      \[\leadsto \frac{ky}{\sqrt{{kx}^{2}}} \cdot \sin th \]

    if 0.10000000000000001 < (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64)))))

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Taylor expanded in kx around 0

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
    3. Applied rewrites41.8%

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
    4. Taylor expanded in ky around 0

      \[\leadsto \sin th \]
    5. Applied rewrites23.7%

      \[\leadsto \sin th \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 11: 36.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \leq 0.1:\\ \;\;\;\;\frac{ky}{kx} \cdot \sin th\\ \mathbf{else}:\\ \;\;\;\;\sin th\\ \end{array} \end{array} \]
(FPCore (kx ky th)
 :precision binary64
 (if (<= (/ (sin ky) (sqrt (+ (pow (sin kx) 2.0) (pow (sin ky) 2.0)))) 0.1)
   (* (/ ky kx) (sin th))
   (sin th)))
double code(double kx, double ky, double th) {
	double tmp;
	if ((sin(ky) / sqrt((pow(sin(kx), 2.0) + pow(sin(ky), 2.0)))) <= 0.1) {
		tmp = (ky / kx) * sin(th);
	} else {
		tmp = sin(th);
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(kx, ky, th)
use fmin_fmax_functions
    real(8), intent (in) :: kx
    real(8), intent (in) :: ky
    real(8), intent (in) :: th
    real(8) :: tmp
    if ((sin(ky) / sqrt(((sin(kx) ** 2.0d0) + (sin(ky) ** 2.0d0)))) <= 0.1d0) then
        tmp = (ky / kx) * sin(th)
    else
        tmp = sin(th)
    end if
    code = tmp
end function
public static double code(double kx, double ky, double th) {
	double tmp;
	if ((Math.sin(ky) / Math.sqrt((Math.pow(Math.sin(kx), 2.0) + Math.pow(Math.sin(ky), 2.0)))) <= 0.1) {
		tmp = (ky / kx) * Math.sin(th);
	} else {
		tmp = Math.sin(th);
	}
	return tmp;
}
def code(kx, ky, th):
	tmp = 0
	if (math.sin(ky) / math.sqrt((math.pow(math.sin(kx), 2.0) + math.pow(math.sin(ky), 2.0)))) <= 0.1:
		tmp = (ky / kx) * math.sin(th)
	else:
		tmp = math.sin(th)
	return tmp
function code(kx, ky, th)
	tmp = 0.0
	if (Float64(sin(ky) / sqrt(Float64((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)))) <= 0.1)
		tmp = Float64(Float64(ky / kx) * sin(th));
	else
		tmp = sin(th);
	end
	return tmp
end
function tmp_2 = code(kx, ky, th)
	tmp = 0.0;
	if ((sin(ky) / sqrt(((sin(kx) ^ 2.0) + (sin(ky) ^ 2.0)))) <= 0.1)
		tmp = (ky / kx) * sin(th);
	else
		tmp = sin(th);
	end
	tmp_2 = tmp;
end
code[kx_, ky_, th_] := If[LessEqual[N[(N[Sin[ky], $MachinePrecision] / N[Sqrt[N[(N[Power[N[Sin[kx], $MachinePrecision], 2.0], $MachinePrecision] + N[Power[N[Sin[ky], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], 0.1], N[(N[(ky / kx), $MachinePrecision] * N[Sin[th], $MachinePrecision]), $MachinePrecision], N[Sin[th], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \leq 0.1:\\
\;\;\;\;\frac{ky}{kx} \cdot \sin th\\

\mathbf{else}:\\
\;\;\;\;\sin th\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64))))) < 0.10000000000000001

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Taylor expanded in ky around 0

      \[\leadsto \color{blue}{\frac{ky}{\sqrt{{\sin kx}^{2}}}} \cdot \sin th \]
    3. Applied rewrites36.8%

      \[\leadsto \color{blue}{\frac{ky}{\sqrt{{\sin kx}^{2}}}} \cdot \sin th \]
    4. Taylor expanded in kx around 0

      \[\leadsto \frac{ky}{\color{blue}{kx}} \cdot \sin th \]
    5. Applied rewrites17.0%

      \[\leadsto \frac{ky}{\color{blue}{kx}} \cdot \sin th \]

    if 0.10000000000000001 < (/.f64 (sin.f64 ky) (sqrt.f64 (+.f64 (pow.f64 (sin.f64 kx) #s(literal 2 binary64)) (pow.f64 (sin.f64 ky) #s(literal 2 binary64)))))

    1. Initial program 94.1%

      \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
    2. Taylor expanded in kx around 0

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
    3. Applied rewrites41.8%

      \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
    4. Taylor expanded in ky around 0

      \[\leadsto \sin th \]
    5. Applied rewrites23.7%

      \[\leadsto \sin th \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 12: 23.7% accurate, 5.2× speedup?

\[\begin{array}{l} \\ \sin th \end{array} \]
(FPCore (kx ky th) :precision binary64 (sin th))
double code(double kx, double ky, double th) {
	return sin(th);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(kx, ky, th)
use fmin_fmax_functions
    real(8), intent (in) :: kx
    real(8), intent (in) :: ky
    real(8), intent (in) :: th
    code = sin(th)
end function
public static double code(double kx, double ky, double th) {
	return Math.sin(th);
}
def code(kx, ky, th):
	return math.sin(th)
function code(kx, ky, th)
	return sin(th)
end
function tmp = code(kx, ky, th)
	tmp = sin(th);
end
code[kx_, ky_, th_] := N[Sin[th], $MachinePrecision]
\begin{array}{l}

\\
\sin th
\end{array}
Derivation
  1. Initial program 94.1%

    \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
  2. Taylor expanded in kx around 0

    \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
  3. Applied rewrites41.8%

    \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
  4. Taylor expanded in ky around 0

    \[\leadsto \sin th \]
  5. Applied rewrites23.7%

    \[\leadsto \sin th \]
  6. Add Preprocessing

Alternative 13: 13.8% accurate, 170.4× speedup?

\[\begin{array}{l} \\ th \end{array} \]
(FPCore (kx ky th) :precision binary64 th)
double code(double kx, double ky, double th) {
	return th;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(kx, ky, th)
use fmin_fmax_functions
    real(8), intent (in) :: kx
    real(8), intent (in) :: ky
    real(8), intent (in) :: th
    code = th
end function
public static double code(double kx, double ky, double th) {
	return th;
}
def code(kx, ky, th):
	return th
function code(kx, ky, th)
	return th
end
function tmp = code(kx, ky, th)
	tmp = th;
end
code[kx_, ky_, th_] := th
\begin{array}{l}

\\
th
\end{array}
Derivation
  1. Initial program 94.1%

    \[\frac{\sin ky}{\sqrt{{\sin kx}^{2} + {\sin ky}^{2}}} \cdot \sin th \]
  2. Taylor expanded in kx around 0

    \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
  3. Applied rewrites41.8%

    \[\leadsto \color{blue}{\frac{\sin ky \cdot \sin th}{\sqrt{{\sin ky}^{2}}}} \]
  4. Taylor expanded in ky around 0

    \[\leadsto \sin th \]
  5. Applied rewrites23.7%

    \[\leadsto \sin th \]
  6. Taylor expanded in th around 0

    \[\leadsto th \]
  7. Applied rewrites13.8%

    \[\leadsto th \]
  8. Add Preprocessing

Reproduce

?
herbie shell --seed 2025161 
(FPCore (kx ky th)
  :name "Toniolo and Linder, Equation (3b), real"
  :precision binary64
  (* (/ (sin ky) (sqrt (+ (pow (sin kx) 2.0) (pow (sin ky) 2.0)))) (sin th)))