Average Error: 25.8 → 13.7
Time: 5.0s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \le 3.7042360551829521 \cdot 10^{296}:\\ \;\;\;\;\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{-1 \cdot 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}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \le 3.7042360551829521 \cdot 10^{296}:\\
\;\;\;\;\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{-1 \cdot a}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\

\end{array}
double f(double a, double b, double c, double d) {
        double r92456 = a;
        double r92457 = c;
        double r92458 = r92456 * r92457;
        double r92459 = b;
        double r92460 = d;
        double r92461 = r92459 * r92460;
        double r92462 = r92458 + r92461;
        double r92463 = r92457 * r92457;
        double r92464 = r92460 * r92460;
        double r92465 = r92463 + r92464;
        double r92466 = r92462 / r92465;
        return r92466;
}

double f(double a, double b, double c, double d) {
        double r92467 = a;
        double r92468 = c;
        double r92469 = r92467 * r92468;
        double r92470 = b;
        double r92471 = d;
        double r92472 = r92470 * r92471;
        double r92473 = r92469 + r92472;
        double r92474 = r92468 * r92468;
        double r92475 = r92471 * r92471;
        double r92476 = r92474 + r92475;
        double r92477 = r92473 / r92476;
        double r92478 = 3.704236055182952e+296;
        bool r92479 = r92477 <= r92478;
        double r92480 = fma(r92467, r92468, r92472);
        double r92481 = hypot(r92468, r92471);
        double r92482 = r92480 / r92481;
        double r92483 = 1.0;
        double r92484 = r92481 * r92483;
        double r92485 = r92482 / r92484;
        double r92486 = -1.0;
        double r92487 = r92486 * r92467;
        double r92488 = r92487 / r92484;
        double r92489 = r92479 ? r92485 : r92488;
        return r92489;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.8
Target0.4
Herbie13.7
\[\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 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))) < 3.704236055182952e+296

    1. Initial program 13.8

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt13.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-identity13.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-frac13.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. Simplified13.8

      \[\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. Simplified2.7

      \[\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/2.7

      \[\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. Simplified2.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}\]

    if 3.704236055182952e+296 < (/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))

    1. Initial program 63.4

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt63.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-identity63.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-frac63.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. Simplified63.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. Simplified60.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/60.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. Simplified60.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 48.5

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \le 3.7042360551829521 \cdot 10^{296}:\\ \;\;\;\;\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{-1 \cdot a}{\mathsf{hypot}\left(c, d\right) \cdot 1}\\ \end{array}\]

Reproduce

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