Average Error: 26.0 → 16.1
Time: 5.6s
Precision: binary64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \leq -4.962548969300428 \cdot 10^{+153}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -1.7482251795713455 \cdot 10^{-17}:\\ \;\;\;\;\frac{\left(-a\right) - \frac{d \cdot b}{c}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;c \leq 1.1035576422910915 \cdot 10^{-08}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}\\ \mathbf{elif}\;c \leq 1.3319656876646648 \cdot 10^{+161}:\\ \;\;\;\;\frac{\frac{d \cdot b + c \cdot a}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \leq -4.962548969300428 \cdot 10^{+153}:\\
\;\;\;\;\frac{a}{c}\\

\mathbf{elif}\;c \leq -1.7482251795713455 \cdot 10^{-17}:\\
\;\;\;\;\frac{\left(-a\right) - \frac{d \cdot b}{c}}{\sqrt{c \cdot c + d \cdot d}}\\

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

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

\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\

\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 (<= c -4.962548969300428e+153)
   (/ a c)
   (if (<= c -1.7482251795713455e-17)
     (/ (- (- a) (/ (* d b) c)) (sqrt (+ (* c c) (* d d))))
     (if (<= c 1.1035576422910915e-08)
       (+ (/ b d) (/ (* c a) (pow d 2.0)))
       (if (<= c 1.3319656876646648e+161)
         (/
          (/ (+ (* d b) (* c a)) (sqrt (+ (* c c) (* d d))))
          (sqrt (+ (* c c) (* d d))))
         (/ a c))))))
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 (c <= -4.962548969300428e+153) {
		tmp = a / c;
	} else if (c <= -1.7482251795713455e-17) {
		tmp = (-a - ((d * b) / c)) / sqrt((c * c) + (d * d));
	} else if (c <= 1.1035576422910915e-08) {
		tmp = (b / d) + ((c * a) / pow(d, 2.0));
	} else if (c <= 1.3319656876646648e+161) {
		tmp = (((d * b) + (c * a)) / sqrt((c * c) + (d * d))) / sqrt((c * c) + (d * d));
	} else {
		tmp = a / c;
	}
	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.0
Target0.5
Herbie16.1
\[\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 c < -4.96254896930042763e153 or 1.33196568766466476e161 < c

    1. Initial program 44.5

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied *-un-lft-identity_binary6444.5

      \[\leadsto \frac{a \cdot c + b \cdot d}{\color{blue}{1 \cdot \left(c \cdot c + d \cdot d\right)}}\]
    4. Applied *-un-lft-identity_binary6444.5

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

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

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

      \[\leadsto 1 \cdot \color{blue}{\frac{d \cdot b + c \cdot a}{c \cdot c + d \cdot d}}\]
    8. Taylor expanded around 0 12.7

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

    if -4.96254896930042763e153 < c < -1.7482251795713455e-17

    1. Initial program 20.0

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

      \[\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*_binary6419.9

      \[\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. Simplified19.9

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

      \[\leadsto \frac{\color{blue}{-\left(\frac{d \cdot b}{c} + a\right)}}{\sqrt{c \cdot c + d \cdot d}}\]
    7. Simplified20.7

      \[\leadsto \frac{\color{blue}{\left(-a\right) - \frac{d \cdot b}{c}}}{\sqrt{c \cdot c + d \cdot d}}\]

    if -1.7482251795713455e-17 < c < 1.103557642291092e-8

    1. Initial program 19.1

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied *-un-lft-identity_binary6419.1

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

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

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

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

      \[\leadsto 1 \cdot \color{blue}{\frac{d \cdot b + c \cdot a}{c \cdot c + d \cdot d}}\]
    8. Taylor expanded around inf 15.2

      \[\leadsto 1 \cdot \color{blue}{\left(\frac{b}{d} + \frac{a \cdot c}{{d}^{2}}\right)}\]

    if 1.103557642291092e-8 < c < 1.33196568766466476e161

    1. Initial program 20.2

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt_binary6420.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*_binary6420.1

      \[\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}}}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification16.1

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -4.962548969300428 \cdot 10^{+153}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -1.7482251795713455 \cdot 10^{-17}:\\ \;\;\;\;\frac{\left(-a\right) - \frac{d \cdot b}{c}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{elif}\;c \leq 1.1035576422910915 \cdot 10^{-08}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}\\ \mathbf{elif}\;c \leq 1.3319656876646648 \cdot 10^{+161}:\\ \;\;\;\;\frac{\frac{d \cdot b + c \cdot a}{\sqrt{c \cdot c + d \cdot d}}}{\sqrt{c \cdot c + d \cdot d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array}\]

Reproduce

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