Average Error: 17.8 → 8.2
Time: 16.3s
Precision: binary64
Cost: 47300
\[\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}} \]
\[\begin{array}{l} t_0 := \cos \left(\frac{K}{2}\right)\\ \mathbf{if}\;\left(\left(-2 \cdot J\right) \cdot t_0\right) \cdot \sqrt{1 + {\left(\frac{U}{t_0 \cdot \left(J \cdot 2\right)}\right)}^{2}} \leq 5 \cdot 10^{+307}:\\ \;\;\;\;\left(-2 \cdot J\right) \cdot \left(t_0 \cdot \mathsf{hypot}\left(1, \frac{U}{J \cdot \left(2 \cdot t_0\right)}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(2, \frac{{\cos \left(K \cdot 0.5\right)}^{2}}{\frac{U}{J \cdot J}}, U\right)\\ \end{array} \]
(FPCore (J K U)
 :precision binary64
 (*
  (* (* -2.0 J) (cos (/ K 2.0)))
  (sqrt (+ 1.0 (pow (/ U (* (* 2.0 J) (cos (/ K 2.0)))) 2.0)))))
(FPCore (J K U)
 :precision binary64
 (let* ((t_0 (cos (/ K 2.0))))
   (if (<=
        (* (* (* -2.0 J) t_0) (sqrt (+ 1.0 (pow (/ U (* t_0 (* J 2.0))) 2.0))))
        5e+307)
     (* (* -2.0 J) (* t_0 (hypot 1.0 (/ U (* J (* 2.0 t_0))))))
     (fma 2.0 (/ (pow (cos (* K 0.5)) 2.0) (/ U (* J J))) U))))
double code(double J, double K, double U) {
	return ((-2.0 * J) * cos((K / 2.0))) * sqrt((1.0 + pow((U / ((2.0 * J) * cos((K / 2.0)))), 2.0)));
}
double code(double J, double K, double U) {
	double t_0 = cos((K / 2.0));
	double tmp;
	if ((((-2.0 * J) * t_0) * sqrt((1.0 + pow((U / (t_0 * (J * 2.0))), 2.0)))) <= 5e+307) {
		tmp = (-2.0 * J) * (t_0 * hypot(1.0, (U / (J * (2.0 * t_0)))));
	} else {
		tmp = fma(2.0, (pow(cos((K * 0.5)), 2.0) / (U / (J * J))), U);
	}
	return tmp;
}
function code(J, K, U)
	return Float64(Float64(Float64(-2.0 * J) * cos(Float64(K / 2.0))) * sqrt(Float64(1.0 + (Float64(U / Float64(Float64(2.0 * J) * cos(Float64(K / 2.0)))) ^ 2.0))))
end
function code(J, K, U)
	t_0 = cos(Float64(K / 2.0))
	tmp = 0.0
	if (Float64(Float64(Float64(-2.0 * J) * t_0) * sqrt(Float64(1.0 + (Float64(U / Float64(t_0 * Float64(J * 2.0))) ^ 2.0)))) <= 5e+307)
		tmp = Float64(Float64(-2.0 * J) * Float64(t_0 * hypot(1.0, Float64(U / Float64(J * Float64(2.0 * t_0))))));
	else
		tmp = fma(2.0, Float64((cos(Float64(K * 0.5)) ^ 2.0) / Float64(U / Float64(J * J))), U);
	end
	return tmp
end
code[J_, K_, U_] := N[(N[(N[(-2.0 * J), $MachinePrecision] * N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * N[Sqrt[N[(1.0 + N[Power[N[(U / N[(N[(2.0 * J), $MachinePrecision] * N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
code[J_, K_, U_] := Block[{t$95$0 = N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[N[(N[(N[(-2.0 * J), $MachinePrecision] * t$95$0), $MachinePrecision] * N[Sqrt[N[(1.0 + N[Power[N[(U / N[(t$95$0 * N[(J * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], 5e+307], N[(N[(-2.0 * J), $MachinePrecision] * N[(t$95$0 * N[Sqrt[1.0 ^ 2 + N[(U / N[(J * N[(2.0 * t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(2.0 * N[(N[Power[N[Cos[N[(K * 0.5), $MachinePrecision]], $MachinePrecision], 2.0], $MachinePrecision] / N[(U / N[(J * J), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + U), $MachinePrecision]]]
\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}}
\begin{array}{l}
t_0 := \cos \left(\frac{K}{2}\right)\\
\mathbf{if}\;\left(\left(-2 \cdot J\right) \cdot t_0\right) \cdot \sqrt{1 + {\left(\frac{U}{t_0 \cdot \left(J \cdot 2\right)}\right)}^{2}} \leq 5 \cdot 10^{+307}:\\
\;\;\;\;\left(-2 \cdot J\right) \cdot \left(t_0 \cdot \mathsf{hypot}\left(1, \frac{U}{J \cdot \left(2 \cdot t_0\right)}\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(2, \frac{{\cos \left(K \cdot 0.5\right)}^{2}}{\frac{U}{J \cdot J}}, U\right)\\


\end{array}

Error

Derivation

  1. Split input into 2 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)))) < 5e307

    1. Initial program 10.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. Simplified4.6

      \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \mathsf{hypot}\left(1, \frac{U}{J \cdot \left(2 \cdot \cos \left(\frac{K}{2}\right)\right)}\right)\right)} \]
      Proof
      (*.f64 (*.f64 -2 J) (*.f64 (cos.f64 (/.f64 K 2)) (hypot.f64 1 (/.f64 U (*.f64 J (*.f64 2 (cos.f64 (/.f64 K 2)))))))): 0 points increase in error, 0 points decrease in error
      (*.f64 (*.f64 -2 J) (*.f64 (cos.f64 (/.f64 K 2)) (hypot.f64 1 (/.f64 U (Rewrite<= associate-*l*_binary64 (*.f64 (*.f64 J 2) (cos.f64 (/.f64 K 2)))))))): 0 points increase in error, 0 points decrease in error
      (*.f64 (*.f64 -2 J) (*.f64 (cos.f64 (/.f64 K 2)) (hypot.f64 1 (/.f64 U (*.f64 (Rewrite<= *-commutative_binary64 (*.f64 2 J)) (cos.f64 (/.f64 K 2))))))): 0 points increase in error, 0 points decrease in error
      (*.f64 (*.f64 -2 J) (*.f64 (cos.f64 (/.f64 K 2)) (Rewrite<= hypot-1-def_binary64 (sqrt.f64 (+.f64 1 (*.f64 (/.f64 U (*.f64 (*.f64 2 J) (cos.f64 (/.f64 K 2)))) (/.f64 U (*.f64 (*.f64 2 J) (cos.f64 (/.f64 K 2)))))))))): 35 points increase in error, 0 points decrease in error
      (*.f64 (*.f64 -2 J) (*.f64 (cos.f64 (/.f64 K 2)) (sqrt.f64 (+.f64 1 (Rewrite<= unpow2_binary64 (pow.f64 (/.f64 U (*.f64 (*.f64 2 J) (cos.f64 (/.f64 K 2)))) 2)))))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate-*l*_binary64 (*.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))))): 0 points increase in error, 10 points decrease in error

    if 5e307 < (*.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 63.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. Simplified28.2

      \[\leadsto \color{blue}{\left(-2 \cdot J\right) \cdot \left(\cos \left(\frac{K}{2}\right) \cdot \mathsf{hypot}\left(1, \frac{U}{J \cdot \left(2 \cdot \cos \left(\frac{K}{2}\right)\right)}\right)\right)} \]
      Proof
      (*.f64 (*.f64 -2 J) (*.f64 (cos.f64 (/.f64 K 2)) (hypot.f64 1 (/.f64 U (*.f64 J (*.f64 2 (cos.f64 (/.f64 K 2)))))))): 0 points increase in error, 0 points decrease in error
      (*.f64 (*.f64 -2 J) (*.f64 (cos.f64 (/.f64 K 2)) (hypot.f64 1 (/.f64 U (Rewrite<= associate-*l*_binary64 (*.f64 (*.f64 J 2) (cos.f64 (/.f64 K 2)))))))): 0 points increase in error, 0 points decrease in error
      (*.f64 (*.f64 -2 J) (*.f64 (cos.f64 (/.f64 K 2)) (hypot.f64 1 (/.f64 U (*.f64 (Rewrite<= *-commutative_binary64 (*.f64 2 J)) (cos.f64 (/.f64 K 2))))))): 0 points increase in error, 0 points decrease in error
      (*.f64 (*.f64 -2 J) (*.f64 (cos.f64 (/.f64 K 2)) (Rewrite<= hypot-1-def_binary64 (sqrt.f64 (+.f64 1 (*.f64 (/.f64 U (*.f64 (*.f64 2 J) (cos.f64 (/.f64 K 2)))) (/.f64 U (*.f64 (*.f64 2 J) (cos.f64 (/.f64 K 2)))))))))): 35 points increase in error, 0 points decrease in error
      (*.f64 (*.f64 -2 J) (*.f64 (cos.f64 (/.f64 K 2)) (sqrt.f64 (+.f64 1 (Rewrite<= unpow2_binary64 (pow.f64 (/.f64 U (*.f64 (*.f64 2 J) (cos.f64 (/.f64 K 2)))) 2)))))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= associate-*l*_binary64 (*.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))))): 0 points increase in error, 10 points decrease in error
    3. Taylor expanded in U around -inf 30.0

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(2, \frac{{\cos \left(0.5 \cdot K\right)}^{2}}{\frac{U}{J \cdot J}}, U\right)} \]
      Proof
      (fma.f64 2 (/.f64 (pow.f64 (cos.f64 (*.f64 1/2 K)) 2) (/.f64 U (*.f64 J J))) U): 0 points increase in error, 0 points decrease in error
      (fma.f64 2 (/.f64 (pow.f64 (cos.f64 (*.f64 1/2 K)) 2) (/.f64 U (Rewrite<= unpow2_binary64 (pow.f64 J 2)))) U): 0 points increase in error, 0 points decrease in error
      (fma.f64 2 (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 (pow.f64 (cos.f64 (*.f64 1/2 K)) 2) (pow.f64 J 2)) U)) U): 7 points increase in error, 5 points decrease in error
      (Rewrite<= fma-def_binary64 (+.f64 (*.f64 2 (/.f64 (*.f64 (pow.f64 (cos.f64 (*.f64 1/2 K)) 2) (pow.f64 J 2)) U)) U)): 0 points increase in error, 0 points decrease in error
  3. Recombined 2 regimes into one program.
  4. Final simplification8.2

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

Alternatives

Alternative 1
Error25.5
Cost7376
\[\begin{array}{l} t_0 := \left(-2 \cdot J\right) \cdot \cos \left(K \cdot 0.5\right)\\ \mathbf{if}\;J \leq -1.35 \cdot 10^{-76}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;J \leq -3 \cdot 10^{-275}:\\ \;\;\;\;U + 2 \cdot \left(J \cdot \frac{J}{U}\right)\\ \mathbf{elif}\;J \leq 4.8 \cdot 10^{-266}:\\ \;\;\;\;-U\\ \mathbf{elif}\;J \leq 2.1 \cdot 10^{-125}:\\ \;\;\;\;U\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 2
Error19.6
Cost7304
\[\begin{array}{l} t_0 := \left(-2 \cdot J\right) \cdot \cos \left(K \cdot 0.5\right)\\ \mathbf{if}\;K \leq -130000000000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;K \leq 0.00185:\\ \;\;\;\;J \cdot \left(-2 \cdot \mathsf{hypot}\left(1, 0.5 \cdot \frac{U}{J}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 3
Error37.8
Cost840
\[\begin{array}{l} \mathbf{if}\;J \leq -1.65 \cdot 10^{-78}:\\ \;\;\;\;-2 \cdot J\\ \mathbf{elif}\;J \leq -2.4 \cdot 10^{-275}:\\ \;\;\;\;U + 2 \cdot \left(J \cdot \frac{J}{U}\right)\\ \mathbf{elif}\;J \leq 5.2 \cdot 10^{-266}:\\ \;\;\;\;-U\\ \mathbf{elif}\;J \leq 1.4 \cdot 10^{-123}:\\ \;\;\;\;U\\ \mathbf{else}:\\ \;\;\;\;-2 \cdot J\\ \end{array} \]
Alternative 4
Error46.7
Cost788
\[\begin{array}{l} \mathbf{if}\;K \leq -1.1 \cdot 10^{-98}:\\ \;\;\;\;-U\\ \mathbf{elif}\;K \leq 5.8 \cdot 10^{-199}:\\ \;\;\;\;U\\ \mathbf{elif}\;K \leq 1.96 \cdot 10^{-131}:\\ \;\;\;\;-U\\ \mathbf{elif}\;K \leq 1.15 \cdot 10^{-75}:\\ \;\;\;\;U\\ \mathbf{elif}\;K \leq 6.6 \cdot 10^{+89}:\\ \;\;\;\;-U\\ \mathbf{else}:\\ \;\;\;\;U\\ \end{array} \]
Alternative 5
Error38.1
Cost524
\[\begin{array}{l} \mathbf{if}\;U \leq -1.35 \cdot 10^{-22}:\\ \;\;\;\;U\\ \mathbf{elif}\;U \leq 4.5 \cdot 10^{+27}:\\ \;\;\;\;-2 \cdot J\\ \mathbf{elif}\;U \leq 1.1 \cdot 10^{+204}:\\ \;\;\;\;-U\\ \mathbf{else}:\\ \;\;\;\;U\\ \end{array} \]
Alternative 6
Error46.6
Cost64
\[U \]

Error

Reproduce

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