Maksimov and Kolovsky, Equation (3)

Percentage Accurate: 73.3% → 99.5%
Time: 15.8s
Alternatives: 10
Speedup: 1.3×

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 10 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.3% 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.3× 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}}\\ \mathbf{if}\;t_1 \leq -\infty:\\ \;\;\;\;-U_m\\ \mathbf{elif}\;t_1 \leq 4 \cdot 10^{+304}:\\ \;\;\;\;t_1\\ \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))))))
   (if (<= t_1 (- INFINITY)) (- U_m) (if (<= t_1 4e+304) t_1 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 tmp;
	if (t_1 <= -((double) INFINITY)) {
		tmp = -U_m;
	} else if (t_1 <= 4e+304) {
		tmp = t_1;
	} 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 tmp;
	if (t_1 <= -Double.POSITIVE_INFINITY) {
		tmp = -U_m;
	} else if (t_1 <= 4e+304) {
		tmp = t_1;
	} 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)))
	tmp = 0
	if t_1 <= -math.inf:
		tmp = -U_m
	elif t_1 <= 4e+304:
		tmp = t_1
	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))))
	tmp = 0.0
	if (t_1 <= Float64(-Inf))
		tmp = Float64(-U_m);
	elseif (t_1 <= 4e+304)
		tmp = t_1;
	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)));
	tmp = 0.0;
	if (t_1 <= -Inf)
		tmp = -U_m;
	elseif (t_1 <= 4e+304)
		tmp = t_1;
	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]}, If[LessEqual[t$95$1, (-Infinity)], (-U$95$m), If[LessEqual[t$95$1, 4e+304], t$95$1, 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}}\\
\mathbf{if}\;t_1 \leq -\infty:\\
\;\;\;\;-U_m\\

\mathbf{elif}\;t_1 \leq 4 \cdot 10^{+304}:\\
\;\;\;\;t_1\\

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 2: 85.0% accurate, 1.3× speedup?

\[\begin{array}{l} U_m = \left|U\right| \\ \begin{array}{l} t_0 := \cos \left(\frac{K}{2}\right)\\ \mathbf{if}\;U_m \leq 3 \cdot 10^{+209}:\\ \;\;\;\;-2 \cdot \left(t_0 \cdot \left(J \cdot \mathsf{hypot}\left(1, \frac{\frac{U_m}{2}}{J \cdot t_0}\right)\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))))
   (if (<= U_m 3e+209)
     (* -2.0 (* t_0 (* J (hypot 1.0 (/ (/ U_m 2.0) (* J 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 tmp;
	if (U_m <= 3e+209) {
		tmp = -2.0 * (t_0 * (J * hypot(1.0, ((U_m / 2.0) / (J * 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 tmp;
	if (U_m <= 3e+209) {
		tmp = -2.0 * (t_0 * (J * Math.hypot(1.0, ((U_m / 2.0) / (J * 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))
	tmp = 0
	if U_m <= 3e+209:
		tmp = -2.0 * (t_0 * (J * math.hypot(1.0, ((U_m / 2.0) / (J * 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))
	tmp = 0.0
	if (U_m <= 3e+209)
		tmp = Float64(-2.0 * Float64(t_0 * Float64(J * hypot(1.0, Float64(Float64(U_m / 2.0) / Float64(J * t_0))))));
	else
		tmp = Float64(-U_m);
	end
	return tmp
end
U_m = abs(U);
function tmp_2 = code(J, K, U_m)
	t_0 = cos((K / 2.0));
	tmp = 0.0;
	if (U_m <= 3e+209)
		tmp = -2.0 * (t_0 * (J * hypot(1.0, ((U_m / 2.0) / (J * 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]}, If[LessEqual[U$95$m, 3e+209], N[(-2.0 * N[(t$95$0 * N[(J * N[Sqrt[1.0 ^ 2 + N[(N[(U$95$m / 2.0), $MachinePrecision] / N[(J * t$95$0), $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision]), $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)\\
\mathbf{if}\;U_m \leq 3 \cdot 10^{+209}:\\
\;\;\;\;-2 \cdot \left(t_0 \cdot \left(J \cdot \mathsf{hypot}\left(1, \frac{\frac{U_m}{2}}{J \cdot t_0}\right)\right)\right)\\

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 3: 85.1% accurate, 1.3× speedup?

\[\begin{array}{l} U_m = \left|U\right| \\ \begin{array}{l} t_0 := J \cdot \cos \left(\frac{K}{2}\right)\\ \mathbf{if}\;U_m \leq 5 \cdot 10^{+210}:\\ \;\;\;\;-2 \cdot \left(t_0 \cdot \mathsf{hypot}\left(1, \frac{\frac{U_m}{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 (* J (cos (/ K 2.0)))))
   (if (<= U_m 5e+210)
     (* -2.0 (* t_0 (hypot 1.0 (/ (/ U_m 2.0) t_0))))
     (- U_m))))
U_m = fabs(U);
double code(double J, double K, double U_m) {
	double t_0 = J * cos((K / 2.0));
	double tmp;
	if (U_m <= 5e+210) {
		tmp = -2.0 * (t_0 * hypot(1.0, ((U_m / 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 = J * Math.cos((K / 2.0));
	double tmp;
	if (U_m <= 5e+210) {
		tmp = -2.0 * (t_0 * Math.hypot(1.0, ((U_m / 2.0) / t_0)));
	} else {
		tmp = -U_m;
	}
	return tmp;
}
U_m = math.fabs(U)
def code(J, K, U_m):
	t_0 = J * math.cos((K / 2.0))
	tmp = 0
	if U_m <= 5e+210:
		tmp = -2.0 * (t_0 * math.hypot(1.0, ((U_m / 2.0) / t_0)))
	else:
		tmp = -U_m
	return tmp
U_m = abs(U)
function code(J, K, U_m)
	t_0 = Float64(J * cos(Float64(K / 2.0)))
	tmp = 0.0
	if (U_m <= 5e+210)
		tmp = Float64(-2.0 * Float64(t_0 * hypot(1.0, Float64(Float64(U_m / 2.0) / t_0))));
	else
		tmp = Float64(-U_m);
	end
	return tmp
end
U_m = abs(U);
function tmp_2 = code(J, K, U_m)
	t_0 = J * cos((K / 2.0));
	tmp = 0.0;
	if (U_m <= 5e+210)
		tmp = -2.0 * (t_0 * hypot(1.0, ((U_m / 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[(J * N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[U$95$m, 5e+210], N[(-2.0 * N[(t$95$0 * N[Sqrt[1.0 ^ 2 + N[(N[(U$95$m / 2.0), $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 := J \cdot \cos \left(\frac{K}{2}\right)\\
\mathbf{if}\;U_m \leq 5 \cdot 10^{+210}:\\
\;\;\;\;-2 \cdot \left(t_0 \cdot \mathsf{hypot}\left(1, \frac{\frac{U_m}{2}}{t_0}\right)\right)\\

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 4: 71.9% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;U_m \leq 4.3 \cdot 10^{+136}:\\
\;\;\;\;-2 \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \left(J \cdot \mathsf{hypot}\left(1, 0.5 \cdot \frac{U_m}{J}\right)\right)\right)\\

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 5: 63.6% accurate, 3.7× speedup?

\[\begin{array}{l} U_m = \left|U\right| \\ \begin{array}{l} \mathbf{if}\;\frac{K}{2} \leq 0.001:\\ \;\;\;\;-2 \cdot \left(J \cdot \mathsf{hypot}\left(1, \frac{U_m \cdot 0.5}{J}\right)\right)\\ \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 (<= (/ K 2.0) 0.001)
   (* -2.0 (* J (hypot 1.0 (/ (* U_m 0.5) J))))
   (* (cos (* K 0.5)) (* -2.0 J))))
U_m = fabs(U);
double code(double J, double K, double U_m) {
	double tmp;
	if ((K / 2.0) <= 0.001) {
		tmp = -2.0 * (J * hypot(1.0, ((U_m * 0.5) / J)));
	} else {
		tmp = cos((K * 0.5)) * (-2.0 * J);
	}
	return tmp;
}
U_m = Math.abs(U);
public static double code(double J, double K, double U_m) {
	double tmp;
	if ((K / 2.0) <= 0.001) {
		tmp = -2.0 * (J * Math.hypot(1.0, ((U_m * 0.5) / J)));
	} 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 (K / 2.0) <= 0.001:
		tmp = -2.0 * (J * math.hypot(1.0, ((U_m * 0.5) / J)))
	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 (Float64(K / 2.0) <= 0.001)
		tmp = Float64(-2.0 * Float64(J * hypot(1.0, Float64(Float64(U_m * 0.5) / J))));
	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 ((K / 2.0) <= 0.001)
		tmp = -2.0 * (J * hypot(1.0, ((U_m * 0.5) / J)));
	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[N[(K / 2.0), $MachinePrecision], 0.001], N[(-2.0 * N[(J * N[Sqrt[1.0 ^ 2 + N[(N[(U$95$m * 0.5), $MachinePrecision] / J), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 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}\;\frac{K}{2} \leq 0.001:\\
\;\;\;\;-2 \cdot \left(J \cdot \mathsf{hypot}\left(1, \frac{U_m \cdot 0.5}{J}\right)\right)\\

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 6: 57.5% accurate, 3.9× speedup?

\[\begin{array}{l} U_m = \left|U\right| \\ \begin{array}{l} \mathbf{if}\;U_m \leq 3.4 \cdot 10^{+19}:\\ \;\;\;\;\cos \left(K \cdot 0.5\right) \cdot \left(-2 \cdot J\right)\\ \mathbf{else}:\\ \;\;\;\;-U_m\\ \end{array} \end{array} \]
U_m = (fabs.f64 U)
(FPCore (J K U_m)
 :precision binary64
 (if (<= U_m 3.4e+19) (* (cos (* K 0.5)) (* -2.0 J)) (- U_m)))
U_m = fabs(U);
double code(double J, double K, double U_m) {
	double tmp;
	if (U_m <= 3.4e+19) {
		tmp = cos((K * 0.5)) * (-2.0 * J);
	} 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 <= 3.4d+19) then
        tmp = cos((k * 0.5d0)) * ((-2.0d0) * j)
    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 <= 3.4e+19) {
		tmp = Math.cos((K * 0.5)) * (-2.0 * J);
	} else {
		tmp = -U_m;
	}
	return tmp;
}
U_m = math.fabs(U)
def code(J, K, U_m):
	tmp = 0
	if U_m <= 3.4e+19:
		tmp = math.cos((K * 0.5)) * (-2.0 * J)
	else:
		tmp = -U_m
	return tmp
U_m = abs(U)
function code(J, K, U_m)
	tmp = 0.0
	if (U_m <= 3.4e+19)
		tmp = Float64(cos(Float64(K * 0.5)) * Float64(-2.0 * J));
	else
		tmp = Float64(-U_m);
	end
	return tmp
end
U_m = abs(U);
function tmp_2 = code(J, K, U_m)
	tmp = 0.0;
	if (U_m <= 3.4e+19)
		tmp = cos((K * 0.5)) * (-2.0 * J);
	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, 3.4e+19], N[(N[Cos[N[(K * 0.5), $MachinePrecision]], $MachinePrecision] * N[(-2.0 * J), $MachinePrecision]), $MachinePrecision], (-U$95$m)]
\begin{array}{l}
U_m = \left|U\right|

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

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 7: 30.6% accurate, 21.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;J \leq 6.1 \cdot 10^{+84}:\\
\;\;\;\;-U_m\\

\mathbf{elif}\;J \leq 4.1 \cdot 10^{+132} \lor \neg \left(J \leq 4.8 \cdot 10^{+160}\right):\\
\;\;\;\;-2 \cdot J\\

\mathbf{else}:\\
\;\;\;\;-2 \cdot \left(J \cdot \left(0.5 \cdot \frac{U_m}{J} + \frac{J}{U_m}\right)\right)\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 8: 31.1% accurate, 83.0× speedup?

\[\begin{array}{l} U_m = \left|U\right| \\ \begin{array}{l} \mathbf{if}\;J \leq 5.1 \cdot 10^{+84}:\\ \;\;\;\;-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 5.1e+84) (- U_m) (* -2.0 J)))
U_m = fabs(U);
double code(double J, double K, double U_m) {
	double tmp;
	if (J <= 5.1e+84) {
		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 <= 5.1d+84) 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 <= 5.1e+84) {
		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 <= 5.1e+84:
		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 <= 5.1e+84)
		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 <= 5.1e+84)
		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, 5.1e+84], (-U$95$m), N[(-2.0 * J), $MachinePrecision]]
\begin{array}{l}
U_m = \left|U\right|

\\
\begin{array}{l}
\mathbf{if}\;J \leq 5.1 \cdot 10^{+84}:\\
\;\;\;\;-U_m\\

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 9: 26.1% accurate, 210.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 Float64(-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
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 10: 27.6% 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
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Reproduce

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