Average Error: 25.9 → 26.0
Time: 17.9s
Precision: 64
Internal Precision: 128
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le 1.4830868861678625 \cdot 10^{+72}:\\ \;\;\;\;\frac{\frac{a \cdot c + d \cdot b}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{elif}\;d \le 3.4978915880616634 \cdot 10^{+152}:\\ \;\;\;\;\frac{b}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\sqrt{d \cdot d + c \cdot c}} \cdot \frac{\frac{a \cdot c + d \cdot b}{\sqrt{\sqrt{d \cdot d + c \cdot c}}}}{\sqrt{\sqrt{d \cdot d + c \cdot c}}}\\ \end{array}\]

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.9
Target0.5
Herbie26.0
\[\begin{array}{l} \mathbf{if}\;\left|d\right| \lt \left|c\right|:\\ \;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c + d \cdot \frac{d}{c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d + c \cdot \frac{c}{d}}\\ \end{array}\]

Derivation

  1. Split input into 3 regimes
  2. if d < 1.4830868861678625e+72

    1. Initial program 22.6

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

      \[\leadsto \frac{a \cdot c + b \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    4. Applied associate-/r*22.5

      \[\leadsto \color{blue}{\frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}}\]

    if 1.4830868861678625e+72 < d < 3.4978915880616634e+152

    1. Initial program 23.4

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

      \[\leadsto \frac{a \cdot c + b \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    4. Applied associate-/r*23.2

      \[\leadsto \color{blue}{\frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}}\]
    5. Taylor expanded around 0 25.2

      \[\leadsto \frac{\color{blue}{b}}{\sqrt{c \cdot c + d \cdot d}}\]

    if 3.4978915880616634e+152 < d

    1. Initial program 46.7

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

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

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

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
    6. Using strategy rm
    7. Applied add-sqr-sqrt46.7

      \[\leadsto \frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}}\]
    8. Applied sqrt-prod46.7

      \[\leadsto \frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\color{blue}{\sqrt{\sqrt{c \cdot c + d \cdot d}} \cdot \sqrt{\sqrt{c \cdot c + d \cdot d}}}}\]
    9. Applied associate-/r*46.7

      \[\leadsto \frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \color{blue}{\frac{\frac{a \cdot c + b \cdot d}{\sqrt{\sqrt{c \cdot c + d \cdot d}}}}{\sqrt{\sqrt{c \cdot c + d \cdot d}}}}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification26.0

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \le 1.4830868861678625 \cdot 10^{+72}:\\ \;\;\;\;\frac{\frac{a \cdot c + d \cdot b}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{elif}\;d \le 3.4978915880616634 \cdot 10^{+152}:\\ \;\;\;\;\frac{b}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\sqrt{d \cdot d + c \cdot c}} \cdot \frac{\frac{a \cdot c + d \cdot b}{\sqrt{\sqrt{d \cdot d + c \cdot c}}}}{\sqrt{\sqrt{d \cdot d + c \cdot c}}}\\ \end{array}\]

Reproduce

herbie shell --seed 2019053 
(FPCore (a b c d)
  :name "Complex division, real part"

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

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