Maksimov and Kolovsky, Equation (3)

Percentage Accurate: 73.7% → 99.8%
Time: 12.8s
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.7% 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.8% 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(\left(-2 \cdot J\right) \cdot t_0\right) \cdot \sqrt{1 + {\left(\frac{U_m}{t_0 \cdot \left(J \cdot 2\right)}\right)}^{2}}\\ \mathbf{if}\;t_1 \leq -\infty:\\ \;\;\;\;-U_m\\ \mathbf{elif}\;t_1 \leq 2 \cdot 10^{+306}:\\ \;\;\;\;\left(-2 \cdot J\right) \cdot \left(t_0 \cdot \mathsf{hypot}\left(1, \frac{\frac{U_m}{J \cdot 2}}{t_0}\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
         (*
          (* (* -2.0 J) t_0)
          (sqrt (+ 1.0 (pow (/ U_m (* t_0 (* J 2.0))) 2.0))))))
   (if (<= t_1 (- INFINITY))
     (- U_m)
     (if (<= t_1 2e+306)
       (* (* -2.0 J) (* t_0 (hypot 1.0 (/ (/ U_m (* J 2.0)) t_0))))
       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 = ((-2.0 * J) * t_0) * sqrt((1.0 + pow((U_m / (t_0 * (J * 2.0))), 2.0)));
	double tmp;
	if (t_1 <= -((double) INFINITY)) {
		tmp = -U_m;
	} else if (t_1 <= 2e+306) {
		tmp = (-2.0 * J) * (t_0 * hypot(1.0, ((U_m / (J * 2.0)) / t_0)));
	} 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 = ((-2.0 * J) * t_0) * Math.sqrt((1.0 + Math.pow((U_m / (t_0 * (J * 2.0))), 2.0)));
	double tmp;
	if (t_1 <= -Double.POSITIVE_INFINITY) {
		tmp = -U_m;
	} else if (t_1 <= 2e+306) {
		tmp = (-2.0 * J) * (t_0 * Math.hypot(1.0, ((U_m / (J * 2.0)) / t_0)));
	} 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 = ((-2.0 * J) * t_0) * math.sqrt((1.0 + math.pow((U_m / (t_0 * (J * 2.0))), 2.0)))
	tmp = 0
	if t_1 <= -math.inf:
		tmp = -U_m
	elif t_1 <= 2e+306:
		tmp = (-2.0 * J) * (t_0 * math.hypot(1.0, ((U_m / (J * 2.0)) / t_0)))
	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(Float64(-2.0 * J) * t_0) * sqrt(Float64(1.0 + (Float64(U_m / Float64(t_0 * Float64(J * 2.0))) ^ 2.0))))
	tmp = 0.0
	if (t_1 <= Float64(-Inf))
		tmp = Float64(-U_m);
	elseif (t_1 <= 2e+306)
		tmp = Float64(Float64(-2.0 * J) * Float64(t_0 * hypot(1.0, Float64(Float64(U_m / Float64(J * 2.0)) / t_0))));
	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 = ((-2.0 * J) * t_0) * sqrt((1.0 + ((U_m / (t_0 * (J * 2.0))) ^ 2.0)));
	tmp = 0.0;
	if (t_1 <= -Inf)
		tmp = -U_m;
	elseif (t_1 <= 2e+306)
		tmp = (-2.0 * J) * (t_0 * hypot(1.0, ((U_m / (J * 2.0)) / t_0)));
	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[(N[(-2.0 * J), $MachinePrecision] * t$95$0), $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]}, If[LessEqual[t$95$1, (-Infinity)], (-U$95$m), If[LessEqual[t$95$1, 2e+306], N[(N[(-2.0 * J), $MachinePrecision] * N[(t$95$0 * N[Sqrt[1.0 ^ 2 + N[(N[(U$95$m / N[(J * 2.0), $MachinePrecision]), $MachinePrecision] / t$95$0), $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(\left(-2 \cdot J\right) \cdot t_0\right) \cdot \sqrt{1 + {\left(\frac{U_m}{t_0 \cdot \left(J \cdot 2\right)}\right)}^{2}}\\
\mathbf{if}\;t_1 \leq -\infty:\\
\;\;\;\;-U_m\\

\mathbf{elif}\;t_1 \leq 2 \cdot 10^{+306}:\\
\;\;\;\;\left(-2 \cdot J\right) \cdot \left(t_0 \cdot \mathsf{hypot}\left(1, \frac{\frac{U_m}{J \cdot 2}}{t_0}\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.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. Simplified64.1%

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

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

        \[\leadsto \color{blue}{-U} \]
    5. Simplified65.9%

      \[\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)))) < 2.00000000000000003e306

    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. Simplified99.8%

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

    if 2.00000000000000003e306 < (*.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 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. Simplified58.3%

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

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

    \[\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 2 \cdot 10^{+306}:\\ \;\;\;\;\left(-2 \cdot J\right) \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \mathsf{hypot}\left(1, \frac{\frac{U}{J \cdot 2}}{\cos \left(\frac{K}{2}\right)}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;U\\ \end{array} \]

Alternative 2: 46.7% accurate, 1.9× speedup?

\[\begin{array}{l} U_m = \left|U\right| \\ \begin{array}{l} \mathbf{if}\;J \leq 6.9 \cdot 10^{-181}:\\ \;\;\;\;-U_m\\ \mathbf{else}:\\ \;\;\;\;\left(-2 \cdot J\right) \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \mathsf{hypot}\left(1, 0.5 \cdot \frac{U_m}{J}\right)\right)\\ \end{array} \end{array} \]
U_m = (fabs.f64 U)
(FPCore (J K U_m)
 :precision binary64
 (if (<= J 6.9e-181)
   (- U_m)
   (* (* -2.0 J) (* (cos (/ K 2.0)) (hypot 1.0 (* 0.5 (/ U_m J)))))))
U_m = fabs(U);
double code(double J, double K, double U_m) {
	double tmp;
	if (J <= 6.9e-181) {
		tmp = -U_m;
	} else {
		tmp = (-2.0 * J) * (cos((K / 2.0)) * hypot(1.0, (0.5 * (U_m / J))));
	}
	return tmp;
}
U_m = Math.abs(U);
public static double code(double J, double K, double U_m) {
	double tmp;
	if (J <= 6.9e-181) {
		tmp = -U_m;
	} else {
		tmp = (-2.0 * J) * (Math.cos((K / 2.0)) * Math.hypot(1.0, (0.5 * (U_m / J))));
	}
	return tmp;
}
U_m = math.fabs(U)
def code(J, K, U_m):
	tmp = 0
	if J <= 6.9e-181:
		tmp = -U_m
	else:
		tmp = (-2.0 * J) * (math.cos((K / 2.0)) * math.hypot(1.0, (0.5 * (U_m / J))))
	return tmp
U_m = abs(U)
function code(J, K, U_m)
	tmp = 0.0
	if (J <= 6.9e-181)
		tmp = Float64(-U_m);
	else
		tmp = Float64(Float64(-2.0 * J) * Float64(cos(Float64(K / 2.0)) * hypot(1.0, Float64(0.5 * Float64(U_m / J)))));
	end
	return tmp
end
U_m = abs(U);
function tmp_2 = code(J, K, U_m)
	tmp = 0.0;
	if (J <= 6.9e-181)
		tmp = -U_m;
	else
		tmp = (-2.0 * J) * (cos((K / 2.0)) * hypot(1.0, (0.5 * (U_m / J))));
	end
	tmp_2 = tmp;
end
U_m = N[Abs[U], $MachinePrecision]
code[J_, K_, U$95$m_] := If[LessEqual[J, 6.9e-181], (-U$95$m), N[(N[(-2.0 * J), $MachinePrecision] * N[(N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision] * N[Sqrt[1.0 ^ 2 + N[(0.5 * N[(U$95$m / J), $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
U_m = \left|U\right|

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if J < 6.90000000000000053e-181

    1. Initial program 73.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. Simplified87.4%

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

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

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

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

    if 6.90000000000000053e-181 < J

    1. Initial program 90.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. Simplified98.1%

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

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

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

Alternative 3: 59.4% accurate, 3.9× speedup?

\[\begin{array}{l} U_m = \left|U\right| \\ \begin{array}{l} \mathbf{if}\;U_m \leq 1.02 \cdot 10^{+140}:\\ \;\;\;\;\left(-2 \cdot J\right) \cdot \cos \left(K \cdot 0.5\right)\\ \mathbf{elif}\;U_m \leq 2.7 \cdot 10^{+216} \lor \neg \left(U_m \leq 1.4 \cdot 10^{+230}\right):\\ \;\;\;\;-U_m\\ \mathbf{else}:\\ \;\;\;\;U_m\\ \end{array} \end{array} \]
U_m = (fabs.f64 U)
(FPCore (J K U_m)
 :precision binary64
 (if (<= U_m 1.02e+140)
   (* (* -2.0 J) (cos (* K 0.5)))
   (if (or (<= U_m 2.7e+216) (not (<= U_m 1.4e+230))) (- U_m) U_m)))
U_m = fabs(U);
double code(double J, double K, double U_m) {
	double tmp;
	if (U_m <= 1.02e+140) {
		tmp = (-2.0 * J) * cos((K * 0.5));
	} else if ((U_m <= 2.7e+216) || !(U_m <= 1.4e+230)) {
		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 (u_m <= 1.02d+140) then
        tmp = ((-2.0d0) * j) * cos((k * 0.5d0))
    else if ((u_m <= 2.7d+216) .or. (.not. (u_m <= 1.4d+230))) 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 (U_m <= 1.02e+140) {
		tmp = (-2.0 * J) * Math.cos((K * 0.5));
	} else if ((U_m <= 2.7e+216) || !(U_m <= 1.4e+230)) {
		tmp = -U_m;
	} else {
		tmp = U_m;
	}
	return tmp;
}
U_m = math.fabs(U)
def code(J, K, U_m):
	tmp = 0
	if U_m <= 1.02e+140:
		tmp = (-2.0 * J) * math.cos((K * 0.5))
	elif (U_m <= 2.7e+216) or not (U_m <= 1.4e+230):
		tmp = -U_m
	else:
		tmp = U_m
	return tmp
U_m = abs(U)
function code(J, K, U_m)
	tmp = 0.0
	if (U_m <= 1.02e+140)
		tmp = Float64(Float64(-2.0 * J) * cos(Float64(K * 0.5)));
	elseif ((U_m <= 2.7e+216) || !(U_m <= 1.4e+230))
		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 (U_m <= 1.02e+140)
		tmp = (-2.0 * J) * cos((K * 0.5));
	elseif ((U_m <= 2.7e+216) || ~((U_m <= 1.4e+230)))
		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[LessEqual[U$95$m, 1.02e+140], N[(N[(-2.0 * J), $MachinePrecision] * N[Cos[N[(K * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[U$95$m, 2.7e+216], N[Not[LessEqual[U$95$m, 1.4e+230]], $MachinePrecision]], (-U$95$m), U$95$m]]
\begin{array}{l}
U_m = \left|U\right|

\\
\begin{array}{l}
\mathbf{if}\;U_m \leq 1.02 \cdot 10^{+140}:\\
\;\;\;\;\left(-2 \cdot J\right) \cdot \cos \left(K \cdot 0.5\right)\\

\mathbf{elif}\;U_m \leq 2.7 \cdot 10^{+216} \lor \neg \left(U_m \leq 1.4 \cdot 10^{+230}\right):\\
\;\;\;\;-U_m\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if U < 1.02000000000000007e140

    1. Initial program 84.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. Simplified95.9%

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

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

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

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

    if 1.02000000000000007e140 < U < 2.7000000000000001e216 or 1.4000000000000001e230 < U

    1. Initial program 55.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. Simplified68.9%

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

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

        \[\leadsto \color{blue}{-U} \]
    5. Simplified53.2%

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

    if 2.7000000000000001e216 < U < 1.4000000000000001e230

    1. Initial program 53.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. Simplified76.5%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;U \leq 1.02 \cdot 10^{+140}:\\ \;\;\;\;\left(-2 \cdot J\right) \cdot \cos \left(K \cdot 0.5\right)\\ \mathbf{elif}\;U \leq 2.7 \cdot 10^{+216} \lor \neg \left(U \leq 1.4 \cdot 10^{+230}\right):\\ \;\;\;\;-U\\ \mathbf{else}:\\ \;\;\;\;U\\ \end{array} \]

Alternative 4: 40.9% accurate, 51.2× speedup?

\[\begin{array}{l} U_m = \left|U\right| \\ \begin{array}{l} \mathbf{if}\;U_m \leq 6500000:\\ \;\;\;\;-2 \cdot J\\ \mathbf{elif}\;U_m \leq 2.7 \cdot 10^{+216} \lor \neg \left(U_m \leq 1.4 \cdot 10^{+230}\right):\\ \;\;\;\;-U_m\\ \mathbf{else}:\\ \;\;\;\;U_m\\ \end{array} \end{array} \]
U_m = (fabs.f64 U)
(FPCore (J K U_m)
 :precision binary64
 (if (<= U_m 6500000.0)
   (* -2.0 J)
   (if (or (<= U_m 2.7e+216) (not (<= U_m 1.4e+230))) (- U_m) U_m)))
U_m = fabs(U);
double code(double J, double K, double U_m) {
	double tmp;
	if (U_m <= 6500000.0) {
		tmp = -2.0 * J;
	} else if ((U_m <= 2.7e+216) || !(U_m <= 1.4e+230)) {
		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 (u_m <= 6500000.0d0) then
        tmp = (-2.0d0) * j
    else if ((u_m <= 2.7d+216) .or. (.not. (u_m <= 1.4d+230))) 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 (U_m <= 6500000.0) {
		tmp = -2.0 * J;
	} else if ((U_m <= 2.7e+216) || !(U_m <= 1.4e+230)) {
		tmp = -U_m;
	} else {
		tmp = U_m;
	}
	return tmp;
}
U_m = math.fabs(U)
def code(J, K, U_m):
	tmp = 0
	if U_m <= 6500000.0:
		tmp = -2.0 * J
	elif (U_m <= 2.7e+216) or not (U_m <= 1.4e+230):
		tmp = -U_m
	else:
		tmp = U_m
	return tmp
U_m = abs(U)
function code(J, K, U_m)
	tmp = 0.0
	if (U_m <= 6500000.0)
		tmp = Float64(-2.0 * J);
	elseif ((U_m <= 2.7e+216) || !(U_m <= 1.4e+230))
		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 (U_m <= 6500000.0)
		tmp = -2.0 * J;
	elseif ((U_m <= 2.7e+216) || ~((U_m <= 1.4e+230)))
		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[LessEqual[U$95$m, 6500000.0], N[(-2.0 * J), $MachinePrecision], If[Or[LessEqual[U$95$m, 2.7e+216], N[Not[LessEqual[U$95$m, 1.4e+230]], $MachinePrecision]], (-U$95$m), U$95$m]]
\begin{array}{l}
U_m = \left|U\right|

\\
\begin{array}{l}
\mathbf{if}\;U_m \leq 6500000:\\
\;\;\;\;-2 \cdot J\\

\mathbf{elif}\;U_m \leq 2.7 \cdot 10^{+216} \lor \neg \left(U_m \leq 1.4 \cdot 10^{+230}\right):\\
\;\;\;\;-U_m\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if U < 6.5e6

    1. Initial program 86.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. Simplified96.4%

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

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

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

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

      \[\leadsto \color{blue}{-2 \cdot J} \]

    if 6.5e6 < U < 2.7000000000000001e216 or 1.4000000000000001e230 < U

    1. Initial program 64.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. Simplified78.4%

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

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

        \[\leadsto \color{blue}{-U} \]
    5. Simplified46.7%

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

    if 2.7000000000000001e216 < U < 1.4000000000000001e230

    1. Initial program 53.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. Simplified76.5%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;U \leq 6500000:\\ \;\;\;\;-2 \cdot J\\ \mathbf{elif}\;U \leq 2.7 \cdot 10^{+216} \lor \neg \left(U \leq 1.4 \cdot 10^{+230}\right):\\ \;\;\;\;-U\\ \mathbf{else}:\\ \;\;\;\;U\\ \end{array} \]

Alternative 5: 26.3% accurate, 103.4× speedup?

\[\begin{array}{l} U_m = \left|U\right| \\ \begin{array}{l} \mathbf{if}\;K \leq 3.1:\\ \;\;\;\;-U_m\\ \mathbf{else}:\\ \;\;\;\;U_m\\ \end{array} \end{array} \]
U_m = (fabs.f64 U)
(FPCore (J K U_m) :precision binary64 (if (<= K 3.1) (- U_m) U_m))
U_m = fabs(U);
double code(double J, double K, double U_m) {
	double tmp;
	if (K <= 3.1) {
		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 <= 3.1d0) 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 <= 3.1) {
		tmp = -U_m;
	} else {
		tmp = U_m;
	}
	return tmp;
}
U_m = math.fabs(U)
def code(J, K, U_m):
	tmp = 0
	if K <= 3.1:
		tmp = -U_m
	else:
		tmp = U_m
	return tmp
U_m = abs(U)
function code(J, K, U_m)
	tmp = 0.0
	if (K <= 3.1)
		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 <= 3.1)
		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[LessEqual[K, 3.1], (-U$95$m), U$95$m]
\begin{array}{l}
U_m = \left|U\right|

\\
\begin{array}{l}
\mathbf{if}\;K \leq 3.1:\\
\;\;\;\;-U_m\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if K < 3.10000000000000009

    1. Initial program 80.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. Simplified91.9%

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

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

        \[\leadsto \color{blue}{-U} \]
    5. Simplified26.0%

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

    if 3.10000000000000009 < K

    1. Initial program 80.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. Simplified91.8%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;K \leq 3.1:\\ \;\;\;\;-U\\ \mathbf{else}:\\ \;\;\;\;U\\ \end{array} \]

Alternative 6: 26.4% 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 80.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. Simplified91.8%

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

    \[\leadsto \color{blue}{U} \]
  4. Final simplification23.7%

    \[\leadsto U \]

Reproduce

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