Average Error: 25.6 → 14.2
Time: 6.4s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \frac{a}{c} + \frac{\frac{b}{c}}{\frac{c}{d}}\\ \mathbf{if}\;c \leq -4.2044927924302215 \cdot 10^{+81}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_1 := b \cdot d + c \cdot a\\ t_2 := d \cdot d + c \cdot c\\ \mathbf{if}\;c \leq -5.831437032843926 \cdot 10^{-31}:\\ \;\;\;\;\frac{t_1}{t_2}\\ \mathbf{elif}\;c \leq -6.071421943859113 \cdot 10^{-88}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_3 := \sqrt{t_2}\\ \mathbf{if}\;c \leq -2.567158710874279 \cdot 10^{-166}:\\ \;\;\;\;\frac{\frac{1}{\frac{t_3}{t_1}}}{t_3}\\ \mathbf{elif}\;c \leq 8.191619328525054 \cdot 10^{-172}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 1.2016013962778195 \cdot 10^{+76}:\\ \;\;\;\;t_1 \cdot \frac{\frac{1}{t_3}}{t_3}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array}\\ \end{array}\\ \end{array} \]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{a}{c} + \frac{\frac{b}{c}}{\frac{c}{d}}\\
\mathbf{if}\;c \leq -4.2044927924302215 \cdot 10^{+81}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_1 := b \cdot d + c \cdot a\\
t_2 := d \cdot d + c \cdot c\\
\mathbf{if}\;c \leq -5.831437032843926 \cdot 10^{-31}:\\
\;\;\;\;\frac{t_1}{t_2}\\

\mathbf{elif}\;c \leq -6.071421943859113 \cdot 10^{-88}:\\
\;\;\;\;\frac{b}{d} + \frac{c \cdot a}{d \cdot d}\\

\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_3 := \sqrt{t_2}\\
\mathbf{if}\;c \leq -2.567158710874279 \cdot 10^{-166}:\\
\;\;\;\;\frac{\frac{1}{\frac{t_3}{t_1}}}{t_3}\\

\mathbf{elif}\;c \leq 8.191619328525054 \cdot 10^{-172}:\\
\;\;\;\;\frac{b}{d}\\

\mathbf{elif}\;c \leq 1.2016013962778195 \cdot 10^{+76}:\\
\;\;\;\;t_1 \cdot \frac{\frac{1}{t_3}}{t_3}\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}\\


\end{array}\\


\end{array}
(FPCore (a b c d)
 :precision binary64
 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (+ (/ a c) (/ (/ b c) (/ c d)))))
   (if (<= c -4.2044927924302215e+81)
     t_0
     (let* ((t_1 (+ (* b d) (* c a))) (t_2 (+ (* d d) (* c c))))
       (if (<= c -5.831437032843926e-31)
         (/ t_1 t_2)
         (if (<= c -6.071421943859113e-88)
           (+ (/ b d) (/ (* c a) (* d d)))
           (let* ((t_3 (sqrt t_2)))
             (if (<= c -2.567158710874279e-166)
               (/ (/ 1.0 (/ t_3 t_1)) t_3)
               (if (<= c 8.191619328525054e-172)
                 (/ b d)
                 (if (<= c 1.2016013962778195e+76)
                   (* t_1 (/ (/ 1.0 t_3) t_3))
                   t_0))))))))))
double code(double a, double b, double c, double d) {
	return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
double code(double a, double b, double c, double d) {
	double t_0 = (a / c) + ((b / c) / (c / d));
	double tmp;
	if (c <= -4.2044927924302215e+81) {
		tmp = t_0;
	} else {
		double t_1 = (b * d) + (c * a);
		double t_2 = (d * d) + (c * c);
		double tmp_1;
		if (c <= -5.831437032843926e-31) {
			tmp_1 = t_1 / t_2;
		} else if (c <= -6.071421943859113e-88) {
			tmp_1 = (b / d) + ((c * a) / (d * d));
		} else {
			double t_3 = sqrt(t_2);
			double tmp_2;
			if (c <= -2.567158710874279e-166) {
				tmp_2 = (1.0 / (t_3 / t_1)) / t_3;
			} else if (c <= 8.191619328525054e-172) {
				tmp_2 = b / d;
			} else if (c <= 1.2016013962778195e+76) {
				tmp_2 = t_1 * ((1.0 / t_3) / t_3);
			} else {
				tmp_2 = t_0;
			}
			tmp_1 = tmp_2;
		}
		tmp = tmp_1;
	}
	return tmp;
}

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original25.6
Target0.4
Herbie14.2
\[\begin{array}{l} \mathbf{if}\;\left|d\right| < \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 6 regimes
  2. if c < -4.20449279243022152e81 or 1.20160139627781954e76 < c

    1. Initial program 37.2

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

      \[\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 associate-/r*_binary6437.2

      \[\leadsto \color{blue}{\frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}} \]
    5. Simplified37.2

      \[\leadsto \frac{\color{blue}{\frac{d \cdot b + c \cdot a}{\sqrt{d \cdot d + c \cdot c}}}}{\sqrt{c \cdot c + d \cdot d}} \]
    6. Taylor expanded around 0 16.3

      \[\leadsto \color{blue}{\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}} \]
    7. Simplified15.8

      \[\leadsto \color{blue}{\frac{a}{c} + \frac{b}{\frac{c \cdot c}{d}}} \]
    8. Using strategy rm
    9. Applied *-un-lft-identity_binary6415.8

      \[\leadsto \frac{a}{c} + \frac{b}{\frac{c \cdot c}{\color{blue}{1 \cdot d}}} \]
    10. Applied times-frac_binary6414.5

      \[\leadsto \frac{a}{c} + \frac{b}{\color{blue}{\frac{c}{1} \cdot \frac{c}{d}}} \]
    11. Applied associate-/r*_binary6411.0

      \[\leadsto \frac{a}{c} + \color{blue}{\frac{\frac{b}{\frac{c}{1}}}{\frac{c}{d}}} \]
    12. Simplified11.0

      \[\leadsto \frac{a}{c} + \frac{\color{blue}{\frac{b}{c}}}{\frac{c}{d}} \]

    if -4.20449279243022152e81 < c < -5.8314370328439262e-31

    1. Initial program 16.6

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Using strategy rm
    3. Applied add-sqr-sqrt_binary6416.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 associate-/r*_binary6416.5

      \[\leadsto \color{blue}{\frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}} \]
    5. Simplified16.5

      \[\leadsto \frac{\color{blue}{\frac{d \cdot b + c \cdot a}{\sqrt{d \cdot d + c \cdot c}}}}{\sqrt{c \cdot c + d \cdot d}} \]
    6. Using strategy rm
    7. Applied associate-/l/_binary6416.6

      \[\leadsto \color{blue}{\frac{d \cdot b + c \cdot a}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{d \cdot d + c \cdot c}}} \]
    8. Simplified16.6

      \[\leadsto \frac{d \cdot b + c \cdot a}{\color{blue}{d \cdot d + c \cdot c}} \]

    if -5.8314370328439262e-31 < c < -6.07142194385911326e-88

    1. Initial program 13.6

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Taylor expanded around 0 26.9

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}} \]
    3. Simplified26.9

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{c \cdot a}{d \cdot d}} \]

    if -6.07142194385911326e-88 < c < -2.5671587108743e-166

    1. Initial program 15.3

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Using strategy rm
    3. Applied add-sqr-sqrt_binary6415.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 associate-/r*_binary6415.3

      \[\leadsto \color{blue}{\frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}} \]
    5. Simplified15.3

      \[\leadsto \frac{\color{blue}{\frac{d \cdot b + c \cdot a}{\sqrt{d \cdot d + c \cdot c}}}}{\sqrt{c \cdot c + d \cdot d}} \]
    6. Using strategy rm
    7. Applied clear-num_binary6415.3

      \[\leadsto \frac{\color{blue}{\frac{1}{\frac{\sqrt{d \cdot d + c \cdot c}}{d \cdot b + c \cdot a}}}}{\sqrt{c \cdot c + d \cdot d}} \]

    if -2.5671587108743e-166 < c < 8.1916193285250536e-172

    1. Initial program 23.4

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Taylor expanded around 0 14.7

      \[\leadsto \color{blue}{\frac{b}{d}} \]

    if 8.1916193285250536e-172 < c < 1.20160139627781954e76

    1. Initial program 15.6

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Using strategy rm
    3. Applied add-sqr-sqrt_binary6415.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 associate-/r*_binary6415.5

      \[\leadsto \color{blue}{\frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}} \]
    5. Simplified15.5

      \[\leadsto \frac{\color{blue}{\frac{d \cdot b + c \cdot a}{\sqrt{d \cdot d + c \cdot c}}}}{\sqrt{c \cdot c + d \cdot d}} \]
    6. Using strategy rm
    7. Applied *-un-lft-identity_binary6415.5

      \[\leadsto \frac{\frac{d \cdot b + c \cdot a}{\sqrt{d \cdot d + c \cdot c}}}{\color{blue}{1 \cdot \sqrt{c \cdot c + d \cdot d}}} \]
    8. Applied div-inv_binary6415.6

      \[\leadsto \frac{\color{blue}{\left(d \cdot b + c \cdot a\right) \cdot \frac{1}{\sqrt{d \cdot d + c \cdot c}}}}{1 \cdot \sqrt{c \cdot c + d \cdot d}} \]
    9. Applied times-frac_binary6415.7

      \[\leadsto \color{blue}{\frac{d \cdot b + c \cdot a}{1} \cdot \frac{\frac{1}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{c \cdot c + d \cdot d}}} \]
  3. Recombined 6 regimes into one program.
  4. Final simplification14.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -4.2044927924302215 \cdot 10^{+81}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{c}}{\frac{c}{d}}\\ \mathbf{elif}\;c \leq -5.831437032843926 \cdot 10^{-31}:\\ \;\;\;\;\frac{b \cdot d + c \cdot a}{d \cdot d + c \cdot c}\\ \mathbf{elif}\;c \leq -6.071421943859113 \cdot 10^{-88}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{d \cdot d}\\ \mathbf{elif}\;c \leq -2.567158710874279 \cdot 10^{-166}:\\ \;\;\;\;\frac{\frac{1}{\frac{\sqrt{d \cdot d + c \cdot c}}{b \cdot d + c \cdot a}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{elif}\;c \leq 8.191619328525054 \cdot 10^{-172}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 1.2016013962778195 \cdot 10^{+76}:\\ \;\;\;\;\left(b \cdot d + c \cdot a\right) \cdot \frac{\frac{1}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{c}}{\frac{c}{d}}\\ \end{array} \]

Reproduce

herbie shell --seed 2021205 
(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))))