Maksimov and Kolovsky, Equation (3)

Percentage Accurate: 73.2% → 99.5%
Time: 16.0s
Alternatives: 6
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 6 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.2% 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: 99.5% accurate, 0.4× speedup?

\[\begin{array}{l} U_m = \left|U\right| \\ \begin{array}{l} t_0 := \cos \left(\frac{K}{2}\right)\\ t_1 := \left(t_0 \cdot \left(-2 \cdot J\right)\right) \cdot \sqrt{1 + {\left(\frac{U_m}{t_0 \cdot \left(J \cdot 2\right)}\right)}^{2}}\\ t_2 := J \cdot t_0\\ \mathbf{if}\;t_1 \leq -\infty:\\ \;\;\;\;-U_m\\ \mathbf{elif}\;t_1 \leq 5 \cdot 10^{+301}:\\ \;\;\;\;-2 \cdot \left(t_2 \cdot \mathsf{hypot}\left(1, \frac{\frac{U_m}{2}}{t_2}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;U_m\\ \end{array} \end{array} \]
U_m = (fabs.f64 U)
(FPCore (J K U_m)
 :precision binary64
 (let* ((t_0 (cos (/ K 2.0)))
        (t_1
         (*
          (* t_0 (* -2.0 J))
          (sqrt (+ 1.0 (pow (/ U_m (* t_0 (* J 2.0))) 2.0)))))
        (t_2 (* J t_0)))
   (if (<= t_1 (- INFINITY))
     (- U_m)
     (if (<= t_1 5e+301)
       (* -2.0 (* t_2 (hypot 1.0 (/ (/ U_m 2.0) t_2))))
       U_m))))
U_m = fabs(U);
double code(double J, double K, double U_m) {
	double t_0 = cos((K / 2.0));
	double t_1 = (t_0 * (-2.0 * J)) * sqrt((1.0 + pow((U_m / (t_0 * (J * 2.0))), 2.0)));
	double t_2 = J * t_0;
	double tmp;
	if (t_1 <= -((double) INFINITY)) {
		tmp = -U_m;
	} else if (t_1 <= 5e+301) {
		tmp = -2.0 * (t_2 * hypot(1.0, ((U_m / 2.0) / t_2)));
	} else {
		tmp = U_m;
	}
	return tmp;
}
U_m = Math.abs(U);
public static double code(double J, double K, double U_m) {
	double t_0 = Math.cos((K / 2.0));
	double t_1 = (t_0 * (-2.0 * J)) * Math.sqrt((1.0 + Math.pow((U_m / (t_0 * (J * 2.0))), 2.0)));
	double t_2 = J * t_0;
	double tmp;
	if (t_1 <= -Double.POSITIVE_INFINITY) {
		tmp = -U_m;
	} else if (t_1 <= 5e+301) {
		tmp = -2.0 * (t_2 * Math.hypot(1.0, ((U_m / 2.0) / t_2)));
	} else {
		tmp = U_m;
	}
	return tmp;
}
U_m = math.fabs(U)
def code(J, K, U_m):
	t_0 = math.cos((K / 2.0))
	t_1 = (t_0 * (-2.0 * J)) * math.sqrt((1.0 + math.pow((U_m / (t_0 * (J * 2.0))), 2.0)))
	t_2 = J * t_0
	tmp = 0
	if t_1 <= -math.inf:
		tmp = -U_m
	elif t_1 <= 5e+301:
		tmp = -2.0 * (t_2 * math.hypot(1.0, ((U_m / 2.0) / t_2)))
	else:
		tmp = U_m
	return tmp
U_m = abs(U)
function code(J, K, U_m)
	t_0 = cos(Float64(K / 2.0))
	t_1 = Float64(Float64(t_0 * Float64(-2.0 * J)) * sqrt(Float64(1.0 + (Float64(U_m / Float64(t_0 * Float64(J * 2.0))) ^ 2.0))))
	t_2 = Float64(J * t_0)
	tmp = 0.0
	if (t_1 <= Float64(-Inf))
		tmp = Float64(-U_m);
	elseif (t_1 <= 5e+301)
		tmp = Float64(-2.0 * Float64(t_2 * hypot(1.0, Float64(Float64(U_m / 2.0) / t_2))));
	else
		tmp = U_m;
	end
	return tmp
end
U_m = abs(U);
function tmp_2 = code(J, K, U_m)
	t_0 = cos((K / 2.0));
	t_1 = (t_0 * (-2.0 * J)) * sqrt((1.0 + ((U_m / (t_0 * (J * 2.0))) ^ 2.0)));
	t_2 = J * t_0;
	tmp = 0.0;
	if (t_1 <= -Inf)
		tmp = -U_m;
	elseif (t_1 <= 5e+301)
		tmp = -2.0 * (t_2 * hypot(1.0, ((U_m / 2.0) / t_2)));
	else
		tmp = U_m;
	end
	tmp_2 = tmp;
end
U_m = N[Abs[U], $MachinePrecision]
code[J_, K_, U$95$m_] := Block[{t$95$0 = N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[(t$95$0 * N[(-2.0 * J), $MachinePrecision]), $MachinePrecision] * N[Sqrt[N[(1.0 + N[Power[N[(U$95$m / N[(t$95$0 * N[(J * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(J * t$95$0), $MachinePrecision]}, If[LessEqual[t$95$1, (-Infinity)], (-U$95$m), If[LessEqual[t$95$1, 5e+301], N[(-2.0 * N[(t$95$2 * N[Sqrt[1.0 ^ 2 + N[(N[(U$95$m / 2.0), $MachinePrecision] / t$95$2), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], U$95$m]]]]]
\begin{array}{l}
U_m = \left|U\right|

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

\mathbf{elif}\;t_1 \leq 5 \cdot 10^{+301}:\\
\;\;\;\;-2 \cdot \left(t_2 \cdot \mathsf{hypot}\left(1, \frac{\frac{U_m}{2}}{t_2}\right)\right)\\

\mathbf{else}:\\
\;\;\;\;U_m\\


\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.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. Simplified5.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 J around 0 67.0%

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

        \[\leadsto \color{blue}{-U} \]
    6. Simplified67.0%

      \[\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)))) < 5.0000000000000004e301

    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 5.0000000000000004e301 < (*.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 7.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. Simplified7.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 56.2%

      \[\leadsto \color{blue}{U} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification88.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(\cos \left(\frac{K}{2}\right) \cdot \left(-2 \cdot J\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(\cos \left(\frac{K}{2}\right) \cdot \left(-2 \cdot J\right)\right) \cdot \sqrt{1 + {\left(\frac{U}{\cos \left(\frac{K}{2}\right) \cdot \left(J \cdot 2\right)}\right)}^{2}} \leq 5 \cdot 10^{+301}:\\ \;\;\;\;-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: 64.7% accurate, 0.7× speedup?

\[\begin{array}{l} U_m = \left|U\right| \\ \begin{array}{l} t_0 := \cos \left(K \cdot 0.5\right)\\ t_1 := \cos \left(\frac{K}{2}\right)\\ t_2 := -2 \cdot \left(U_m \cdot -0.5 - \frac{{\left(J \cdot t_0\right)}^{2}}{U_m}\right)\\ t_3 := t_0 \cdot \left(-2 \cdot J\right)\\ \mathbf{if}\;t_1 \leq -0.8:\\ \;\;\;\;t_2\\ \mathbf{elif}\;t_1 \leq -0.42:\\ \;\;\;\;t_3\\ \mathbf{elif}\;t_1 \leq -0.012:\\ \;\;\;\;t_2\\ \mathbf{elif}\;t_1 \leq 0.999:\\ \;\;\;\;t_3\\ \mathbf{else}:\\ \;\;\;\;\left(-2 \cdot J\right) \cdot \mathsf{hypot}\left(1, \frac{\frac{U_m}{J \cdot 2}}{t_1}\right)\\ \end{array} \end{array} \]
U_m = (fabs.f64 U)
(FPCore (J K U_m)
 :precision binary64
 (let* ((t_0 (cos (* K 0.5)))
        (t_1 (cos (/ K 2.0)))
        (t_2 (* -2.0 (- (* U_m -0.5) (/ (pow (* J t_0) 2.0) U_m))))
        (t_3 (* t_0 (* -2.0 J))))
   (if (<= t_1 -0.8)
     t_2
     (if (<= t_1 -0.42)
       t_3
       (if (<= t_1 -0.012)
         t_2
         (if (<= t_1 0.999)
           t_3
           (* (* -2.0 J) (hypot 1.0 (/ (/ U_m (* J 2.0)) t_1)))))))))
U_m = fabs(U);
double code(double J, double K, double U_m) {
	double t_0 = cos((K * 0.5));
	double t_1 = cos((K / 2.0));
	double t_2 = -2.0 * ((U_m * -0.5) - (pow((J * t_0), 2.0) / U_m));
	double t_3 = t_0 * (-2.0 * J);
	double tmp;
	if (t_1 <= -0.8) {
		tmp = t_2;
	} else if (t_1 <= -0.42) {
		tmp = t_3;
	} else if (t_1 <= -0.012) {
		tmp = t_2;
	} else if (t_1 <= 0.999) {
		tmp = t_3;
	} else {
		tmp = (-2.0 * J) * hypot(1.0, ((U_m / (J * 2.0)) / t_1));
	}
	return tmp;
}
U_m = Math.abs(U);
public static double code(double J, double K, double U_m) {
	double t_0 = Math.cos((K * 0.5));
	double t_1 = Math.cos((K / 2.0));
	double t_2 = -2.0 * ((U_m * -0.5) - (Math.pow((J * t_0), 2.0) / U_m));
	double t_3 = t_0 * (-2.0 * J);
	double tmp;
	if (t_1 <= -0.8) {
		tmp = t_2;
	} else if (t_1 <= -0.42) {
		tmp = t_3;
	} else if (t_1 <= -0.012) {
		tmp = t_2;
	} else if (t_1 <= 0.999) {
		tmp = t_3;
	} else {
		tmp = (-2.0 * J) * Math.hypot(1.0, ((U_m / (J * 2.0)) / t_1));
	}
	return tmp;
}
U_m = math.fabs(U)
def code(J, K, U_m):
	t_0 = math.cos((K * 0.5))
	t_1 = math.cos((K / 2.0))
	t_2 = -2.0 * ((U_m * -0.5) - (math.pow((J * t_0), 2.0) / U_m))
	t_3 = t_0 * (-2.0 * J)
	tmp = 0
	if t_1 <= -0.8:
		tmp = t_2
	elif t_1 <= -0.42:
		tmp = t_3
	elif t_1 <= -0.012:
		tmp = t_2
	elif t_1 <= 0.999:
		tmp = t_3
	else:
		tmp = (-2.0 * J) * math.hypot(1.0, ((U_m / (J * 2.0)) / t_1))
	return tmp
U_m = abs(U)
function code(J, K, U_m)
	t_0 = cos(Float64(K * 0.5))
	t_1 = cos(Float64(K / 2.0))
	t_2 = Float64(-2.0 * Float64(Float64(U_m * -0.5) - Float64((Float64(J * t_0) ^ 2.0) / U_m)))
	t_3 = Float64(t_0 * Float64(-2.0 * J))
	tmp = 0.0
	if (t_1 <= -0.8)
		tmp = t_2;
	elseif (t_1 <= -0.42)
		tmp = t_3;
	elseif (t_1 <= -0.012)
		tmp = t_2;
	elseif (t_1 <= 0.999)
		tmp = t_3;
	else
		tmp = Float64(Float64(-2.0 * J) * hypot(1.0, Float64(Float64(U_m / Float64(J * 2.0)) / t_1)));
	end
	return tmp
end
U_m = abs(U);
function tmp_2 = code(J, K, U_m)
	t_0 = cos((K * 0.5));
	t_1 = cos((K / 2.0));
	t_2 = -2.0 * ((U_m * -0.5) - (((J * t_0) ^ 2.0) / U_m));
	t_3 = t_0 * (-2.0 * J);
	tmp = 0.0;
	if (t_1 <= -0.8)
		tmp = t_2;
	elseif (t_1 <= -0.42)
		tmp = t_3;
	elseif (t_1 <= -0.012)
		tmp = t_2;
	elseif (t_1 <= 0.999)
		tmp = t_3;
	else
		tmp = (-2.0 * J) * hypot(1.0, ((U_m / (J * 2.0)) / t_1));
	end
	tmp_2 = tmp;
end
U_m = N[Abs[U], $MachinePrecision]
code[J_, K_, U$95$m_] := Block[{t$95$0 = N[Cos[N[(K * 0.5), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$2 = N[(-2.0 * N[(N[(U$95$m * -0.5), $MachinePrecision] - N[(N[Power[N[(J * t$95$0), $MachinePrecision], 2.0], $MachinePrecision] / U$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$3 = N[(t$95$0 * N[(-2.0 * J), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, -0.8], t$95$2, If[LessEqual[t$95$1, -0.42], t$95$3, If[LessEqual[t$95$1, -0.012], t$95$2, If[LessEqual[t$95$1, 0.999], t$95$3, N[(N[(-2.0 * J), $MachinePrecision] * N[Sqrt[1.0 ^ 2 + N[(N[(U$95$m / N[(J * 2.0), $MachinePrecision]), $MachinePrecision] / t$95$1), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]]]]]]]]]
\begin{array}{l}
U_m = \left|U\right|

\\
\begin{array}{l}
t_0 := \cos \left(K \cdot 0.5\right)\\
t_1 := \cos \left(\frac{K}{2}\right)\\
t_2 := -2 \cdot \left(U_m \cdot -0.5 - \frac{{\left(J \cdot t_0\right)}^{2}}{U_m}\right)\\
t_3 := t_0 \cdot \left(-2 \cdot J\right)\\
\mathbf{if}\;t_1 \leq -0.8:\\
\;\;\;\;t_2\\

\mathbf{elif}\;t_1 \leq -0.42:\\
\;\;\;\;t_3\\

\mathbf{elif}\;t_1 \leq -0.012:\\
\;\;\;\;t_2\\

\mathbf{elif}\;t_1 \leq 0.999:\\
\;\;\;\;t_3\\

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


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

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

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

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

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

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

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

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

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

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

      \[\leadsto -2 \cdot \color{blue}{\left(-1 \cdot \frac{{J}^{2} \cdot {\cos \left(0.5 \cdot K\right)}^{2}}{U} + -0.5 \cdot U\right)} \]
    6. Step-by-step derivation
      1. +-commutative32.0%

        \[\leadsto -2 \cdot \color{blue}{\left(-0.5 \cdot U + -1 \cdot \frac{{J}^{2} \cdot {\cos \left(0.5 \cdot K\right)}^{2}}{U}\right)} \]
      2. *-commutative32.0%

        \[\leadsto -2 \cdot \left(\color{blue}{U \cdot -0.5} + -1 \cdot \frac{{J}^{2} \cdot {\cos \left(0.5 \cdot K\right)}^{2}}{U}\right) \]
      3. mul-1-neg32.0%

        \[\leadsto -2 \cdot \left(U \cdot -0.5 + \color{blue}{\left(-\frac{{J}^{2} \cdot {\cos \left(0.5 \cdot K\right)}^{2}}{U}\right)}\right) \]
      4. unsub-neg32.0%

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

        \[\leadsto -2 \cdot \left(U \cdot -0.5 - \frac{{J}^{2} \cdot {\cos \color{blue}{\left(K \cdot 0.5\right)}}^{2}}{U}\right) \]
      6. unpow232.0%

        \[\leadsto -2 \cdot \left(U \cdot -0.5 - \frac{\color{blue}{\left(J \cdot J\right)} \cdot {\cos \left(K \cdot 0.5\right)}^{2}}{U}\right) \]
      7. unpow232.0%

        \[\leadsto -2 \cdot \left(U \cdot -0.5 - \frac{\left(J \cdot J\right) \cdot \color{blue}{\left(\cos \left(K \cdot 0.5\right) \cdot \cos \left(K \cdot 0.5\right)\right)}}{U}\right) \]
      8. swap-sqr32.0%

        \[\leadsto -2 \cdot \left(U \cdot -0.5 - \frac{\color{blue}{\left(J \cdot \cos \left(K \cdot 0.5\right)\right) \cdot \left(J \cdot \cos \left(K \cdot 0.5\right)\right)}}{U}\right) \]
      9. unpow232.0%

        \[\leadsto -2 \cdot \left(U \cdot -0.5 - \frac{\color{blue}{{\left(J \cdot \cos \left(K \cdot 0.5\right)\right)}^{2}}}{U}\right) \]
      10. *-commutative32.0%

        \[\leadsto -2 \cdot \left(U \cdot -0.5 - \frac{{\left(J \cdot \cos \color{blue}{\left(0.5 \cdot K\right)}\right)}^{2}}{U}\right) \]
    7. Simplified32.0%

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

    if -0.80000000000000004 < (cos.f64 (/.f64 K 2)) < -0.419999999999999984 or -0.012 < (cos.f64 (/.f64 K 2)) < 0.998999999999999999

    1. Initial program 81.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. Simplified81.8%

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

      \[\leadsto \color{blue}{-2 \cdot \left(J \cdot \cos \left(0.5 \cdot K\right)\right)} \]
    5. Step-by-step derivation
      1. associate-*r*64.9%

        \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \cos \left(0.5 \cdot K\right)} \]
      2. *-commutative64.9%

        \[\leadsto \color{blue}{\left(J \cdot -2\right)} \cdot \cos \left(0.5 \cdot K\right) \]
    6. Simplified64.9%

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

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

    1. Initial program 73.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. unpow273.0%

        \[\leadsto \left(\left(-2 \cdot J\right) \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)}}} \]
      2. hypot-1-def86.9%

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

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

        \[\leadsto \left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{2 \cdot J}}{\color{blue}{\cos \left(-\frac{K}{2}\right)}}\right) \]
      5. distribute-frac-neg86.9%

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

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

        \[\leadsto \left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, \color{blue}{\frac{\frac{U}{2 \cdot J}}{\cos \left(\frac{-K}{2}\right)}}\right) \]
      8. *-commutative86.9%

        \[\leadsto \left(\left(-2 \cdot J\right) \cdot \cos \left(\frac{K}{2}\right)\right) \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{\color{blue}{J \cdot 2}}}{\cos \left(\frac{-K}{2}\right)}\right) \]
      9. distribute-frac-neg86.9%

        \[\leadsto \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 \color{blue}{\left(-\frac{K}{2}\right)}}\right) \]
      10. cos-neg86.9%

        \[\leadsto \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}}{\color{blue}{\cos \left(\frac{K}{2}\right)}}\right) \]
    3. Simplified86.9%

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

      \[\leadsto \color{blue}{\left(-2 \cdot J\right)} \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right) \]
    6. Step-by-step derivation
      1. *-commutative85.5%

        \[\leadsto \color{blue}{\left(J \cdot -2\right)} \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right) \]
    7. Simplified85.5%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\cos \left(\frac{K}{2}\right) \leq -0.8:\\ \;\;\;\;-2 \cdot \left(U \cdot -0.5 - \frac{{\left(J \cdot \cos \left(K \cdot 0.5\right)\right)}^{2}}{U}\right)\\ \mathbf{elif}\;\cos \left(\frac{K}{2}\right) \leq -0.42:\\ \;\;\;\;\cos \left(K \cdot 0.5\right) \cdot \left(-2 \cdot J\right)\\ \mathbf{elif}\;\cos \left(\frac{K}{2}\right) \leq -0.012:\\ \;\;\;\;-2 \cdot \left(U \cdot -0.5 - \frac{{\left(J \cdot \cos \left(K \cdot 0.5\right)\right)}^{2}}{U}\right)\\ \mathbf{elif}\;\cos \left(\frac{K}{2}\right) \leq 0.999:\\ \;\;\;\;\cos \left(K \cdot 0.5\right) \cdot \left(-2 \cdot J\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-2 \cdot J\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 3: 37.9% accurate, 3.2× speedup?

\[\begin{array}{l} U_m = \left|U\right| \\ \begin{array}{l} \mathbf{if}\;J \leq 2.65 \cdot 10^{-228}:\\ \;\;\;\;-U_m\\ \mathbf{elif}\;J \leq 1.55 \cdot 10^{-202}:\\ \;\;\;\;U_m\\ \mathbf{elif}\;J \leq 7.2 \cdot 10^{-41} \lor \neg \left(J \leq 3.6 \cdot 10^{-19}\right) \land J \leq 2 \cdot 10^{+25}:\\ \;\;\;\;-U_m\\ \mathbf{else}:\\ \;\;\;\;\cos \left(K \cdot 0.5\right) \cdot \left(-2 \cdot J\right)\\ \end{array} \end{array} \]
U_m = (fabs.f64 U)
(FPCore (J K U_m)
 :precision binary64
 (if (<= J 2.65e-228)
   (- U_m)
   (if (<= J 1.55e-202)
     U_m
     (if (or (<= J 7.2e-41) (and (not (<= J 3.6e-19)) (<= J 2e+25)))
       (- U_m)
       (* (cos (* K 0.5)) (* -2.0 J))))))
U_m = fabs(U);
double code(double J, double K, double U_m) {
	double tmp;
	if (J <= 2.65e-228) {
		tmp = -U_m;
	} else if (J <= 1.55e-202) {
		tmp = U_m;
	} else if ((J <= 7.2e-41) || (!(J <= 3.6e-19) && (J <= 2e+25))) {
		tmp = -U_m;
	} else {
		tmp = cos((K * 0.5)) * (-2.0 * J);
	}
	return tmp;
}
U_m = abs(U)
real(8) function code(j, k, u_m)
    real(8), intent (in) :: j
    real(8), intent (in) :: k
    real(8), intent (in) :: u_m
    real(8) :: tmp
    if (j <= 2.65d-228) then
        tmp = -u_m
    else if (j <= 1.55d-202) then
        tmp = u_m
    else if ((j <= 7.2d-41) .or. (.not. (j <= 3.6d-19)) .and. (j <= 2d+25)) then
        tmp = -u_m
    else
        tmp = cos((k * 0.5d0)) * ((-2.0d0) * j)
    end if
    code = tmp
end function
U_m = Math.abs(U);
public static double code(double J, double K, double U_m) {
	double tmp;
	if (J <= 2.65e-228) {
		tmp = -U_m;
	} else if (J <= 1.55e-202) {
		tmp = U_m;
	} else if ((J <= 7.2e-41) || (!(J <= 3.6e-19) && (J <= 2e+25))) {
		tmp = -U_m;
	} else {
		tmp = Math.cos((K * 0.5)) * (-2.0 * J);
	}
	return tmp;
}
U_m = math.fabs(U)
def code(J, K, U_m):
	tmp = 0
	if J <= 2.65e-228:
		tmp = -U_m
	elif J <= 1.55e-202:
		tmp = U_m
	elif (J <= 7.2e-41) or (not (J <= 3.6e-19) and (J <= 2e+25)):
		tmp = -U_m
	else:
		tmp = math.cos((K * 0.5)) * (-2.0 * J)
	return tmp
U_m = abs(U)
function code(J, K, U_m)
	tmp = 0.0
	if (J <= 2.65e-228)
		tmp = Float64(-U_m);
	elseif (J <= 1.55e-202)
		tmp = U_m;
	elseif ((J <= 7.2e-41) || (!(J <= 3.6e-19) && (J <= 2e+25)))
		tmp = Float64(-U_m);
	else
		tmp = Float64(cos(Float64(K * 0.5)) * Float64(-2.0 * J));
	end
	return tmp
end
U_m = abs(U);
function tmp_2 = code(J, K, U_m)
	tmp = 0.0;
	if (J <= 2.65e-228)
		tmp = -U_m;
	elseif (J <= 1.55e-202)
		tmp = U_m;
	elseif ((J <= 7.2e-41) || (~((J <= 3.6e-19)) && (J <= 2e+25)))
		tmp = -U_m;
	else
		tmp = cos((K * 0.5)) * (-2.0 * J);
	end
	tmp_2 = tmp;
end
U_m = N[Abs[U], $MachinePrecision]
code[J_, K_, U$95$m_] := If[LessEqual[J, 2.65e-228], (-U$95$m), If[LessEqual[J, 1.55e-202], U$95$m, If[Or[LessEqual[J, 7.2e-41], And[N[Not[LessEqual[J, 3.6e-19]], $MachinePrecision], LessEqual[J, 2e+25]]], (-U$95$m), N[(N[Cos[N[(K * 0.5), $MachinePrecision]], $MachinePrecision] * N[(-2.0 * J), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
U_m = \left|U\right|

\\
\begin{array}{l}
\mathbf{if}\;J \leq 2.65 \cdot 10^{-228}:\\
\;\;\;\;-U_m\\

\mathbf{elif}\;J \leq 1.55 \cdot 10^{-202}:\\
\;\;\;\;U_m\\

\mathbf{elif}\;J \leq 7.2 \cdot 10^{-41} \lor \neg \left(J \leq 3.6 \cdot 10^{-19}\right) \land J \leq 2 \cdot 10^{+25}:\\
\;\;\;\;-U_m\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if J < 2.64999999999999992e-228 or 1.55e-202 < J < 7.2e-41 or 3.6000000000000001e-19 < J < 2.00000000000000018e25

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

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

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

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

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

    if 2.64999999999999992e-228 < J < 1.55e-202

    1. Initial program 39.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. Simplified39.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 U around -inf 37.9%

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

    if 7.2e-41 < J < 3.6000000000000001e-19 or 2.00000000000000018e25 < J

    1. Initial program 97.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. Simplified97.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 inf 85.1%

      \[\leadsto \color{blue}{-2 \cdot \left(J \cdot \cos \left(0.5 \cdot K\right)\right)} \]
    5. Step-by-step derivation
      1. associate-*r*85.1%

        \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \cos \left(0.5 \cdot K\right)} \]
      2. *-commutative85.1%

        \[\leadsto \color{blue}{\left(J \cdot -2\right)} \cdot \cos \left(0.5 \cdot K\right) \]
    6. Simplified85.1%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;J \leq 2.65 \cdot 10^{-228}:\\ \;\;\;\;-U\\ \mathbf{elif}\;J \leq 1.55 \cdot 10^{-202}:\\ \;\;\;\;U\\ \mathbf{elif}\;J \leq 7.2 \cdot 10^{-41} \lor \neg \left(J \leq 3.6 \cdot 10^{-19}\right) \land J \leq 2 \cdot 10^{+25}:\\ \;\;\;\;-U\\ \mathbf{else}:\\ \;\;\;\;\cos \left(K \cdot 0.5\right) \cdot \left(-2 \cdot J\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 30.1% accurate, 14.9× speedup?

\[\begin{array}{l} U_m = \left|U\right| \\ \begin{array}{l} \mathbf{if}\;J \leq 1.3 \cdot 10^{-228}:\\ \;\;\;\;-U_m\\ \mathbf{elif}\;J \leq 1.55 \cdot 10^{-202}:\\ \;\;\;\;U_m\\ \mathbf{elif}\;J \leq 2.8 \cdot 10^{-40} \lor \neg \left(J \leq 1.5 \cdot 10^{-19}\right) \land J \leq 1.6 \cdot 10^{+40}:\\ \;\;\;\;-U_m\\ \mathbf{else}:\\ \;\;\;\;-2 \cdot J\\ \end{array} \end{array} \]
U_m = (fabs.f64 U)
(FPCore (J K U_m)
 :precision binary64
 (if (<= J 1.3e-228)
   (- U_m)
   (if (<= J 1.55e-202)
     U_m
     (if (or (<= J 2.8e-40) (and (not (<= J 1.5e-19)) (<= J 1.6e+40)))
       (- U_m)
       (* -2.0 J)))))
U_m = fabs(U);
double code(double J, double K, double U_m) {
	double tmp;
	if (J <= 1.3e-228) {
		tmp = -U_m;
	} else if (J <= 1.55e-202) {
		tmp = U_m;
	} else if ((J <= 2.8e-40) || (!(J <= 1.5e-19) && (J <= 1.6e+40))) {
		tmp = -U_m;
	} else {
		tmp = -2.0 * J;
	}
	return tmp;
}
U_m = abs(U)
real(8) function code(j, k, u_m)
    real(8), intent (in) :: j
    real(8), intent (in) :: k
    real(8), intent (in) :: u_m
    real(8) :: tmp
    if (j <= 1.3d-228) then
        tmp = -u_m
    else if (j <= 1.55d-202) then
        tmp = u_m
    else if ((j <= 2.8d-40) .or. (.not. (j <= 1.5d-19)) .and. (j <= 1.6d+40)) then
        tmp = -u_m
    else
        tmp = (-2.0d0) * j
    end if
    code = tmp
end function
U_m = Math.abs(U);
public static double code(double J, double K, double U_m) {
	double tmp;
	if (J <= 1.3e-228) {
		tmp = -U_m;
	} else if (J <= 1.55e-202) {
		tmp = U_m;
	} else if ((J <= 2.8e-40) || (!(J <= 1.5e-19) && (J <= 1.6e+40))) {
		tmp = -U_m;
	} else {
		tmp = -2.0 * J;
	}
	return tmp;
}
U_m = math.fabs(U)
def code(J, K, U_m):
	tmp = 0
	if J <= 1.3e-228:
		tmp = -U_m
	elif J <= 1.55e-202:
		tmp = U_m
	elif (J <= 2.8e-40) or (not (J <= 1.5e-19) and (J <= 1.6e+40)):
		tmp = -U_m
	else:
		tmp = -2.0 * J
	return tmp
U_m = abs(U)
function code(J, K, U_m)
	tmp = 0.0
	if (J <= 1.3e-228)
		tmp = Float64(-U_m);
	elseif (J <= 1.55e-202)
		tmp = U_m;
	elseif ((J <= 2.8e-40) || (!(J <= 1.5e-19) && (J <= 1.6e+40)))
		tmp = Float64(-U_m);
	else
		tmp = Float64(-2.0 * J);
	end
	return tmp
end
U_m = abs(U);
function tmp_2 = code(J, K, U_m)
	tmp = 0.0;
	if (J <= 1.3e-228)
		tmp = -U_m;
	elseif (J <= 1.55e-202)
		tmp = U_m;
	elseif ((J <= 2.8e-40) || (~((J <= 1.5e-19)) && (J <= 1.6e+40)))
		tmp = -U_m;
	else
		tmp = -2.0 * J;
	end
	tmp_2 = tmp;
end
U_m = N[Abs[U], $MachinePrecision]
code[J_, K_, U$95$m_] := If[LessEqual[J, 1.3e-228], (-U$95$m), If[LessEqual[J, 1.55e-202], U$95$m, If[Or[LessEqual[J, 2.8e-40], And[N[Not[LessEqual[J, 1.5e-19]], $MachinePrecision], LessEqual[J, 1.6e+40]]], (-U$95$m), N[(-2.0 * J), $MachinePrecision]]]]
\begin{array}{l}
U_m = \left|U\right|

\\
\begin{array}{l}
\mathbf{if}\;J \leq 1.3 \cdot 10^{-228}:\\
\;\;\;\;-U_m\\

\mathbf{elif}\;J \leq 1.55 \cdot 10^{-202}:\\
\;\;\;\;U_m\\

\mathbf{elif}\;J \leq 2.8 \cdot 10^{-40} \lor \neg \left(J \leq 1.5 \cdot 10^{-19}\right) \land J \leq 1.6 \cdot 10^{+40}:\\
\;\;\;\;-U_m\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if J < 1.3e-228 or 1.55e-202 < J < 2.8e-40 or 1.49999999999999996e-19 < J < 1.5999999999999999e40

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

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

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

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

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

    if 1.3e-228 < J < 1.55e-202

    1. Initial program 39.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. Simplified39.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 U around -inf 37.9%

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

    if 2.8e-40 < J < 1.49999999999999996e-19 or 1.5999999999999999e40 < 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. Simplified97.2%

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

      \[\leadsto \color{blue}{-2 \cdot \left(J \cdot \cos \left(0.5 \cdot K\right)\right)} \]
    5. Step-by-step derivation
      1. associate-*r*85.9%

        \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \cos \left(0.5 \cdot K\right)} \]
      2. *-commutative85.9%

        \[\leadsto \color{blue}{\left(J \cdot -2\right)} \cdot \cos \left(0.5 \cdot K\right) \]
    6. Simplified85.9%

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

      \[\leadsto \color{blue}{-2 \cdot J} \]
    8. Step-by-step derivation
      1. *-commutative50.5%

        \[\leadsto \color{blue}{J \cdot -2} \]
    9. Simplified50.5%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;J \leq 1.3 \cdot 10^{-228}:\\ \;\;\;\;-U\\ \mathbf{elif}\;J \leq 1.55 \cdot 10^{-202}:\\ \;\;\;\;U\\ \mathbf{elif}\;J \leq 2.8 \cdot 10^{-40} \lor \neg \left(J \leq 1.5 \cdot 10^{-19}\right) \land J \leq 1.6 \cdot 10^{+40}:\\ \;\;\;\;-U\\ \mathbf{else}:\\ \;\;\;\;-2 \cdot J\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 26.5% accurate, 24.6× speedup?

\[\begin{array}{l} U_m = \left|U\right| \\ \begin{array}{l} \mathbf{if}\;K \leq 13000 \lor \neg \left(K \leq 4.9 \cdot 10^{+210}\right) \land K \leq 3 \cdot 10^{+237}:\\ \;\;\;\;-U_m\\ \mathbf{else}:\\ \;\;\;\;U_m\\ \end{array} \end{array} \]
U_m = (fabs.f64 U)
(FPCore (J K U_m)
 :precision binary64
 (if (or (<= K 13000.0) (and (not (<= K 4.9e+210)) (<= K 3e+237)))
   (- U_m)
   U_m))
U_m = fabs(U);
double code(double J, double K, double U_m) {
	double tmp;
	if ((K <= 13000.0) || (!(K <= 4.9e+210) && (K <= 3e+237))) {
		tmp = -U_m;
	} else {
		tmp = U_m;
	}
	return tmp;
}
U_m = abs(U)
real(8) function code(j, k, u_m)
    real(8), intent (in) :: j
    real(8), intent (in) :: k
    real(8), intent (in) :: u_m
    real(8) :: tmp
    if ((k <= 13000.0d0) .or. (.not. (k <= 4.9d+210)) .and. (k <= 3d+237)) then
        tmp = -u_m
    else
        tmp = u_m
    end if
    code = tmp
end function
U_m = Math.abs(U);
public static double code(double J, double K, double U_m) {
	double tmp;
	if ((K <= 13000.0) || (!(K <= 4.9e+210) && (K <= 3e+237))) {
		tmp = -U_m;
	} else {
		tmp = U_m;
	}
	return tmp;
}
U_m = math.fabs(U)
def code(J, K, U_m):
	tmp = 0
	if (K <= 13000.0) or (not (K <= 4.9e+210) and (K <= 3e+237)):
		tmp = -U_m
	else:
		tmp = U_m
	return tmp
U_m = abs(U)
function code(J, K, U_m)
	tmp = 0.0
	if ((K <= 13000.0) || (!(K <= 4.9e+210) && (K <= 3e+237)))
		tmp = Float64(-U_m);
	else
		tmp = U_m;
	end
	return tmp
end
U_m = abs(U);
function tmp_2 = code(J, K, U_m)
	tmp = 0.0;
	if ((K <= 13000.0) || (~((K <= 4.9e+210)) && (K <= 3e+237)))
		tmp = -U_m;
	else
		tmp = U_m;
	end
	tmp_2 = tmp;
end
U_m = N[Abs[U], $MachinePrecision]
code[J_, K_, U$95$m_] := If[Or[LessEqual[K, 13000.0], And[N[Not[LessEqual[K, 4.9e+210]], $MachinePrecision], LessEqual[K, 3e+237]]], (-U$95$m), U$95$m]
\begin{array}{l}
U_m = \left|U\right|

\\
\begin{array}{l}
\mathbf{if}\;K \leq 13000 \lor \neg \left(K \leq 4.9 \cdot 10^{+210}\right) \land K \leq 3 \cdot 10^{+237}:\\
\;\;\;\;-U_m\\

\mathbf{else}:\\
\;\;\;\;U_m\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if K < 13000 or 4.90000000000000007e210 < K < 3e237

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

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

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

        \[\leadsto \color{blue}{-U} \]
    6. Simplified27.7%

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

    if 13000 < K < 4.90000000000000007e210 or 3e237 < K

    1. Initial program 82.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. Simplified82.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 22.1%

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

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

Alternative 6: 27.2% accurate, 420.0× speedup?

\[\begin{array}{l} U_m = \left|U\right| \\ U_m \end{array} \]
U_m = (fabs.f64 U)
(FPCore (J K U_m) :precision binary64 U_m)
U_m = fabs(U);
double code(double J, double K, double U_m) {
	return U_m;
}
U_m = abs(U)
real(8) function code(j, k, u_m)
    real(8), intent (in) :: j
    real(8), intent (in) :: k
    real(8), intent (in) :: u_m
    code = u_m
end function
U_m = Math.abs(U);
public static double code(double J, double K, double U_m) {
	return U_m;
}
U_m = math.fabs(U)
def code(J, K, U_m):
	return U_m
U_m = abs(U)
function code(J, K, U_m)
	return U_m
end
U_m = abs(U);
function tmp = code(J, K, U_m)
	tmp = U_m;
end
U_m = N[Abs[U], $MachinePrecision]
code[J_, K_, U$95$m_] := U$95$m
\begin{array}{l}
U_m = \left|U\right|

\\
U_m
\end{array}
Derivation
  1. Initial program 73.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. Simplified73.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 26.4%

    \[\leadsto \color{blue}{U} \]
  5. Final simplification26.4%

    \[\leadsto U \]
  6. Add Preprocessing

Reproduce

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