Average Error: 43.0 → 0.5
Time: 12.1s
Precision: binary64
Cost: 39300
\[\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 5 \cdot 10^{-5}:\\ \;\;\;\;\mathsf{fma}\left(\sin re, -im, \sin re \cdot \left({im}^{3} \cdot -0.16666666666666666\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \left(\sin re \cdot 0.5\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 (<= t_0 5e-5)
     (fma (sin re) (- im) (* (sin re) (* (pow im 3.0) -0.16666666666666666)))
     (* t_0 (* (sin re) 0.5)))))
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 <= 5e-5) {
		tmp = fma(sin(re), -im, (sin(re) * (pow(im, 3.0) * -0.16666666666666666)));
	} else {
		tmp = t_0 * (sin(re) * 0.5);
	}
	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 <= 5e-5)
		tmp = fma(sin(re), Float64(-im), Float64(sin(re) * Float64((im ^ 3.0) * -0.16666666666666666)));
	else
		tmp = Float64(t_0 * Float64(sin(re) * 0.5));
	end
	return 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[LessEqual[t$95$0, 5e-5], N[(N[Sin[re], $MachinePrecision] * (-im) + N[(N[Sin[re], $MachinePrecision] * N[(N[Power[im, 3.0], $MachinePrecision] * -0.16666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(t$95$0 * N[(N[Sin[re], $MachinePrecision] * 0.5), $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 5 \cdot 10^{-5}:\\
\;\;\;\;\mathsf{fma}\left(\sin re, -im, \sin re \cdot \left({im}^{3} \cdot -0.16666666666666666\right)\right)\\

\mathbf{else}:\\
\;\;\;\;t_0 \cdot \left(\sin re \cdot 0.5\right)\\


\end{array}

Error

Target

Original43.0
Target0.2
Herbie0.5
\[\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)) < 5.00000000000000024e-5

    1. Initial program 43.4

      \[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right) \]
    2. Taylor expanded in im around 0 0.5

      \[\leadsto \color{blue}{-0.16666666666666666 \cdot \left(\sin re \cdot {im}^{3}\right) + -1 \cdot \left(\sin re \cdot im\right)} \]
    3. Applied egg-rr0.5

      \[\leadsto \color{blue}{\mathsf{fma}\left(\sin re, -im, \sin re \cdot \left({im}^{3} \cdot -0.16666666666666666\right)\right)} \]

    if 5.00000000000000024e-5 < (-.f64 (exp.f64 (neg.f64 im)) (exp.f64 im))

    1. Initial program 3.5

      \[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right) \]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{-im} - e^{im} \leq 5 \cdot 10^{-5}:\\ \;\;\;\;\mathsf{fma}\left(\sin re, -im, \sin re \cdot \left({im}^{3} \cdot -0.16666666666666666\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(e^{-im} - e^{im}\right) \cdot \left(\sin re \cdot 0.5\right)\\ \end{array} \]

Alternatives

Alternative 1
Error0.5
Cost32964
\[\begin{array}{l} t_0 := e^{-im} - e^{im}\\ \mathbf{if}\;t_0 \leq 5 \cdot 10^{-5}:\\ \;\;\;\;-0.16666666666666666 \cdot \left(\sin re \cdot {im}^{3}\right) - im \cdot \sin re\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \left(\sin re \cdot 0.5\right)\\ \end{array} \]
Alternative 2
Error0.5
Cost32836
\[\begin{array}{l} t_0 := e^{-im} - e^{im}\\ \mathbf{if}\;t_0 \leq 5 \cdot 10^{-5}:\\ \;\;\;\;\sin re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right)\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \left(\sin re \cdot 0.5\right)\\ \end{array} \]
Alternative 3
Error0.9
Cost13312
\[\sin re \cdot \left({im}^{3} \cdot -0.16666666666666666 - im\right) \]
Alternative 4
Error1.2
Cost6656
\[im \cdot \left(-\sin re\right) \]
Alternative 5
Error31.3
Cost256
\[im \cdot \left(-re\right) \]

Error

Reproduce

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