Average Error: 25.8 → 25.2
Time: 5.3s
Precision: binary64
\[\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} \leq 3.3068320798254434 \cdot 10^{+305}:\\ \;\;\;\;\frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\sqrt[3]{c \cdot c + d \cdot d} \cdot \sqrt[3]{c \cdot c + d \cdot d}} \cdot \left(\left(a \cdot c\right) \cdot \sqrt[3]{\frac{1}{d \cdot d}} + b \cdot \sqrt[3]{d}\right)\\ \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} \leq 3.3068320798254434 \cdot 10^{+305}:\\
\;\;\;\;\frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{\sqrt[3]{c \cdot c + d \cdot d} \cdot \sqrt[3]{c \cdot c + d \cdot d}} \cdot \left(\left(a \cdot c\right) \cdot \sqrt[3]{\frac{1}{d \cdot d}} + b \cdot \sqrt[3]{d}\right)\\

\end{array}
(FPCore (a b c d)
 :precision binary64
 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (if (<= (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))) 3.3068320798254434e+305)
   (/
    (/ (+ (* a c) (* b d)) (sqrt (+ (* c c) (* d d))))
    (sqrt (+ (* c c) (* d d))))
   (*
    (/ 1.0 (* (cbrt (+ (* c c) (* d d))) (cbrt (+ (* c c) (* d d)))))
    (+ (* (* a c) (cbrt (/ 1.0 (* d d)))) (* b (cbrt d))))))
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 tmp;
	if ((((a * c) + (b * d)) / ((c * c) + (d * d))) <= 3.3068320798254434e+305) {
		tmp = (((a * c) + (b * d)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
	} else {
		tmp = (1.0 / (cbrt((c * c) + (d * d)) * cbrt((c * c) + (d * d)))) * (((a * c) * cbrt(1.0 / (d * d))) + (b * cbrt(d)));
	}
	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.8
Target0.4
Herbie25.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 2 regimes
  2. if (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) < 3.30683207982544344e305

    1. Initial program 13.9

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

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

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

    if 3.30683207982544344e305 < (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d)))

    1. Initial program 63.9

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

      \[\leadsto \frac{a \cdot c + b \cdot d}{\color{blue}{\left(\sqrt[3]{c \cdot c + d \cdot d} \cdot \sqrt[3]{c \cdot c + d \cdot d}\right) \cdot \sqrt[3]{c \cdot c + d \cdot d}}}\]
    4. Applied *-un-lft-identity_binary64_283163.9

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

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

      \[\leadsto \frac{1}{\sqrt[3]{c \cdot c + d \cdot d} \cdot \sqrt[3]{c \cdot c + d \cdot d}} \cdot \color{blue}{\left({\left(\frac{1}{{d}^{2}}\right)}^{0.3333333333333333} \cdot \left(a \cdot c\right) + {d}^{0.3333333333333333} \cdot b\right)}\]
    7. Simplified61.4

      \[\leadsto \frac{1}{\sqrt[3]{c \cdot c + d \cdot d} \cdot \sqrt[3]{c \cdot c + d \cdot d}} \cdot \color{blue}{\left(\left(c \cdot a\right) \cdot \sqrt[3]{\frac{1}{d \cdot d}} + b \cdot \sqrt[3]{d}\right)}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification25.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \leq 3.3068320798254434 \cdot 10^{+305}:\\ \;\;\;\;\frac{\frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\sqrt[3]{c \cdot c + d \cdot d} \cdot \sqrt[3]{c \cdot c + d \cdot d}} \cdot \left(\left(a \cdot c\right) \cdot \sqrt[3]{\frac{1}{d \cdot d}} + b \cdot \sqrt[3]{d}\right)\\ \end{array}\]

Reproduce

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