Average Error: 26.1 → 13.0
Time: 16.6s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -1.1443562208296078 \cdot 10^{+200}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 1.3614475866611724 \cdot 10^{+128}:\\ \;\;\;\;\frac{\mathsf{fma}\left(a, c, \left(d \cdot b\right)\right) \cdot \frac{1}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le -1.1443562208296078 \cdot 10^{+200}:\\
\;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\

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

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

\end{array}
double f(double a, double b, double c, double d) {
        double r3417366 = a;
        double r3417367 = c;
        double r3417368 = r3417366 * r3417367;
        double r3417369 = b;
        double r3417370 = d;
        double r3417371 = r3417369 * r3417370;
        double r3417372 = r3417368 + r3417371;
        double r3417373 = r3417367 * r3417367;
        double r3417374 = r3417370 * r3417370;
        double r3417375 = r3417373 + r3417374;
        double r3417376 = r3417372 / r3417375;
        return r3417376;
}

double f(double a, double b, double c, double d) {
        double r3417377 = c;
        double r3417378 = -1.1443562208296078e+200;
        bool r3417379 = r3417377 <= r3417378;
        double r3417380 = a;
        double r3417381 = -r3417380;
        double r3417382 = d;
        double r3417383 = hypot(r3417382, r3417377);
        double r3417384 = r3417381 / r3417383;
        double r3417385 = 1.3614475866611724e+128;
        bool r3417386 = r3417377 <= r3417385;
        double r3417387 = b;
        double r3417388 = r3417382 * r3417387;
        double r3417389 = fma(r3417380, r3417377, r3417388);
        double r3417390 = 1.0;
        double r3417391 = r3417390 / r3417383;
        double r3417392 = r3417389 * r3417391;
        double r3417393 = r3417392 / r3417383;
        double r3417394 = r3417380 / r3417383;
        double r3417395 = r3417386 ? r3417393 : r3417394;
        double r3417396 = r3417379 ? r3417384 : r3417395;
        return r3417396;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.1
Target0.4
Herbie13.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 c < -1.1443562208296078e+200

    1. Initial program 41.6

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified41.6

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt41.6

      \[\leadsto \frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\color{blue}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)} \cdot \sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}\]
    5. Applied associate-/r*41.6

      \[\leadsto \color{blue}{\frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}\]
    6. Using strategy rm
    7. Applied fma-udef41.6

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    8. Applied hypot-def41.6

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\color{blue}{\mathsf{hypot}\left(d, c\right)}}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    9. Using strategy rm
    10. Applied fma-udef41.6

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\mathsf{hypot}\left(d, c\right)}}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}\]
    11. Applied hypot-def28.5

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

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

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

    if -1.1443562208296078e+200 < c < 1.3614475866611724e+128

    1. Initial program 20.9

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

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt20.9

      \[\leadsto \frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\color{blue}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)} \cdot \sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}\]
    5. Applied associate-/r*20.8

      \[\leadsto \color{blue}{\frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}\]
    6. Using strategy rm
    7. Applied fma-udef20.8

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    8. Applied hypot-def20.8

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\color{blue}{\mathsf{hypot}\left(d, c\right)}}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    9. Using strategy rm
    10. Applied fma-udef20.8

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\mathsf{hypot}\left(d, c\right)}}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}\]
    11. Applied hypot-def12.9

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\mathsf{hypot}\left(d, c\right)}}{\color{blue}{\mathsf{hypot}\left(d, c\right)}}\]
    12. Using strategy rm
    13. Applied div-inv13.0

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

    if 1.3614475866611724e+128 < c

    1. Initial program 42.4

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

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt42.4

      \[\leadsto \frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\color{blue}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)} \cdot \sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}\]
    5. Applied associate-/r*42.4

      \[\leadsto \color{blue}{\frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}}\]
    6. Using strategy rm
    7. Applied fma-udef42.4

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    8. Applied hypot-def42.4

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\color{blue}{\mathsf{hypot}\left(d, c\right)}}}{\sqrt{\mathsf{fma}\left(d, d, \left(c \cdot c\right)\right)}}\]
    9. Using strategy rm
    10. Applied fma-udef42.4

      \[\leadsto \frac{\frac{\mathsf{fma}\left(a, c, \left(b \cdot d\right)\right)}{\mathsf{hypot}\left(d, c\right)}}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}\]
    11. Applied hypot-def28.2

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -1.1443562208296078 \cdot 10^{+200}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;c \le 1.3614475866611724 \cdot 10^{+128}:\\ \;\;\;\;\frac{\mathsf{fma}\left(a, c, \left(d \cdot b\right)\right) \cdot \frac{1}{\mathsf{hypot}\left(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(d, c\right)}\\ \end{array}\]

Reproduce

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