Maksimov and Kolovsky, Equation (3)

Percentage Accurate: 73.1% → 97.0%
Time: 10.2s
Alternatives: 7
Speedup: 1.9×

Specification

?
\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\frac{K}{2}\right)\\ \left(\left(-2 \cdot J\right) \cdot t\_0\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot t\_0}\right)}^{2}} \end{array} \end{array} \]
(FPCore (J K U)
 :precision binary64
 (let* ((t_0 (cos (/ K 2.0))))
   (* (* (* -2.0 J) t_0) (sqrt (+ 1.0 (pow (/ U (* (* 2.0 J) t_0)) 2.0))))))
double code(double J, double K, double U) {
	double t_0 = cos((K / 2.0));
	return ((-2.0 * J) * t_0) * sqrt((1.0 + pow((U / ((2.0 * J) * t_0)), 2.0)));
}
real(8) function code(j, k, u)
    real(8), intent (in) :: j
    real(8), intent (in) :: k
    real(8), intent (in) :: u
    real(8) :: t_0
    t_0 = cos((k / 2.0d0))
    code = (((-2.0d0) * j) * t_0) * sqrt((1.0d0 + ((u / ((2.0d0 * j) * t_0)) ** 2.0d0)))
end function
public static double code(double J, double K, double U) {
	double t_0 = Math.cos((K / 2.0));
	return ((-2.0 * J) * t_0) * Math.sqrt((1.0 + Math.pow((U / ((2.0 * J) * t_0)), 2.0)));
}
def code(J, K, U):
	t_0 = math.cos((K / 2.0))
	return ((-2.0 * J) * t_0) * math.sqrt((1.0 + math.pow((U / ((2.0 * J) * t_0)), 2.0)))
function code(J, K, U)
	t_0 = cos(Float64(K / 2.0))
	return Float64(Float64(Float64(-2.0 * J) * t_0) * sqrt(Float64(1.0 + (Float64(U / Float64(Float64(2.0 * J) * t_0)) ^ 2.0))))
end
function tmp = code(J, K, U)
	t_0 = cos((K / 2.0));
	tmp = ((-2.0 * J) * t_0) * sqrt((1.0 + ((U / ((2.0 * J) * t_0)) ^ 2.0)));
end
code[J_, K_, U_] := Block[{t$95$0 = N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]}, N[(N[(N[(-2.0 * J), $MachinePrecision] * t$95$0), $MachinePrecision] * N[Sqrt[N[(1.0 + N[Power[N[(U / N[(N[(2.0 * J), $MachinePrecision] * t$95$0), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\frac{K}{2}\right)\\
\left(\left(-2 \cdot J\right) \cdot t\_0\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot t\_0}\right)}^{2}}
\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 7 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: 73.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\frac{K}{2}\right)\\ \left(\left(-2 \cdot J\right) \cdot t\_0\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot t\_0}\right)}^{2}} \end{array} \end{array} \]
(FPCore (J K U)
 :precision binary64
 (let* ((t_0 (cos (/ K 2.0))))
   (* (* (* -2.0 J) t_0) (sqrt (+ 1.0 (pow (/ U (* (* 2.0 J) t_0)) 2.0))))))
double code(double J, double K, double U) {
	double t_0 = cos((K / 2.0));
	return ((-2.0 * J) * t_0) * sqrt((1.0 + pow((U / ((2.0 * J) * t_0)), 2.0)));
}
real(8) function code(j, k, u)
    real(8), intent (in) :: j
    real(8), intent (in) :: k
    real(8), intent (in) :: u
    real(8) :: t_0
    t_0 = cos((k / 2.0d0))
    code = (((-2.0d0) * j) * t_0) * sqrt((1.0d0 + ((u / ((2.0d0 * j) * t_0)) ** 2.0d0)))
end function
public static double code(double J, double K, double U) {
	double t_0 = Math.cos((K / 2.0));
	return ((-2.0 * J) * t_0) * Math.sqrt((1.0 + Math.pow((U / ((2.0 * J) * t_0)), 2.0)));
}
def code(J, K, U):
	t_0 = math.cos((K / 2.0))
	return ((-2.0 * J) * t_0) * math.sqrt((1.0 + math.pow((U / ((2.0 * J) * t_0)), 2.0)))
function code(J, K, U)
	t_0 = cos(Float64(K / 2.0))
	return Float64(Float64(Float64(-2.0 * J) * t_0) * sqrt(Float64(1.0 + (Float64(U / Float64(Float64(2.0 * J) * t_0)) ^ 2.0))))
end
function tmp = code(J, K, U)
	t_0 = cos((K / 2.0));
	tmp = ((-2.0 * J) * t_0) * sqrt((1.0 + ((U / ((2.0 * J) * t_0)) ^ 2.0)));
end
code[J_, K_, U_] := Block[{t$95$0 = N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]}, N[(N[(N[(-2.0 * J), $MachinePrecision] * t$95$0), $MachinePrecision] * N[Sqrt[N[(1.0 + N[Power[N[(U / N[(N[(2.0 * J), $MachinePrecision] * t$95$0), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\frac{K}{2}\right)\\
\left(\left(-2 \cdot J\right) \cdot t\_0\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot t\_0}\right)}^{2}}
\end{array}
\end{array}

Alternative 1: 97.0% accurate, 0.6× speedup?

\[\begin{array}{l} U_m = \left|U\right| \\ J\_m = \left|J\right| \\ J\_s = \mathsf{copysign}\left(1, J\right) \\ \begin{array}{l} t_0 := \cos \left(\frac{K}{2}\right)\\ t_1 := \left(-2 \cdot J\_m\right) \cdot t\_0\\ J\_s \cdot \begin{array}{l} \mathbf{if}\;t\_1 \cdot \sqrt{1 + {\left(\frac{U\_m}{t\_0 \cdot \left(J\_m \cdot 2\right)}\right)}^{2}} \leq -\infty:\\ \;\;\;\;0 - U\_m\\ \mathbf{else}:\\ \;\;\;\;t\_1 \cdot \mathsf{hypot}\left(1, \frac{\frac{U\_m}{J\_m \cdot 2}}{t\_0}\right)\\ \end{array} \end{array} \end{array} \]
U_m = (fabs.f64 U)
J\_m = (fabs.f64 J)
J\_s = (copysign.f64 #s(literal 1 binary64) J)
(FPCore (J_s J_m K U_m)
 :precision binary64
 (let* ((t_0 (cos (/ K 2.0))) (t_1 (* (* -2.0 J_m) t_0)))
   (*
    J_s
    (if (<=
         (* t_1 (sqrt (+ 1.0 (pow (/ U_m (* t_0 (* J_m 2.0))) 2.0))))
         (- INFINITY))
      (- 0.0 U_m)
      (* t_1 (hypot 1.0 (/ (/ U_m (* J_m 2.0)) t_0)))))))
U_m = fabs(U);
J\_m = fabs(J);
J\_s = copysign(1.0, J);
double code(double J_s, double J_m, double K, double U_m) {
	double t_0 = cos((K / 2.0));
	double t_1 = (-2.0 * J_m) * t_0;
	double tmp;
	if ((t_1 * sqrt((1.0 + pow((U_m / (t_0 * (J_m * 2.0))), 2.0)))) <= -((double) INFINITY)) {
		tmp = 0.0 - U_m;
	} else {
		tmp = t_1 * hypot(1.0, ((U_m / (J_m * 2.0)) / t_0));
	}
	return J_s * tmp;
}
U_m = Math.abs(U);
J\_m = Math.abs(J);
J\_s = Math.copySign(1.0, J);
public static double code(double J_s, double J_m, double K, double U_m) {
	double t_0 = Math.cos((K / 2.0));
	double t_1 = (-2.0 * J_m) * t_0;
	double tmp;
	if ((t_1 * Math.sqrt((1.0 + Math.pow((U_m / (t_0 * (J_m * 2.0))), 2.0)))) <= -Double.POSITIVE_INFINITY) {
		tmp = 0.0 - U_m;
	} else {
		tmp = t_1 * Math.hypot(1.0, ((U_m / (J_m * 2.0)) / t_0));
	}
	return J_s * tmp;
}
U_m = math.fabs(U)
J\_m = math.fabs(J)
J\_s = math.copysign(1.0, J)
def code(J_s, J_m, K, U_m):
	t_0 = math.cos((K / 2.0))
	t_1 = (-2.0 * J_m) * t_0
	tmp = 0
	if (t_1 * math.sqrt((1.0 + math.pow((U_m / (t_0 * (J_m * 2.0))), 2.0)))) <= -math.inf:
		tmp = 0.0 - U_m
	else:
		tmp = t_1 * math.hypot(1.0, ((U_m / (J_m * 2.0)) / t_0))
	return J_s * tmp
U_m = abs(U)
J\_m = abs(J)
J\_s = copysign(1.0, J)
function code(J_s, J_m, K, U_m)
	t_0 = cos(Float64(K / 2.0))
	t_1 = Float64(Float64(-2.0 * J_m) * t_0)
	tmp = 0.0
	if (Float64(t_1 * sqrt(Float64(1.0 + (Float64(U_m / Float64(t_0 * Float64(J_m * 2.0))) ^ 2.0)))) <= Float64(-Inf))
		tmp = Float64(0.0 - U_m);
	else
		tmp = Float64(t_1 * hypot(1.0, Float64(Float64(U_m / Float64(J_m * 2.0)) / t_0)));
	end
	return Float64(J_s * tmp)
end
U_m = abs(U);
J\_m = abs(J);
J\_s = sign(J) * abs(1.0);
function tmp_2 = code(J_s, J_m, K, U_m)
	t_0 = cos((K / 2.0));
	t_1 = (-2.0 * J_m) * t_0;
	tmp = 0.0;
	if ((t_1 * sqrt((1.0 + ((U_m / (t_0 * (J_m * 2.0))) ^ 2.0)))) <= -Inf)
		tmp = 0.0 - U_m;
	else
		tmp = t_1 * hypot(1.0, ((U_m / (J_m * 2.0)) / t_0));
	end
	tmp_2 = J_s * tmp;
end
U_m = N[Abs[U], $MachinePrecision]
J\_m = N[Abs[J], $MachinePrecision]
J\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[J]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[J$95$s_, J$95$m_, K_, U$95$m_] := Block[{t$95$0 = N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[(-2.0 * J$95$m), $MachinePrecision] * t$95$0), $MachinePrecision]}, N[(J$95$s * If[LessEqual[N[(t$95$1 * N[Sqrt[N[(1.0 + N[Power[N[(U$95$m / N[(t$95$0 * N[(J$95$m * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], (-Infinity)], N[(0.0 - U$95$m), $MachinePrecision], N[(t$95$1 * N[Sqrt[1.0 ^ 2 + N[(N[(U$95$m / N[(J$95$m * 2.0), $MachinePrecision]), $MachinePrecision] / t$95$0), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]]]
\begin{array}{l}
U_m = \left|U\right|
\\
J\_m = \left|J\right|
\\
J\_s = \mathsf{copysign}\left(1, J\right)

\\
\begin{array}{l}
t_0 := \cos \left(\frac{K}{2}\right)\\
t_1 := \left(-2 \cdot J\_m\right) \cdot t\_0\\
J\_s \cdot \begin{array}{l}
\mathbf{if}\;t\_1 \cdot \sqrt{1 + {\left(\frac{U\_m}{t\_0 \cdot \left(J\_m \cdot 2\right)}\right)}^{2}} \leq -\infty:\\
\;\;\;\;0 - U\_m\\

\mathbf{else}:\\
\;\;\;\;t\_1 \cdot \mathsf{hypot}\left(1, \frac{\frac{U\_m}{J\_m \cdot 2}}{t\_0}\right)\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (*.f64 (*.f64 #s(literal -2 binary64) J) (cos.f64 (/.f64 K #s(literal 2 binary64)))) (sqrt.f64 (+.f64 #s(literal 1 binary64) (pow.f64 (/.f64 U (*.f64 (*.f64 #s(literal 2 binary64) J) (cos.f64 (/.f64 K #s(literal 2 binary64))))) #s(literal 2 binary64))))) < -inf.0

    1. Initial program 4.7%

      \[\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
    2. Add Preprocessing
    3. Taylor expanded in J around 0

      \[\leadsto \color{blue}{-1 \cdot U} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \mathsf{neg}\left(U\right) \]
      2. neg-sub0N/A

        \[\leadsto 0 - \color{blue}{U} \]
      3. --lowering--.f6441.8%

        \[\leadsto \mathsf{\_.f64}\left(0, \color{blue}{U}\right) \]
    5. Simplified41.8%

      \[\leadsto \color{blue}{0 - U} \]
    6. Step-by-step derivation
      1. sub0-negN/A

        \[\leadsto \mathsf{neg}\left(U\right) \]
      2. neg-lowering-neg.f6441.8%

        \[\leadsto \mathsf{neg.f64}\left(U\right) \]
    7. Applied egg-rr41.8%

      \[\leadsto \color{blue}{-U} \]

    if -inf.0 < (*.f64 (*.f64 (*.f64 #s(literal -2 binary64) J) (cos.f64 (/.f64 K #s(literal 2 binary64)))) (sqrt.f64 (+.f64 #s(literal 1 binary64) (pow.f64 (/.f64 U (*.f64 (*.f64 #s(literal 2 binary64) J) (cos.f64 (/.f64 K #s(literal 2 binary64))))) #s(literal 2 binary64)))))

    1. Initial program 86.5%

      \[\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
    2. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right), \color{blue}{\left(\sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)}\right) \]
      2. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\left(-2 \cdot J\right), \cos \left(\frac{K}{2}\right)\right), \left(\sqrt{\color{blue}{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}}}\right)\right) \]
      3. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \cos \left(\frac{K}{2}\right)\right), \left(\sqrt{\color{blue}{1} + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)\right) \]
      4. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\left(\frac{K}{2}\right)\right)\right), \left(\sqrt{1 + \color{blue}{{\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}}}\right)\right) \]
      5. /-lowering-/.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \left(\sqrt{1 + {\color{blue}{\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}}^{2}}\right)\right) \]
      6. unpow2N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \left(\sqrt{1 + \frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)} \cdot \frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}}\right)\right) \]
      7. hypot-1-defN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \left(\mathsf{hypot}\left(1, \color{blue}{\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}}\right)\right)\right) \]
      8. hypot-lowering-hypot.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \color{blue}{\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}\right)\right) \]
      9. associate-/r*N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \left(\frac{\frac{U}{2 \cdot J}}{\color{blue}{\cos \left(\frac{K}{2}\right)}}\right)\right)\right) \]
      10. /-lowering-/.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{/.f64}\left(\left(\frac{U}{2 \cdot J}\right), \color{blue}{\cos \left(\frac{K}{2}\right)}\right)\right)\right) \]
      11. /-lowering-/.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{/.f64}\left(\mathsf{/.f64}\left(U, \left(2 \cdot J\right)\right), \cos \color{blue}{\left(\frac{K}{2}\right)}\right)\right)\right) \]
      12. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{/.f64}\left(\mathsf{/.f64}\left(U, \left(J \cdot 2\right)\right), \cos \left(\frac{K}{\color{blue}{2}}\right)\right)\right)\right) \]
      13. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{/.f64}\left(\mathsf{/.f64}\left(U, \mathsf{*.f64}\left(J, 2\right)\right), \cos \left(\frac{K}{\color{blue}{2}}\right)\right)\right)\right) \]
      14. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{/.f64}\left(\mathsf{/.f64}\left(U, \mathsf{*.f64}\left(J, 2\right)\right), \mathsf{cos.f64}\left(\left(\frac{K}{2}\right)\right)\right)\right)\right) \]
      15. /-lowering-/.f6491.8%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{/.f64}\left(\mathsf{/.f64}\left(U, \mathsf{*.f64}\left(J, 2\right)\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right)\right)\right) \]
    3. Simplified91.8%

      \[\leadsto \color{blue}{\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)} \]
    4. Add Preprocessing
  3. Recombined 2 regimes into one program.
  4. Final simplification86.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\cos \left(\frac{K}{2}\right) \cdot \left(J \cdot 2\right)}\right)}^{2}} \leq -\infty:\\ \;\;\;\;0 - U\\ \mathbf{else}:\\ \;\;\;\;\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 70.5% accurate, 1.0× speedup?

\[\begin{array}{l} U_m = \left|U\right| \\ J\_m = \left|J\right| \\ J\_s = \mathsf{copysign}\left(1, J\right) \\ \begin{array}{l} t_0 := \cos \left(\frac{K}{2}\right)\\ t_1 := \left(-2 \cdot J\_m\right) \cdot \cos \left(K \cdot 0.5\right)\\ J\_s \cdot \begin{array}{l} \mathbf{if}\;t\_0 \leq -0.67:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;t\_0 \leq -0.05:\\ \;\;\;\;U\_m\\ \mathbf{elif}\;t\_0 \leq 0.95:\\ \;\;\;\;t\_1\\ \mathbf{else}:\\ \;\;\;\;\left(-2 \cdot J\_m\right) \cdot \mathsf{hypot}\left(1, 0.5 \cdot \frac{U\_m}{J\_m}\right)\\ \end{array} \end{array} \end{array} \]
U_m = (fabs.f64 U)
J\_m = (fabs.f64 J)
J\_s = (copysign.f64 #s(literal 1 binary64) J)
(FPCore (J_s J_m K U_m)
 :precision binary64
 (let* ((t_0 (cos (/ K 2.0))) (t_1 (* (* -2.0 J_m) (cos (* K 0.5)))))
   (*
    J_s
    (if (<= t_0 -0.67)
      t_1
      (if (<= t_0 -0.05)
        U_m
        (if (<= t_0 0.95)
          t_1
          (* (* -2.0 J_m) (hypot 1.0 (* 0.5 (/ U_m J_m))))))))))
U_m = fabs(U);
J\_m = fabs(J);
J\_s = copysign(1.0, J);
double code(double J_s, double J_m, double K, double U_m) {
	double t_0 = cos((K / 2.0));
	double t_1 = (-2.0 * J_m) * cos((K * 0.5));
	double tmp;
	if (t_0 <= -0.67) {
		tmp = t_1;
	} else if (t_0 <= -0.05) {
		tmp = U_m;
	} else if (t_0 <= 0.95) {
		tmp = t_1;
	} else {
		tmp = (-2.0 * J_m) * hypot(1.0, (0.5 * (U_m / J_m)));
	}
	return J_s * tmp;
}
U_m = Math.abs(U);
J\_m = Math.abs(J);
J\_s = Math.copySign(1.0, J);
public static double code(double J_s, double J_m, double K, double U_m) {
	double t_0 = Math.cos((K / 2.0));
	double t_1 = (-2.0 * J_m) * Math.cos((K * 0.5));
	double tmp;
	if (t_0 <= -0.67) {
		tmp = t_1;
	} else if (t_0 <= -0.05) {
		tmp = U_m;
	} else if (t_0 <= 0.95) {
		tmp = t_1;
	} else {
		tmp = (-2.0 * J_m) * Math.hypot(1.0, (0.5 * (U_m / J_m)));
	}
	return J_s * tmp;
}
U_m = math.fabs(U)
J\_m = math.fabs(J)
J\_s = math.copysign(1.0, J)
def code(J_s, J_m, K, U_m):
	t_0 = math.cos((K / 2.0))
	t_1 = (-2.0 * J_m) * math.cos((K * 0.5))
	tmp = 0
	if t_0 <= -0.67:
		tmp = t_1
	elif t_0 <= -0.05:
		tmp = U_m
	elif t_0 <= 0.95:
		tmp = t_1
	else:
		tmp = (-2.0 * J_m) * math.hypot(1.0, (0.5 * (U_m / J_m)))
	return J_s * tmp
U_m = abs(U)
J\_m = abs(J)
J\_s = copysign(1.0, J)
function code(J_s, J_m, K, U_m)
	t_0 = cos(Float64(K / 2.0))
	t_1 = Float64(Float64(-2.0 * J_m) * cos(Float64(K * 0.5)))
	tmp = 0.0
	if (t_0 <= -0.67)
		tmp = t_1;
	elseif (t_0 <= -0.05)
		tmp = U_m;
	elseif (t_0 <= 0.95)
		tmp = t_1;
	else
		tmp = Float64(Float64(-2.0 * J_m) * hypot(1.0, Float64(0.5 * Float64(U_m / J_m))));
	end
	return Float64(J_s * tmp)
end
U_m = abs(U);
J\_m = abs(J);
J\_s = sign(J) * abs(1.0);
function tmp_2 = code(J_s, J_m, K, U_m)
	t_0 = cos((K / 2.0));
	t_1 = (-2.0 * J_m) * cos((K * 0.5));
	tmp = 0.0;
	if (t_0 <= -0.67)
		tmp = t_1;
	elseif (t_0 <= -0.05)
		tmp = U_m;
	elseif (t_0 <= 0.95)
		tmp = t_1;
	else
		tmp = (-2.0 * J_m) * hypot(1.0, (0.5 * (U_m / J_m)));
	end
	tmp_2 = J_s * tmp;
end
U_m = N[Abs[U], $MachinePrecision]
J\_m = N[Abs[J], $MachinePrecision]
J\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[J]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[J$95$s_, J$95$m_, K_, U$95$m_] := Block[{t$95$0 = N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[(-2.0 * J$95$m), $MachinePrecision] * N[Cos[N[(K * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, N[(J$95$s * If[LessEqual[t$95$0, -0.67], t$95$1, If[LessEqual[t$95$0, -0.05], U$95$m, If[LessEqual[t$95$0, 0.95], t$95$1, N[(N[(-2.0 * J$95$m), $MachinePrecision] * N[Sqrt[1.0 ^ 2 + N[(0.5 * N[(U$95$m / J$95$m), $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]]]]), $MachinePrecision]]]
\begin{array}{l}
U_m = \left|U\right|
\\
J\_m = \left|J\right|
\\
J\_s = \mathsf{copysign}\left(1, J\right)

\\
\begin{array}{l}
t_0 := \cos \left(\frac{K}{2}\right)\\
t_1 := \left(-2 \cdot J\_m\right) \cdot \cos \left(K \cdot 0.5\right)\\
J\_s \cdot \begin{array}{l}
\mathbf{if}\;t\_0 \leq -0.67:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;t\_0 \leq -0.05:\\
\;\;\;\;U\_m\\

\mathbf{elif}\;t\_0 \leq 0.95:\\
\;\;\;\;t\_1\\

\mathbf{else}:\\
\;\;\;\;\left(-2 \cdot J\_m\right) \cdot \mathsf{hypot}\left(1, 0.5 \cdot \frac{U\_m}{J\_m}\right)\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (cos.f64 (/.f64 K #s(literal 2 binary64))) < -0.67000000000000004 or -0.050000000000000003 < (cos.f64 (/.f64 K #s(literal 2 binary64))) < 0.94999999999999996

    1. Initial program 89.8%

      \[\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
    2. Add Preprocessing
    3. Taylor expanded in J around inf

      \[\leadsto \color{blue}{-2 \cdot \left(J \cdot \cos \left(\frac{1}{2} \cdot K\right)\right)} \]
    4. Step-by-step derivation
      1. associate-*r*N/A

        \[\leadsto \left(-2 \cdot J\right) \cdot \color{blue}{\cos \left(\frac{1}{2} \cdot K\right)} \]
      2. *-commutativeN/A

        \[\leadsto \cos \left(\frac{1}{2} \cdot K\right) \cdot \color{blue}{\left(-2 \cdot J\right)} \]
      3. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\cos \left(\frac{1}{2} \cdot K\right), \color{blue}{\left(-2 \cdot J\right)}\right) \]
      4. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\left(\frac{1}{2} \cdot K\right)\right), \left(\color{blue}{-2} \cdot J\right)\right) \]
      5. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, K\right)\right), \left(-2 \cdot J\right)\right) \]
      6. *-lowering-*.f6467.3%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, K\right)\right), \mathsf{*.f64}\left(-2, \color{blue}{J}\right)\right) \]
    5. Simplified67.3%

      \[\leadsto \color{blue}{\cos \left(0.5 \cdot K\right) \cdot \left(-2 \cdot J\right)} \]

    if -0.67000000000000004 < (cos.f64 (/.f64 K #s(literal 2 binary64))) < -0.050000000000000003

    1. Initial program 47.4%

      \[\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
    2. Add Preprocessing
    3. Taylor expanded in U around -inf

      \[\leadsto \color{blue}{U} \]
    4. Step-by-step derivation
      1. Simplified28.8%

        \[\leadsto \color{blue}{U} \]

      if 0.94999999999999996 < (cos.f64 (/.f64 K #s(literal 2 binary64)))

      1. Initial program 75.3%

        \[\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
      2. Step-by-step derivation
        1. *-lowering-*.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right), \color{blue}{\left(\sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)}\right) \]
        2. *-lowering-*.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\left(-2 \cdot J\right), \cos \left(\frac{K}{2}\right)\right), \left(\sqrt{\color{blue}{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}}}\right)\right) \]
        3. *-lowering-*.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \cos \left(\frac{K}{2}\right)\right), \left(\sqrt{\color{blue}{1} + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)\right) \]
        4. cos-lowering-cos.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\left(\frac{K}{2}\right)\right)\right), \left(\sqrt{1 + \color{blue}{{\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}}}\right)\right) \]
        5. /-lowering-/.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \left(\sqrt{1 + {\color{blue}{\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}}^{2}}\right)\right) \]
        6. unpow2N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \left(\sqrt{1 + \frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)} \cdot \frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}}\right)\right) \]
        7. hypot-1-defN/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \left(\mathsf{hypot}\left(1, \color{blue}{\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}}\right)\right)\right) \]
        8. hypot-lowering-hypot.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \color{blue}{\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}\right)\right) \]
        9. associate-/r*N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \left(\frac{\frac{U}{2 \cdot J}}{\color{blue}{\cos \left(\frac{K}{2}\right)}}\right)\right)\right) \]
        10. /-lowering-/.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{/.f64}\left(\left(\frac{U}{2 \cdot J}\right), \color{blue}{\cos \left(\frac{K}{2}\right)}\right)\right)\right) \]
        11. /-lowering-/.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{/.f64}\left(\mathsf{/.f64}\left(U, \left(2 \cdot J\right)\right), \cos \color{blue}{\left(\frac{K}{2}\right)}\right)\right)\right) \]
        12. *-commutativeN/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{/.f64}\left(\mathsf{/.f64}\left(U, \left(J \cdot 2\right)\right), \cos \left(\frac{K}{\color{blue}{2}}\right)\right)\right)\right) \]
        13. *-lowering-*.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{/.f64}\left(\mathsf{/.f64}\left(U, \mathsf{*.f64}\left(J, 2\right)\right), \cos \left(\frac{K}{\color{blue}{2}}\right)\right)\right)\right) \]
        14. cos-lowering-cos.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{/.f64}\left(\mathsf{/.f64}\left(U, \mathsf{*.f64}\left(J, 2\right)\right), \mathsf{cos.f64}\left(\left(\frac{K}{2}\right)\right)\right)\right)\right) \]
        15. /-lowering-/.f6486.7%

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{/.f64}\left(\mathsf{/.f64}\left(U, \mathsf{*.f64}\left(J, 2\right)\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right)\right)\right) \]
      3. Simplified86.7%

        \[\leadsto \color{blue}{\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)} \]
      4. Add Preprocessing
      5. Taylor expanded in K around 0

        \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \color{blue}{\left(\frac{1}{2} \cdot \frac{U}{J}\right)}\right)\right) \]
      6. Step-by-step derivation
        1. *-lowering-*.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{*.f64}\left(\frac{1}{2}, \color{blue}{\left(\frac{U}{J}\right)}\right)\right)\right) \]
        2. /-lowering-/.f6482.4%

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(U, \color{blue}{J}\right)\right)\right)\right) \]
      7. Simplified82.4%

        \[\leadsto \left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, \color{blue}{0.5 \cdot \frac{U}{J}}\right) \]
      8. Taylor expanded in K around 0

        \[\leadsto \mathsf{*.f64}\left(\color{blue}{\left(-2 \cdot J\right)}, \mathsf{hypot.f64}\left(1, \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(U, J\right)\right)\right)\right) \]
      9. Step-by-step derivation
        1. *-lowering-*.f6483.7%

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{hypot.f64}\left(\color{blue}{1}, \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(U, J\right)\right)\right)\right) \]
      10. Simplified83.7%

        \[\leadsto \color{blue}{\left(-2 \cdot J\right)} \cdot \mathsf{hypot}\left(1, 0.5 \cdot \frac{U}{J}\right) \]
    5. Recombined 3 regimes into one program.
    6. Final simplification74.1%

      \[\leadsto \begin{array}{l} \mathbf{if}\;\cos \left(\frac{K}{2}\right) \leq -0.67:\\ \;\;\;\;\left(-2 \cdot J\right) \cdot \cos \left(K \cdot 0.5\right)\\ \mathbf{elif}\;\cos \left(\frac{K}{2}\right) \leq -0.05:\\ \;\;\;\;U\\ \mathbf{elif}\;\cos \left(\frac{K}{2}\right) \leq 0.95:\\ \;\;\;\;\left(-2 \cdot J\right) \cdot \cos \left(K \cdot 0.5\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-2 \cdot J\right) \cdot \mathsf{hypot}\left(1, 0.5 \cdot \frac{U}{J}\right)\\ \end{array} \]
    7. Add Preprocessing

    Alternative 3: 78.1% accurate, 1.9× speedup?

    \[\begin{array}{l} U_m = \left|U\right| \\ J\_m = \left|J\right| \\ J\_s = \mathsf{copysign}\left(1, J\right) \\ J\_s \cdot \begin{array}{l} \mathbf{if}\;J\_m \leq 4.4 \cdot 10^{-122}:\\ \;\;\;\;0 - U\_m\\ \mathbf{else}:\\ \;\;\;\;\left(\left(-2 \cdot J\_m\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, 0.5 \cdot \frac{U\_m}{J\_m}\right)\\ \end{array} \end{array} \]
    U_m = (fabs.f64 U)
    J\_m = (fabs.f64 J)
    J\_s = (copysign.f64 #s(literal 1 binary64) J)
    (FPCore (J_s J_m K U_m)
     :precision binary64
     (*
      J_s
      (if (<= J_m 4.4e-122)
        (- 0.0 U_m)
        (* (* (* -2.0 J_m) (cos (/ K 2.0))) (hypot 1.0 (* 0.5 (/ U_m J_m)))))))
    U_m = fabs(U);
    J\_m = fabs(J);
    J\_s = copysign(1.0, J);
    double code(double J_s, double J_m, double K, double U_m) {
    	double tmp;
    	if (J_m <= 4.4e-122) {
    		tmp = 0.0 - U_m;
    	} else {
    		tmp = ((-2.0 * J_m) * cos((K / 2.0))) * hypot(1.0, (0.5 * (U_m / J_m)));
    	}
    	return J_s * tmp;
    }
    
    U_m = Math.abs(U);
    J\_m = Math.abs(J);
    J\_s = Math.copySign(1.0, J);
    public static double code(double J_s, double J_m, double K, double U_m) {
    	double tmp;
    	if (J_m <= 4.4e-122) {
    		tmp = 0.0 - U_m;
    	} else {
    		tmp = ((-2.0 * J_m) * Math.cos((K / 2.0))) * Math.hypot(1.0, (0.5 * (U_m / J_m)));
    	}
    	return J_s * tmp;
    }
    
    U_m = math.fabs(U)
    J\_m = math.fabs(J)
    J\_s = math.copysign(1.0, J)
    def code(J_s, J_m, K, U_m):
    	tmp = 0
    	if J_m <= 4.4e-122:
    		tmp = 0.0 - U_m
    	else:
    		tmp = ((-2.0 * J_m) * math.cos((K / 2.0))) * math.hypot(1.0, (0.5 * (U_m / J_m)))
    	return J_s * tmp
    
    U_m = abs(U)
    J\_m = abs(J)
    J\_s = copysign(1.0, J)
    function code(J_s, J_m, K, U_m)
    	tmp = 0.0
    	if (J_m <= 4.4e-122)
    		tmp = Float64(0.0 - U_m);
    	else
    		tmp = Float64(Float64(Float64(-2.0 * J_m) * cos(Float64(K / 2.0))) * hypot(1.0, Float64(0.5 * Float64(U_m / J_m))));
    	end
    	return Float64(J_s * tmp)
    end
    
    U_m = abs(U);
    J\_m = abs(J);
    J\_s = sign(J) * abs(1.0);
    function tmp_2 = code(J_s, J_m, K, U_m)
    	tmp = 0.0;
    	if (J_m <= 4.4e-122)
    		tmp = 0.0 - U_m;
    	else
    		tmp = ((-2.0 * J_m) * cos((K / 2.0))) * hypot(1.0, (0.5 * (U_m / J_m)));
    	end
    	tmp_2 = J_s * tmp;
    end
    
    U_m = N[Abs[U], $MachinePrecision]
    J\_m = N[Abs[J], $MachinePrecision]
    J\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[J]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
    code[J$95$s_, J$95$m_, K_, U$95$m_] := N[(J$95$s * If[LessEqual[J$95$m, 4.4e-122], N[(0.0 - U$95$m), $MachinePrecision], N[(N[(N[(-2.0 * J$95$m), $MachinePrecision] * N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Sqrt[1.0 ^ 2 + N[(0.5 * N[(U$95$m / J$95$m), $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
    
    \begin{array}{l}
    U_m = \left|U\right|
    \\
    J\_m = \left|J\right|
    \\
    J\_s = \mathsf{copysign}\left(1, J\right)
    
    \\
    J\_s \cdot \begin{array}{l}
    \mathbf{if}\;J\_m \leq 4.4 \cdot 10^{-122}:\\
    \;\;\;\;0 - U\_m\\
    
    \mathbf{else}:\\
    \;\;\;\;\left(\left(-2 \cdot J\_m\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, 0.5 \cdot \frac{U\_m}{J\_m}\right)\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if J < 4.4e-122

      1. Initial program 71.1%

        \[\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
      2. Add Preprocessing
      3. Taylor expanded in J around 0

        \[\leadsto \color{blue}{-1 \cdot U} \]
      4. Step-by-step derivation
        1. mul-1-negN/A

          \[\leadsto \mathsf{neg}\left(U\right) \]
        2. neg-sub0N/A

          \[\leadsto 0 - \color{blue}{U} \]
        3. --lowering--.f6432.2%

          \[\leadsto \mathsf{\_.f64}\left(0, \color{blue}{U}\right) \]
      5. Simplified32.2%

        \[\leadsto \color{blue}{0 - U} \]
      6. Step-by-step derivation
        1. sub0-negN/A

          \[\leadsto \mathsf{neg}\left(U\right) \]
        2. neg-lowering-neg.f6432.2%

          \[\leadsto \mathsf{neg.f64}\left(U\right) \]
      7. Applied egg-rr32.2%

        \[\leadsto \color{blue}{-U} \]

      if 4.4e-122 < J

      1. Initial program 89.1%

        \[\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
      2. Step-by-step derivation
        1. *-lowering-*.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right), \color{blue}{\left(\sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)}\right) \]
        2. *-lowering-*.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\left(-2 \cdot J\right), \cos \left(\frac{K}{2}\right)\right), \left(\sqrt{\color{blue}{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}}}\right)\right) \]
        3. *-lowering-*.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \cos \left(\frac{K}{2}\right)\right), \left(\sqrt{\color{blue}{1} + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}}\right)\right) \]
        4. cos-lowering-cos.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\left(\frac{K}{2}\right)\right)\right), \left(\sqrt{1 + \color{blue}{{\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}}}\right)\right) \]
        5. /-lowering-/.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \left(\sqrt{1 + {\color{blue}{\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}}^{2}}\right)\right) \]
        6. unpow2N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \left(\sqrt{1 + \frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)} \cdot \frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}}\right)\right) \]
        7. hypot-1-defN/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \left(\mathsf{hypot}\left(1, \color{blue}{\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}}\right)\right)\right) \]
        8. hypot-lowering-hypot.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \color{blue}{\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}\right)\right) \]
        9. associate-/r*N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \left(\frac{\frac{U}{2 \cdot J}}{\color{blue}{\cos \left(\frac{K}{2}\right)}}\right)\right)\right) \]
        10. /-lowering-/.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{/.f64}\left(\left(\frac{U}{2 \cdot J}\right), \color{blue}{\cos \left(\frac{K}{2}\right)}\right)\right)\right) \]
        11. /-lowering-/.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{/.f64}\left(\mathsf{/.f64}\left(U, \left(2 \cdot J\right)\right), \cos \color{blue}{\left(\frac{K}{2}\right)}\right)\right)\right) \]
        12. *-commutativeN/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{/.f64}\left(\mathsf{/.f64}\left(U, \left(J \cdot 2\right)\right), \cos \left(\frac{K}{\color{blue}{2}}\right)\right)\right)\right) \]
        13. *-lowering-*.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{/.f64}\left(\mathsf{/.f64}\left(U, \mathsf{*.f64}\left(J, 2\right)\right), \cos \left(\frac{K}{\color{blue}{2}}\right)\right)\right)\right) \]
        14. cos-lowering-cos.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{/.f64}\left(\mathsf{/.f64}\left(U, \mathsf{*.f64}\left(J, 2\right)\right), \mathsf{cos.f64}\left(\left(\frac{K}{2}\right)\right)\right)\right)\right) \]
        15. /-lowering-/.f6497.7%

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{/.f64}\left(\mathsf{/.f64}\left(U, \mathsf{*.f64}\left(J, 2\right)\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right)\right)\right) \]
      3. Simplified97.7%

        \[\leadsto \color{blue}{\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)} \]
      4. Add Preprocessing
      5. Taylor expanded in K around 0

        \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \color{blue}{\left(\frac{1}{2} \cdot \frac{U}{J}\right)}\right)\right) \]
      6. Step-by-step derivation
        1. *-lowering-*.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{*.f64}\left(\frac{1}{2}, \color{blue}{\left(\frac{U}{J}\right)}\right)\right)\right) \]
        2. /-lowering-/.f6486.1%

          \[\leadsto \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(-2, J\right), \mathsf{cos.f64}\left(\mathsf{/.f64}\left(K, 2\right)\right)\right), \mathsf{hypot.f64}\left(1, \mathsf{*.f64}\left(\frac{1}{2}, \mathsf{/.f64}\left(U, \color{blue}{J}\right)\right)\right)\right) \]
      7. Simplified86.1%

        \[\leadsto \left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, \color{blue}{0.5 \cdot \frac{U}{J}}\right) \]
    3. Recombined 2 regimes into one program.
    4. Final simplification50.5%

      \[\leadsto \begin{array}{l} \mathbf{if}\;J \leq 4.4 \cdot 10^{-122}:\\ \;\;\;\;0 - U\\ \mathbf{else}:\\ \;\;\;\;\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, 0.5 \cdot \frac{U}{J}\right)\\ \end{array} \]
    5. Add Preprocessing

    Alternative 4: 66.1% accurate, 3.7× speedup?

    \[\begin{array}{l} U_m = \left|U\right| \\ J\_m = \left|J\right| \\ J\_s = \mathsf{copysign}\left(1, J\right) \\ J\_s \cdot \begin{array}{l} \mathbf{if}\;J\_m \leq 9.5 \cdot 10^{-76}:\\ \;\;\;\;0 - U\_m\\ \mathbf{else}:\\ \;\;\;\;\left(-2 \cdot J\_m\right) \cdot \cos \left(K \cdot 0.5\right)\\ \end{array} \end{array} \]
    U_m = (fabs.f64 U)
    J\_m = (fabs.f64 J)
    J\_s = (copysign.f64 #s(literal 1 binary64) J)
    (FPCore (J_s J_m K U_m)
     :precision binary64
     (* J_s (if (<= J_m 9.5e-76) (- 0.0 U_m) (* (* -2.0 J_m) (cos (* K 0.5))))))
    U_m = fabs(U);
    J\_m = fabs(J);
    J\_s = copysign(1.0, J);
    double code(double J_s, double J_m, double K, double U_m) {
    	double tmp;
    	if (J_m <= 9.5e-76) {
    		tmp = 0.0 - U_m;
    	} else {
    		tmp = (-2.0 * J_m) * cos((K * 0.5));
    	}
    	return J_s * tmp;
    }
    
    U_m = abs(u)
    J\_m = abs(j)
    J\_s = copysign(1.0d0, j)
    real(8) function code(j_s, j_m, k, u_m)
        real(8), intent (in) :: j_s
        real(8), intent (in) :: j_m
        real(8), intent (in) :: k
        real(8), intent (in) :: u_m
        real(8) :: tmp
        if (j_m <= 9.5d-76) then
            tmp = 0.0d0 - u_m
        else
            tmp = ((-2.0d0) * j_m) * cos((k * 0.5d0))
        end if
        code = j_s * tmp
    end function
    
    U_m = Math.abs(U);
    J\_m = Math.abs(J);
    J\_s = Math.copySign(1.0, J);
    public static double code(double J_s, double J_m, double K, double U_m) {
    	double tmp;
    	if (J_m <= 9.5e-76) {
    		tmp = 0.0 - U_m;
    	} else {
    		tmp = (-2.0 * J_m) * Math.cos((K * 0.5));
    	}
    	return J_s * tmp;
    }
    
    U_m = math.fabs(U)
    J\_m = math.fabs(J)
    J\_s = math.copysign(1.0, J)
    def code(J_s, J_m, K, U_m):
    	tmp = 0
    	if J_m <= 9.5e-76:
    		tmp = 0.0 - U_m
    	else:
    		tmp = (-2.0 * J_m) * math.cos((K * 0.5))
    	return J_s * tmp
    
    U_m = abs(U)
    J\_m = abs(J)
    J\_s = copysign(1.0, J)
    function code(J_s, J_m, K, U_m)
    	tmp = 0.0
    	if (J_m <= 9.5e-76)
    		tmp = Float64(0.0 - U_m);
    	else
    		tmp = Float64(Float64(-2.0 * J_m) * cos(Float64(K * 0.5)));
    	end
    	return Float64(J_s * tmp)
    end
    
    U_m = abs(U);
    J\_m = abs(J);
    J\_s = sign(J) * abs(1.0);
    function tmp_2 = code(J_s, J_m, K, U_m)
    	tmp = 0.0;
    	if (J_m <= 9.5e-76)
    		tmp = 0.0 - U_m;
    	else
    		tmp = (-2.0 * J_m) * cos((K * 0.5));
    	end
    	tmp_2 = J_s * tmp;
    end
    
    U_m = N[Abs[U], $MachinePrecision]
    J\_m = N[Abs[J], $MachinePrecision]
    J\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[J]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
    code[J$95$s_, J$95$m_, K_, U$95$m_] := N[(J$95$s * If[LessEqual[J$95$m, 9.5e-76], N[(0.0 - U$95$m), $MachinePrecision], N[(N[(-2.0 * J$95$m), $MachinePrecision] * N[Cos[N[(K * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
    
    \begin{array}{l}
    U_m = \left|U\right|
    \\
    J\_m = \left|J\right|
    \\
    J\_s = \mathsf{copysign}\left(1, J\right)
    
    \\
    J\_s \cdot \begin{array}{l}
    \mathbf{if}\;J\_m \leq 9.5 \cdot 10^{-76}:\\
    \;\;\;\;0 - U\_m\\
    
    \mathbf{else}:\\
    \;\;\;\;\left(-2 \cdot J\_m\right) \cdot \cos \left(K \cdot 0.5\right)\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if J < 9.49999999999999984e-76

      1. Initial program 69.7%

        \[\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
      2. Add Preprocessing
      3. Taylor expanded in J around 0

        \[\leadsto \color{blue}{-1 \cdot U} \]
      4. Step-by-step derivation
        1. mul-1-negN/A

          \[\leadsto \mathsf{neg}\left(U\right) \]
        2. neg-sub0N/A

          \[\leadsto 0 - \color{blue}{U} \]
        3. --lowering--.f6433.1%

          \[\leadsto \mathsf{\_.f64}\left(0, \color{blue}{U}\right) \]
      5. Simplified33.1%

        \[\leadsto \color{blue}{0 - U} \]
      6. Step-by-step derivation
        1. sub0-negN/A

          \[\leadsto \mathsf{neg}\left(U\right) \]
        2. neg-lowering-neg.f6433.1%

          \[\leadsto \mathsf{neg.f64}\left(U\right) \]
      7. Applied egg-rr33.1%

        \[\leadsto \color{blue}{-U} \]

      if 9.49999999999999984e-76 < J

      1. Initial program 94.9%

        \[\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
      2. Add Preprocessing
      3. Taylor expanded in J around inf

        \[\leadsto \color{blue}{-2 \cdot \left(J \cdot \cos \left(\frac{1}{2} \cdot K\right)\right)} \]
      4. Step-by-step derivation
        1. associate-*r*N/A

          \[\leadsto \left(-2 \cdot J\right) \cdot \color{blue}{\cos \left(\frac{1}{2} \cdot K\right)} \]
        2. *-commutativeN/A

          \[\leadsto \cos \left(\frac{1}{2} \cdot K\right) \cdot \color{blue}{\left(-2 \cdot J\right)} \]
        3. *-lowering-*.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\cos \left(\frac{1}{2} \cdot K\right), \color{blue}{\left(-2 \cdot J\right)}\right) \]
        4. cos-lowering-cos.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\left(\frac{1}{2} \cdot K\right)\right), \left(\color{blue}{-2} \cdot J\right)\right) \]
        5. *-lowering-*.f64N/A

          \[\leadsto \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, K\right)\right), \left(-2 \cdot J\right)\right) \]
        6. *-lowering-*.f6467.9%

          \[\leadsto \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, K\right)\right), \mathsf{*.f64}\left(-2, \color{blue}{J}\right)\right) \]
      5. Simplified67.9%

        \[\leadsto \color{blue}{\cos \left(0.5 \cdot K\right) \cdot \left(-2 \cdot J\right)} \]
    3. Recombined 2 regimes into one program.
    4. Final simplification43.4%

      \[\leadsto \begin{array}{l} \mathbf{if}\;J \leq 9.5 \cdot 10^{-76}:\\ \;\;\;\;0 - U\\ \mathbf{else}:\\ \;\;\;\;\left(-2 \cdot J\right) \cdot \cos \left(K \cdot 0.5\right)\\ \end{array} \]
    5. Add Preprocessing

    Alternative 5: 39.9% accurate, 32.2× speedup?

    \[\begin{array}{l} U_m = \left|U\right| \\ J\_m = \left|J\right| \\ J\_s = \mathsf{copysign}\left(1, J\right) \\ J\_s \cdot \begin{array}{l} \mathbf{if}\;K \leq 1.75 \cdot 10^{+28}:\\ \;\;\;\;0 - U\_m\\ \mathbf{elif}\;K \leq 3.1 \cdot 10^{+103}:\\ \;\;\;\;U\_m\\ \mathbf{else}:\\ \;\;\;\;0 - U\_m\\ \end{array} \end{array} \]
    U_m = (fabs.f64 U)
    J\_m = (fabs.f64 J)
    J\_s = (copysign.f64 #s(literal 1 binary64) J)
    (FPCore (J_s J_m K U_m)
     :precision binary64
     (* J_s (if (<= K 1.75e+28) (- 0.0 U_m) (if (<= K 3.1e+103) U_m (- 0.0 U_m)))))
    U_m = fabs(U);
    J\_m = fabs(J);
    J\_s = copysign(1.0, J);
    double code(double J_s, double J_m, double K, double U_m) {
    	double tmp;
    	if (K <= 1.75e+28) {
    		tmp = 0.0 - U_m;
    	} else if (K <= 3.1e+103) {
    		tmp = U_m;
    	} else {
    		tmp = 0.0 - U_m;
    	}
    	return J_s * tmp;
    }
    
    U_m = abs(u)
    J\_m = abs(j)
    J\_s = copysign(1.0d0, j)
    real(8) function code(j_s, j_m, k, u_m)
        real(8), intent (in) :: j_s
        real(8), intent (in) :: j_m
        real(8), intent (in) :: k
        real(8), intent (in) :: u_m
        real(8) :: tmp
        if (k <= 1.75d+28) then
            tmp = 0.0d0 - u_m
        else if (k <= 3.1d+103) then
            tmp = u_m
        else
            tmp = 0.0d0 - u_m
        end if
        code = j_s * tmp
    end function
    
    U_m = Math.abs(U);
    J\_m = Math.abs(J);
    J\_s = Math.copySign(1.0, J);
    public static double code(double J_s, double J_m, double K, double U_m) {
    	double tmp;
    	if (K <= 1.75e+28) {
    		tmp = 0.0 - U_m;
    	} else if (K <= 3.1e+103) {
    		tmp = U_m;
    	} else {
    		tmp = 0.0 - U_m;
    	}
    	return J_s * tmp;
    }
    
    U_m = math.fabs(U)
    J\_m = math.fabs(J)
    J\_s = math.copysign(1.0, J)
    def code(J_s, J_m, K, U_m):
    	tmp = 0
    	if K <= 1.75e+28:
    		tmp = 0.0 - U_m
    	elif K <= 3.1e+103:
    		tmp = U_m
    	else:
    		tmp = 0.0 - U_m
    	return J_s * tmp
    
    U_m = abs(U)
    J\_m = abs(J)
    J\_s = copysign(1.0, J)
    function code(J_s, J_m, K, U_m)
    	tmp = 0.0
    	if (K <= 1.75e+28)
    		tmp = Float64(0.0 - U_m);
    	elseif (K <= 3.1e+103)
    		tmp = U_m;
    	else
    		tmp = Float64(0.0 - U_m);
    	end
    	return Float64(J_s * tmp)
    end
    
    U_m = abs(U);
    J\_m = abs(J);
    J\_s = sign(J) * abs(1.0);
    function tmp_2 = code(J_s, J_m, K, U_m)
    	tmp = 0.0;
    	if (K <= 1.75e+28)
    		tmp = 0.0 - U_m;
    	elseif (K <= 3.1e+103)
    		tmp = U_m;
    	else
    		tmp = 0.0 - U_m;
    	end
    	tmp_2 = J_s * tmp;
    end
    
    U_m = N[Abs[U], $MachinePrecision]
    J\_m = N[Abs[J], $MachinePrecision]
    J\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[J]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
    code[J$95$s_, J$95$m_, K_, U$95$m_] := N[(J$95$s * If[LessEqual[K, 1.75e+28], N[(0.0 - U$95$m), $MachinePrecision], If[LessEqual[K, 3.1e+103], U$95$m, N[(0.0 - U$95$m), $MachinePrecision]]]), $MachinePrecision]
    
    \begin{array}{l}
    U_m = \left|U\right|
    \\
    J\_m = \left|J\right|
    \\
    J\_s = \mathsf{copysign}\left(1, J\right)
    
    \\
    J\_s \cdot \begin{array}{l}
    \mathbf{if}\;K \leq 1.75 \cdot 10^{+28}:\\
    \;\;\;\;0 - U\_m\\
    
    \mathbf{elif}\;K \leq 3.1 \cdot 10^{+103}:\\
    \;\;\;\;U\_m\\
    
    \mathbf{else}:\\
    \;\;\;\;0 - U\_m\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if K < 1.75e28 or 3.1000000000000002e103 < K

      1. Initial program 76.7%

        \[\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
      2. Add Preprocessing
      3. Taylor expanded in J around 0

        \[\leadsto \color{blue}{-1 \cdot U} \]
      4. Step-by-step derivation
        1. mul-1-negN/A

          \[\leadsto \mathsf{neg}\left(U\right) \]
        2. neg-sub0N/A

          \[\leadsto 0 - \color{blue}{U} \]
        3. --lowering--.f6428.5%

          \[\leadsto \mathsf{\_.f64}\left(0, \color{blue}{U}\right) \]
      5. Simplified28.5%

        \[\leadsto \color{blue}{0 - U} \]
      6. Step-by-step derivation
        1. sub0-negN/A

          \[\leadsto \mathsf{neg}\left(U\right) \]
        2. neg-lowering-neg.f6428.5%

          \[\leadsto \mathsf{neg.f64}\left(U\right) \]
      7. Applied egg-rr28.5%

        \[\leadsto \color{blue}{-U} \]

      if 1.75e28 < K < 3.1000000000000002e103

      1. Initial program 86.6%

        \[\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
      2. Add Preprocessing
      3. Taylor expanded in U around -inf

        \[\leadsto \color{blue}{U} \]
      4. Step-by-step derivation
        1. Simplified23.5%

          \[\leadsto \color{blue}{U} \]
      5. Recombined 2 regimes into one program.
      6. Final simplification28.2%

        \[\leadsto \begin{array}{l} \mathbf{if}\;K \leq 1.75 \cdot 10^{+28}:\\ \;\;\;\;0 - U\\ \mathbf{elif}\;K \leq 3.1 \cdot 10^{+103}:\\ \;\;\;\;U\\ \mathbf{else}:\\ \;\;\;\;0 - U\\ \end{array} \]
      7. Add Preprocessing

      Alternative 6: 49.3% accurate, 52.4× speedup?

      \[\begin{array}{l} U_m = \left|U\right| \\ J\_m = \left|J\right| \\ J\_s = \mathsf{copysign}\left(1, J\right) \\ J\_s \cdot \begin{array}{l} \mathbf{if}\;J\_m \leq 5.5 \cdot 10^{-31}:\\ \;\;\;\;0 - U\_m\\ \mathbf{else}:\\ \;\;\;\;-2 \cdot J\_m\\ \end{array} \end{array} \]
      U_m = (fabs.f64 U)
      J\_m = (fabs.f64 J)
      J\_s = (copysign.f64 #s(literal 1 binary64) J)
      (FPCore (J_s J_m K U_m)
       :precision binary64
       (* J_s (if (<= J_m 5.5e-31) (- 0.0 U_m) (* -2.0 J_m))))
      U_m = fabs(U);
      J\_m = fabs(J);
      J\_s = copysign(1.0, J);
      double code(double J_s, double J_m, double K, double U_m) {
      	double tmp;
      	if (J_m <= 5.5e-31) {
      		tmp = 0.0 - U_m;
      	} else {
      		tmp = -2.0 * J_m;
      	}
      	return J_s * tmp;
      }
      
      U_m = abs(u)
      J\_m = abs(j)
      J\_s = copysign(1.0d0, j)
      real(8) function code(j_s, j_m, k, u_m)
          real(8), intent (in) :: j_s
          real(8), intent (in) :: j_m
          real(8), intent (in) :: k
          real(8), intent (in) :: u_m
          real(8) :: tmp
          if (j_m <= 5.5d-31) then
              tmp = 0.0d0 - u_m
          else
              tmp = (-2.0d0) * j_m
          end if
          code = j_s * tmp
      end function
      
      U_m = Math.abs(U);
      J\_m = Math.abs(J);
      J\_s = Math.copySign(1.0, J);
      public static double code(double J_s, double J_m, double K, double U_m) {
      	double tmp;
      	if (J_m <= 5.5e-31) {
      		tmp = 0.0 - U_m;
      	} else {
      		tmp = -2.0 * J_m;
      	}
      	return J_s * tmp;
      }
      
      U_m = math.fabs(U)
      J\_m = math.fabs(J)
      J\_s = math.copysign(1.0, J)
      def code(J_s, J_m, K, U_m):
      	tmp = 0
      	if J_m <= 5.5e-31:
      		tmp = 0.0 - U_m
      	else:
      		tmp = -2.0 * J_m
      	return J_s * tmp
      
      U_m = abs(U)
      J\_m = abs(J)
      J\_s = copysign(1.0, J)
      function code(J_s, J_m, K, U_m)
      	tmp = 0.0
      	if (J_m <= 5.5e-31)
      		tmp = Float64(0.0 - U_m);
      	else
      		tmp = Float64(-2.0 * J_m);
      	end
      	return Float64(J_s * tmp)
      end
      
      U_m = abs(U);
      J\_m = abs(J);
      J\_s = sign(J) * abs(1.0);
      function tmp_2 = code(J_s, J_m, K, U_m)
      	tmp = 0.0;
      	if (J_m <= 5.5e-31)
      		tmp = 0.0 - U_m;
      	else
      		tmp = -2.0 * J_m;
      	end
      	tmp_2 = J_s * tmp;
      end
      
      U_m = N[Abs[U], $MachinePrecision]
      J\_m = N[Abs[J], $MachinePrecision]
      J\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[J]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
      code[J$95$s_, J$95$m_, K_, U$95$m_] := N[(J$95$s * If[LessEqual[J$95$m, 5.5e-31], N[(0.0 - U$95$m), $MachinePrecision], N[(-2.0 * J$95$m), $MachinePrecision]]), $MachinePrecision]
      
      \begin{array}{l}
      U_m = \left|U\right|
      \\
      J\_m = \left|J\right|
      \\
      J\_s = \mathsf{copysign}\left(1, J\right)
      
      \\
      J\_s \cdot \begin{array}{l}
      \mathbf{if}\;J\_m \leq 5.5 \cdot 10^{-31}:\\
      \;\;\;\;0 - U\_m\\
      
      \mathbf{else}:\\
      \;\;\;\;-2 \cdot J\_m\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if J < 5.49999999999999958e-31

        1. Initial program 69.9%

          \[\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
        2. Add Preprocessing
        3. Taylor expanded in J around 0

          \[\leadsto \color{blue}{-1 \cdot U} \]
        4. Step-by-step derivation
          1. mul-1-negN/A

            \[\leadsto \mathsf{neg}\left(U\right) \]
          2. neg-sub0N/A

            \[\leadsto 0 - \color{blue}{U} \]
          3. --lowering--.f6432.4%

            \[\leadsto \mathsf{\_.f64}\left(0, \color{blue}{U}\right) \]
        5. Simplified32.4%

          \[\leadsto \color{blue}{0 - U} \]
        6. Step-by-step derivation
          1. sub0-negN/A

            \[\leadsto \mathsf{neg}\left(U\right) \]
          2. neg-lowering-neg.f6432.4%

            \[\leadsto \mathsf{neg.f64}\left(U\right) \]
        7. Applied egg-rr32.4%

          \[\leadsto \color{blue}{-U} \]

        if 5.49999999999999958e-31 < J

        1. Initial program 97.2%

          \[\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
        2. Add Preprocessing
        3. Taylor expanded in J around inf

          \[\leadsto \color{blue}{-2 \cdot \left(J \cdot \cos \left(\frac{1}{2} \cdot K\right)\right)} \]
        4. Step-by-step derivation
          1. associate-*r*N/A

            \[\leadsto \left(-2 \cdot J\right) \cdot \color{blue}{\cos \left(\frac{1}{2} \cdot K\right)} \]
          2. *-commutativeN/A

            \[\leadsto \cos \left(\frac{1}{2} \cdot K\right) \cdot \color{blue}{\left(-2 \cdot J\right)} \]
          3. *-lowering-*.f64N/A

            \[\leadsto \mathsf{*.f64}\left(\cos \left(\frac{1}{2} \cdot K\right), \color{blue}{\left(-2 \cdot J\right)}\right) \]
          4. cos-lowering-cos.f64N/A

            \[\leadsto \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\left(\frac{1}{2} \cdot K\right)\right), \left(\color{blue}{-2} \cdot J\right)\right) \]
          5. *-lowering-*.f64N/A

            \[\leadsto \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, K\right)\right), \left(-2 \cdot J\right)\right) \]
          6. *-lowering-*.f6468.8%

            \[\leadsto \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, K\right)\right), \mathsf{*.f64}\left(-2, \color{blue}{J}\right)\right) \]
        5. Simplified68.8%

          \[\leadsto \color{blue}{\cos \left(0.5 \cdot K\right) \cdot \left(-2 \cdot J\right)} \]
        6. Taylor expanded in K around 0

          \[\leadsto \color{blue}{-2 \cdot J} \]
        7. Step-by-step derivation
          1. *-lowering-*.f6447.2%

            \[\leadsto \mathsf{*.f64}\left(-2, \color{blue}{J}\right) \]
        8. Simplified47.2%

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

        \[\leadsto \begin{array}{l} \mathbf{if}\;J \leq 5.5 \cdot 10^{-31}:\\ \;\;\;\;0 - U\\ \mathbf{else}:\\ \;\;\;\;-2 \cdot J\\ \end{array} \]
      5. Add Preprocessing

      Alternative 7: 14.1% accurate, 420.0× speedup?

      \[\begin{array}{l} U_m = \left|U\right| \\ J\_m = \left|J\right| \\ J\_s = \mathsf{copysign}\left(1, J\right) \\ J\_s \cdot U\_m \end{array} \]
      U_m = (fabs.f64 U)
      J\_m = (fabs.f64 J)
      J\_s = (copysign.f64 #s(literal 1 binary64) J)
      (FPCore (J_s J_m K U_m) :precision binary64 (* J_s U_m))
      U_m = fabs(U);
      J\_m = fabs(J);
      J\_s = copysign(1.0, J);
      double code(double J_s, double J_m, double K, double U_m) {
      	return J_s * U_m;
      }
      
      U_m = abs(u)
      J\_m = abs(j)
      J\_s = copysign(1.0d0, j)
      real(8) function code(j_s, j_m, k, u_m)
          real(8), intent (in) :: j_s
          real(8), intent (in) :: j_m
          real(8), intent (in) :: k
          real(8), intent (in) :: u_m
          code = j_s * u_m
      end function
      
      U_m = Math.abs(U);
      J\_m = Math.abs(J);
      J\_s = Math.copySign(1.0, J);
      public static double code(double J_s, double J_m, double K, double U_m) {
      	return J_s * U_m;
      }
      
      U_m = math.fabs(U)
      J\_m = math.fabs(J)
      J\_s = math.copysign(1.0, J)
      def code(J_s, J_m, K, U_m):
      	return J_s * U_m
      
      U_m = abs(U)
      J\_m = abs(J)
      J\_s = copysign(1.0, J)
      function code(J_s, J_m, K, U_m)
      	return Float64(J_s * U_m)
      end
      
      U_m = abs(U);
      J\_m = abs(J);
      J\_s = sign(J) * abs(1.0);
      function tmp = code(J_s, J_m, K, U_m)
      	tmp = J_s * U_m;
      end
      
      U_m = N[Abs[U], $MachinePrecision]
      J\_m = N[Abs[J], $MachinePrecision]
      J\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[J]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
      code[J$95$s_, J$95$m_, K_, U$95$m_] := N[(J$95$s * U$95$m), $MachinePrecision]
      
      \begin{array}{l}
      U_m = \left|U\right|
      \\
      J\_m = \left|J\right|
      \\
      J\_s = \mathsf{copysign}\left(1, J\right)
      
      \\
      J\_s \cdot U\_m
      \end{array}
      
      Derivation
      1. Initial program 77.2%

        \[\left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
      2. Add Preprocessing
      3. Taylor expanded in U around -inf

        \[\leadsto \color{blue}{U} \]
      4. Step-by-step derivation
        1. Simplified26.8%

          \[\leadsto \color{blue}{U} \]
        2. Add Preprocessing

        Reproduce

        ?
        herbie shell --seed 2024152 
        (FPCore (J K U)
          :name "Maksimov and Kolovsky, Equation (3)"
          :precision binary64
          (* (* (* -2.0 J) (cos (/ K 2.0))) (sqrt (+ 1.0 (pow (/ U (* (* 2.0 J) (cos (/ K 2.0)))) 2.0)))))