Average Error: 25.9 → 13.4
Time: 13.1s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le -3.114802040969311027115783701676385953635 \cdot 10^{109}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \le 6.370255659610306169101339211646465545796 \cdot 10^{-234}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \le 4.694847582105375997704206679714705497211 \cdot 10^{-213}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \le 1.048412145020906876185869327079410027246 \cdot 10^{133}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(d, b, c \cdot a\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 -3.114802040969311027115783701676385953635 \cdot 10^{109}:\\
\;\;\;\;\frac{-b}{\mathsf{hypot}\left(c, d\right)}\\

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

\mathbf{elif}\;d \le 4.694847582105375997704206679714705497211 \cdot 10^{-213}:\\
\;\;\;\;\frac{a}{\mathsf{hypot}\left(c, d\right)}\\

\mathbf{elif}\;d \le 1.048412145020906876185869327079410027246 \cdot 10^{133}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(d, b, c \cdot a\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 r107962 = a;
        double r107963 = c;
        double r107964 = r107962 * r107963;
        double r107965 = b;
        double r107966 = d;
        double r107967 = r107965 * r107966;
        double r107968 = r107964 + r107967;
        double r107969 = r107963 * r107963;
        double r107970 = r107966 * r107966;
        double r107971 = r107969 + r107970;
        double r107972 = r107968 / r107971;
        return r107972;
}

double f(double a, double b, double c, double d) {
        double r107973 = d;
        double r107974 = -3.114802040969311e+109;
        bool r107975 = r107973 <= r107974;
        double r107976 = b;
        double r107977 = -r107976;
        double r107978 = c;
        double r107979 = hypot(r107978, r107973);
        double r107980 = r107977 / r107979;
        double r107981 = 6.370255659610306e-234;
        bool r107982 = r107973 <= r107981;
        double r107983 = a;
        double r107984 = r107978 * r107983;
        double r107985 = fma(r107973, r107976, r107984);
        double r107986 = r107985 / r107979;
        double r107987 = r107986 / r107979;
        double r107988 = 4.694847582105376e-213;
        bool r107989 = r107973 <= r107988;
        double r107990 = r107983 / r107979;
        double r107991 = 1.0484121450209069e+133;
        bool r107992 = r107973 <= r107991;
        double r107993 = r107976 / r107979;
        double r107994 = r107992 ? r107987 : r107993;
        double r107995 = r107989 ? r107990 : r107994;
        double r107996 = r107982 ? r107987 : r107995;
        double r107997 = r107975 ? r107980 : r107996;
        return r107997;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.9
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 4 regimes
  2. if d < -3.114802040969311e+109

    1. Initial program 40.1

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt40.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-identity40.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-frac40.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. Simplified40.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. Simplified26.8

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

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

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

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

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

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

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

    if -3.114802040969311e+109 < d < 6.370255659610306e-234 or 4.694847582105376e-213 < d < 1.0484121450209069e+133

    1. Initial program 18.3

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt18.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-identity18.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-frac18.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. Simplified18.3

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

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

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

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

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

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

      \[\leadsto {\left(\frac{\color{blue}{\mathsf{fma}\left(d, b, c \cdot a\right) \cdot \frac{1}{\mathsf{hypot}\left(c, d\right)}}}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\]
    15. Using strategy rm
    16. Applied *-un-lft-identity12.0

      \[\leadsto {\left(\frac{\color{blue}{\left(1 \cdot \mathsf{fma}\left(d, b, c \cdot a\right)\right)} \cdot \frac{1}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\]
    17. Applied associate-*l*12.0

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

      \[\leadsto {\left(\frac{1 \cdot \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 6.370255659610306e-234 < d < 4.694847582105376e-213

    1. Initial program 27.1

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

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

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

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

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

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

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

    if 1.0484121450209069e+133 < d

    1. Initial program 42.6

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

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

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

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

      \[\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. Simplified27.2

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \le -3.114802040969311027115783701676385953635 \cdot 10^{109}:\\ \;\;\;\;\frac{-b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \le 6.370255659610306169101339211646465545796 \cdot 10^{-234}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \le 4.694847582105375997704206679714705497211 \cdot 10^{-213}:\\ \;\;\;\;\frac{a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \le 1.048412145020906876185869327079410027246 \cdot 10^{133}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(d, b, c \cdot a\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 2019208 +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))))