math.cos on complex, imaginary part

?

Percentage Accurate: 66.0% → 99.8%
Time: 8.0s
Precision: binary64
Cost: 53001

?

\[\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 -4 \cdot 10^{+23} \lor \neg \left(t_0 \leq 0.1\right):\\ \;\;\;\;t_0 \cdot \left(0.5 \cdot \sin re\right)\\ \mathbf{else}:\\ \;\;\;\;\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)\\ \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 -4e+23) (not (<= t_0 0.1)))
     (* t_0 (* 0.5 (sin re)))
     (*
      (sin re)
      (+
       (* (pow im 5.0) -0.008333333333333333)
       (+
        (* (pow im 7.0) -0.0001984126984126984)
        (- (* (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 <= -4e+23) || !(t_0 <= 0.1)) {
		tmp = t_0 * (0.5 * sin(re));
	} else {
		tmp = sin(re) * ((pow(im, 5.0) * -0.008333333333333333) + ((pow(im, 7.0) * -0.0001984126984126984) + ((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 <= (-4d+23)) .or. (.not. (t_0 <= 0.1d0))) then
        tmp = t_0 * (0.5d0 * sin(re))
    else
        tmp = sin(re) * (((im ** 5.0d0) * (-0.008333333333333333d0)) + (((im ** 7.0d0) * (-0.0001984126984126984d0)) + (((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 <= -4e+23) || !(t_0 <= 0.1)) {
		tmp = t_0 * (0.5 * Math.sin(re));
	} else {
		tmp = Math.sin(re) * ((Math.pow(im, 5.0) * -0.008333333333333333) + ((Math.pow(im, 7.0) * -0.0001984126984126984) + ((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 <= -4e+23) or not (t_0 <= 0.1):
		tmp = t_0 * (0.5 * math.sin(re))
	else:
		tmp = math.sin(re) * ((math.pow(im, 5.0) * -0.008333333333333333) + ((math.pow(im, 7.0) * -0.0001984126984126984) + ((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 <= -4e+23) || !(t_0 <= 0.1))
		tmp = Float64(t_0 * Float64(0.5 * sin(re)));
	else
		tmp = Float64(sin(re) * Float64(Float64((im ^ 5.0) * -0.008333333333333333) + Float64(Float64((im ^ 7.0) * -0.0001984126984126984) + 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 <= -4e+23) || ~((t_0 <= 0.1)))
		tmp = t_0 * (0.5 * sin(re));
	else
		tmp = sin(re) * (((im ^ 5.0) * -0.008333333333333333) + (((im ^ 7.0) * -0.0001984126984126984) + (((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, -4e+23], N[Not[LessEqual[t$95$0, 0.1]], $MachinePrecision]], N[(t$95$0 * N[(0.5 * N[Sin[re], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Sin[re], $MachinePrecision] * N[(N[(N[Power[im, 5.0], $MachinePrecision] * -0.008333333333333333), $MachinePrecision] + N[(N[(N[Power[im, 7.0], $MachinePrecision] * -0.0001984126984126984), $MachinePrecision] + N[(N[(N[Power[im, 3.0], $MachinePrecision] * -0.16666666666666666), $MachinePrecision] - im), $MachinePrecision]), $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 -4 \cdot 10^{+23} \lor \neg \left(t_0 \leq 0.1\right):\\
\;\;\;\;t_0 \cdot \left(0.5 \cdot \sin re\right)\\

\mathbf{else}:\\
\;\;\;\;\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)\\


\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 14 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

Original66.0%
Target99.8%
Herbie99.8%
\[\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)) < -3.9999999999999997e23 or 0.10000000000000001 < (-.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 -3.9999999999999997e23 < (-.f64 (exp.f64 (neg.f64 im)) (exp.f64 im)) < 0.10000000000000001

    1. Initial program 30.1%

      \[\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)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{-im} - e^{im} \leq -4 \cdot 10^{+23} \lor \neg \left(e^{-im} - e^{im} \leq 0.1\right):\\ \;\;\;\;\left(e^{-im} - e^{im}\right) \cdot \left(0.5 \cdot \sin re\right)\\ \mathbf{else}:\\ \;\;\;\;\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)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy99.8%
Cost53001
\[\begin{array}{l} t_0 := e^{-im} - e^{im}\\ \mathbf{if}\;t_0 \leq -4 \cdot 10^{+23} \lor \neg \left(t_0 \leq 0.1\right):\\ \;\;\;\;t_0 \cdot \left(0.5 \cdot \sin re\right)\\ \mathbf{else}:\\ \;\;\;\;\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)\\ \end{array} \]
Alternative 2
Accuracy99.5%
Cost45961
\[\begin{array}{l} t_0 := e^{-im} - e^{im}\\ \mathbf{if}\;t_0 \leq -4 \cdot 10^{+23} \lor \neg \left(t_0 \leq 2 \cdot 10^{-9}\right):\\ \;\;\;\;t_0 \cdot \left(0.5 \cdot \sin re\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \end{array} \]
Alternative 3
Accuracy94.6%
Cost13708
\[\begin{array}{l} t_0 := {im}^{7} \cdot \left(\sin re \cdot -0.0001984126984126984\right)\\ \mathbf{if}\;im \leq -2.45 \cdot 10^{+67}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -19.5:\\ \;\;\;\;0.5 \cdot \left(\left(e^{-im} - e^{im}\right) \cdot re\right)\\ \mathbf{elif}\;im \leq 5.6:\\ \;\;\;\;\sin re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 4
Accuracy94.3%
Cost13580
\[\begin{array}{l} t_0 := {im}^{7} \cdot \left(\sin re \cdot -0.0001984126984126984\right)\\ \mathbf{if}\;im \leq -2.45 \cdot 10^{+67}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -19.5:\\ \;\;\;\;0.5 \cdot \left(\left(e^{-im} - e^{im}\right) \cdot re\right)\\ \mathbf{elif}\;im \leq 4.2:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Accuracy92.2%
Cost13449
\[\begin{array}{l} \mathbf{if}\;im \leq -4.2 \lor \neg \left(im \leq 4.2\right):\\ \;\;\;\;{im}^{7} \cdot \left(\sin re \cdot -0.0001984126984126984\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \end{array} \]
Alternative 6
Accuracy59.6%
Cost7180
\[\begin{array}{l} t_0 := 0.16666666666666666 \cdot \left(im \cdot {re}^{3}\right)\\ \mathbf{if}\;im \leq -3.4 \cdot 10^{+14}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 580:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \mathbf{elif}\;im \leq 6.4 \cdot 10^{+125}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;im \cdot \left(-re\right)\\ \end{array} \]
Alternative 7
Accuracy81.3%
Cost7049
\[\begin{array}{l} \mathbf{if}\;im \leq -22000000000000 \lor \neg \left(im \leq 9.4 \cdot 10^{+29}\right):\\ \;\;\;\;{im}^{7} \cdot \left(re \cdot -0.0001984126984126984\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \end{array} \]
Alternative 8
Accuracy55.7%
Cost6921
\[\begin{array}{l} \mathbf{if}\;im \leq -6.4 \cdot 10^{+103} \lor \neg \left(im \leq 5.9 \cdot 10^{+134}\right):\\ \;\;\;\;im \cdot \left(-re\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \end{array} \]
Alternative 9
Accuracy32.7%
Cost256
\[im \cdot \left(-re\right) \]
Alternative 10
Accuracy2.7%
Cost64
\[-1.5 \]
Alternative 11
Accuracy2.7%
Cost64
\[-0.004166666666666667 \]
Alternative 12
Accuracy2.8%
Cost64
\[-3.9055157630365495 \cdot 10^{-12} \]
Alternative 13
Accuracy2.9%
Cost64
\[-2.382841615671091 \cdot 10^{-34} \]
Alternative 14
Accuracy14.8%
Cost64
\[0 \]

Reproduce?

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