Average Error: 21.7 → 0.2
Time: 12.7s
Precision: binary64
\[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right) \]
\[\begin{array}{l} t_0 := e^{-im} - e^{im}\\ t_1 := 0.5 \cdot \sin re\\ \mathbf{if}\;t_0 \leq -\infty:\\ \;\;\;\;t_0 \cdot t_1\\ \mathbf{elif}\;t_0 \leq 0.008696040391028559:\\ \;\;\;\;\left(\sin re \cdot {im}^{3}\right) \cdot -0.16666666666666666 - \sin re \cdot \left(im + 0.008333333333333333 \cdot {im}^{5}\right)\\ \mathbf{else}:\\ \;\;\;\;t_1 \cdot e^{\log t_0}\\ \end{array} \]
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right)
\begin{array}{l}
t_0 := e^{-im} - e^{im}\\
t_1 := 0.5 \cdot \sin re\\
\mathbf{if}\;t_0 \leq -\infty:\\
\;\;\;\;t_0 \cdot t_1\\

\mathbf{elif}\;t_0 \leq 0.008696040391028559:\\
\;\;\;\;\left(\sin re \cdot {im}^{3}\right) \cdot -0.16666666666666666 - \sin re \cdot \left(im + 0.008333333333333333 \cdot {im}^{5}\right)\\

\mathbf{else}:\\
\;\;\;\;t_1 \cdot e^{\log t_0}\\


\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))) (t_1 (* 0.5 (sin re))))
   (if (<= t_0 (- INFINITY))
     (* t_0 t_1)
     (if (<= t_0 0.008696040391028559)
       (-
        (* (* (sin re) (pow im 3.0)) -0.16666666666666666)
        (* (sin re) (+ im (* 0.008333333333333333 (pow im 5.0)))))
       (* t_1 (exp (log t_0)))))))
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 t_1 = 0.5 * sin(re);
	double tmp;
	if (t_0 <= -((double) INFINITY)) {
		tmp = t_0 * t_1;
	} else if (t_0 <= 0.008696040391028559) {
		tmp = ((sin(re) * pow(im, 3.0)) * -0.16666666666666666) - (sin(re) * (im + (0.008333333333333333 * pow(im, 5.0))));
	} else {
		tmp = t_1 * exp(log(t_0));
	}
	return tmp;
}

Error

Bits error versus re

Bits error versus im

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original21.7
Target0.2
Herbie0.2
\[\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 3 regimes
  2. if (-.f64 (exp.f64 (neg.f64 im)) (exp.f64 im)) < -inf.0

    1. Initial program 0.0

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

    if -inf.0 < (-.f64 (exp.f64 (neg.f64 im)) (exp.f64 im)) < 0.008696040391028559

    1. Initial program 43.1

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

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

      \[\leadsto \color{blue}{\left(-\sin re \cdot \left(im + 0.008333333333333333 \cdot {im}^{5}\right)\right) - \sin re \cdot \left(0.16666666666666666 \cdot {im}^{3} + 0.0001984126984126984 \cdot {im}^{7}\right)} \]
    4. Taylor expanded in im around 0 0.4

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

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

    if 0.008696040391028559 < (-.f64 (exp.f64 (neg.f64 im)) (exp.f64 im))

    1. Initial program 0.1

      \[\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} - e^{im}\right) \]
    2. Applied add-exp-log_binary640.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;e^{-im} - e^{im} \leq -\infty:\\ \;\;\;\;\left(e^{-im} - e^{im}\right) \cdot \left(0.5 \cdot \sin re\right)\\ \mathbf{elif}\;e^{-im} - e^{im} \leq 0.008696040391028559:\\ \;\;\;\;\left(\sin re \cdot {im}^{3}\right) \cdot -0.16666666666666666 - \sin re \cdot \left(im + 0.008333333333333333 \cdot {im}^{5}\right)\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot \sin re\right) \cdot e^{\log \left(e^{-im} - e^{im}\right)}\\ \end{array} \]

Reproduce

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