Average Error: 26.3 → 13.7
Time: 5.8s
Precision: binary64
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \frac{b}{c} - \frac{a}{c} \cdot \frac{d}{c}\\ \mathbf{if}\;c \leq -1.3940742026862542 \cdot 10^{+133}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_1 := \sqrt{d \cdot d + c \cdot c}\\ t_2 := \frac{\frac{c \cdot b - a \cdot d}{t_1}}{t_1}\\ \mathbf{if}\;c \leq -1.3557623343064346 \cdot 10^{-10}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;c \leq 2.046408208770978 \cdot 10^{-175}:\\ \;\;\;\;\frac{c \cdot b}{d \cdot d} - \frac{a}{d}\\ \mathbf{elif}\;c \leq 1.5513558310683366 \cdot 10^{+151}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array}\\ \end{array} \]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{b}{c} - \frac{a}{c} \cdot \frac{d}{c}\\
\mathbf{if}\;c \leq -1.3940742026862542 \cdot 10^{+133}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;c \leq 2.046408208770978 \cdot 10^{-175}:\\
\;\;\;\;\frac{c \cdot b}{d \cdot d} - \frac{a}{d}\\

\mathbf{elif}\;c \leq 1.5513558310683366 \cdot 10^{+151}:\\
\;\;\;\;t_2\\

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


\end{array}\\


\end{array}
(FPCore (a b c d)
 :precision binary64
 (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (- (/ b c) (* (/ a c) (/ d c)))))
   (if (<= c -1.3940742026862542e+133)
     t_0
     (let* ((t_1 (sqrt (+ (* d d) (* c c))))
            (t_2 (/ (/ (- (* c b) (* a d)) t_1) t_1)))
       (if (<= c -1.3557623343064346e-10)
         t_2
         (if (<= c 2.046408208770978e-175)
           (- (/ (* c b) (* d d)) (/ a d))
           (if (<= c 1.5513558310683366e+151) t_2 t_0)))))))
double code(double a, double b, double c, double d) {
	return ((b * c) - (a * d)) / ((c * c) + (d * d));
}
double code(double a, double b, double c, double d) {
	double t_0 = (b / c) - ((a / c) * (d / c));
	double tmp;
	if (c <= -1.3940742026862542e+133) {
		tmp = t_0;
	} else {
		double t_1 = sqrt((d * d) + (c * c));
		double t_2 = (((c * b) - (a * d)) / t_1) / t_1;
		double tmp_1;
		if (c <= -1.3557623343064346e-10) {
			tmp_1 = t_2;
		} else if (c <= 2.046408208770978e-175) {
			tmp_1 = ((c * b) / (d * d)) - (a / d);
		} else if (c <= 1.5513558310683366e+151) {
			tmp_1 = t_2;
		} else {
			tmp_1 = t_0;
		}
		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

Original26.3
Target0.5
Herbie13.7
\[\begin{array}{l} \mathbf{if}\;\left|d\right| < \left|c\right|:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c + d \cdot \frac{d}{c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(-a\right) + b \cdot \frac{c}{d}}{d + c \cdot \frac{c}{d}}\\ \end{array} \]

Derivation

  1. Split input into 3 regimes
  2. if c < -1.39407420268625422e133 or 1.5513558310683366e151 < c

    1. Initial program 43.3

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

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

      \[\leadsto \color{blue}{\frac{b}{c} - \frac{a \cdot d}{c \cdot c}} \]
    4. Using strategy rm
    5. Applied times-frac_binary647.5

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

    if -1.39407420268625422e133 < c < -1.3557623343064346e-10 or 2.0464082087709779e-175 < c < 1.5513558310683366e151

    1. Initial program 17.5

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

      \[\leadsto \frac{b \cdot c - a \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}} \]
    4. Applied associate-/r*_binary6417.4

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

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

    if -1.3557623343064346e-10 < c < 2.0464082087709779e-175

    1. Initial program 21.5

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

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

      \[\leadsto \color{blue}{\frac{c \cdot b}{d \cdot d} - \frac{a}{d}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification13.7

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -1.3940742026862542 \cdot 10^{+133}:\\ \;\;\;\;\frac{b}{c} - \frac{a}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;c \leq -1.3557623343064346 \cdot 10^{-10}:\\ \;\;\;\;\frac{\frac{c \cdot b - a \cdot d}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{elif}\;c \leq 2.046408208770978 \cdot 10^{-175}:\\ \;\;\;\;\frac{c \cdot b}{d \cdot d} - \frac{a}{d}\\ \mathbf{elif}\;c \leq 1.5513558310683366 \cdot 10^{+151}:\\ \;\;\;\;\frac{\frac{c \cdot b - a \cdot d}{\sqrt{d \cdot d + c \cdot c}}}{\sqrt{d \cdot d + c \cdot c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c} - \frac{a}{c} \cdot \frac{d}{c}\\ \end{array} \]

Reproduce

herbie shell --seed 2021205 
(FPCore (a b c d)
  :name "Complex division, imag part"
  :precision binary64

  :herbie-target
  (if (< (fabs d) (fabs c)) (/ (- b (* a (/ d c))) (+ c (* d (/ d c)))) (/ (+ (- a) (* b (/ c d))) (+ d (* c (/ c d)))))

  (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))