Maksimov and Kolovsky, Equation (3)

Percentage Accurate: 73.6% → 98.7%
Time: 14.6s
Alternatives: 7
Speedup: 0.4×

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.6% 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: 98.7% accurate, 0.4× 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 := J\_m \cdot t\_0\\ t_2 := \left(\left(-2 \cdot J\_m\right) \cdot t\_0\right) \cdot \sqrt{1 + {\left(\frac{U\_m}{t\_0 \cdot \left(J\_m \cdot 2\right)}\right)}^{2}}\\ J\_s \cdot \begin{array}{l} \mathbf{if}\;t\_2 \leq -\infty:\\ \;\;\;\;-U\_m\\ \mathbf{elif}\;t\_2 \leq 4 \cdot 10^{+272}:\\ \;\;\;\;-2 \cdot \left(t\_1 \cdot \mathsf{hypot}\left(1, \frac{\frac{U\_m}{2}}{t\_1}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;U\_m\\ \end{array} \end{array} \end{array} \]
U_m = (fabs.f64 U)
J_m = (fabs.f64 J)
J_s = (copysign.f64 1 J)
(FPCore (J_s J_m K U_m)
 :precision binary64
 (let* ((t_0 (cos (/ K 2.0)))
        (t_1 (* J_m t_0))
        (t_2
         (*
          (* (* -2.0 J_m) t_0)
          (sqrt (+ 1.0 (pow (/ U_m (* t_0 (* J_m 2.0))) 2.0))))))
   (*
    J_s
    (if (<= t_2 (- INFINITY))
      (- U_m)
      (if (<= t_2 4e+272)
        (* -2.0 (* t_1 (hypot 1.0 (/ (/ U_m 2.0) t_1))))
        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 t_0 = cos((K / 2.0));
	double t_1 = J_m * t_0;
	double t_2 = ((-2.0 * J_m) * t_0) * sqrt((1.0 + pow((U_m / (t_0 * (J_m * 2.0))), 2.0)));
	double tmp;
	if (t_2 <= -((double) INFINITY)) {
		tmp = -U_m;
	} else if (t_2 <= 4e+272) {
		tmp = -2.0 * (t_1 * hypot(1.0, ((U_m / 2.0) / t_1)));
	} else {
		tmp = U_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 = J_m * t_0;
	double t_2 = ((-2.0 * J_m) * t_0) * Math.sqrt((1.0 + Math.pow((U_m / (t_0 * (J_m * 2.0))), 2.0)));
	double tmp;
	if (t_2 <= -Double.POSITIVE_INFINITY) {
		tmp = -U_m;
	} else if (t_2 <= 4e+272) {
		tmp = -2.0 * (t_1 * Math.hypot(1.0, ((U_m / 2.0) / t_1)));
	} else {
		tmp = 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):
	t_0 = math.cos((K / 2.0))
	t_1 = J_m * t_0
	t_2 = ((-2.0 * J_m) * t_0) * math.sqrt((1.0 + math.pow((U_m / (t_0 * (J_m * 2.0))), 2.0)))
	tmp = 0
	if t_2 <= -math.inf:
		tmp = -U_m
	elif t_2 <= 4e+272:
		tmp = -2.0 * (t_1 * math.hypot(1.0, ((U_m / 2.0) / t_1)))
	else:
		tmp = 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)
	t_0 = cos(Float64(K / 2.0))
	t_1 = Float64(J_m * t_0)
	t_2 = Float64(Float64(Float64(-2.0 * J_m) * t_0) * sqrt(Float64(1.0 + (Float64(U_m / Float64(t_0 * Float64(J_m * 2.0))) ^ 2.0))))
	tmp = 0.0
	if (t_2 <= Float64(-Inf))
		tmp = Float64(-U_m);
	elseif (t_2 <= 4e+272)
		tmp = Float64(-2.0 * Float64(t_1 * hypot(1.0, Float64(Float64(U_m / 2.0) / t_1))));
	else
		tmp = 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)
	t_0 = cos((K / 2.0));
	t_1 = J_m * t_0;
	t_2 = ((-2.0 * J_m) * t_0) * sqrt((1.0 + ((U_m / (t_0 * (J_m * 2.0))) ^ 2.0)));
	tmp = 0.0;
	if (t_2 <= -Inf)
		tmp = -U_m;
	elseif (t_2 <= 4e+272)
		tmp = -2.0 * (t_1 * hypot(1.0, ((U_m / 2.0) / t_1)));
	else
		tmp = 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_] := Block[{t$95$0 = N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(J$95$m * t$95$0), $MachinePrecision]}, Block[{t$95$2 = N[(N[(N[(-2.0 * J$95$m), $MachinePrecision] * t$95$0), $MachinePrecision] * 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]}, N[(J$95$s * If[LessEqual[t$95$2, (-Infinity)], (-U$95$m), If[LessEqual[t$95$2, 4e+272], N[(-2.0 * N[(t$95$1 * N[Sqrt[1.0 ^ 2 + N[(N[(U$95$m / 2.0), $MachinePrecision] / t$95$1), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 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)

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

\mathbf{elif}\;t\_2 \leq 4 \cdot 10^{+272}:\\
\;\;\;\;-2 \cdot \left(t\_1 \cdot \mathsf{hypot}\left(1, \frac{\frac{U\_m}{2}}{t\_1}\right)\right)\\

\mathbf{else}:\\
\;\;\;\;U\_m\\


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

    1. Initial program 5.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. Simplified5.4%

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

      \[\leadsto \color{blue}{-1 \cdot U} \]
    5. Step-by-step derivation
      1. mul-1-neg50.5%

        \[\leadsto \color{blue}{-U} \]
    6. Simplified50.5%

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

    if -inf.0 < (*.f64 (*.f64 (*.f64 -2 J) (cos.f64 (/.f64 K 2))) (sqrt.f64 (+.f64 1 (pow.f64 (/.f64 U (*.f64 (*.f64 2 J) (cos.f64 (/.f64 K 2)))) 2)))) < 4.0000000000000003e272

    1. Initial program 99.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. Step-by-step derivation
      1. associate-*l*99.8%

        \[\leadsto \color{blue}{\left(-2 \cdot \left(J \cdot \cos \left(\frac{K}{2}\right)\right)\right)} \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
      2. associate-*l*99.8%

        \[\leadsto \color{blue}{-2 \cdot \left(\left(J \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}}\right)} \]
      3. unpow299.8%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\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) \]
      4. sqr-neg99.8%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right) \cdot \left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}}\right) \]
      5. distribute-frac-neg99.8%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}} \cdot \left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}\right) \]
      6. distribute-frac-neg99.8%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)} \cdot \color{blue}{\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}}}\right) \]
      7. unpow299.8%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{{\left(\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}}}\right) \]
    3. Simplified99.8%

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

    if 4.0000000000000003e272 < (*.f64 (*.f64 (*.f64 -2 J) (cos.f64 (/.f64 K 2))) (sqrt.f64 (+.f64 1 (pow.f64 (/.f64 U (*.f64 (*.f64 2 J) (cos.f64 (/.f64 K 2)))) 2))))

    1. Initial program 18.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. Simplified18.9%

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

      \[\leadsto \color{blue}{U} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification83.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:\\ \;\;\;\;-U\\ \mathbf{elif}\;\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 4 \cdot 10^{+272}:\\ \;\;\;\;-2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{2}}{J \cdot \cos \left(\frac{K}{2}\right)}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;U\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 51.7% accurate, 0.8× 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 := -2 \cdot \left(J\_m \cdot \cos \left(K \cdot 0.5\right)\right)\\ J\_s \cdot \begin{array}{l} \mathbf{if}\;t\_0 \leq -0.545:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;t\_0 \leq -0.002:\\ \;\;\;\;-2 \cdot \left(U\_m \cdot -0.5 + J\_m \cdot \left(J\_m \cdot \frac{-1}{U\_m}\right)\right)\\ \mathbf{elif}\;t\_0 \leq 1:\\ \;\;\;\;t\_1\\ \mathbf{else}:\\ \;\;\;\;-2 \cdot \left(J\_m \cdot \mathsf{hypot}\left(1, \frac{\frac{U\_m}{2}}{J\_m \cdot t\_0}\right)\right)\\ \end{array} \end{array} \end{array} \]
U_m = (fabs.f64 U)
J_m = (fabs.f64 J)
J_s = (copysign.f64 1 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.545)
      t_1
      (if (<= t_0 -0.002)
        (* -2.0 (+ (* U_m -0.5) (* J_m (* J_m (/ -1.0 U_m)))))
        (if (<= t_0 1.0)
          t_1
          (* -2.0 (* J_m (hypot 1.0 (/ (/ U_m 2.0) (* J_m 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 * cos((K * 0.5)));
	double tmp;
	if (t_0 <= -0.545) {
		tmp = t_1;
	} else if (t_0 <= -0.002) {
		tmp = -2.0 * ((U_m * -0.5) + (J_m * (J_m * (-1.0 / U_m))));
	} else if (t_0 <= 1.0) {
		tmp = t_1;
	} else {
		tmp = -2.0 * (J_m * hypot(1.0, ((U_m / 2.0) / (J_m * 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 * Math.cos((K * 0.5)));
	double tmp;
	if (t_0 <= -0.545) {
		tmp = t_1;
	} else if (t_0 <= -0.002) {
		tmp = -2.0 * ((U_m * -0.5) + (J_m * (J_m * (-1.0 / U_m))));
	} else if (t_0 <= 1.0) {
		tmp = t_1;
	} else {
		tmp = -2.0 * (J_m * Math.hypot(1.0, ((U_m / 2.0) / (J_m * 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 * math.cos((K * 0.5)))
	tmp = 0
	if t_0 <= -0.545:
		tmp = t_1
	elif t_0 <= -0.002:
		tmp = -2.0 * ((U_m * -0.5) + (J_m * (J_m * (-1.0 / U_m))))
	elif t_0 <= 1.0:
		tmp = t_1
	else:
		tmp = -2.0 * (J_m * math.hypot(1.0, ((U_m / 2.0) / (J_m * 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(-2.0 * Float64(J_m * cos(Float64(K * 0.5))))
	tmp = 0.0
	if (t_0 <= -0.545)
		tmp = t_1;
	elseif (t_0 <= -0.002)
		tmp = Float64(-2.0 * Float64(Float64(U_m * -0.5) + Float64(J_m * Float64(J_m * Float64(-1.0 / U_m)))));
	elseif (t_0 <= 1.0)
		tmp = t_1;
	else
		tmp = Float64(-2.0 * Float64(J_m * hypot(1.0, Float64(Float64(U_m / 2.0) / Float64(J_m * 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 * cos((K * 0.5)));
	tmp = 0.0;
	if (t_0 <= -0.545)
		tmp = t_1;
	elseif (t_0 <= -0.002)
		tmp = -2.0 * ((U_m * -0.5) + (J_m * (J_m * (-1.0 / U_m))));
	elseif (t_0 <= 1.0)
		tmp = t_1;
	else
		tmp = -2.0 * (J_m * hypot(1.0, ((U_m / 2.0) / (J_m * 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[(-2.0 * N[(J$95$m * N[Cos[N[(K * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, N[(J$95$s * If[LessEqual[t$95$0, -0.545], t$95$1, If[LessEqual[t$95$0, -0.002], N[(-2.0 * N[(N[(U$95$m * -0.5), $MachinePrecision] + N[(J$95$m * N[(J$95$m * N[(-1.0 / U$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 1.0], t$95$1, N[(-2.0 * N[(J$95$m * N[Sqrt[1.0 ^ 2 + N[(N[(U$95$m / 2.0), $MachinePrecision] / N[(J$95$m * t$95$0), $MachinePrecision]), $MachinePrecision] ^ 2], $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)

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

\mathbf{elif}\;t\_0 \leq -0.002:\\
\;\;\;\;-2 \cdot \left(U\_m \cdot -0.5 + J\_m \cdot \left(J\_m \cdot \frac{-1}{U\_m}\right)\right)\\

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

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


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

    1. Initial program 75.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. Step-by-step derivation
      1. associate-*l*75.7%

        \[\leadsto \color{blue}{\left(-2 \cdot \left(J \cdot \cos \left(\frac{K}{2}\right)\right)\right)} \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
      2. associate-*l*75.7%

        \[\leadsto \color{blue}{-2 \cdot \left(\left(J \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}}\right)} \]
      3. unpow275.7%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\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) \]
      4. sqr-neg75.7%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right) \cdot \left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}}\right) \]
      5. distribute-frac-neg75.7%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}} \cdot \left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}\right) \]
      6. distribute-frac-neg75.7%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)} \cdot \color{blue}{\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}}}\right) \]
      7. unpow275.7%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{{\left(\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}}}\right) \]
    3. Simplified89.6%

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

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

    if -0.54500000000000004 < (cos.f64 (/.f64 K 2)) < -2e-3

    1. Initial program 73.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. Step-by-step derivation
      1. associate-*l*73.2%

        \[\leadsto \color{blue}{\left(-2 \cdot \left(J \cdot \cos \left(\frac{K}{2}\right)\right)\right)} \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
      2. associate-*l*73.2%

        \[\leadsto \color{blue}{-2 \cdot \left(\left(J \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}}\right)} \]
      3. unpow273.2%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\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) \]
      4. sqr-neg73.2%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right) \cdot \left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}}\right) \]
      5. distribute-frac-neg73.2%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}} \cdot \left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}\right) \]
      6. distribute-frac-neg73.2%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)} \cdot \color{blue}{\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}}}\right) \]
      7. unpow273.2%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{{\left(\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}}}\right) \]
    3. Simplified92.4%

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

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

      \[\leadsto -2 \cdot \left(\mathsf{hypot}\left(1, \frac{\frac{U}{2}}{\color{blue}{J}}\right) \cdot J\right) \]
    7. Taylor expanded in U around -inf 33.5%

      \[\leadsto -2 \cdot \color{blue}{\left(-1 \cdot \frac{{J}^{2}}{U} + -0.5 \cdot U\right)} \]
    8. Step-by-step derivation
      1. +-commutative33.5%

        \[\leadsto -2 \cdot \color{blue}{\left(-0.5 \cdot U + -1 \cdot \frac{{J}^{2}}{U}\right)} \]
      2. mul-1-neg33.5%

        \[\leadsto -2 \cdot \left(-0.5 \cdot U + \color{blue}{\left(-\frac{{J}^{2}}{U}\right)}\right) \]
      3. unsub-neg33.5%

        \[\leadsto -2 \cdot \color{blue}{\left(-0.5 \cdot U - \frac{{J}^{2}}{U}\right)} \]
      4. *-commutative33.5%

        \[\leadsto -2 \cdot \left(\color{blue}{U \cdot -0.5} - \frac{{J}^{2}}{U}\right) \]
    9. Simplified33.5%

      \[\leadsto -2 \cdot \color{blue}{\left(U \cdot -0.5 - \frac{{J}^{2}}{U}\right)} \]
    10. Step-by-step derivation
      1. div-inv33.5%

        \[\leadsto -2 \cdot \left(U \cdot -0.5 - \color{blue}{{J}^{2} \cdot \frac{1}{U}}\right) \]
      2. unpow233.5%

        \[\leadsto -2 \cdot \left(U \cdot -0.5 - \color{blue}{\left(J \cdot J\right)} \cdot \frac{1}{U}\right) \]
      3. associate-*l*37.1%

        \[\leadsto -2 \cdot \left(U \cdot -0.5 - \color{blue}{J \cdot \left(J \cdot \frac{1}{U}\right)}\right) \]
    11. Applied egg-rr37.1%

      \[\leadsto -2 \cdot \left(U \cdot -0.5 - \color{blue}{J \cdot \left(J \cdot \frac{1}{U}\right)}\right) \]

    if 1 < (cos.f64 (/.f64 K 2))

    1. Initial program 75.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. Step-by-step derivation
      1. associate-*l*75.4%

        \[\leadsto \color{blue}{\left(-2 \cdot \left(J \cdot \cos \left(\frac{K}{2}\right)\right)\right)} \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
      2. associate-*l*75.4%

        \[\leadsto \color{blue}{-2 \cdot \left(\left(J \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}}\right)} \]
      3. unpow275.4%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\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) \]
      4. sqr-neg75.4%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right) \cdot \left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}}\right) \]
      5. distribute-frac-neg75.4%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}} \cdot \left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}\right) \]
      6. distribute-frac-neg75.4%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)} \cdot \color{blue}{\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}}}\right) \]
      7. unpow275.4%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{{\left(\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}}}\right) \]
    3. Simplified89.9%

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

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

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

Alternative 3: 66.2% accurate, 3.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}\;J\_m \leq 3.05 \cdot 10^{-136} \lor \neg \left(J\_m \leq 2.7 \cdot 10^{-114}\right) \land \left(J\_m \leq 1.55 \cdot 10^{-29} \lor \neg \left(J\_m \leq 4 \cdot 10^{+46}\right) \land J\_m \leq 1.16 \cdot 10^{+51}\right):\\ \;\;\;\;-U\_m\\ \mathbf{else}:\\ \;\;\;\;-2 \cdot \left(J\_m \cdot \cos \left(K \cdot 0.5\right)\right)\\ \end{array} \end{array} \]
U_m = (fabs.f64 U)
J_m = (fabs.f64 J)
J_s = (copysign.f64 1 J)
(FPCore (J_s J_m K U_m)
 :precision binary64
 (*
  J_s
  (if (or (<= J_m 3.05e-136)
          (and (not (<= J_m 2.7e-114))
               (or (<= J_m 1.55e-29)
                   (and (not (<= J_m 4e+46)) (<= J_m 1.16e+51)))))
    (- 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 <= 3.05e-136) || (!(J_m <= 2.7e-114) && ((J_m <= 1.55e-29) || (!(J_m <= 4e+46) && (J_m <= 1.16e+51))))) {
		tmp = -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 <= 3.05d-136) .or. (.not. (j_m <= 2.7d-114)) .and. (j_m <= 1.55d-29) .or. (.not. (j_m <= 4d+46)) .and. (j_m <= 1.16d+51)) then
        tmp = -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 <= 3.05e-136) || (!(J_m <= 2.7e-114) && ((J_m <= 1.55e-29) || (!(J_m <= 4e+46) && (J_m <= 1.16e+51))))) {
		tmp = -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 <= 3.05e-136) or (not (J_m <= 2.7e-114) and ((J_m <= 1.55e-29) or (not (J_m <= 4e+46) and (J_m <= 1.16e+51)))):
		tmp = -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 <= 3.05e-136) || (!(J_m <= 2.7e-114) && ((J_m <= 1.55e-29) || (!(J_m <= 4e+46) && (J_m <= 1.16e+51)))))
		tmp = Float64(-U_m);
	else
		tmp = Float64(-2.0 * Float64(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 <= 3.05e-136) || (~((J_m <= 2.7e-114)) && ((J_m <= 1.55e-29) || (~((J_m <= 4e+46)) && (J_m <= 1.16e+51)))))
		tmp = -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[Or[LessEqual[J$95$m, 3.05e-136], And[N[Not[LessEqual[J$95$m, 2.7e-114]], $MachinePrecision], Or[LessEqual[J$95$m, 1.55e-29], And[N[Not[LessEqual[J$95$m, 4e+46]], $MachinePrecision], LessEqual[J$95$m, 1.16e+51]]]]], (-U$95$m), N[(-2.0 * N[(J$95$m * N[Cos[N[(K * 0.5), $MachinePrecision]], $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 3.05 \cdot 10^{-136} \lor \neg \left(J\_m \leq 2.7 \cdot 10^{-114}\right) \land \left(J\_m \leq 1.55 \cdot 10^{-29} \lor \neg \left(J\_m \leq 4 \cdot 10^{+46}\right) \land J\_m \leq 1.16 \cdot 10^{+51}\right):\\
\;\;\;\;-U\_m\\

\mathbf{else}:\\
\;\;\;\;-2 \cdot \left(J\_m \cdot \cos \left(K \cdot 0.5\right)\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if J < 3.0499999999999999e-136 or 2.7e-114 < J < 1.55000000000000013e-29 or 4e46 < J < 1.16e51

    1. Initial program 66.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. Simplified66.4%

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

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

        \[\leadsto \color{blue}{-U} \]
    6. Simplified37.4%

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

    if 3.0499999999999999e-136 < J < 2.7e-114 or 1.55000000000000013e-29 < J < 4e46 or 1.16e51 < J

    1. Initial program 94.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. Step-by-step derivation
      1. associate-*l*94.2%

        \[\leadsto \color{blue}{\left(-2 \cdot \left(J \cdot \cos \left(\frac{K}{2}\right)\right)\right)} \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
      2. associate-*l*94.2%

        \[\leadsto \color{blue}{-2 \cdot \left(\left(J \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}}\right)} \]
      3. unpow294.2%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\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) \]
      4. sqr-neg94.2%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right) \cdot \left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}}\right) \]
      5. distribute-frac-neg94.2%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}} \cdot \left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}\right) \]
      6. distribute-frac-neg94.2%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)} \cdot \color{blue}{\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}}}\right) \]
      7. unpow294.2%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{{\left(\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}}}\right) \]
    3. Simplified99.8%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;J \leq 3.05 \cdot 10^{-136} \lor \neg \left(J \leq 2.7 \cdot 10^{-114}\right) \land \left(J \leq 1.55 \cdot 10^{-29} \lor \neg \left(J \leq 4 \cdot 10^{+46}\right) \land J \leq 1.16 \cdot 10^{+51}\right):\\ \;\;\;\;-U\\ \mathbf{else}:\\ \;\;\;\;-2 \cdot \left(J \cdot \cos \left(K \cdot 0.5\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 62.7% 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}\;K \leq 1.35 \cdot 10^{-17}:\\ \;\;\;\;-2 \cdot \left(J\_m \cdot \mathsf{hypot}\left(1, \frac{\frac{U\_m}{2}}{J\_m}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;-2 \cdot \left(J\_m \cdot \cos \left(K \cdot 0.5\right)\right)\\ \end{array} \end{array} \]
U_m = (fabs.f64 U)
J_m = (fabs.f64 J)
J_s = (copysign.f64 1 J)
(FPCore (J_s J_m K U_m)
 :precision binary64
 (*
  J_s
  (if (<= K 1.35e-17)
    (* -2.0 (* J_m (hypot 1.0 (/ (/ U_m 2.0) J_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 (K <= 1.35e-17) {
		tmp = -2.0 * (J_m * hypot(1.0, ((U_m / 2.0) / J_m)));
	} else {
		tmp = -2.0 * (J_m * cos((K * 0.5)));
	}
	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 (K <= 1.35e-17) {
		tmp = -2.0 * (J_m * Math.hypot(1.0, ((U_m / 2.0) / J_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 K <= 1.35e-17:
		tmp = -2.0 * (J_m * math.hypot(1.0, ((U_m / 2.0) / J_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 (K <= 1.35e-17)
		tmp = Float64(-2.0 * Float64(J_m * hypot(1.0, Float64(Float64(U_m / 2.0) / J_m))));
	else
		tmp = Float64(-2.0 * Float64(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 (K <= 1.35e-17)
		tmp = -2.0 * (J_m * hypot(1.0, ((U_m / 2.0) / J_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[K, 1.35e-17], N[(-2.0 * N[(J$95$m * N[Sqrt[1.0 ^ 2 + N[(N[(U$95$m / 2.0), $MachinePrecision] / J$95$m), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(-2.0 * N[(J$95$m * N[Cos[N[(K * 0.5), $MachinePrecision]], $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}\;K \leq 1.35 \cdot 10^{-17}:\\
\;\;\;\;-2 \cdot \left(J\_m \cdot \mathsf{hypot}\left(1, \frac{\frac{U\_m}{2}}{J\_m}\right)\right)\\

\mathbf{else}:\\
\;\;\;\;-2 \cdot \left(J\_m \cdot \cos \left(K \cdot 0.5\right)\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if K < 1.3500000000000001e-17

    1. Initial program 76.0%

      \[\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. associate-*l*76.0%

        \[\leadsto \color{blue}{\left(-2 \cdot \left(J \cdot \cos \left(\frac{K}{2}\right)\right)\right)} \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
      2. associate-*l*76.0%

        \[\leadsto \color{blue}{-2 \cdot \left(\left(J \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}}\right)} \]
      3. unpow276.0%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\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) \]
      4. sqr-neg76.0%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right) \cdot \left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}}\right) \]
      5. distribute-frac-neg76.0%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}} \cdot \left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}\right) \]
      6. distribute-frac-neg76.0%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)} \cdot \color{blue}{\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}}}\right) \]
      7. unpow276.0%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{{\left(\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}}}\right) \]
    3. Simplified91.4%

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

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

      \[\leadsto -2 \cdot \left(\mathsf{hypot}\left(1, \frac{\frac{U}{2}}{\color{blue}{J}}\right) \cdot J\right) \]

    if 1.3500000000000001e-17 < K

    1. Initial program 74.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. associate-*l*74.1%

        \[\leadsto \color{blue}{\left(-2 \cdot \left(J \cdot \cos \left(\frac{K}{2}\right)\right)\right)} \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
      2. associate-*l*74.1%

        \[\leadsto \color{blue}{-2 \cdot \left(\left(J \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}}\right)} \]
      3. unpow274.1%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\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) \]
      4. sqr-neg74.1%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right) \cdot \left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}}\right) \]
      5. distribute-frac-neg74.1%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}} \cdot \left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}\right) \]
      6. distribute-frac-neg74.1%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)} \cdot \color{blue}{\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}}}\right) \]
      7. unpow274.1%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{{\left(\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}}}\right) \]
    3. Simplified86.4%

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

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

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

Alternative 5: 50.4% accurate, 23.3× 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 1650000000000 \lor \neg \left(J\_m \leq 3.8 \cdot 10^{+38}\right) \land J\_m \leq 7.2 \cdot 10^{+71}:\\ \;\;\;\;-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 1 J)
(FPCore (J_s J_m K U_m)
 :precision binary64
 (*
  J_s
  (if (or (<= J_m 1650000000000.0)
          (and (not (<= J_m 3.8e+38)) (<= J_m 7.2e+71)))
    (- 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 <= 1650000000000.0) || (!(J_m <= 3.8e+38) && (J_m <= 7.2e+71))) {
		tmp = -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 <= 1650000000000.0d0) .or. (.not. (j_m <= 3.8d+38)) .and. (j_m <= 7.2d+71)) then
        tmp = -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 <= 1650000000000.0) || (!(J_m <= 3.8e+38) && (J_m <= 7.2e+71))) {
		tmp = -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 <= 1650000000000.0) or (not (J_m <= 3.8e+38) and (J_m <= 7.2e+71)):
		tmp = -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 <= 1650000000000.0) || (!(J_m <= 3.8e+38) && (J_m <= 7.2e+71)))
		tmp = Float64(-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 <= 1650000000000.0) || (~((J_m <= 3.8e+38)) && (J_m <= 7.2e+71)))
		tmp = -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[Or[LessEqual[J$95$m, 1650000000000.0], And[N[Not[LessEqual[J$95$m, 3.8e+38]], $MachinePrecision], LessEqual[J$95$m, 7.2e+71]]], (-U$95$m), 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 1650000000000 \lor \neg \left(J\_m \leq 3.8 \cdot 10^{+38}\right) \land J\_m \leq 7.2 \cdot 10^{+71}:\\
\;\;\;\;-U\_m\\

\mathbf{else}:\\
\;\;\;\;-2 \cdot J\_m\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if J < 1.65e12 or 3.7999999999999998e38 < J < 7.1999999999999999e71

    1. Initial program 68.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. Simplified68.4%

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

      \[\leadsto \color{blue}{-1 \cdot U} \]
    5. Step-by-step derivation
      1. mul-1-neg35.9%

        \[\leadsto \color{blue}{-U} \]
    6. Simplified35.9%

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

    if 1.65e12 < J < 3.7999999999999998e38 or 7.1999999999999999e71 < J

    1. Initial program 97.0%

      \[\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. associate-*l*97.0%

        \[\leadsto \color{blue}{\left(-2 \cdot \left(J \cdot \cos \left(\frac{K}{2}\right)\right)\right)} \cdot \sqrt{1 + {\left(\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}} \]
      2. associate-*l*97.0%

        \[\leadsto \color{blue}{-2 \cdot \left(\left(J \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}}\right)} \]
      3. unpow297.0%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\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) \]
      4. sqr-neg97.0%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right) \cdot \left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}}\right) \]
      5. distribute-frac-neg97.0%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}} \cdot \left(-\frac{U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}\right) \]
      6. distribute-frac-neg97.0%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)} \cdot \color{blue}{\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}}}\right) \]
      7. unpow297.0%

        \[\leadsto -2 \cdot \left(\left(J \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \sqrt{1 + \color{blue}{{\left(\frac{-U}{\left(2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)}\right)}^{2}}}\right) \]
    3. Simplified99.8%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;J \leq 1650000000000 \lor \neg \left(J \leq 3.8 \cdot 10^{+38}\right) \land J \leq 7.2 \cdot 10^{+71}:\\ \;\;\;\;-U\\ \mathbf{else}:\\ \;\;\;\;-2 \cdot J\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 39.4% accurate, 24.6× 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 54 \lor \neg \left(K \leq 2.3 \cdot 10^{+94}\right) \land K \leq 8.6 \cdot 10^{+237}:\\ \;\;\;\;-U\_m\\ \mathbf{else}:\\ \;\;\;\;U\_m\\ \end{array} \end{array} \]
U_m = (fabs.f64 U)
J_m = (fabs.f64 J)
J_s = (copysign.f64 1 J)
(FPCore (J_s J_m K U_m)
 :precision binary64
 (*
  J_s
  (if (or (<= K 54.0) (and (not (<= K 2.3e+94)) (<= K 8.6e+237)))
    (- U_m)
    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 <= 54.0) || (!(K <= 2.3e+94) && (K <= 8.6e+237))) {
		tmp = -U_m;
	} else {
		tmp = 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 <= 54.0d0) .or. (.not. (k <= 2.3d+94)) .and. (k <= 8.6d+237)) then
        tmp = -u_m
    else
        tmp = 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 <= 54.0) || (!(K <= 2.3e+94) && (K <= 8.6e+237))) {
		tmp = -U_m;
	} else {
		tmp = 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 <= 54.0) or (not (K <= 2.3e+94) and (K <= 8.6e+237)):
		tmp = -U_m
	else:
		tmp = 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 <= 54.0) || (!(K <= 2.3e+94) && (K <= 8.6e+237)))
		tmp = Float64(-U_m);
	else
		tmp = 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 <= 54.0) || (~((K <= 2.3e+94)) && (K <= 8.6e+237)))
		tmp = -U_m;
	else
		tmp = 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[Or[LessEqual[K, 54.0], And[N[Not[LessEqual[K, 2.3e+94]], $MachinePrecision], LessEqual[K, 8.6e+237]]], (-U$95$m), 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 \begin{array}{l}
\mathbf{if}\;K \leq 54 \lor \neg \left(K \leq 2.3 \cdot 10^{+94}\right) \land K \leq 8.6 \cdot 10^{+237}:\\
\;\;\;\;-U\_m\\

\mathbf{else}:\\
\;\;\;\;U\_m\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if K < 54 or 2.3e94 < K < 8.5999999999999997e237

    1. Initial program 76.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. Simplified76.3%

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

      \[\leadsto \color{blue}{-1 \cdot U} \]
    5. Step-by-step derivation
      1. mul-1-neg31.1%

        \[\leadsto \color{blue}{-U} \]
    6. Simplified31.1%

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

    if 54 < K < 2.3e94 or 8.5999999999999997e237 < K

    1. Initial program 70.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. Simplified70.5%

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

      \[\leadsto \color{blue}{U} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification30.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;K \leq 54 \lor \neg \left(K \leq 2.3 \cdot 10^{+94}\right) \land K \leq 8.6 \cdot 10^{+237}:\\ \;\;\;\;-U\\ \mathbf{else}:\\ \;\;\;\;U\\ \end{array} \]
  5. Add Preprocessing

Alternative 7: 13.9% 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 1 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 75.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. Simplified75.4%

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

    \[\leadsto \color{blue}{U} \]
  5. Final simplification20.3%

    \[\leadsto U \]
  6. Add Preprocessing

Reproduce

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