Average Error: 26.5 → 13.2
Time: 11.3s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le -9.286239316634065508779525526512632626448 \cdot 10^{127}:\\ \;\;\;\;\frac{b}{-\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \le 1.146162966717313437848362144780778950088 \cdot 10^{82}:\\ \;\;\;\;\frac{\frac{-\mathsf{fma}\left(d, b, a \cdot c\right)}{\mathsf{hypot}\left(c, d\right)}}{-\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\mathsf{hypot}\left(c, d\right)}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;d \le -9.286239316634065508779525526512632626448 \cdot 10^{127}:\\
\;\;\;\;\frac{b}{-\mathsf{hypot}\left(c, d\right)}\\

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

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

\end{array}
double f(double a, double b, double c, double d) {
        double r68351 = a;
        double r68352 = c;
        double r68353 = r68351 * r68352;
        double r68354 = b;
        double r68355 = d;
        double r68356 = r68354 * r68355;
        double r68357 = r68353 + r68356;
        double r68358 = r68352 * r68352;
        double r68359 = r68355 * r68355;
        double r68360 = r68358 + r68359;
        double r68361 = r68357 / r68360;
        return r68361;
}

double f(double a, double b, double c, double d) {
        double r68362 = d;
        double r68363 = -9.286239316634066e+127;
        bool r68364 = r68362 <= r68363;
        double r68365 = b;
        double r68366 = c;
        double r68367 = hypot(r68366, r68362);
        double r68368 = -r68367;
        double r68369 = r68365 / r68368;
        double r68370 = 1.1461629667173134e+82;
        bool r68371 = r68362 <= r68370;
        double r68372 = a;
        double r68373 = r68372 * r68366;
        double r68374 = fma(r68362, r68365, r68373);
        double r68375 = -r68374;
        double r68376 = r68375 / r68367;
        double r68377 = r68376 / r68368;
        double r68378 = r68365 / r68367;
        double r68379 = r68371 ? r68377 : r68378;
        double r68380 = r68364 ? r68369 : r68379;
        return r68380;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.5
Target0.5
Herbie13.2
\[\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 < -9.286239316634066e+127

    1. Initial program 42.7

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{{\left(\frac{1}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}} \cdot {\left(\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\]
    11. Applied pow-prod-down29.1

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

      \[\leadsto {\color{blue}{\left(\frac{\frac{\mathsf{fma}\left(c, a, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\right)}}^{1}\]
    13. Using strategy rm
    14. Applied frac-2neg29.0

      \[\leadsto {\color{blue}{\left(\frac{-\frac{\mathsf{fma}\left(c, a, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{-\mathsf{hypot}\left(c, d\right)}\right)}}^{1}\]
    15. Simplified29.0

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

      \[\leadsto {\left(\frac{-\color{blue}{-1 \cdot b}}{-\mathsf{hypot}\left(c, d\right)}\right)}^{1}\]
    17. Simplified14.3

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

    if -9.286239316634066e+127 < d < 1.1461629667173134e+82

    1. Initial program 19.1

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

      \[\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-identity19.1

      \[\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-frac19.1

      \[\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. Simplified19.1

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

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

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

      \[\leadsto \color{blue}{{\left(\frac{1}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}} \cdot {\left(\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\]
    11. Applied pow-prod-down12.0

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

      \[\leadsto {\color{blue}{\left(\frac{\frac{\mathsf{fma}\left(c, a, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\right)}}^{1}\]
    13. Using strategy rm
    14. Applied frac-2neg11.9

      \[\leadsto {\color{blue}{\left(\frac{-\frac{\mathsf{fma}\left(c, a, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{-\mathsf{hypot}\left(c, d\right)}\right)}}^{1}\]
    15. Simplified11.9

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

    if 1.1461629667173134e+82 < d

    1. Initial program 38.5

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt38.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-identity38.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-frac38.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. Simplified38.5

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

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

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

      \[\leadsto \color{blue}{{\left(\frac{1}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}} \cdot {\left(\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\]
    11. Applied pow-prod-down26.1

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

      \[\leadsto {\color{blue}{\left(\frac{\frac{\mathsf{fma}\left(c, a, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\right)}}^{1}\]
    13. Taylor expanded around 0 17.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \le -9.286239316634065508779525526512632626448 \cdot 10^{127}:\\ \;\;\;\;\frac{b}{-\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \le 1.146162966717313437848362144780778950088 \cdot 10^{82}:\\ \;\;\;\;\frac{\frac{-\mathsf{fma}\left(d, b, a \cdot c\right)}{\mathsf{hypot}\left(c, d\right)}}{-\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\mathsf{hypot}\left(c, d\right)}\\ \end{array}\]

Reproduce

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