Average Error: 25.6 → 12.7
Time: 5.7s
Precision: 64
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -1.6740965059493151 \cdot 10^{204}:\\ \;\;\;\;{\left(\frac{-1 \cdot a}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\\ \mathbf{elif}\;c \le 3.3012917942497675 \cdot 10^{146}:\\ \;\;\;\;{\left(\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\\ \mathbf{else}:\\ \;\;\;\;{\left(\frac{a}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\\ \end{array}\]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;c \le -1.6740965059493151 \cdot 10^{204}:\\
\;\;\;\;{\left(\frac{-1 \cdot a}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\\

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

\mathbf{else}:\\
\;\;\;\;{\left(\frac{a}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\\

\end{array}
double code(double a, double b, double c, double d) {
	return ((double) (((double) (((double) (a * c)) + ((double) (b * d)))) / ((double) (((double) (c * c)) + ((double) (d * d))))));
}
double code(double a, double b, double c, double d) {
	double VAR;
	if ((c <= -1.674096505949315e+204)) {
		VAR = ((double) pow(((double) (((double) (-1.0 * a)) / ((double) hypot(c, d)))), 1.0));
	} else {
		double VAR_1;
		if ((c <= 3.3012917942497675e+146)) {
			VAR_1 = ((double) pow(((double) (((double) (((double) fma(a, c, ((double) (b * d)))) / ((double) hypot(c, d)))) / ((double) hypot(c, d)))), 1.0));
		} else {
			VAR_1 = ((double) pow(((double) (a / ((double) hypot(c, d)))), 1.0));
		}
		VAR = VAR_1;
	}
	return VAR;
}

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
Herbie12.7
\[\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 3 regimes
  2. if c < -1.674096505949315e+204

    1. Initial program 41.9

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt41.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 *-un-lft-identity41.9

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

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

      \[\leadsto \color{blue}{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{1}}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}\]
    7. Simplified30.0

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

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

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

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

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

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

    if -1.674096505949315e+204 < c < 3.3012917942497675e+146

    1. Initial program 20.6

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt20.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-identity20.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-frac20.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. Simplified20.6

      \[\leadsto \color{blue}{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{1}}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}\]
    7. Simplified12.9

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

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

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

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

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

    if 3.3012917942497675e+146 < c

    1. Initial program 43.4

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt43.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 *-un-lft-identity43.4

      \[\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-frac43.4

      \[\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. Simplified43.4

      \[\leadsto \color{blue}{\frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{1}}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}\]
    7. Simplified28.5

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -1.6740965059493151 \cdot 10^{204}:\\ \;\;\;\;{\left(\frac{-1 \cdot a}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\\ \mathbf{elif}\;c \le 3.3012917942497675 \cdot 10^{146}:\\ \;\;\;\;{\left(\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\\ \mathbf{else}:\\ \;\;\;\;{\left(\frac{a}{\mathsf{hypot}\left(c, d\right)}\right)}^{1}\\ \end{array}\]

Reproduce

herbie shell --seed 2020113 +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))))