Average Error: 26.3 → 13.4
Time: 4.0s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -3.942116530425900725431758127434894134157 \cdot 10^{195}:\\ \;\;\;\;\frac{-1 \cdot a}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\ \mathbf{elif}\;c \le 4.355931456769410941675116004628825267272 \cdot 10^{151}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le -3.942116530425900725431758127434894134157 \cdot 10^{195}:\\
\;\;\;\;\frac{-1 \cdot a}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\

\mathbf{elif}\;c \le 4.355931456769410941675116004628825267272 \cdot 10^{151}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\

\mathbf{else}:\\
\;\;\;\;\frac{a}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\

\end{array}
double f(double a, double b, double c, double d) {
        double r114277 = a;
        double r114278 = c;
        double r114279 = r114277 * r114278;
        double r114280 = b;
        double r114281 = d;
        double r114282 = r114280 * r114281;
        double r114283 = r114279 + r114282;
        double r114284 = r114278 * r114278;
        double r114285 = r114281 * r114281;
        double r114286 = r114284 + r114285;
        double r114287 = r114283 / r114286;
        return r114287;
}

double f(double a, double b, double c, double d) {
        double r114288 = c;
        double r114289 = -3.9421165304259007e+195;
        bool r114290 = r114288 <= r114289;
        double r114291 = -1.0;
        double r114292 = a;
        double r114293 = r114291 * r114292;
        double r114294 = d;
        double r114295 = hypot(r114288, r114294);
        double r114296 = 1.0;
        double r114297 = r114295 * r114296;
        double r114298 = r114293 / r114297;
        double r114299 = 4.355931456769411e+151;
        bool r114300 = r114288 <= r114299;
        double r114301 = b;
        double r114302 = r114301 * r114294;
        double r114303 = fma(r114292, r114288, r114302);
        double r114304 = r114303 / r114295;
        double r114305 = r114304 / r114297;
        double r114306 = r114292 / r114297;
        double r114307 = r114300 ? r114305 : r114306;
        double r114308 = r114290 ? r114298 : r114307;
        return r114308;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.3
Target0.5
Herbie13.4
\[\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 c < -3.9421165304259007e+195

    1. Initial program 43.4

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt43.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 *-un-lft-identity43.4

      \[\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-frac43.4

      \[\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. Simplified43.4

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1} \cdot \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right) \cdot 1}}\]
    8. Using strategy rm
    9. Applied associate-*r/31.4

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

      \[\leadsto \frac{\color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}}{\mathsf{hypot}\left(c, d\right) \cdot 1}\]
    11. Taylor expanded around -inf 12.9

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

    if -3.9421165304259007e+195 < c < 4.355931456769411e+151

    1. Initial program 20.9

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

      \[\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-identity21.0

      \[\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-frac21.0

      \[\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. Simplified21.0

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1} \cdot \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right) \cdot 1}}\]
    8. Using strategy rm
    9. Applied associate-*r/13.4

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

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

    if 4.355931456769411e+151 < c

    1. Initial program 44.3

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

      \[\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-identity44.3

      \[\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-frac44.3

      \[\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. Simplified44.3

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot 1} \cdot \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right) \cdot 1}}\]
    8. Using strategy rm
    9. Applied associate-*r/28.6

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

      \[\leadsto \frac{\color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}}{\mathsf{hypot}\left(c, d\right) \cdot 1}\]
    11. Taylor expanded around inf 13.9

      \[\leadsto \frac{\color{blue}{a}}{\mathsf{hypot}\left(c, d\right) \cdot 1}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification13.4

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -3.942116530425900725431758127434894134157 \cdot 10^{195}:\\ \;\;\;\;\frac{-1 \cdot a}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\ \mathbf{elif}\;c \le 4.355931456769410941675116004628825267272 \cdot 10^{151}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\ \end{array}\]

Reproduce

herbie shell --seed 2019354 +o rules:numerics
(FPCore (a b c d)
  :name "Complex division, real part"
  :precision binary64

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