Average Error: 26.3 → 0.5
Time: 2.1m
Precision: 64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\frac{1 \cdot \left(\frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{c}} - \frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}\right)}{\frac{\mathsf{hypot}\left(c, d\right)}{1}}\]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\frac{1 \cdot \left(\frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{c}} - \frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}\right)}{\frac{\mathsf{hypot}\left(c, d\right)}{1}}
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 ((1.0 * ((b / (hypot(c, d) / c)) - (a / (hypot(c, d) / d)))) / (hypot(c, d) / 1.0));
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original26.3
Target0.4
Herbie0.5
\[\begin{array}{l} \mathbf{if}\;\left|d\right| \lt \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.3

    \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
  2. Using strategy rm
  3. Applied add-sqr-sqrt26.3

    \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
  4. Applied *-un-lft-identity26.3

    \[\leadsto \frac{\color{blue}{1 \cdot \left(b \cdot c - a \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\]
  5. Applied times-frac26.3

    \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
  6. Simplified26.3

    \[\leadsto \color{blue}{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{1}}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}\]
  7. Simplified16.8

    \[\leadsto \frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{1}} \cdot \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right) \cdot 1}}\]
  8. Using strategy rm
  9. Applied div-sub16.8

    \[\leadsto \frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{1}} \cdot \color{blue}{\left(\frac{b \cdot c}{\mathsf{hypot}\left(c, d\right) \cdot 1} - \frac{a \cdot d}{\mathsf{hypot}\left(c, d\right) \cdot 1}\right)}\]
  10. Simplified9.3

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

    \[\leadsto \frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{1}} \cdot \left(\frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{c}} - \color{blue}{\frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}}\right)\]
  12. Using strategy rm
  13. Applied *-un-lft-identity0.7

    \[\leadsto \frac{1}{\color{blue}{1 \cdot \frac{\mathsf{hypot}\left(c, d\right)}{1}}} \cdot \left(\frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{c}} - \frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}\right)\]
  14. Applied associate-/r*0.7

    \[\leadsto \color{blue}{\frac{\frac{1}{1}}{\frac{\mathsf{hypot}\left(c, d\right)}{1}}} \cdot \left(\frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{c}} - \frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}\right)\]
  15. Applied associate-*l/0.5

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

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

Reproduce

herbie shell --seed 2020066 +o rules:numerics
(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))))