math.cos on complex, imaginary part

?

Percentage Accurate: 65.7% → 99.7%
Time: 15.2s
Precision: binary64
Cost: 59529

?

\[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right) \]
\[\begin{array}{l} t_0 := e^{-im} - e^{im}\\ \mathbf{if}\;t_0 \leq -5 \lor \neg \left(t_0 \leq 5 \cdot 10^{-11}\right):\\ \;\;\;\;t_0 \cdot \left(0.5 \cdot \sin re\right)\\ \mathbf{else}:\\ \;\;\;\;\sin re \cdot \left(-0.008333333333333333 \cdot {im}^{5} + -0.0001984126984126984 \cdot {im}^{7}\right) + \sin re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right)\\ \end{array} \]
(FPCore (re im)
 :precision binary64
 (* (* 0.5 (sin re)) (- (exp (- im)) (exp im))))
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (- (exp (- im)) (exp im))))
   (if (or (<= t_0 -5.0) (not (<= t_0 5e-11)))
     (* t_0 (* 0.5 (sin re)))
     (+
      (*
       (sin re)
       (+
        (* -0.008333333333333333 (pow im 5.0))
        (* -0.0001984126984126984 (pow im 7.0))))
      (* (sin re) (- (* (pow im 3.0) -0.16666666666666666) im))))))
double code(double re, double im) {
	return (0.5 * sin(re)) * (exp(-im) - exp(im));
}
double code(double re, double im) {
	double t_0 = exp(-im) - exp(im);
	double tmp;
	if ((t_0 <= -5.0) || !(t_0 <= 5e-11)) {
		tmp = t_0 * (0.5 * sin(re));
	} else {
		tmp = (sin(re) * ((-0.008333333333333333 * pow(im, 5.0)) + (-0.0001984126984126984 * pow(im, 7.0)))) + (sin(re) * ((pow(im, 3.0) * -0.16666666666666666) - im));
	}
	return tmp;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = (0.5d0 * sin(re)) * (exp(-im) - exp(im))
end function
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8) :: t_0
    real(8) :: tmp
    t_0 = exp(-im) - exp(im)
    if ((t_0 <= (-5.0d0)) .or. (.not. (t_0 <= 5d-11))) then
        tmp = t_0 * (0.5d0 * sin(re))
    else
        tmp = (sin(re) * (((-0.008333333333333333d0) * (im ** 5.0d0)) + ((-0.0001984126984126984d0) * (im ** 7.0d0)))) + (sin(re) * (((im ** 3.0d0) * (-0.16666666666666666d0)) - im))
    end if
    code = tmp
end function
public static double code(double re, double im) {
	return (0.5 * Math.sin(re)) * (Math.exp(-im) - Math.exp(im));
}
public static double code(double re, double im) {
	double t_0 = Math.exp(-im) - Math.exp(im);
	double tmp;
	if ((t_0 <= -5.0) || !(t_0 <= 5e-11)) {
		tmp = t_0 * (0.5 * Math.sin(re));
	} else {
		tmp = (Math.sin(re) * ((-0.008333333333333333 * Math.pow(im, 5.0)) + (-0.0001984126984126984 * Math.pow(im, 7.0)))) + (Math.sin(re) * ((Math.pow(im, 3.0) * -0.16666666666666666) - im));
	}
	return tmp;
}
def code(re, im):
	return (0.5 * math.sin(re)) * (math.exp(-im) - math.exp(im))
def code(re, im):
	t_0 = math.exp(-im) - math.exp(im)
	tmp = 0
	if (t_0 <= -5.0) or not (t_0 <= 5e-11):
		tmp = t_0 * (0.5 * math.sin(re))
	else:
		tmp = (math.sin(re) * ((-0.008333333333333333 * math.pow(im, 5.0)) + (-0.0001984126984126984 * math.pow(im, 7.0)))) + (math.sin(re) * ((math.pow(im, 3.0) * -0.16666666666666666) - im))
	return tmp
function code(re, im)
	return Float64(Float64(0.5 * sin(re)) * Float64(exp(Float64(-im)) - exp(im)))
end
function code(re, im)
	t_0 = Float64(exp(Float64(-im)) - exp(im))
	tmp = 0.0
	if ((t_0 <= -5.0) || !(t_0 <= 5e-11))
		tmp = Float64(t_0 * Float64(0.5 * sin(re)));
	else
		tmp = Float64(Float64(sin(re) * Float64(Float64(-0.008333333333333333 * (im ^ 5.0)) + Float64(-0.0001984126984126984 * (im ^ 7.0)))) + Float64(sin(re) * Float64(Float64((im ^ 3.0) * -0.16666666666666666) - im)));
	end
	return tmp
end
function tmp = code(re, im)
	tmp = (0.5 * sin(re)) * (exp(-im) - exp(im));
end
function tmp_2 = code(re, im)
	t_0 = exp(-im) - exp(im);
	tmp = 0.0;
	if ((t_0 <= -5.0) || ~((t_0 <= 5e-11)))
		tmp = t_0 * (0.5 * sin(re));
	else
		tmp = (sin(re) * ((-0.008333333333333333 * (im ^ 5.0)) + (-0.0001984126984126984 * (im ^ 7.0)))) + (sin(re) * (((im ^ 3.0) * -0.16666666666666666) - im));
	end
	tmp_2 = tmp;
end
code[re_, im_] := N[(N[(0.5 * N[Sin[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] - N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[re_, im_] := Block[{t$95$0 = N[(N[Exp[(-im)], $MachinePrecision] - N[Exp[im], $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, -5.0], N[Not[LessEqual[t$95$0, 5e-11]], $MachinePrecision]], N[(t$95$0 * N[(0.5 * N[Sin[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(N[Sin[re], $MachinePrecision] * N[(N[(-0.008333333333333333 * N[Power[im, 5.0], $MachinePrecision]), $MachinePrecision] + N[(-0.0001984126984126984 * N[Power[im, 7.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[Sin[re], $MachinePrecision] * N[(N[(N[Power[im, 3.0], $MachinePrecision] * -0.16666666666666666), $MachinePrecision] - im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
\begin{array}{l}
t_0 := e^{-im} - e^{im}\\
\mathbf{if}\;t_0 \leq -5 \lor \neg \left(t_0 \leq 5 \cdot 10^{-11}\right):\\
\;\;\;\;t_0 \cdot \left(0.5 \cdot \sin re\right)\\

\mathbf{else}:\\
\;\;\;\;\sin re \cdot \left(-0.008333333333333333 \cdot {im}^{5} + -0.0001984126984126984 \cdot {im}^{7}\right) + \sin re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right)\\


\end{array}

Local Percentage Accuracy?

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.

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original65.7%
Target99.8%
Herbie99.7%
\[\begin{array}{l} \mathbf{if}\;\left|im\right| < 1:\\ \;\;\;\;-\sin re \cdot \left(\left(im + \left(\left(0.16666666666666666 \cdot im\right) \cdot im\right) \cdot im\right) + \left(\left(\left(\left(0.008333333333333333 \cdot im\right) \cdot im\right) \cdot im\right) \cdot im\right) \cdot im\right)\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)\\ \end{array} \]

Derivation?

  1. Split input into 2 regimes
  2. if (-.f64 (exp.f64 (neg.f64 im)) (exp.f64 im)) < -5 or 5.00000000000000018e-11 < (-.f64 (exp.f64 (neg.f64 im)) (exp.f64 im))

    1. Initial program 100.0%

      \[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right) \]

    if -5 < (-.f64 (exp.f64 (neg.f64 im)) (exp.f64 im)) < 5.00000000000000018e-11

    1. Initial program 31.4%

      \[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right) \]
    2. Taylor expanded in im around 0 99.8%

      \[\leadsto \color{blue}{-0.0001984126984126984 \cdot \left(\sin re \cdot {im}^{7}\right) + \left(-0.16666666666666666 \cdot \left(\sin re \cdot {im}^{3}\right) + \left(-0.008333333333333333 \cdot \left(\sin re \cdot {im}^{5}\right) + -1 \cdot \left(\sin re \cdot im\right)\right)\right)} \]
    3. Simplified99.8%

      \[\leadsto \color{blue}{\sin re \cdot \left({im}^{5} \cdot -0.008333333333333333 + \left({im}^{7} \cdot -0.0001984126984126984 + \left({im}^{3} \cdot -0.16666666666666666 - im\right)\right)\right)} \]
      Step-by-step derivation

      [Start]99.8

      \[ -0.0001984126984126984 \cdot \left(\sin re \cdot {im}^{7}\right) + \left(-0.16666666666666666 \cdot \left(\sin re \cdot {im}^{3}\right) + \left(-0.008333333333333333 \cdot \left(\sin re \cdot {im}^{5}\right) + -1 \cdot \left(\sin re \cdot im\right)\right)\right) \]

      associate-+r+ [=>]99.8

      \[ \color{blue}{\left(-0.0001984126984126984 \cdot \left(\sin re \cdot {im}^{7}\right) + -0.16666666666666666 \cdot \left(\sin re \cdot {im}^{3}\right)\right) + \left(-0.008333333333333333 \cdot \left(\sin re \cdot {im}^{5}\right) + -1 \cdot \left(\sin re \cdot im\right)\right)} \]

      +-commutative [=>]99.8

      \[ \color{blue}{\left(-0.008333333333333333 \cdot \left(\sin re \cdot {im}^{5}\right) + -1 \cdot \left(\sin re \cdot im\right)\right) + \left(-0.0001984126984126984 \cdot \left(\sin re \cdot {im}^{7}\right) + -0.16666666666666666 \cdot \left(\sin re \cdot {im}^{3}\right)\right)} \]

      associate-+l+ [=>]99.8

      \[ \color{blue}{-0.008333333333333333 \cdot \left(\sin re \cdot {im}^{5}\right) + \left(-1 \cdot \left(\sin re \cdot im\right) + \left(-0.0001984126984126984 \cdot \left(\sin re \cdot {im}^{7}\right) + -0.16666666666666666 \cdot \left(\sin re \cdot {im}^{3}\right)\right)\right)} \]

      associate-*r* [=>]99.8

      \[ \color{blue}{\left(-0.008333333333333333 \cdot \sin re\right) \cdot {im}^{5}} + \left(-1 \cdot \left(\sin re \cdot im\right) + \left(-0.0001984126984126984 \cdot \left(\sin re \cdot {im}^{7}\right) + -0.16666666666666666 \cdot \left(\sin re \cdot {im}^{3}\right)\right)\right) \]

      *-commutative [=>]99.8

      \[ \color{blue}{\left(\sin re \cdot -0.008333333333333333\right)} \cdot {im}^{5} + \left(-1 \cdot \left(\sin re \cdot im\right) + \left(-0.0001984126984126984 \cdot \left(\sin re \cdot {im}^{7}\right) + -0.16666666666666666 \cdot \left(\sin re \cdot {im}^{3}\right)\right)\right) \]

      associate-*l* [=>]99.8

      \[ \color{blue}{\sin re \cdot \left(-0.008333333333333333 \cdot {im}^{5}\right)} + \left(-1 \cdot \left(\sin re \cdot im\right) + \left(-0.0001984126984126984 \cdot \left(\sin re \cdot {im}^{7}\right) + -0.16666666666666666 \cdot \left(\sin re \cdot {im}^{3}\right)\right)\right) \]

      +-commutative [<=]99.8

      \[ \sin re \cdot \left(-0.008333333333333333 \cdot {im}^{5}\right) + \color{blue}{\left(\left(-0.0001984126984126984 \cdot \left(\sin re \cdot {im}^{7}\right) + -0.16666666666666666 \cdot \left(\sin re \cdot {im}^{3}\right)\right) + -1 \cdot \left(\sin re \cdot im\right)\right)} \]

      mul-1-neg [=>]99.8

      \[ \sin re \cdot \left(-0.008333333333333333 \cdot {im}^{5}\right) + \left(\left(-0.0001984126984126984 \cdot \left(\sin re \cdot {im}^{7}\right) + -0.16666666666666666 \cdot \left(\sin re \cdot {im}^{3}\right)\right) + \color{blue}{\left(-\sin re \cdot im\right)}\right) \]

      unsub-neg [=>]99.8

      \[ \sin re \cdot \left(-0.008333333333333333 \cdot {im}^{5}\right) + \color{blue}{\left(\left(-0.0001984126984126984 \cdot \left(\sin re \cdot {im}^{7}\right) + -0.16666666666666666 \cdot \left(\sin re \cdot {im}^{3}\right)\right) - \sin re \cdot im\right)} \]
    4. Applied egg-rr99.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left({im}^{5}, -0.008333333333333333, {im}^{7} \cdot -0.0001984126984126984\right) \cdot \sin re + \left({im}^{3} \cdot -0.16666666666666666 - im\right) \cdot \sin re} \]
      Step-by-step derivation

      [Start]99.8

      \[ \sin re \cdot \left({im}^{5} \cdot -0.008333333333333333 + \left({im}^{7} \cdot -0.0001984126984126984 + \left({im}^{3} \cdot -0.16666666666666666 - im\right)\right)\right) \]

      associate-+r+ [=>]99.8

      \[ \sin re \cdot \color{blue}{\left(\left({im}^{5} \cdot -0.008333333333333333 + {im}^{7} \cdot -0.0001984126984126984\right) + \left({im}^{3} \cdot -0.16666666666666666 - im\right)\right)} \]

      distribute-rgt-in [=>]99.8

      \[ \color{blue}{\left({im}^{5} \cdot -0.008333333333333333 + {im}^{7} \cdot -0.0001984126984126984\right) \cdot \sin re + \left({im}^{3} \cdot -0.16666666666666666 - im\right) \cdot \sin re} \]

      fma-def [=>]99.8

      \[ \color{blue}{\mathsf{fma}\left({im}^{5}, -0.008333333333333333, {im}^{7} \cdot -0.0001984126984126984\right)} \cdot \sin re + \left({im}^{3} \cdot -0.16666666666666666 - im\right) \cdot \sin re \]
    5. Taylor expanded in re around inf 99.8%

      \[\leadsto \color{blue}{\sin re \cdot \left(-0.008333333333333333 \cdot {im}^{5} + -0.0001984126984126984 \cdot {im}^{7}\right)} + \left({im}^{3} \cdot -0.16666666666666666 - im\right) \cdot \sin re \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{-im} - e^{im} \leq -5 \lor \neg \left(e^{-im} - e^{im} \leq 5 \cdot 10^{-11}\right):\\ \;\;\;\;\left(e^{-im} - e^{im}\right) \cdot \left(0.5 \cdot \sin re\right)\\ \mathbf{else}:\\ \;\;\;\;\sin re \cdot \left(-0.008333333333333333 \cdot {im}^{5} + -0.0001984126984126984 \cdot {im}^{7}\right) + \sin re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy99.7%
Cost53001
\[\begin{array}{l} t_0 := e^{-im} - e^{im}\\ \mathbf{if}\;t_0 \leq -5 \lor \neg \left(t_0 \leq 5 \cdot 10^{-11}\right):\\ \;\;\;\;t_0 \cdot \left(0.5 \cdot \sin re\right)\\ \mathbf{else}:\\ \;\;\;\;\sin re \cdot \left(-0.008333333333333333 \cdot {im}^{5} + \left(\left({im}^{3} \cdot -0.16666666666666666 - im\right) + -0.0001984126984126984 \cdot {im}^{7}\right)\right)\\ \end{array} \]
Alternative 2
Accuracy99.7%
Cost46089
\[\begin{array}{l} t_0 := e^{-im} - e^{im}\\ \mathbf{if}\;t_0 \leq -0.02 \lor \neg \left(t_0 \leq 5 \cdot 10^{-11}\right):\\ \;\;\;\;t_0 \cdot \left(0.5 \cdot \sin re\right)\\ \mathbf{else}:\\ \;\;\;\;-0.16666666666666666 \cdot \left(\sin re \cdot {im}^{3}\right) - im \cdot \sin re\\ \end{array} \]
Alternative 3
Accuracy99.7%
Cost45961
\[\begin{array}{l} t_0 := e^{-im} - e^{im}\\ \mathbf{if}\;t_0 \leq -0.02 \lor \neg \left(t_0 \leq 5 \cdot 10^{-11}\right):\\ \;\;\;\;t_0 \cdot \left(0.5 \cdot \sin re\right)\\ \mathbf{else}:\\ \;\;\;\;\sin re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right)\\ \end{array} \]
Alternative 4
Accuracy97.3%
Cost13840
\[\begin{array}{l} t_0 := 0.5 \cdot \left(\left(e^{-im} - e^{im}\right) \cdot re\right)\\ t_1 := -0.0001984126984126984 \cdot \left(\sin re \cdot {im}^{7}\right)\\ \mathbf{if}\;im \leq -2.7 \cdot 10^{+52}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -0.55:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 2.3:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \mathbf{elif}\;im \leq 1.1 \cdot 10^{+44}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 5
Accuracy97.6%
Cost13840
\[\begin{array}{l} t_0 := 0.5 \cdot \left(\left(e^{-im} - e^{im}\right) \cdot re\right)\\ t_1 := -0.0001984126984126984 \cdot \left(\sin re \cdot {im}^{7}\right)\\ \mathbf{if}\;im \leq -2.7 \cdot 10^{+52}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -0.55:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 2.3:\\ \;\;\;\;\sin re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right)\\ \mathbf{elif}\;im \leq 1.1 \cdot 10^{+44}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 6
Accuracy92.7%
Cost13449
\[\begin{array}{l} \mathbf{if}\;im \leq -4.2 \lor \neg \left(im \leq 4.2\right):\\ \;\;\;\;-0.0001984126984126984 \cdot \left(\sin re \cdot {im}^{7}\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \end{array} \]
Alternative 7
Accuracy77.4%
Cost8072
\[\begin{array}{l} t_0 := im \cdot \left(im \cdot -0.3333333333333333\right)\\ \mathbf{if}\;im \leq -5.5 \cdot 10^{+128}:\\ \;\;\;\;0.5 \cdot \left(\left(im \cdot re\right) \cdot \left(-0.3333333333333333 \cdot \left(im \cdot im\right) + -2\right)\right)\\ \mathbf{elif}\;im \leq -3.3 \cdot 10^{+27}:\\ \;\;\;\;0.5 \cdot \left(\left(im \cdot re\right) \cdot \frac{4 - {t_0}^{2}}{-2 - t_0}\right)\\ \mathbf{elif}\;im \leq 80000000000:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(re \cdot \left(im \cdot t_0\right) + re \cdot \left(im \cdot -2\right)\right)\\ \end{array} \]
Alternative 8
Accuracy77.1%
Cost6921
\[\begin{array}{l} \mathbf{if}\;im \leq -3.7 \cdot 10^{+27} \lor \neg \left(im \leq 2600000\right):\\ \;\;\;\;0.5 \cdot \left(re \cdot \left(im \cdot \left(im \cdot \left(im \cdot -0.3333333333333333\right)\right)\right) + re \cdot \left(im \cdot -2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \end{array} \]
Alternative 9
Accuracy52.6%
Cost1088
\[0.5 \cdot \left(re \cdot \left(im \cdot \left(im \cdot \left(im \cdot -0.3333333333333333\right)\right)\right) + re \cdot \left(im \cdot -2\right)\right) \]
Alternative 10
Accuracy49.5%
Cost832
\[0.5 \cdot \left(\left(im \cdot re\right) \cdot \left(-0.3333333333333333 \cdot \left(im \cdot im\right) + -2\right)\right) \]
Alternative 11
Accuracy32.1%
Cost256
\[im \cdot \left(-re\right) \]
Alternative 12
Accuracy2.7%
Cost64
\[-3 \]
Alternative 13
Accuracy2.7%
Cost64
\[-0.5 \]
Alternative 14
Accuracy14.7%
Cost64
\[0 \]

Error

Reproduce?

herbie shell --seed 2023159 
(FPCore (re im)
  :name "math.cos on complex, imaginary part"
  :precision binary64

  :herbie-target
  (if (< (fabs im) 1.0) (- (* (sin re) (+ (+ im (* (* (* 0.16666666666666666 im) im) im)) (* (* (* (* (* 0.008333333333333333 im) im) im) im) im)))) (* (* 0.5 (sin re)) (- (exp (- im)) (exp im))))

  (* (* 0.5 (sin re)) (- (exp (- im)) (exp im))))