math.sin on complex, imaginary part

?

Percentage Accurate: 54.7% → 99.9%
Time: 14.3s
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 -0.01 \lor \neg \left(t_0 \leq 0.02\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 -0.01) (not (<= t_0 0.02)))
     (* 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 <= -0.01) || !(t_0 <= 0.02)) {
		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 <= (-0.01d0)) .or. (.not. (t_0 <= 0.02d0))) 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 <= -0.01) || !(t_0 <= 0.02)) {
		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 <= -0.01) or not (t_0 <= 0.02):
		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 <= -0.01) || !(t_0 <= 0.02))
		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 <= -0.01) || ~((t_0 <= 0.02)))
		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, -0.01], N[Not[LessEqual[t$95$0, 0.02]], $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 -0.01 \lor \neg \left(t_0 \leq 0.02\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 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 13 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

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original54.7%
Target99.8%
Herbie99.9%
\[\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)) < -0.0100000000000000002 or 0.0200000000000000004 < (-.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 -0.0100000000000000002 < (-.f64 (exp.f64 (-.f64 0 im)) (exp.f64 im)) < 0.0200000000000000004

    1. Initial program 8.2%

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

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

      [Start]8.2%

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

      sub0-neg [=>]8.2%

      \[ \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 -0.01 \lor \neg \left(e^{-im} - e^{im} \leq 0.02\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.9%
Cost46537
\[\begin{array}{l} t_0 := e^{-im} - e^{im}\\ t_1 := 0.5 \cdot \cos re\\ \mathbf{if}\;t_0 \leq -0.01 \lor \neg \left(t_0 \leq 0.02\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} \]
Alternative 2
Accuracy99.7%
Cost46089
\[\begin{array}{l} t_0 := e^{-im} - e^{im}\\ \mathbf{if}\;t_0 \leq -0.01 \lor \neg \left(t_0 \leq 2 \cdot 10^{-12}\right):\\ \;\;\;\;\left(0.5 \cdot \cos re\right) \cdot t_0\\ \mathbf{else}:\\ \;\;\;\;{im}^{3} \cdot \left(\cos re \cdot -0.16666666666666666\right) - im \cdot \cos re\\ \end{array} \]
Alternative 3
Accuracy99.7%
Cost45961
\[\begin{array}{l} t_0 := e^{-im} - e^{im}\\ \mathbf{if}\;t_0 \leq -0.01 \lor \neg \left(t_0 \leq 2 \cdot 10^{-12}\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 4
Accuracy95.0%
Cost13842
\[\begin{array}{l} \mathbf{if}\;im \leq -9 \cdot 10^{+84} \lor \neg \left(im \leq -0.0069 \lor \neg \left(im \leq 0.0085\right) \land im \leq 5.6 \cdot 10^{+102}\right):\\ \;\;\;\;\cos re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(e^{-im} - e^{im}\right)\\ \end{array} \]
Alternative 5
Accuracy87.1%
Cost13581
\[\begin{array}{l} \mathbf{if}\;im \leq -3 \cdot 10^{+265}:\\ \;\;\;\;\left({im}^{3} \cdot -0.16666666666666666 - im\right) \cdot \left(\left(re \cdot re\right) \cdot -0.5 + 1\right)\\ \mathbf{elif}\;im \leq -0.0013 \lor \neg \left(im \leq 0.00043\right):\\ \;\;\;\;0.5 \cdot \left(e^{-im} - e^{im}\right)\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(-\cos re\right)\\ \end{array} \]
Alternative 6
Accuracy78.1%
Cost7825
\[\begin{array}{l} t_0 := {im}^{3} \cdot -0.16666666666666666 - im\\ t_1 := t_0 \cdot \left(\left(re \cdot re\right) \cdot -0.5 + 1\right)\\ \mathbf{if}\;im \leq -2 \cdot 10^{+266}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -1.5 \cdot 10^{+161}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -0.0014 \lor \neg \left(im \leq 0.0035\right):\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(-\cos re\right)\\ \end{array} \]
Alternative 7
Accuracy64.1%
Cost7184
\[\begin{array}{l} t_0 := \frac{im \cdot \left(-im\right)}{im + 0.5 \cdot \left(im \cdot \left(re \cdot re\right)\right)}\\ t_1 := im \cdot \left(0.5 \cdot \left(re \cdot re\right) + -1\right)\\ \mathbf{if}\;im \leq -2 \cdot 10^{+273}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -1.9 \cdot 10^{+184}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -700:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 1.7 \cdot 10^{+156}:\\ \;\;\;\;im \cdot \left(-\cos re\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Accuracy74.3%
Cost7181
\[\begin{array}{l} \mathbf{if}\;im \leq -2.6 \cdot 10^{+273}:\\ \;\;\;\;im \cdot \left(0.5 \cdot \left(re \cdot re\right) + -1\right)\\ \mathbf{elif}\;im \leq -0.00034 \lor \neg \left(im \leq 0.0042\right):\\ \;\;\;\;{im}^{3} \cdot -0.16666666666666666 - im\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(-\cos re\right)\\ \end{array} \]
Alternative 9
Accuracy43.7%
Cost1556
\[\begin{array}{l} t_0 := \frac{im \cdot \left(-im\right)}{im + 0.5 \cdot \left(im \cdot \left(re \cdot re\right)\right)}\\ t_1 := im \cdot \left(0.5 \cdot \left(re \cdot re\right) + -1\right)\\ \mathbf{if}\;im \leq -2.6 \cdot 10^{+273}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -2.45 \cdot 10^{+184}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -8 \cdot 10^{-13}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 0.0085:\\ \;\;\;\;-im\\ \mathbf{elif}\;im \leq 1.7 \cdot 10^{+156}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 10
Accuracy36.9%
Cost713
\[\begin{array}{l} \mathbf{if}\;im \leq -480 \lor \neg \left(im \leq 700\right):\\ \;\;\;\;im \cdot \left(0.5 \cdot \left(re \cdot re\right)\right)\\ \mathbf{else}:\\ \;\;\;\;-im\\ \end{array} \]
Alternative 11
Accuracy35.7%
Cost708
\[\begin{array}{l} \mathbf{if}\;re \leq -3.05 \cdot 10^{+189}:\\ \;\;\;\;-3 \cdot \left(0.5 + re \cdot \left(re \cdot -0.25\right)\right)\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(0.5 \cdot \left(re \cdot re\right) + -1\right)\\ \end{array} \]
Alternative 12
Accuracy35.7%
Cost708
\[\begin{array}{l} \mathbf{if}\;re \leq -2.8 \cdot 10^{+193}:\\ \;\;\;\;-3 \cdot \left(0.5 + re \cdot \left(re \cdot -0.25\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot re\right) \cdot \left(im \cdot re\right) - im\\ \end{array} \]
Alternative 13
Accuracy29.2%
Cost128
\[-im \]

Reproduce?

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