math.sin on complex, imaginary part

?

Percentage Accurate: 54.0% → 99.5%
Time: 10.6s
Precision: binary64
Cost: 46537

?

\[\left(0.5 \cdot \cos re\right) \cdot \left(e^{0 - im} - e^{im}\right) \]
\[\begin{array}{l} t_0 := 0.5 \cdot \cos re\\ t_1 := e^{-im} - e^{im}\\ \mathbf{if}\;t_1 \leq -\infty \lor \neg \left(t_1 \leq 2 \cdot 10^{-9}\right):\\ \;\;\;\;t_0 \cdot t_1\\ \mathbf{else}:\\ \;\;\;\;t_0 \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 (* 0.5 (cos re))) (t_1 (- (exp (- im)) (exp im))))
   (if (or (<= t_1 (- INFINITY)) (not (<= t_1 2e-9)))
     (* t_0 t_1)
     (*
      t_0
      (+
       (* 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 = 0.5 * cos(re);
	double t_1 = exp(-im) - exp(im);
	double tmp;
	if ((t_1 <= -((double) INFINITY)) || !(t_1 <= 2e-9)) {
		tmp = t_0 * t_1;
	} else {
		tmp = t_0 * ((im * -2.0) + ((-0.016666666666666666 * pow(im, 5.0)) + (-0.3333333333333333 * pow(im, 3.0))));
	}
	return tmp;
}
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 = 0.5 * Math.cos(re);
	double t_1 = Math.exp(-im) - Math.exp(im);
	double tmp;
	if ((t_1 <= -Double.POSITIVE_INFINITY) || !(t_1 <= 2e-9)) {
		tmp = t_0 * t_1;
	} else {
		tmp = t_0 * ((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 = 0.5 * math.cos(re)
	t_1 = math.exp(-im) - math.exp(im)
	tmp = 0
	if (t_1 <= -math.inf) or not (t_1 <= 2e-9):
		tmp = t_0 * t_1
	else:
		tmp = t_0 * ((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(0.5 * cos(re))
	t_1 = Float64(exp(Float64(-im)) - exp(im))
	tmp = 0.0
	if ((t_1 <= Float64(-Inf)) || !(t_1 <= 2e-9))
		tmp = Float64(t_0 * t_1);
	else
		tmp = Float64(t_0 * 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 = 0.5 * cos(re);
	t_1 = exp(-im) - exp(im);
	tmp = 0.0;
	if ((t_1 <= -Inf) || ~((t_1 <= 2e-9)))
		tmp = t_0 * t_1;
	else
		tmp = t_0 * ((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[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Exp[(-im)], $MachinePrecision] - N[Exp[im], $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$1, (-Infinity)], N[Not[LessEqual[t$95$1, 2e-9]], $MachinePrecision]], N[(t$95$0 * t$95$1), $MachinePrecision], N[(t$95$0 * 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 := 0.5 \cdot \cos re\\
t_1 := e^{-im} - e^{im}\\
\mathbf{if}\;t_1 \leq -\infty \lor \neg \left(t_1 \leq 2 \cdot 10^{-9}\right):\\
\;\;\;\;t_0 \cdot t_1\\

\mathbf{else}:\\
\;\;\;\;t_0 \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 19 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.0%
Target99.8%
Herbie99.5%
\[\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)) < -inf.0 or 2.00000000000000012e-9 < (-.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 -inf.0 < (-.f64 (exp.f64 (-.f64 0 im)) (exp.f64 im)) < 2.00000000000000012e-9

    1. Initial program 7.8%

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

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

      [Start]7.8%

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

      sub0-neg [=>]7.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 -\infty \lor \neg \left(e^{-im} - e^{im} \leq 2 \cdot 10^{-9}\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.5%
Cost46537
\[\begin{array}{l} t_0 := 0.5 \cdot \cos re\\ t_1 := e^{-im} - e^{im}\\ \mathbf{if}\;t_1 \leq -\infty \lor \neg \left(t_1 \leq 2 \cdot 10^{-9}\right):\\ \;\;\;\;t_0 \cdot t_1\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \left(im \cdot -2 + \left(-0.016666666666666666 \cdot {im}^{5} + -0.3333333333333333 \cdot {im}^{3}\right)\right)\\ \end{array} \]
Alternative 2
Accuracy99.5%
Cost45961
\[\begin{array}{l} t_0 := e^{-im} - e^{im}\\ \mathbf{if}\;t_0 \leq -\infty \lor \neg \left(t_0 \leq 2 \cdot 10^{-9}\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 3
Accuracy96.8%
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 -1.16 \cdot 10^{+76}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -0.00075:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 0.0013:\\ \;\;\;\;\cos re \cdot \left(-im\right)\\ \mathbf{elif}\;im \leq 4.5 \cdot 10^{+61}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 4
Accuracy97.0%
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 -1.16 \cdot 10^{+76}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -0.092:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 0.0034:\\ \;\;\;\;\cos re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right)\\ \mathbf{elif}\;im \leq 4.5 \cdot 10^{+61}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 5
Accuracy86.9%
Cost13580
\[\begin{array}{l} t_0 := 0.5 \cdot \left(e^{-im} - e^{im}\right)\\ \mathbf{if}\;im \leq -0.00034:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 0.0106:\\ \;\;\;\;\cos re \cdot \left(-im\right)\\ \mathbf{elif}\;im \leq 2 \cdot 10^{+183}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\left({im}^{3} \cdot -0.16666666666666666 - im\right) \cdot \left(\left(re \cdot re\right) \cdot -0.5 + 1\right)\\ \end{array} \]
Alternative 6
Accuracy75.9%
Cost13516
\[\begin{array}{l} t_0 := {im}^{3} \cdot -0.16666666666666666 - im\\ \mathbf{if}\;im \leq -2.15 \cdot 10^{+40}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 2.1 \cdot 10^{+61}:\\ \;\;\;\;\cos re \cdot \left(-im\right)\\ \mathbf{elif}\;im \leq 1.65 \cdot 10^{+97}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(re \cdot \left(re \cdot 0.75\right)\right)\right)\\ \mathbf{elif}\;im \leq 1.8 \cdot 10^{+183}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \left(\left(re \cdot re\right) \cdot -0.5 + 1\right)\\ \end{array} \]
Alternative 7
Accuracy75.9%
Cost7824
\[\begin{array}{l} t_0 := {im}^{3} \cdot -0.16666666666666666 - im\\ \mathbf{if}\;im \leq -5 \cdot 10^{+39}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 480:\\ \;\;\;\;\cos re \cdot \left(-im\right)\\ \mathbf{elif}\;im \leq 4.5 \cdot 10^{+99}:\\ \;\;\;\;\left(re \cdot re\right) \cdot -6.75\\ \mathbf{elif}\;im \leq 2 \cdot 10^{+183}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \left(\left(re \cdot re\right) \cdot -0.5 + 1\right)\\ \end{array} \]
Alternative 8
Accuracy75.8%
Cost7180
\[\begin{array}{l} t_0 := {im}^{3} \cdot -0.16666666666666666 - im\\ \mathbf{if}\;im \leq -9.8 \cdot 10^{+39}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 620:\\ \;\;\;\;\cos re \cdot \left(-im\right)\\ \mathbf{elif}\;im \leq 5.5 \cdot 10^{+100}:\\ \;\;\;\;\left(re \cdot re\right) \cdot -6.75\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 9
Accuracy57.7%
Cost6920
\[\begin{array}{l} \mathbf{if}\;im \leq -1.2 \cdot 10^{+70}:\\ \;\;\;\;\left(re \cdot re\right) \cdot 0.75\\ \mathbf{elif}\;im \leq 2.2 \cdot 10^{+71}:\\ \;\;\;\;\cos re \cdot \left(-im\right)\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot re\right) \cdot \left(im \cdot re\right) - im\\ \end{array} \]
Alternative 10
Accuracy35.0%
Cost972
\[\begin{array}{l} \mathbf{if}\;im \leq -9.5 \cdot 10^{+69}:\\ \;\;\;\;\left(re \cdot re\right) \cdot 0.75\\ \mathbf{elif}\;im \leq 520:\\ \;\;\;\;-im\\ \mathbf{elif}\;im \leq 4.2 \cdot 10^{+180}:\\ \;\;\;\;\left(re \cdot re\right) \cdot -6.75\\ \mathbf{else}:\\ \;\;\;\;\left(re \cdot re\right) \cdot \left(\left(im \cdot -2\right) \cdot -0.25\right)\\ \end{array} \]
Alternative 11
Accuracy35.0%
Cost844
\[\begin{array}{l} \mathbf{if}\;im \leq -9.5 \cdot 10^{+69}:\\ \;\;\;\;\left(re \cdot re\right) \cdot 0.75\\ \mathbf{elif}\;im \leq 780:\\ \;\;\;\;-im\\ \mathbf{elif}\;im \leq 1.8 \cdot 10^{+186}:\\ \;\;\;\;\left(re \cdot re\right) \cdot -6.75\\ \mathbf{else}:\\ \;\;\;\;re \cdot \left(re \cdot \left(im \cdot 0.5\right)\right)\\ \end{array} \]
Alternative 12
Accuracy35.8%
Cost840
\[\begin{array}{l} \mathbf{if}\;im \leq -1.32 \cdot 10^{+70}:\\ \;\;\;\;\left(re \cdot re\right) \cdot 0.75\\ \mathbf{elif}\;im \leq 2.15 \cdot 10^{+61}:\\ \;\;\;\;-im\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot re\right) \cdot \left(im \cdot re\right) - im\\ \end{array} \]
Alternative 13
Accuracy35.3%
Cost712
\[\begin{array}{l} \mathbf{if}\;im \leq -9.5 \cdot 10^{+69}:\\ \;\;\;\;\left(re \cdot re\right) \cdot 0.75\\ \mathbf{elif}\;im \leq 2.1 \cdot 10^{+61}:\\ \;\;\;\;-im\\ \mathbf{else}:\\ \;\;\;\;re \cdot \left(re \cdot \left(im \cdot 0.5\right)\right)\\ \end{array} \]
Alternative 14
Accuracy34.0%
Cost585
\[\begin{array}{l} \mathbf{if}\;im \leq -2.5 \cdot 10^{+70} \lor \neg \left(im \leq 6 \cdot 10^{+66}\right):\\ \;\;\;\;\left(re \cdot re\right) \cdot 0.75\\ \mathbf{else}:\\ \;\;\;\;-im\\ \end{array} \]
Alternative 15
Accuracy29.7%
Cost128
\[-im \]
Alternative 16
Accuracy2.9%
Cost64
\[-3 \]
Alternative 17
Accuracy2.9%
Cost64
\[-0.015625 \]
Alternative 18
Accuracy2.9%
Cost64
\[-3.814697265625 \cdot 10^{-6} \]
Alternative 19
Accuracy3.5%
Cost64
\[0 \]

Reproduce?

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