Average Error: 44.0 → 0.7
Time: 8.5s
Precision: binary64
\[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right) \]
\[-\mathsf{fma}\left(0.16666666666666666, \sin re \cdot {im}^{3}, \sin re \cdot \mathsf{fma}\left(0.008333333333333333, {im}^{5}, im\right)\right) \]
(FPCore (re im)
 :precision binary64
 (* (* 0.5 (sin re)) (- (exp (- im)) (exp im))))
(FPCore (re im)
 :precision binary64
 (-
  (fma
   0.16666666666666666
   (* (sin re) (pow im 3.0))
   (* (sin re) (fma 0.008333333333333333 (pow im 5.0) im)))))
double code(double re, double im) {
	return (0.5 * sin(re)) * (exp(-im) - exp(im));
}
double code(double re, double im) {
	return -fma(0.16666666666666666, (sin(re) * pow(im, 3.0)), (sin(re) * fma(0.008333333333333333, pow(im, 5.0), im)));
}
function code(re, im)
	return Float64(Float64(0.5 * sin(re)) * Float64(exp(Float64(-im)) - exp(im)))
end
function code(re, im)
	return Float64(-fma(0.16666666666666666, Float64(sin(re) * (im ^ 3.0)), Float64(sin(re) * fma(0.008333333333333333, (im ^ 5.0), im))))
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_] := (-N[(0.16666666666666666 * N[(N[Sin[re], $MachinePrecision] * N[Power[im, 3.0], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[re], $MachinePrecision] * N[(0.008333333333333333 * N[Power[im, 5.0], $MachinePrecision] + im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision])
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
-\mathsf{fma}\left(0.16666666666666666, \sin re \cdot {im}^{3}, \sin re \cdot \mathsf{fma}\left(0.008333333333333333, {im}^{5}, im\right)\right)

Error

Bits error versus re

Bits error versus im

Target

Original44.0
Target0.3
Herbie0.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. Initial program 44.0

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

    \[\leadsto \left(0.5 \cdot \sin re\right) \cdot \color{blue}{\left(-\left(0.016666666666666666 \cdot {im}^{5} + \left(0.3333333333333333 \cdot {im}^{3} + 2 \cdot im\right)\right)\right)} \]
  3. Simplified0.7

    \[\leadsto \left(0.5 \cdot \sin re\right) \cdot \color{blue}{\mathsf{fma}\left({im}^{5}, -0.016666666666666666, \mathsf{fma}\left({im}^{3}, -0.3333333333333333, -2 \cdot im\right)\right)} \]
  4. Applied egg-rr0.8

    \[\leadsto \left(0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sin re\right)\right)}\right) \cdot \mathsf{fma}\left({im}^{5}, -0.016666666666666666, \mathsf{fma}\left({im}^{3}, -0.3333333333333333, -2 \cdot im\right)\right) \]
  5. Taylor expanded in im around 0 0.7

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

    \[\leadsto \color{blue}{-\mathsf{fma}\left(0.16666666666666666, \sin re \cdot {im}^{3}, \sin re \cdot \mathsf{fma}\left(0.008333333333333333, {im}^{5}, im\right)\right)} \]
  7. Final simplification0.7

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

Reproduce

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