Average Error: 26.1 → 1.5
Time: 8.9s
Precision: binary64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\mathsf{fma}\left(\frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{-c}{-\mathsf{hypot}\left(c, d\right)}, -\frac{a}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{d}{\mathsf{hypot}\left(c, d\right)}\right) \]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\mathsf{fma}\left(\frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{-c}{-\mathsf{hypot}\left(c, d\right)}, -\frac{a}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{d}{\mathsf{hypot}\left(c, d\right)}\right)
(FPCore (a b c d)
 :precision binary64
 (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (fma
  (/ b (hypot c d))
  (/ (- c) (- (hypot c d)))
  (- (* (/ a (hypot c d)) (/ d (hypot c d))))))
double code(double a, double b, double c, double d) {
	return ((b * c) - (a * d)) / ((c * c) + (d * d));
}
double code(double a, double b, double c, double d) {
	return fma((b / hypot(c, d)), (-c / -hypot(c, d)), -((a / hypot(c, d)) * (d / hypot(c, d))));
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.1
Target0.5
Herbie1.5
\[\begin{array}{l} \mathbf{if}\;\left|d\right| < \left|c\right|:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c + d \cdot \frac{d}{c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-a\right) + b \cdot \frac{c}{d}}{d + c \cdot \frac{c}{d}}\\ \end{array} \]

Derivation

  1. Initial program 26.1

    \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
  2. Simplified26.1

    \[\leadsto \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{fma}\left(c, c, d \cdot d\right)}} \]
  3. Applied egg-rr15.0

    \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{c}{\mathsf{hypot}\left(c, d\right)}, -\frac{a \cdot d}{{\left(\mathsf{hypot}\left(c, d\right)\right)}^{2}}\right)} \]
  4. Applied egg-rr1.5

    \[\leadsto \mathsf{fma}\left(\frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{c}{\mathsf{hypot}\left(c, d\right)}, -\color{blue}{\frac{a}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{d}{\mathsf{hypot}\left(c, d\right)}}\right) \]
  5. Applied egg-rr1.8

    \[\leadsto \mathsf{fma}\left(\frac{b}{\mathsf{hypot}\left(c, d\right)}, \color{blue}{\frac{1}{\sqrt{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{c}{\sqrt{\mathsf{hypot}\left(c, d\right)}}}, -\frac{a}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{d}{\mathsf{hypot}\left(c, d\right)}\right) \]
  6. Applied egg-rr1.5

    \[\leadsto \mathsf{fma}\left(\frac{b}{\mathsf{hypot}\left(c, d\right)}, \color{blue}{\frac{c \cdot -1}{-\mathsf{hypot}\left(c, d\right)}}, -\frac{a}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{d}{\mathsf{hypot}\left(c, d\right)}\right) \]
  7. Final simplification1.5

    \[\leadsto \mathsf{fma}\left(\frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{-c}{-\mathsf{hypot}\left(c, d\right)}, -\frac{a}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{d}{\mathsf{hypot}\left(c, d\right)}\right) \]

Reproduce

herbie shell --seed 2022130 
(FPCore (a b c d)
  :name "Complex division, imag part"
  :precision binary64

  :herbie-target
  (if (< (fabs d) (fabs c)) (/ (- b (* a (/ d c))) (+ c (* d (/ d c)))) (/ (+ (- a) (* b (/ c d))) (+ d (* c (/ c d)))))

  (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))