math.sin on complex, imaginary part

?

Percentage Accurate: 53.8% → 99.7%
Time: 18.8s
Precision: binary64
Cost: 46537

?

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

\mathbf{else}:\\
\;\;\;\;t_1 \cdot \left(im \cdot -2 + \left(-0.016666666666666666 \cdot {im}^{5} + -0.3333333333333333 \cdot {im}^{3}\right)\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

Original53.8%
Target99.8%
Herbie99.7%
\[\begin{array}{l} \mathbf{if}\;\left|im\right| < 1:\\ \;\;\;\;-\cos 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 \cos re\right) \cdot \left(e^{0 - im} - e^{im}\right)\\ \end{array} \]

Derivation?

  1. Split input into 2 regimes
  2. if (-.f64 (exp.f64 (-.f64 0 im)) (exp.f64 im)) < -4e3 or 4.99999999999999977e-7 < (-.f64 (exp.f64 (-.f64 0 im)) (exp.f64 im))

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{0 - im} - e^{im}\right) \]
    2. Simplified100.0%

      \[\leadsto \color{blue}{\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} - e^{im}\right)} \]
      Step-by-step derivation

      [Start]100.0

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

      sub0-neg [=>]100.0

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

    if -4e3 < (-.f64 (exp.f64 (-.f64 0 im)) (exp.f64 im)) < 4.99999999999999977e-7

    1. Initial program 8.8%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{0 - im} - e^{im}\right) \]
    2. Simplified8.8%

      \[\leadsto \color{blue}{\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} - e^{im}\right)} \]
      Step-by-step derivation

      [Start]8.8

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

      sub0-neg [=>]8.8

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

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\left(-2 \cdot im + \left(-0.016666666666666666 \cdot {im}^{5} + -0.3333333333333333 \cdot {im}^{3}\right)\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{-im} - e^{im} \leq -4000 \lor \neg \left(e^{-im} - e^{im} \leq 5 \cdot 10^{-7}\right):\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} - e^{im}\right)\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(im \cdot -2 + \left(-0.016666666666666666 \cdot {im}^{5} + -0.3333333333333333 \cdot {im}^{3}\right)\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy99.8%
Cost45961
\[\begin{array}{l} t_0 := e^{-im} - e^{im}\\ \mathbf{if}\;t_0 \leq -0.004 \lor \neg \left(t_0 \leq 5 \cdot 10^{-7}\right):\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot t_0\\ \mathbf{else}:\\ \;\;\;\;\cos re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right)\\ \end{array} \]
Alternative 2
Accuracy96.3%
Cost13832
\[\begin{array}{l} t_0 := e^{-im} - e^{im}\\ t_1 := {im}^{5} \cdot \left(\cos re \cdot -0.008333333333333333\right)\\ \mathbf{if}\;im \leq -7.6 \cdot 10^{+89}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -0.082:\\ \;\;\;\;t_0 \cdot \left(0.5 + re \cdot \left(re \cdot -0.25\right)\right)\\ \mathbf{elif}\;im \leq 8.5:\\ \;\;\;\;\cos re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right)\\ \mathbf{elif}\;im \leq 4.2 \cdot 10^{+61}:\\ \;\;\;\;0.5 \cdot t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 3
Accuracy92.4%
Cost13712
\[\begin{array}{l} t_0 := \frac{\cos re}{im} \cdot \left(im \cdot \left(-im\right)\right)\\ t_1 := 0.5 \cdot \left(e^{-im} - e^{im}\right)\\ \mathbf{if}\;im \leq -1.32 \cdot 10^{+154}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -0.0036:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 8.5:\\ \;\;\;\;im \cdot \left(-\cos re\right)\\ \mathbf{elif}\;im \leq 1.55 \cdot 10^{+85}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 5.8 \cdot 10^{+102}:\\ \;\;\;\;\left(0.5 + re \cdot \left(re \cdot -0.25\right)\right) \cdot \left(im \cdot -2 + -0.3333333333333333 \cdot {im}^{3}\right)\\ \mathbf{elif}\;im \leq 3.9 \cdot 10^{+151}:\\ \;\;\;\;{im}^{3} \cdot -0.16666666666666666 - im\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 4
Accuracy96.9%
Cost13712
\[\begin{array}{l} t_0 := 0.5 \cdot \left(e^{-im} - e^{im}\right)\\ t_1 := {im}^{5} \cdot \left(\cos re \cdot -0.008333333333333333\right)\\ \mathbf{if}\;im \leq -3.3 \cdot 10^{+61}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -0.00081:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 8.5:\\ \;\;\;\;im \cdot \left(-\cos re\right)\\ \mathbf{elif}\;im \leq 4.2 \cdot 10^{+61}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 5
Accuracy97.2%
Cost13712
\[\begin{array}{l} t_0 := 0.5 \cdot \left(e^{-im} - e^{im}\right)\\ t_1 := {im}^{5} \cdot \left(\cos re \cdot -0.008333333333333333\right)\\ \mathbf{if}\;im \leq -3.3 \cdot 10^{+61}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -0.082:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 8.5:\\ \;\;\;\;\cos re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right)\\ \mathbf{elif}\;im \leq 4.2 \cdot 10^{+61}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 6
Accuracy85.2%
Cost8084
\[\begin{array}{l} t_0 := \frac{\cos re}{im} \cdot \left(im \cdot \left(-im\right)\right)\\ t_1 := {im}^{5} \cdot -0.008333333333333333 - im\\ \mathbf{if}\;im \leq -1.32 \cdot 10^{+154}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -9.5 \cdot 10^{+54}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 4.8 \cdot 10^{+27}:\\ \;\;\;\;im \cdot \left(-\cos re\right)\\ \mathbf{elif}\;im \leq 1.7 \cdot 10^{+85}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 5.2 \cdot 10^{+103}:\\ \;\;\;\;\left(0.5 + re \cdot \left(re \cdot -0.25\right)\right) \cdot \left(im \cdot -2 + -0.3333333333333333 \cdot {im}^{3}\right)\\ \mathbf{elif}\;im \leq 3.9 \cdot 10^{+151}:\\ \;\;\;\;{im}^{3} \cdot -0.16666666666666666 - im\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 7
Accuracy85.2%
Cost7704
\[\begin{array}{l} t_0 := \frac{\cos re}{im} \cdot \left(im \cdot \left(-im\right)\right)\\ t_1 := {im}^{5} \cdot -0.008333333333333333 - im\\ \mathbf{if}\;im \leq -1.32 \cdot 10^{+154}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -9.5 \cdot 10^{+54}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 4.8 \cdot 10^{+27}:\\ \;\;\;\;im \cdot \left(-\cos re\right)\\ \mathbf{elif}\;im \leq 1.7 \cdot 10^{+85}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 5.5 \cdot 10^{+102}:\\ \;\;\;\;im \cdot \left(0.08333333333333333 \cdot \left(\left(re \cdot re\right) \cdot \left(im \cdot im\right)\right)\right)\\ \mathbf{elif}\;im \leq 3.9 \cdot 10^{+151}:\\ \;\;\;\;{im}^{3} \cdot -0.16666666666666666 - im\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Accuracy76.1%
Cost7445
\[\begin{array}{l} t_0 := {im}^{3} \cdot -0.16666666666666666 - im\\ \mathbf{if}\;im \leq -7.8 \cdot 10^{+86}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -840:\\ \;\;\;\;0.5 \cdot \left(re \cdot \left(im \cdot re\right)\right) - im\\ \mathbf{elif}\;im \leq 5.2 \cdot 10^{+20}:\\ \;\;\;\;im \cdot \left(-\cos re\right)\\ \mathbf{elif}\;im \leq 5.5 \cdot 10^{+102} \lor \neg \left(im \leq 1.9 \cdot 10^{+278}\right):\\ \;\;\;\;im \cdot \left(0.08333333333333333 \cdot \left(\left(re \cdot re\right) \cdot \left(im \cdot im\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 9
Accuracy78.1%
Cost7445
\[\begin{array}{l} t_0 := {im}^{5} \cdot -0.008333333333333333 - im\\ \mathbf{if}\;im \leq -9.5 \cdot 10^{+54}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 4.5 \cdot 10^{+27}:\\ \;\;\;\;im \cdot \left(-\cos re\right)\\ \mathbf{elif}\;im \leq 1.7 \cdot 10^{+85}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 4.8 \cdot 10^{+102} \lor \neg \left(im \leq 1.9 \cdot 10^{+278}\right):\\ \;\;\;\;im \cdot \left(0.08333333333333333 \cdot \left(\left(re \cdot re\right) \cdot \left(im \cdot im\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;{im}^{3} \cdot -0.16666666666666666 - im\\ \end{array} \]
Alternative 10
Accuracy60.7%
Cost6920
\[\begin{array}{l} \mathbf{if}\;im \leq -500:\\ \;\;\;\;0.5 \cdot \left(re \cdot \left(im \cdot re\right)\right) - im\\ \mathbf{elif}\;im \leq 4 \cdot 10^{+20}:\\ \;\;\;\;im \cdot \left(-\cos re\right)\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(0.08333333333333333 \cdot \left(\left(re \cdot re\right) \cdot \left(im \cdot im\right)\right)\right)\\ \end{array} \]
Alternative 11
Accuracy38.7%
Cost968
\[\begin{array}{l} \mathbf{if}\;im \leq -7.2 \cdot 10^{-24}:\\ \;\;\;\;0.5 \cdot \left(re \cdot \left(im \cdot re\right)\right) - im\\ \mathbf{elif}\;im \leq 4 \cdot 10^{+20}:\\ \;\;\;\;-im\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(0.08333333333333333 \cdot \left(\left(re \cdot re\right) \cdot \left(im \cdot im\right)\right)\right)\\ \end{array} \]
Alternative 12
Accuracy38.3%
Cost841
\[\begin{array}{l} \mathbf{if}\;im \leq -7.2 \cdot 10^{-24} \lor \neg \left(im \leq 4 \cdot 10^{+20}\right):\\ \;\;\;\;0.5 \cdot \left(re \cdot \left(im \cdot re\right)\right) - im\\ \mathbf{else}:\\ \;\;\;\;-im\\ \end{array} \]
Alternative 13
Accuracy38.3%
Cost840
\[\begin{array}{l} \mathbf{if}\;im \leq -5.5 \cdot 10^{-24}:\\ \;\;\;\;0.5 \cdot \left(re \cdot \left(im \cdot re\right)\right) - im\\ \mathbf{elif}\;im \leq 4 \cdot 10^{+20}:\\ \;\;\;\;-im\\ \mathbf{else}:\\ \;\;\;\;re \cdot \left(0.5 \cdot \left(im \cdot re\right)\right) - im\\ \end{array} \]
Alternative 14
Accuracy33.5%
Cost713
\[\begin{array}{l} \mathbf{if}\;re \leq -3.2 \cdot 10^{+158} \lor \neg \left(re \leq 2.2 \cdot 10^{+219}\right):\\ \;\;\;\;-0.25 \cdot \left(\left(re \cdot re\right) \cdot -3\right)\\ \mathbf{else}:\\ \;\;\;\;-im\\ \end{array} \]
Alternative 15
Accuracy37.3%
Cost713
\[\begin{array}{l} \mathbf{if}\;im \leq -660 \lor \neg \left(im \leq 4 \cdot 10^{+20}\right):\\ \;\;\;\;\left(im \cdot re\right) \cdot \left(0.5 \cdot re\right)\\ \mathbf{else}:\\ \;\;\;\;-im\\ \end{array} \]
Alternative 16
Accuracy37.4%
Cost713
\[\begin{array}{l} \mathbf{if}\;im \leq -290 \lor \neg \left(im \leq 4 \cdot 10^{+20}\right):\\ \;\;\;\;0.5 \cdot \left(im \cdot \left(re \cdot re\right)\right)\\ \mathbf{else}:\\ \;\;\;\;-im\\ \end{array} \]
Alternative 17
Accuracy34.6%
Cost712
\[\begin{array}{l} \mathbf{if}\;im \leq -620:\\ \;\;\;\;-0.25 \cdot \left(\left(re \cdot re\right) \cdot 27\right)\\ \mathbf{elif}\;im \leq 4.5 \cdot 10^{+20}:\\ \;\;\;\;-im\\ \mathbf{else}:\\ \;\;\;\;-0.25 \cdot \left(\left(re \cdot re\right) \cdot -3\right)\\ \end{array} \]
Alternative 18
Accuracy30.1%
Cost128
\[-im \]
Alternative 19
Accuracy2.8%
Cost64
\[-3 \]
Alternative 20
Accuracy2.9%
Cost64
\[-0.015625 \]
Alternative 21
Accuracy2.9%
Cost64
\[-3.814697265625 \cdot 10^{-6} \]
Alternative 22
Accuracy3.5%
Cost64
\[0 \]

Error

Reproduce?

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

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

  (* (* 0.5 (cos re)) (- (exp (- 0.0 im)) (exp im))))