Average Error: 26.2 → 13.5
Time: 35.0s
Precision: 64
Internal Precision: 576
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;\frac{1}{\sqrt{c^2 + d^2}^*} \cdot \frac{(b \cdot c + \left(a \cdot \left(-d\right)\right))_*}{\sqrt{c^2 + d^2}^*} \le -4.0883606135300517 \cdot 10^{+300}:\\ \;\;\;\;\frac{1}{\sqrt{c^2 + d^2}^*} \cdot b\\ \mathbf{elif}\;\frac{1}{\sqrt{c^2 + d^2}^*} \cdot \frac{(b \cdot c + \left(a \cdot \left(-d\right)\right))_*}{\sqrt{c^2 + d^2}^*} \le 2.7200961677720655 \cdot 10^{+301}:\\ \;\;\;\;\frac{1}{\sqrt{c^2 + d^2}^*} \cdot \frac{(b \cdot c + \left(a \cdot \left(-d\right)\right))_*}{\sqrt{c^2 + d^2}^*}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\sqrt{c^2 + d^2}^*} \cdot \left(-a\right)\\ \end{array}\]

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.2
Target0.6
Herbie13.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. Split input into 3 regimes
  2. if (* (/ 1 (hypot c d)) (/ (fma b c (- (* a d))) (hypot c d))) < -4.0883606135300517e+300

    1. Initial program 61.8

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

      \[\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-identity61.8

      \[\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-frac61.8

      \[\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. Simplified61.8

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

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

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

    if -4.0883606135300517e+300 < (* (/ 1 (hypot c d)) (/ (fma b c (- (* a d))) (hypot c d))) < 2.7200961677720655e+301

    1. Initial program 13.9

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

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

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

      \[\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. Simplified13.9

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

      \[\leadsto \frac{1}{\sqrt{c^2 + d^2}^*} \cdot \color{blue}{\frac{b \cdot c - a \cdot d}{\sqrt{c^2 + d^2}^*}}\]
    8. Using strategy rm
    9. Applied fma-neg1.5

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

    if 2.7200961677720655e+301 < (* (/ 1 (hypot c d)) (/ (fma b c (- (* a d))) (hypot c d)))

    1. Initial program 61.6

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

      \[\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-identity61.6

      \[\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-frac61.6

      \[\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. Simplified61.6

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

      \[\leadsto \frac{1}{\sqrt{c^2 + d^2}^*} \cdot \color{blue}{\frac{b \cdot c - a \cdot d}{\sqrt{c^2 + d^2}^*}}\]
    8. Taylor expanded around 0 47.5

      \[\leadsto \frac{1}{\sqrt{c^2 + d^2}^*} \cdot \color{blue}{\left(-1 \cdot a\right)}\]
    9. Simplified47.5

      \[\leadsto \frac{1}{\sqrt{c^2 + d^2}^*} \cdot \color{blue}{\left(-a\right)}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification13.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{1}{\sqrt{c^2 + d^2}^*} \cdot \frac{(b \cdot c + \left(a \cdot \left(-d\right)\right))_*}{\sqrt{c^2 + d^2}^*} \le -4.0883606135300517 \cdot 10^{+300}:\\ \;\;\;\;\frac{1}{\sqrt{c^2 + d^2}^*} \cdot b\\ \mathbf{elif}\;\frac{1}{\sqrt{c^2 + d^2}^*} \cdot \frac{(b \cdot c + \left(a \cdot \left(-d\right)\right))_*}{\sqrt{c^2 + d^2}^*} \le 2.7200961677720655 \cdot 10^{+301}:\\ \;\;\;\;\frac{1}{\sqrt{c^2 + d^2}^*} \cdot \frac{(b \cdot c + \left(a \cdot \left(-d\right)\right))_*}{\sqrt{c^2 + d^2}^*}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\sqrt{c^2 + d^2}^*} \cdot \left(-a\right)\\ \end{array}\]

Runtime

Time bar (total: 35.0s)Debug logProfile

herbie shell --seed 2018217 +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))))