?

Average Accuracy: 9.1% → 99.0%
Time: 14.8s
Precision: binary64
Cost: 32836

?

\[\left(0.5 \cdot \cos re\right) \cdot \left(e^{0 - im} - e^{im}\right) \]
\[\begin{array}{l} t_0 := e^{-im} - e^{im}\\ \mathbf{if}\;t_0 \leq -2:\\ \;\;\;\;\cos re \cdot \left(0.5 \cdot t_0\right)\\ \mathbf{else}:\\ \;\;\;\;\cos re \cdot \left(im \cdot \left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right)\right) - im \cdot \cos re\\ \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))))
   (if (<= t_0 -2.0)
     (* (cos re) (* 0.5 t_0))
     (-
      (* (cos re) (* im (* -0.16666666666666666 (* im im))))
      (* im (cos re))))))
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 tmp;
	if (t_0 <= -2.0) {
		tmp = cos(re) * (0.5 * t_0);
	} else {
		tmp = (cos(re) * (im * (-0.16666666666666666 * (im * im)))) - (im * cos(re));
	}
	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) :: tmp
    t_0 = exp(-im) - exp(im)
    if (t_0 <= (-2.0d0)) then
        tmp = cos(re) * (0.5d0 * t_0)
    else
        tmp = (cos(re) * (im * ((-0.16666666666666666d0) * (im * im)))) - (im * cos(re))
    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 tmp;
	if (t_0 <= -2.0) {
		tmp = Math.cos(re) * (0.5 * t_0);
	} else {
		tmp = (Math.cos(re) * (im * (-0.16666666666666666 * (im * im)))) - (im * Math.cos(re));
	}
	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)
	tmp = 0
	if t_0 <= -2.0:
		tmp = math.cos(re) * (0.5 * t_0)
	else:
		tmp = (math.cos(re) * (im * (-0.16666666666666666 * (im * im)))) - (im * math.cos(re))
	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))
	tmp = 0.0
	if (t_0 <= -2.0)
		tmp = Float64(cos(re) * Float64(0.5 * t_0));
	else
		tmp = Float64(Float64(cos(re) * Float64(im * Float64(-0.16666666666666666 * Float64(im * im)))) - Float64(im * cos(re)));
	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);
	tmp = 0.0;
	if (t_0 <= -2.0)
		tmp = cos(re) * (0.5 * t_0);
	else
		tmp = (cos(re) * (im * (-0.16666666666666666 * (im * im)))) - (im * cos(re));
	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]}, If[LessEqual[t$95$0, -2.0], N[(N[Cos[re], $MachinePrecision] * N[(0.5 * t$95$0), $MachinePrecision]), $MachinePrecision], N[(N[(N[Cos[re], $MachinePrecision] * N[(im * N[(-0.16666666666666666 * N[(im * im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(im * N[Cos[re], $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}\\
\mathbf{if}\;t_0 \leq -2:\\
\;\;\;\;\cos re \cdot \left(0.5 \cdot t_0\right)\\

\mathbf{else}:\\
\;\;\;\;\cos re \cdot \left(im \cdot \left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right)\right) - im \cdot \cos re\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original9.1%
Target99.7%
Herbie99.0%
\[\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)) < -2

    1. Initial program 99.6%

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

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

      [Start]99.6

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

      *-commutative [=>]99.6

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

      associate-*l* [=>]99.6

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

      sub-neg [=>]99.6

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

      sub-neg [<=]99.6

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

      sub0-neg [=>]99.6

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

    if -2 < (-.f64 (exp.f64 (-.f64 0 im)) (exp.f64 im))

    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}{\cos re \cdot \left(0.5 \cdot \left(e^{-im} - e^{im}\right)\right)} \]
      Proof

      [Start]8.8

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

      *-commutative [=>]8.8

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

      associate-*l* [=>]8.8

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

      sub-neg [=>]8.8

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

      sub-neg [<=]8.8

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

      sub0-neg [=>]8.8

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

      \[\leadsto \color{blue}{-0.16666666666666666 \cdot \left(\cos re \cdot {im}^{3}\right) + -1 \cdot \left(\cos re \cdot im\right)} \]
    4. Simplified99.0%

      \[\leadsto \color{blue}{\cos re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right)} \]
      Proof

      [Start]99.0

      \[ -0.16666666666666666 \cdot \left(\cos re \cdot {im}^{3}\right) + -1 \cdot \left(\cos re \cdot im\right) \]

      mul-1-neg [=>]99.0

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

      unsub-neg [=>]99.0

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

      *-commutative [=>]99.0

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

      associate-*l* [=>]99.0

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

      distribute-lft-out-- [=>]99.0

      \[ \color{blue}{\cos re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right)} \]
    5. Applied egg-rr99.0%

      \[\leadsto \cos re \cdot \color{blue}{\left(im \cdot \left(-0.16666666666666666 \cdot \left(im \cdot im\right) - 1\right)\right)} \]
      Proof

      [Start]99.0

      \[ \cos re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right) \]

      *-commutative [=>]99.0

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

      unpow3 [=>]99.0

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

      associate-*r* [=>]99.0

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

      *-un-lft-identity [=>]99.0

      \[ \cos re \cdot \left(\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im - \color{blue}{1 \cdot im}\right) \]

      distribute-rgt-out-- [=>]99.0

      \[ \cos re \cdot \color{blue}{\left(im \cdot \left(-0.16666666666666666 \cdot \left(im \cdot im\right) - 1\right)\right)} \]
    6. Applied egg-rr99.0%

      \[\leadsto \color{blue}{\left(\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im\right) \cdot \cos re + \left(-1 \cdot im\right) \cdot \cos re} \]
      Proof

      [Start]99.0

      \[ \cos re \cdot \left(im \cdot \left(-0.16666666666666666 \cdot \left(im \cdot im\right) - 1\right)\right) \]

      sub-neg [=>]99.0

      \[ \cos re \cdot \left(im \cdot \color{blue}{\left(-0.16666666666666666 \cdot \left(im \cdot im\right) + \left(-1\right)\right)}\right) \]

      distribute-rgt-in [=>]99.0

      \[ \cos re \cdot \color{blue}{\left(\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im + \left(-1\right) \cdot im\right)} \]

      distribute-rgt-in [=>]99.0

      \[ \color{blue}{\left(\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im\right) \cdot \cos re + \left(\left(-1\right) \cdot im\right) \cdot \cos re} \]

      metadata-eval [=>]99.0

      \[ \left(\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im\right) \cdot \cos re + \left(\color{blue}{-1} \cdot im\right) \cdot \cos re \]
    7. Taylor expanded in im around 0 99.0%

      \[\leadsto \left(\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im\right) \cdot \cos re + \color{blue}{-1 \cdot \left(\cos re \cdot im\right)} \]
    8. Simplified99.0%

      \[\leadsto \left(\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im\right) \cdot \cos re + \color{blue}{\cos re \cdot \left(-im\right)} \]
      Proof

      [Start]99.0

      \[ \left(\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im\right) \cdot \cos re + -1 \cdot \left(\cos re \cdot im\right) \]

      *-commutative [=>]99.0

      \[ \left(\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im\right) \cdot \cos re + -1 \cdot \color{blue}{\left(im \cdot \cos re\right)} \]

      mul-1-neg [=>]99.0

      \[ \left(\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im\right) \cdot \cos re + \color{blue}{\left(-im \cdot \cos re\right)} \]

      *-commutative [<=]99.0

      \[ \left(\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im\right) \cdot \cos re + \left(-\color{blue}{\cos re \cdot im}\right) \]

      distribute-rgt-neg-in [=>]99.0

      \[ \left(\left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) \cdot im\right) \cdot \cos re + \color{blue}{\cos re \cdot \left(-im\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{-im} - e^{im} \leq -2:\\ \;\;\;\;\cos re \cdot \left(0.5 \cdot \left(e^{-im} - e^{im}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\cos re \cdot \left(im \cdot \left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right)\right) - im \cdot \cos re\\ \end{array} \]

Alternatives

Alternative 1
Accuracy98.7%
Cost13632
\[\cos re \cdot \left(im \cdot \left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right)\right) - im \cdot \cos re \]
Alternative 2
Accuracy98.7%
Cost7104
\[\cos re \cdot \left(im \cdot \left(-0.16666666666666666 \cdot \left(im \cdot im\right) + -1\right)\right) \]
Alternative 3
Accuracy98.7%
Cost7104
\[\cos re \cdot \left(im \cdot \left(-0.16666666666666666 \cdot \left(im \cdot im\right)\right) - im\right) \]
Alternative 4
Accuracy98.2%
Cost6656
\[im \cdot \left(-\cos re\right) \]
Alternative 5
Accuracy54.7%
Cost128
\[-im \]
Alternative 6
Accuracy3.8%
Cost64
\[-3 \]
Alternative 7
Accuracy3.8%
Cost64
\[-1.5 \]
Alternative 8
Accuracy3.9%
Cost64
\[-3.814697265625 \cdot 10^{-6} \]
Alternative 9
Accuracy5.4%
Cost64
\[0 \]

Error

Reproduce?

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