Maksimov and Kolovsky, Equation (4)

?

Percentage Accurate: 86.0% → 99.7%
Time: 13.4s
Precision: binary64
Cost: 52488

?

\[\left(J \cdot \left(e^{\ell} - e^{-\ell}\right)\right) \cdot \cos \left(\frac{K}{2}\right) + U \]
\[\begin{array}{l} \\ \begin{array}{l} t_0 := e^{\ell} - e^{-\ell}\\ t_1 := \cos \left(\frac{K}{2}\right)\\ \mathbf{if}\;t_0 \leq -\infty:\\ \;\;\;\;t_1 \cdot \left(t_0 \cdot J\right) + U\\ \mathbf{elif}\;t_0 \leq 10^{-13}:\\ \;\;\;\;U + \left(0.3333333333333333 \cdot {\ell}^{3} + \left(0.016666666666666666 \cdot {\ell}^{5} + \ell \cdot 2\right)\right) \cdot \left(J \cdot \cos \left(K \cdot 0.5\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(J, t_0 \cdot t_1, U\right)\\ \end{array} \end{array} \]
(FPCore (J l K U)
 :precision binary64
 (+ (* (* J (- (exp l) (exp (- l)))) (cos (/ K 2.0))) U))
(FPCore (J l K U)
 :precision binary64
 (let* ((t_0 (- (exp l) (exp (- l)))) (t_1 (cos (/ K 2.0))))
   (if (<= t_0 (- INFINITY))
     (+ (* t_1 (* t_0 J)) U)
     (if (<= t_0 1e-13)
       (+
        U
        (*
         (+
          (* 0.3333333333333333 (pow l 3.0))
          (+ (* 0.016666666666666666 (pow l 5.0)) (* l 2.0)))
         (* J (cos (* K 0.5)))))
       (fma J (* t_0 t_1) U)))))
double code(double J, double l, double K, double U) {
	return ((J * (exp(l) - exp(-l))) * cos((K / 2.0))) + U;
}
double code(double J, double l, double K, double U) {
	double t_0 = exp(l) - exp(-l);
	double t_1 = cos((K / 2.0));
	double tmp;
	if (t_0 <= -((double) INFINITY)) {
		tmp = (t_1 * (t_0 * J)) + U;
	} else if (t_0 <= 1e-13) {
		tmp = U + (((0.3333333333333333 * pow(l, 3.0)) + ((0.016666666666666666 * pow(l, 5.0)) + (l * 2.0))) * (J * cos((K * 0.5))));
	} else {
		tmp = fma(J, (t_0 * t_1), U);
	}
	return tmp;
}
function code(J, l, K, U)
	return Float64(Float64(Float64(J * Float64(exp(l) - exp(Float64(-l)))) * cos(Float64(K / 2.0))) + U)
end
function code(J, l, K, U)
	t_0 = Float64(exp(l) - exp(Float64(-l)))
	t_1 = cos(Float64(K / 2.0))
	tmp = 0.0
	if (t_0 <= Float64(-Inf))
		tmp = Float64(Float64(t_1 * Float64(t_0 * J)) + U);
	elseif (t_0 <= 1e-13)
		tmp = Float64(U + Float64(Float64(Float64(0.3333333333333333 * (l ^ 3.0)) + Float64(Float64(0.016666666666666666 * (l ^ 5.0)) + Float64(l * 2.0))) * Float64(J * cos(Float64(K * 0.5)))));
	else
		tmp = fma(J, Float64(t_0 * t_1), U);
	end
	return tmp
end
code[J_, l_, K_, U_] := N[(N[(N[(J * N[(N[Exp[l], $MachinePrecision] - N[Exp[(-l)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + U), $MachinePrecision]
code[J_, l_, K_, U_] := Block[{t$95$0 = N[(N[Exp[l], $MachinePrecision] - N[Exp[(-l)], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[Cos[N[(K / 2.0), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[t$95$0, (-Infinity)], N[(N[(t$95$1 * N[(t$95$0 * J), $MachinePrecision]), $MachinePrecision] + U), $MachinePrecision], If[LessEqual[t$95$0, 1e-13], N[(U + N[(N[(N[(0.3333333333333333 * N[Power[l, 3.0], $MachinePrecision]), $MachinePrecision] + N[(N[(0.016666666666666666 * N[Power[l, 5.0], $MachinePrecision]), $MachinePrecision] + N[(l * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(J * N[Cos[N[(K * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(J * N[(t$95$0 * t$95$1), $MachinePrecision] + U), $MachinePrecision]]]]]
\left(J \cdot \left(e^{\ell} - e^{-\ell}\right)\right) \cdot \cos \left(\frac{K}{2}\right) + U
\begin{array}{l}

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

\mathbf{elif}\;t_0 \leq 10^{-13}:\\
\;\;\;\;U + \left(0.3333333333333333 \cdot {\ell}^{3} + \left(0.016666666666666666 \cdot {\ell}^{5} + \ell \cdot 2\right)\right) \cdot \left(J \cdot \cos \left(K \cdot 0.5\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(J, t_0 \cdot t_1, U\right)\\


\end{array}
\end{array}

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.

Herbie found 17 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

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.

Bogosity?

Bogosity

Derivation?

  1. Split input into 3 regimes
  2. if (-.f64 (exp.f64 l) (exp.f64 (neg.f64 l))) < -inf.0

    1. Initial program 100.0%

      \[\left(J \cdot \left(e^{\ell} - e^{-\ell}\right)\right) \cdot \cos \left(\frac{K}{2}\right) + U \]

    if -inf.0 < (-.f64 (exp.f64 l) (exp.f64 (neg.f64 l))) < 1e-13

    1. Initial program 76.7%

      \[\left(J \cdot \left(e^{\ell} - e^{-\ell}\right)\right) \cdot \cos \left(\frac{K}{2}\right) + U \]
    2. Taylor expanded in l around 0 99.9%

      \[\leadsto \left(J \cdot \color{blue}{\left(0.3333333333333333 \cdot {\ell}^{3} + \left(0.016666666666666666 \cdot {\ell}^{5} + 2 \cdot \ell\right)\right)}\right) \cdot \cos \left(\frac{K}{2}\right) + U \]
    3. Taylor expanded in J around 0 99.9%

      \[\leadsto \color{blue}{\left(0.3333333333333333 \cdot {\ell}^{3} + \left(0.016666666666666666 \cdot {\ell}^{5} + 2 \cdot \ell\right)\right) \cdot \left(\cos \left(0.5 \cdot K\right) \cdot J\right)} + U \]

    if 1e-13 < (-.f64 (exp.f64 l) (exp.f64 (neg.f64 l)))

    1. Initial program 100.0%

      \[\left(J \cdot \left(e^{\ell} - e^{-\ell}\right)\right) \cdot \cos \left(\frac{K}{2}\right) + U \]
    2. Simplified100.0%

      \[\leadsto \color{blue}{\mathsf{fma}\left(J, \left(e^{\ell} - e^{-\ell}\right) \cdot \cos \left(\frac{K}{2}\right), U\right)} \]
      Step-by-step derivation

      [Start]100.0%

      \[ \left(J \cdot \left(e^{\ell} - e^{-\ell}\right)\right) \cdot \cos \left(\frac{K}{2}\right) + U \]

      associate-*l* [=>]100.0%

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

      fma-def [=>]100.0%

      \[ \color{blue}{\mathsf{fma}\left(J, \left(e^{\ell} - e^{-\ell}\right) \cdot \cos \left(\frac{K}{2}\right), U\right)} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification99.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{\ell} - e^{-\ell} \leq -\infty:\\ \;\;\;\;\cos \left(\frac{K}{2}\right) \cdot \left(\left(e^{\ell} - e^{-\ell}\right) \cdot J\right) + U\\ \mathbf{elif}\;e^{\ell} - e^{-\ell} \leq 10^{-13}:\\ \;\;\;\;U + \left(0.3333333333333333 \cdot {\ell}^{3} + \left(0.016666666666666666 \cdot {\ell}^{5} + \ell \cdot 2\right)\right) \cdot \left(J \cdot \cos \left(K \cdot 0.5\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(J, \left(e^{\ell} - e^{-\ell}\right) \cdot \cos \left(\frac{K}{2}\right), U\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy99.7%
Cost52488
\[\begin{array}{l} t_0 := e^{\ell} - e^{-\ell}\\ t_1 := \cos \left(\frac{K}{2}\right)\\ \mathbf{if}\;t_0 \leq -\infty:\\ \;\;\;\;t_1 \cdot \left(t_0 \cdot J\right) + U\\ \mathbf{elif}\;t_0 \leq 10^{-13}:\\ \;\;\;\;U + \left(0.3333333333333333 \cdot {\ell}^{3} + \left(0.016666666666666666 \cdot {\ell}^{5} + \ell \cdot 2\right)\right) \cdot \left(J \cdot \cos \left(K \cdot 0.5\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(J, t_0 \cdot t_1, U\right)\\ \end{array} \]
Alternative 2
Accuracy99.7%
Cost46793
\[\begin{array}{l} t_0 := \cos \left(\frac{K}{2}\right)\\ t_1 := e^{\ell} - e^{-\ell}\\ \mathbf{if}\;t_1 \leq -\infty \lor \neg \left(t_1 \leq 10^{-13}\right):\\ \;\;\;\;t_0 \cdot \left(t_1 \cdot J\right) + U\\ \mathbf{else}:\\ \;\;\;\;U + t_0 \cdot \left(J \cdot \left(0.3333333333333333 \cdot {\ell}^{3} + \left(0.016666666666666666 \cdot {\ell}^{5} + \ell \cdot 2\right)\right)\right)\\ \end{array} \]
Alternative 3
Accuracy99.7%
Cost46793
\[\begin{array}{l} t_0 := e^{\ell} - e^{-\ell}\\ \mathbf{if}\;t_0 \leq -\infty \lor \neg \left(t_0 \leq 10^{-13}\right):\\ \;\;\;\;\cos \left(\frac{K}{2}\right) \cdot \left(t_0 \cdot J\right) + U\\ \mathbf{else}:\\ \;\;\;\;U + \left(0.3333333333333333 \cdot {\ell}^{3} + \left(0.016666666666666666 \cdot {\ell}^{5} + \ell \cdot 2\right)\right) \cdot \left(J \cdot \cos \left(K \cdot 0.5\right)\right)\\ \end{array} \]
Alternative 4
Accuracy99.6%
Cost46217
\[\begin{array}{l} t_0 := \cos \left(\frac{K}{2}\right)\\ t_1 := e^{\ell} - e^{-\ell}\\ \mathbf{if}\;t_1 \leq -\infty \lor \neg \left(t_1 \leq 10^{-13}\right):\\ \;\;\;\;t_0 \cdot \left(t_1 \cdot J\right) + U\\ \mathbf{else}:\\ \;\;\;\;U + t_0 \cdot \left(J \cdot \left(0.3333333333333333 \cdot {\ell}^{3} + \ell \cdot 2\right)\right)\\ \end{array} \]
Alternative 5
Accuracy87.1%
Cost39561
\[\begin{array}{l} t_0 := e^{\ell} - e^{-\ell}\\ \mathbf{if}\;t_0 \leq -\infty \lor \neg \left(t_0 \leq 2 \cdot 10^{+58}\right):\\ \;\;\;\;U + t_0 \cdot J\\ \mathbf{else}:\\ \;\;\;\;U + \cos \left(\frac{K}{2}\right) \cdot \left(\ell \cdot \left(J \cdot 2\right)\right)\\ \end{array} \]
Alternative 6
Accuracy94.3%
Cost14220
\[\begin{array}{l} t_0 := \cos \left(\frac{K}{2}\right)\\ t_1 := U + t_0 \cdot \left({\ell}^{5} \cdot \left(J \cdot 0.016666666666666666\right)\right)\\ \mathbf{if}\;\ell \leq -1.42 \cdot 10^{+35}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;\ell \leq -0.08:\\ \;\;\;\;U + \left(e^{\ell} - e^{-\ell}\right) \cdot J\\ \mathbf{elif}\;\ell \leq 5.1 \cdot 10^{-14}:\\ \;\;\;\;U + t_0 \cdot \left(J \cdot \left(0.3333333333333333 \cdot {\ell}^{3} + \ell \cdot 2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 7
Accuracy94.5%
Cost14220
\[\begin{array}{l} t_0 := \cos \left(\frac{K}{2}\right)\\ t_1 := U + t_0 \cdot \left({\ell}^{5} \cdot \left(J \cdot 0.016666666666666666\right)\right)\\ \mathbf{if}\;\ell \leq -3.8 \cdot 10^{+71}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;\ell \leq -3.1:\\ \;\;\;\;U + \left(e^{\ell} - e^{-\ell}\right) \cdot \left(J + J \cdot \left(K \cdot \left(K \cdot -0.125\right)\right)\right)\\ \mathbf{elif}\;\ell \leq 5.1 \cdot 10^{-14}:\\ \;\;\;\;U + t_0 \cdot \left(J \cdot \left(0.3333333333333333 \cdot {\ell}^{3} + \ell \cdot 2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 8
Accuracy94.2%
Cost13964
\[\begin{array}{l} t_0 := \cos \left(\frac{K}{2}\right)\\ t_1 := U + t_0 \cdot \left({\ell}^{5} \cdot \left(J \cdot 0.016666666666666666\right)\right)\\ \mathbf{if}\;\ell \leq -1.42 \cdot 10^{+35}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;\ell \leq -0.0071:\\ \;\;\;\;U + \left(e^{\ell} - e^{-\ell}\right) \cdot J\\ \mathbf{elif}\;\ell \leq 5.1 \cdot 10^{-14}:\\ \;\;\;\;\mathsf{fma}\left(J, t_0 \cdot \left(\ell \cdot 2\right), U\right)\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 9
Accuracy87.1%
Cost13641
\[\begin{array}{l} \mathbf{if}\;\ell \leq -0.003 \lor \neg \left(\ell \leq 140\right):\\ \;\;\;\;U + \left(e^{\ell} - e^{-\ell}\right) \cdot J\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(J, \cos \left(\frac{K}{2}\right) \cdot \left(\ell \cdot 2\right), U\right)\\ \end{array} \]
Alternative 10
Accuracy45.5%
Cost7452
\[\begin{array}{l} t_0 := -8 - U \cdot U\\ t_1 := U + J \cdot \left(-8 + K \cdot K\right)\\ \mathbf{if}\;\ell \leq -2.2 \cdot 10^{+173}:\\ \;\;\;\;{U}^{-8}\\ \mathbf{elif}\;\ell \leq -1.16 \cdot 10^{+86}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;\ell \leq -9 \cdot 10^{+76}:\\ \;\;\;\;U \cdot \left(U - -8\right)\\ \mathbf{elif}\;\ell \leq -2.1 \cdot 10^{+37}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;\ell \leq -290:\\ \;\;\;\;t_1\\ \mathbf{elif}\;\ell \leq 330:\\ \;\;\;\;U\\ \mathbf{elif}\;\ell \leq 1.14 \cdot 10^{+108}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;{U}^{-8}\\ \end{array} \]
Alternative 11
Accuracy64.7%
Cost7104
\[U + \cos \left(\frac{K}{2}\right) \cdot \left(\ell \cdot \left(J \cdot 2\right)\right) \]
Alternative 12
Accuracy44.6%
Cost841
\[\begin{array}{l} \mathbf{if}\;\ell \leq -225 \lor \neg \left(\ell \leq 450\right):\\ \;\;\;\;U + J \cdot \left(-8 + K \cdot K\right)\\ \mathbf{else}:\\ \;\;\;\;U\\ \end{array} \]
Alternative 13
Accuracy42.1%
Cost585
\[\begin{array}{l} \mathbf{if}\;\ell \leq -4.3 \cdot 10^{+75} \lor \neg \left(\ell \leq 1.35 \cdot 10^{+16}\right):\\ \;\;\;\;U \cdot \left(U - -8\right)\\ \mathbf{else}:\\ \;\;\;\;U\\ \end{array} \]
Alternative 14
Accuracy42.2%
Cost585
\[\begin{array}{l} \mathbf{if}\;\ell \leq -0.0004 \lor \neg \left(\ell \leq 330\right):\\ \;\;\;\;-8 - U \cdot U\\ \mathbf{else}:\\ \;\;\;\;U\\ \end{array} \]
Alternative 15
Accuracy42.1%
Cost456
\[\begin{array}{l} \mathbf{if}\;\ell \leq -4 \cdot 10^{+75}:\\ \;\;\;\;U \cdot U\\ \mathbf{elif}\;\ell \leq 3.5 \cdot 10^{+15}:\\ \;\;\;\;U\\ \mathbf{else}:\\ \;\;\;\;U \cdot U\\ \end{array} \]
Alternative 16
Accuracy2.7%
Cost64
\[1 \]
Alternative 17
Accuracy36.9%
Cost64
\[U \]

Reproduce?

herbie shell --seed 2023167 
(FPCore (J l K U)
  :name "Maksimov and Kolovsky, Equation (4)"
  :precision binary64
  (+ (* (* J (- (exp l) (exp (- l)))) (cos (/ K 2.0))) U))