Average Error: 26.9 → 12.9
Time: 8.7s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \leq -6.326900384087905 \cdot 10^{+99}:\\ \;\;\;\;\frac{b}{d} + \frac{1}{\frac{d}{c \cdot \frac{a}{d}}}\\ \mathbf{elif}\;d \leq -1.571220827360316 \cdot 10^{-157}:\\ \;\;\;\;\frac{\frac{c \cdot a + d \cdot b}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;d \leq 1.0832799013795811 \cdot 10^{-106}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}\\ \mathbf{elif}\;d \leq 2.155918540492542 \cdot 10^{+114}:\\ \;\;\;\;\frac{\frac{c \cdot a + d \cdot b}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{\frac{a}{d} \cdot \frac{c}{\sqrt[3]{d} \cdot \sqrt[3]{d}}}{\sqrt[3]{d}}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;d \leq -6.326900384087905 \cdot 10^{+99}:\\
\;\;\;\;\frac{b}{d} + \frac{1}{\frac{d}{c \cdot \frac{a}{d}}}\\

\mathbf{elif}\;d \leq -1.571220827360316 \cdot 10^{-157}:\\
\;\;\;\;\frac{\frac{c \cdot a + d \cdot b}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\

\mathbf{elif}\;d \leq 1.0832799013795811 \cdot 10^{-106}:\\
\;\;\;\;\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}\\

\mathbf{elif}\;d \leq 2.155918540492542 \cdot 10^{+114}:\\
\;\;\;\;\frac{\frac{c \cdot a + d \cdot b}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\

\mathbf{else}:\\
\;\;\;\;\frac{b}{d} + \frac{\frac{a}{d} \cdot \frac{c}{\sqrt[3]{d} \cdot \sqrt[3]{d}}}{\sqrt[3]{d}}\\

\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 (<= d -6.326900384087905e+99)
   (+ (/ b d) (/ 1.0 (/ d (* c (/ a d)))))
   (if (<= d -1.571220827360316e-157)
     (/
      (/ (+ (* c a) (* d b)) (sqrt (+ (* c c) (* d d))))
      (sqrt (+ (* c c) (* d d))))
     (if (<= d 1.0832799013795811e-106)
       (+ (/ a c) (/ (* d b) (pow c 2.0)))
       (if (<= d 2.155918540492542e+114)
         (/
          (/ (+ (* c a) (* d b)) (sqrt (+ (* c c) (* d d))))
          (sqrt (+ (* c c) (* d d))))
         (+ (/ b d) (/ (* (/ a d) (/ c (* (cbrt d) (cbrt d)))) (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 (d <= -6.326900384087905e+99) {
		tmp = (b / d) + (1.0 / (d / (c * (a / d))));
	} else if (d <= -1.571220827360316e-157) {
		tmp = (((c * a) + (d * b)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
	} else if (d <= 1.0832799013795811e-106) {
		tmp = (a / c) + ((d * b) / pow(c, 2.0));
	} else if (d <= 2.155918540492542e+114) {
		tmp = (((c * a) + (d * b)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
	} else {
		tmp = (b / d) + (((a / d) * (c / (cbrt(d) * cbrt(d)))) / 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

Original26.9
Target0.5
Herbie12.9
\[\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 4 regimes
  2. if d < -6.32690038408790547e99

    1. Initial program 39.9

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

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

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{c \cdot a}{d \cdot d}}\]
    4. Using strategy rm
    5. Applied associate-/r*_binary64_309114.7

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

      \[\leadsto \frac{b}{d} + \frac{\color{blue}{c \cdot \frac{a}{d}}}{d}\]
    7. Using strategy rm
    8. Applied clear-num_binary64_314610.9

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

    if -6.32690038408790547e99 < d < -1.5712208273603159e-157 or 1.08327990137958106e-106 < d < 2.15591854049254201e114

    1. Initial program 16.4

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

    if -1.5712208273603159e-157 < d < 1.08327990137958106e-106

    1. Initial program 23.3

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

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

    if 2.15591854049254201e114 < d

    1. Initial program 42.2

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

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

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{c \cdot a}{d \cdot d}}\]
    4. Using strategy rm
    5. Applied associate-/r*_binary64_309114.0

      \[\leadsto \frac{b}{d} + \color{blue}{\frac{\frac{c \cdot a}{d}}{d}}\]
    6. Simplified9.7

      \[\leadsto \frac{b}{d} + \frac{\color{blue}{c \cdot \frac{a}{d}}}{d}\]
    7. Using strategy rm
    8. Applied add-cube-cbrt_binary64_31829.8

      \[\leadsto \frac{b}{d} + \frac{c \cdot \frac{a}{d}}{\color{blue}{\left(\sqrt[3]{d} \cdot \sqrt[3]{d}\right) \cdot \sqrt[3]{d}}}\]
    9. Applied associate-/r*_binary64_30919.8

      \[\leadsto \frac{b}{d} + \color{blue}{\frac{\frac{c \cdot \frac{a}{d}}{\sqrt[3]{d} \cdot \sqrt[3]{d}}}{\sqrt[3]{d}}}\]
    10. Simplified9.8

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -6.326900384087905 \cdot 10^{+99}:\\ \;\;\;\;\frac{b}{d} + \frac{1}{\frac{d}{c \cdot \frac{a}{d}}}\\ \mathbf{elif}\;d \leq -1.571220827360316 \cdot 10^{-157}:\\ \;\;\;\;\frac{\frac{c \cdot a + d \cdot b}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;d \leq 1.0832799013795811 \cdot 10^{-106}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot b}{{c}^{2}}\\ \mathbf{elif}\;d \leq 2.155918540492542 \cdot 10^{+114}:\\ \;\;\;\;\frac{\frac{c \cdot a + d \cdot b}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{\frac{a}{d} \cdot \frac{c}{\sqrt[3]{d} \cdot \sqrt[3]{d}}}{\sqrt[3]{d}}\\ \end{array}\]

Reproduce

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