Average Error: 25.7 → 13.3
Time: 59.7s
Precision: 64
Internal Precision: 576
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1}{\sqrt{c^2 + d^2}^*} \cdot \left(\left(\sqrt[3]{\frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{c^2 + d^2}^*}} \cdot \sqrt[3]{\frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{c^2 + d^2}^*}}\right) \cdot \sqrt[3]{\frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{c^2 + d^2}^*}}\right) \le -1.7792752541260413 \cdot 10^{+308}:\\ \;\;\;\;\frac{b}{\sqrt{c^2 + d^2}^*}\\ \mathbf{if}\;\frac{1}{\sqrt{c^2 + d^2}^*} \cdot \left(\left(\sqrt[3]{\frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{c^2 + d^2}^*}} \cdot \sqrt[3]{\frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{c^2 + d^2}^*}}\right) \cdot \sqrt[3]{\frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{c^2 + d^2}^*}}\right) \le 1.771881421094445 \cdot 10^{+308}:\\ \;\;\;\;\frac{\frac{(d \cdot b + \left(a \cdot c\right))_*}{\sqrt{c^2 + d^2}^*}}{\sqrt{c^2 + d^2}^*}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\sqrt{c^2 + d^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
Herbie13.3
\[\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 2 regimes
  2. if (* (/ 1 (hypot c d)) (* (* (cbrt (/ (fma b d (* c a)) (hypot c d))) (cbrt (/ (fma b d (* c a)) (hypot c d)))) (cbrt (/ (fma b d (* c a)) (hypot c d))))) < -1.7792752541260413e+308 or 1.771881421094445e+308 < (* (/ 1 (hypot c d)) (* (* (cbrt (/ (fma b d (* c a)) (hypot c d))) (cbrt (/ (fma b d (* c a)) (hypot c d)))) (cbrt (/ (fma b d (* c a)) (hypot c d)))))

    1. Initial program 62.5

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

      \[\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-identity62.5

      \[\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-frac62.5

      \[\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. Applied simplify62.5

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c^2 + d^2}^*}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}\]
    7. Applied simplify62.0

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

      \[\leadsto \frac{1}{\sqrt{c^2 + d^2}^*} \cdot \color{blue}{b}\]
    9. Applied simplify49.4

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

    if -1.7792752541260413e+308 < (* (/ 1 (hypot c d)) (* (* (cbrt (/ (fma b d (* c a)) (hypot c d))) (cbrt (/ (fma b d (* c a)) (hypot c d)))) (cbrt (/ (fma b d (* c a)) (hypot c d))))) < 1.771881421094445e+308

    1. Initial program 13.5

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

      \[\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-identity13.5

      \[\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-frac13.5

      \[\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. Applied simplify13.5

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c^2 + d^2}^*}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}\]
    7. Applied simplify1.5

      \[\leadsto \frac{1}{\sqrt{c^2 + d^2}^*} \cdot \color{blue}{\frac{(b \cdot d + \left(c \cdot a\right))_*}{\sqrt{c^2 + d^2}^*}}\]
    8. Using strategy rm
    9. Applied associate-*r/1.5

      \[\leadsto \color{blue}{\frac{\frac{1}{\sqrt{c^2 + d^2}^*} \cdot (b \cdot d + \left(c \cdot a\right))_*}{\sqrt{c^2 + d^2}^*}}\]
    10. Applied simplify1.4

      \[\leadsto \frac{\color{blue}{\frac{(d \cdot b + \left(a \cdot c\right))_*}{\sqrt{c^2 + d^2}^*}}}{\sqrt{c^2 + d^2}^*}\]
  3. Recombined 2 regimes into one program.

Runtime

Time bar (total: 59.7s)Debug logProfile

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