Average Error: 25.7 → 12.7
Time: 30.8s
Precision: 64
Internal Precision: 128
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -2.7923210046731917 \cdot 10^{+118}:\\ \;\;\;\;\frac{-b}{\sqrt{d^2 + c^2}^*}\\ \mathbf{elif}\;c \le 3.9750110958235406 \cdot 10^{+175}:\\ \;\;\;\;\frac{\frac{1}{\frac{\sqrt{d^2 + c^2}^*}{b \cdot c - a \cdot d}}}{\sqrt{d^2 + c^2}^*}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\sqrt{d^2 + c^2}^*}\\ \end{array}\]

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.7
Target0.5
Herbie12.7
\[\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. Split input into 3 regimes
  2. if c < -2.7923210046731917e+118

    1. Initial program 40.9

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified40.9

      \[\leadsto \color{blue}{\frac{b \cdot c - a \cdot d}{(d \cdot d + \left(c \cdot c\right))_*}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt40.9

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    5. Applied associate-/r*40.9

      \[\leadsto \color{blue}{\frac{\frac{b \cdot c - a \cdot d}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    6. Using strategy rm
    7. Applied fma-udef40.9

      \[\leadsto \frac{\frac{b \cdot c - a \cdot d}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}\]
    8. Applied hypot-def40.9

      \[\leadsto \frac{\frac{b \cdot c - a \cdot d}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\color{blue}{\sqrt{d^2 + c^2}^*}}\]
    9. Taylor expanded around -inf 16.7

      \[\leadsto \frac{\color{blue}{-1 \cdot b}}{\sqrt{d^2 + c^2}^*}\]
    10. Simplified16.7

      \[\leadsto \frac{\color{blue}{-b}}{\sqrt{d^2 + c^2}^*}\]

    if -2.7923210046731917e+118 < c < 3.9750110958235406e+175

    1. Initial program 19.4

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified19.4

      \[\leadsto \color{blue}{\frac{b \cdot c - a \cdot d}{(d \cdot d + \left(c \cdot c\right))_*}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt19.4

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    5. Applied associate-/r*19.3

      \[\leadsto \color{blue}{\frac{\frac{b \cdot c - a \cdot d}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    6. Using strategy rm
    7. Applied fma-udef19.3

      \[\leadsto \frac{\frac{b \cdot c - a \cdot d}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}\]
    8. Applied hypot-def19.3

      \[\leadsto \frac{\frac{b \cdot c - a \cdot d}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\color{blue}{\sqrt{d^2 + c^2}^*}}\]
    9. Using strategy rm
    10. Applied fma-udef19.3

      \[\leadsto \frac{\frac{b \cdot c - a \cdot d}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}}{\sqrt{d^2 + c^2}^*}\]
    11. Applied hypot-def12.0

      \[\leadsto \frac{\frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{d^2 + c^2}^*}}}{\sqrt{d^2 + c^2}^*}\]
    12. Using strategy rm
    13. Applied clear-num12.1

      \[\leadsto \frac{\color{blue}{\frac{1}{\frac{\sqrt{d^2 + c^2}^*}{b \cdot c - a \cdot d}}}}{\sqrt{d^2 + c^2}^*}\]

    if 3.9750110958235406e+175 < c

    1. Initial program 44.3

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified44.3

      \[\leadsto \color{blue}{\frac{b \cdot c - a \cdot d}{(d \cdot d + \left(c \cdot c\right))_*}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt44.3

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    5. Applied associate-/r*44.3

      \[\leadsto \color{blue}{\frac{\frac{b \cdot c - a \cdot d}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    6. Using strategy rm
    7. Applied fma-udef44.3

      \[\leadsto \frac{\frac{b \cdot c - a \cdot d}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}\]
    8. Applied hypot-def44.3

      \[\leadsto \frac{\frac{b \cdot c - a \cdot d}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}{\color{blue}{\sqrt{d^2 + c^2}^*}}\]
    9. Using strategy rm
    10. Applied fma-udef44.3

      \[\leadsto \frac{\frac{b \cdot c - a \cdot d}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}}{\sqrt{d^2 + c^2}^*}\]
    11. Applied hypot-def30.7

      \[\leadsto \frac{\frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{d^2 + c^2}^*}}}{\sqrt{d^2 + c^2}^*}\]
    12. Taylor expanded around inf 11.1

      \[\leadsto \frac{\color{blue}{b}}{\sqrt{d^2 + c^2}^*}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification12.7

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -2.7923210046731917 \cdot 10^{+118}:\\ \;\;\;\;\frac{-b}{\sqrt{d^2 + c^2}^*}\\ \mathbf{elif}\;c \le 3.9750110958235406 \cdot 10^{+175}:\\ \;\;\;\;\frac{\frac{1}{\frac{\sqrt{d^2 + c^2}^*}{b \cdot c - a \cdot d}}}{\sqrt{d^2 + c^2}^*}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\sqrt{d^2 + c^2}^*}\\ \end{array}\]

Reproduce

herbie shell --seed 2019053 +o rules:numerics
(FPCore (a b c d)
  :name "Complex division, imag part"

  :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))))