math.cos on complex, imaginary part

?

Percentage Accurate: 65.6% → 99.7%
Time: 13.4s
Precision: binary64
Cost: 46281

?

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

\mathbf{else}:\\
\;\;\;\;\sin re \cdot \left(\left({im}^{3} \cdot -0.16666666666666666 - im\right) + {im}^{5} \cdot -0.008333333333333333\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.6%
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)) < -4e3 or 4.99999999999999977e-7 < (-.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 -4e3 < (-.f64 (exp.f64 (neg.f64 im)) (exp.f64 im)) < 4.99999999999999977e-7

    1. Initial program 30.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.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)} \]
    3. Simplified99.8%

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

      [Start]99.8

      \[ -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) \]

      +-commutative [=>]99.8

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

      associate-+r+ [=>]99.8

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

      +-commutative [=>]99.8

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

      mul-1-neg [=>]99.8

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

      *-commutative [=>]99.8

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

      distribute-lft-neg-in [=>]99.8

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

      *-commutative [=>]99.8

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

      associate-*r* [=>]99.8

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

      distribute-rgt-out [=>]99.8

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

      associate-*r* [=>]99.8

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

      *-commutative [=>]99.8

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

      associate-*l* [=>]99.8

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

      distribute-lft-out [=>]99.8

      \[ \color{blue}{\sin re \cdot \left(\left(\left(-im\right) + -0.16666666666666666 \cdot {im}^{3}\right) + -0.008333333333333333 \cdot {im}^{5}\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(e^{-im} - e^{im}\right) \cdot \left(0.5 \cdot \sin re\right)\\ \mathbf{else}:\\ \;\;\;\;\sin re \cdot \left(\left({im}^{3} \cdot -0.16666666666666666 - im\right) + {im}^{5} \cdot -0.008333333333333333\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):\\ \;\;\;\;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 2
Accuracy96.7%
Cost13840
\[\begin{array}{l} t_0 := 0.5 \cdot \left(\left(e^{-im} - e^{im}\right) \cdot re\right)\\ t_1 := -0.008333333333333333 \cdot \left(\sin re \cdot {im}^{5}\right)\\ \mathbf{if}\;im \leq -4.5 \cdot 10^{+61}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -0.00136:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 8.5:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \mathbf{elif}\;im \leq 4.5 \cdot 10^{+61}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 3
Accuracy97.0%
Cost13840
\[\begin{array}{l} t_0 := 0.5 \cdot \left(\left(e^{-im} - e^{im}\right) \cdot re\right)\\ t_1 := -0.008333333333333333 \cdot \left(\sin re \cdot {im}^{5}\right)\\ \mathbf{if}\;im \leq -4.5 \cdot 10^{+61}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -0.086:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 8.5:\\ \;\;\;\;\sin 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 4
Accuracy90.3%
Cost13580
\[\begin{array}{l} t_0 := -0.008333333333333333 \cdot \left(\sin re \cdot {im}^{5}\right)\\ \mathbf{if}\;im \leq -3.3:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 13500:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \mathbf{elif}\;im \leq 1.3 \cdot 10^{+57}:\\ \;\;\;\;im \cdot \left(0.16666666666666666 \cdot {re}^{3} - re\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Accuracy76.8%
Cost7428
\[\begin{array}{l} \mathbf{if}\;im \leq -1.46 \cdot 10^{-7}:\\ \;\;\;\;0.5 \cdot \left(-0.3333333333333333 \cdot \left(re \cdot {im}^{3}\right) + -2 \cdot \left(im \cdot re\right)\right)\\ \mathbf{elif}\;im \leq 13500:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \mathbf{elif}\;im \leq 1.2 \cdot 10^{+109}:\\ \;\;\;\;im \cdot \left(0.16666666666666666 \cdot {re}^{3} - re\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(re \cdot \left(im \cdot \left(-2 + im \cdot \left(im \cdot -0.3333333333333333\right)\right)\right)\right)\\ \end{array} \]
Alternative 6
Accuracy76.9%
Cost7308
\[\begin{array}{l} \mathbf{if}\;im \leq -550:\\ \;\;\;\;0.5 \cdot \left(-0.3333333333333333 \cdot \left(re \cdot {im}^{3}\right)\right)\\ \mathbf{elif}\;im \leq 14000:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \mathbf{elif}\;im \leq 1.2 \cdot 10^{+109}:\\ \;\;\;\;im \cdot \left(0.16666666666666666 \cdot {re}^{3} - re\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(re \cdot \left(im \cdot \left(-2 + im \cdot \left(im \cdot -0.3333333333333333\right)\right)\right)\right)\\ \end{array} \]
Alternative 7
Accuracy76.8%
Cost7180
\[\begin{array}{l} t_0 := 0.5 \cdot \left(re \cdot \left(im \cdot \left(-2 + im \cdot \left(im \cdot -0.3333333333333333\right)\right)\right)\right)\\ \mathbf{if}\;im \leq -1.46 \cdot 10^{-7}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 13500:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \mathbf{elif}\;im \leq 1.2 \cdot 10^{+109}:\\ \;\;\;\;im \cdot \left(0.16666666666666666 \cdot {re}^{3}\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Accuracy76.8%
Cost7180
\[\begin{array}{l} \mathbf{if}\;im \leq -2200:\\ \;\;\;\;0.5 \cdot \left(-0.3333333333333333 \cdot \left(re \cdot {im}^{3}\right)\right)\\ \mathbf{elif}\;im \leq 13500:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \mathbf{elif}\;im \leq 1.2 \cdot 10^{+109}:\\ \;\;\;\;im \cdot \left(0.16666666666666666 \cdot {re}^{3}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(re \cdot \left(im \cdot \left(-2 + im \cdot \left(im \cdot -0.3333333333333333\right)\right)\right)\right)\\ \end{array} \]
Alternative 9
Accuracy77.0%
Cost6921
\[\begin{array}{l} \mathbf{if}\;im \leq -1.46 \cdot 10^{-7} \lor \neg \left(im \leq 2.4 \cdot 10^{+21}\right):\\ \;\;\;\;0.5 \cdot \left(re \cdot \left(im \cdot \left(-2 + im \cdot \left(im \cdot -0.3333333333333333\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-im\right) \cdot \sin re\\ \end{array} \]
Alternative 10
Accuracy52.8%
Cost969
\[\begin{array}{l} \mathbf{if}\;im \leq -390 \lor \neg \left(im \leq 1.65 \cdot 10^{-11}\right):\\ \;\;\;\;0.5 \cdot \left(re \cdot \left(im \cdot \left(-0.3333333333333333 \cdot \left(im \cdot im\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-im\right) \cdot re\\ \end{array} \]
Alternative 11
Accuracy53.2%
Cost832
\[0.5 \cdot \left(re \cdot \left(im \cdot \left(-2 + im \cdot \left(im \cdot -0.3333333333333333\right)\right)\right)\right) \]
Alternative 12
Accuracy33.4%
Cost256
\[\left(-im\right) \cdot re \]
Alternative 13
Accuracy2.8%
Cost64
\[-1 \]
Alternative 14
Accuracy15.4%
Cost64
\[0 \]

Error

Reproduce?

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