Average Error: 25.8 → 13.5
Time: 24.8s
Precision: binary64
Cost: 20880
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \mathsf{fma}\left(d, d, c \cdot c\right)\\ t_1 := \frac{c}{t_0}\\ \mathbf{if}\;d \leq -5.8 \cdot 10^{+59}:\\ \;\;\;\;\frac{\frac{b}{d} \cdot c + \left(-a\right)}{d}\\ \mathbf{elif}\;d \leq -6.5 \cdot 10^{-115}:\\ \;\;\;\;\mathsf{fma}\left(b, t_1, \left(-d\right) \cdot \frac{a}{t_0}\right)\\ \mathbf{elif}\;d \leq 6.6 \cdot 10^{-97}:\\ \;\;\;\;\frac{b - \frac{d}{c} \cdot a}{c}\\ \mathbf{elif}\;d \leq 1.35 \cdot 10^{-64}:\\ \;\;\;\;\mathsf{fma}\left(b, t_1, \frac{-a \cdot d}{t_0}\right)\\ \mathbf{elif}\;d \leq 1.85 \cdot 10^{+22}:\\ \;\;\;\;\frac{b}{c} + \left(-\frac{d}{c \cdot c} \cdot a\right)\\ \mathbf{else}:\\ \;\;\;\;\left(c \cdot \frac{1}{d}\right) \cdot \frac{b}{d} + \left(-\frac{a}{d}\right)\\ \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 (fma d d (* c c))) (t_1 (/ c t_0)))
   (if (<= d -5.8e+59)
     (/ (+ (* (/ b d) c) (- a)) d)
     (if (<= d -6.5e-115)
       (fma b t_1 (* (- d) (/ a t_0)))
       (if (<= d 6.6e-97)
         (/ (- b (* (/ d c) a)) c)
         (if (<= d 1.35e-64)
           (fma b t_1 (/ (- (* a d)) t_0))
           (if (<= d 1.85e+22)
             (+ (/ b c) (- (* (/ d (* c c)) a)))
             (+ (* (* c (/ 1.0 d)) (/ b d)) (- (/ a d))))))))))
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 = fma(d, d, (c * c));
	double t_1 = c / t_0;
	double tmp;
	if (d <= -5.8e+59) {
		tmp = (((b / d) * c) + -a) / d;
	} else if (d <= -6.5e-115) {
		tmp = fma(b, t_1, (-d * (a / t_0)));
	} else if (d <= 6.6e-97) {
		tmp = (b - ((d / c) * a)) / c;
	} else if (d <= 1.35e-64) {
		tmp = fma(b, t_1, (-(a * d) / t_0));
	} else if (d <= 1.85e+22) {
		tmp = (b / c) + -((d / (c * c)) * a);
	} else {
		tmp = ((c * (1.0 / d)) * (b / d)) + -(a / d);
	}
	return tmp;
}
function code(a, b, c, d)
	return Float64(Float64(Float64(b * c) - Float64(a * d)) / Float64(Float64(c * c) + Float64(d * d)))
end
function code(a, b, c, d)
	t_0 = fma(d, d, Float64(c * c))
	t_1 = Float64(c / t_0)
	tmp = 0.0
	if (d <= -5.8e+59)
		tmp = Float64(Float64(Float64(Float64(b / d) * c) + Float64(-a)) / d);
	elseif (d <= -6.5e-115)
		tmp = fma(b, t_1, Float64(Float64(-d) * Float64(a / t_0)));
	elseif (d <= 6.6e-97)
		tmp = Float64(Float64(b - Float64(Float64(d / c) * a)) / c);
	elseif (d <= 1.35e-64)
		tmp = fma(b, t_1, Float64(Float64(-Float64(a * d)) / t_0));
	elseif (d <= 1.85e+22)
		tmp = Float64(Float64(b / c) + Float64(-Float64(Float64(d / Float64(c * c)) * a)));
	else
		tmp = Float64(Float64(Float64(c * Float64(1.0 / d)) * Float64(b / d)) + Float64(-Float64(a / d)));
	end
	return tmp
end
code[a_, b_, c_, d_] := N[(N[(N[(b * c), $MachinePrecision] - N[(a * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(c / t$95$0), $MachinePrecision]}, If[LessEqual[d, -5.8e+59], N[(N[(N[(N[(b / d), $MachinePrecision] * c), $MachinePrecision] + (-a)), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[d, -6.5e-115], N[(b * t$95$1 + N[((-d) * N[(a / t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 6.6e-97], N[(N[(b - N[(N[(d / c), $MachinePrecision] * a), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 1.35e-64], N[(b * t$95$1 + N[((-N[(a * d), $MachinePrecision]) / t$95$0), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 1.85e+22], N[(N[(b / c), $MachinePrecision] + (-N[(N[(d / N[(c * c), $MachinePrecision]), $MachinePrecision] * a), $MachinePrecision])), $MachinePrecision], N[(N[(N[(c * N[(1.0 / d), $MachinePrecision]), $MachinePrecision] * N[(b / d), $MachinePrecision]), $MachinePrecision] + (-N[(a / d), $MachinePrecision])), $MachinePrecision]]]]]]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \mathsf{fma}\left(d, d, c \cdot c\right)\\
t_1 := \frac{c}{t_0}\\
\mathbf{if}\;d \leq -5.8 \cdot 10^{+59}:\\
\;\;\;\;\frac{\frac{b}{d} \cdot c + \left(-a\right)}{d}\\

\mathbf{elif}\;d \leq -6.5 \cdot 10^{-115}:\\
\;\;\;\;\mathsf{fma}\left(b, t_1, \left(-d\right) \cdot \frac{a}{t_0}\right)\\

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

\mathbf{elif}\;d \leq 1.35 \cdot 10^{-64}:\\
\;\;\;\;\mathsf{fma}\left(b, t_1, \frac{-a \cdot d}{t_0}\right)\\

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

\mathbf{else}:\\
\;\;\;\;\left(c \cdot \frac{1}{d}\right) \cdot \frac{b}{d} + \left(-\frac{a}{d}\right)\\


\end{array}

Error

Target

Original25.8
Target0.4
Herbie13.5
\[\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 6 regimes
  2. if d < -5.79999999999999981e59

    1. Initial program 36.4

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified36.4

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(b, c, -a \cdot d\right)}{\mathsf{fma}\left(d, d, c \cdot c\right)}} \]
      Proof
    3. Taylor expanded in c around 0 39.5

      \[\leadsto \frac{\mathsf{fma}\left(b, c, -a \cdot d\right)}{\color{blue}{{d}^{2}}} \]
    4. Simplified39.5

      \[\leadsto \frac{\mathsf{fma}\left(b, c, -a \cdot d\right)}{\color{blue}{d \cdot d}} \]
      Proof
    5. Taylor expanded in d around inf 16.2

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

      \[\leadsto \color{blue}{\frac{\frac{c \cdot b}{d} + \left(-a\right)}{d}} \]
      Proof
    7. Applied egg-rr12.7

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

    if -5.79999999999999981e59 < d < -6.50000000000000033e-115

    1. Initial program 15.4

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Applied egg-rr13.6

      \[\leadsto \color{blue}{\mathsf{fma}\left(b, \frac{c}{\mathsf{fma}\left(d, d, c \cdot c\right)}, \frac{-a \cdot d}{\mathsf{fma}\left(d, d, c \cdot c\right)}\right)} \]
    3. Applied egg-rr14.6

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

    if -6.50000000000000033e-115 < d < 6.6000000000000002e-97

    1. Initial program 21.7

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

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

      \[\leadsto \color{blue}{\frac{b}{c} + \left(-\left(a \cdot d\right) \cdot {\left(\frac{1}{c}\right)}^{2}\right)} \]
      Proof
    4. Applied egg-rr8.5

      \[\leadsto \color{blue}{\mathsf{fma}\left(-\frac{a \cdot d}{c}, \frac{1}{c}, \frac{b}{c}\right)} \]
    5. Simplified8.4

      \[\leadsto \color{blue}{\frac{b - \frac{a \cdot d}{c}}{c}} \]
      Proof
    6. Applied egg-rr8.2

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

    if 6.6000000000000002e-97 < d < 1.34999999999999993e-64

    1. Initial program 14.6

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Applied egg-rr13.0

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

    if 1.34999999999999993e-64 < d < 1.8499999999999999e22

    1. Initial program 14.3

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

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

      \[\leadsto \color{blue}{\frac{b}{c} + \left(-\left(a \cdot d\right) \cdot {\left(\frac{1}{c}\right)}^{2}\right)} \]
      Proof
    4. Applied egg-rr30.1

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

    if 1.8499999999999999e22 < d

    1. Initial program 33.1

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified33.1

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(b, c, -a \cdot d\right)}{\mathsf{fma}\left(d, d, c \cdot c\right)}} \]
      Proof
    3. Taylor expanded in c around 0 37.6

      \[\leadsto \frac{\mathsf{fma}\left(b, c, -a \cdot d\right)}{\color{blue}{{d}^{2}}} \]
    4. Simplified37.6

      \[\leadsto \frac{\mathsf{fma}\left(b, c, -a \cdot d\right)}{\color{blue}{d \cdot d}} \]
      Proof
    5. Applied egg-rr37.3

      \[\leadsto \color{blue}{\frac{\frac{1}{d}}{d} \cdot \left(b \cdot c\right) + \frac{\frac{1}{d}}{d} \cdot \left(-a \cdot d\right)} \]
    6. Taylor expanded in d around 0 19.8

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

      \[\leadsto \frac{\frac{1}{d}}{d} \cdot \left(b \cdot c\right) + \color{blue}{\left(-\frac{a}{d}\right)} \]
      Proof
    8. Applied egg-rr14.9

      \[\leadsto \color{blue}{\left(c \cdot \frac{1}{d}\right) \cdot \frac{b}{d}} + \left(-\frac{a}{d}\right) \]
  3. Recombined 6 regimes into one program.

Alternatives

Alternative 1
Error13.5
Cost20616
\[\begin{array}{l} t_0 := \mathsf{fma}\left(d, d, c \cdot c\right)\\ \mathbf{if}\;d \leq -5.8 \cdot 10^{+59}:\\ \;\;\;\;\frac{\frac{b}{d} \cdot c + \left(-a\right)}{d}\\ \mathbf{elif}\;d \leq -5.1 \cdot 10^{-113}:\\ \;\;\;\;\mathsf{fma}\left(b, \frac{c}{t_0}, \left(-d\right) \cdot \frac{a}{t_0}\right)\\ \mathbf{elif}\;d \leq 1.4 \cdot 10^{-89}:\\ \;\;\;\;\frac{b - \frac{d}{c} \cdot a}{c}\\ \mathbf{elif}\;d \leq 2.3 \cdot 10^{-63}:\\ \;\;\;\;\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 1.4 \cdot 10^{+21}:\\ \;\;\;\;\frac{b}{c} + \left(-\frac{d}{c \cdot c} \cdot a\right)\\ \mathbf{else}:\\ \;\;\;\;\left(c \cdot \frac{1}{d}\right) \cdot \frac{b}{d} + \left(-\frac{a}{d}\right)\\ \end{array} \]
Alternative 2
Error13.7
Cost7688
\[\begin{array}{l} \mathbf{if}\;d \leq -4.5 \cdot 10^{+127}:\\ \;\;\;\;\frac{\frac{c}{d} \cdot b + \left(-a\right)}{d}\\ \mathbf{elif}\;d \leq -1.45 \cdot 10^{-115}:\\ \;\;\;\;\frac{-1}{\mathsf{fma}\left(d, d, c \cdot c\right)} \cdot \left(a \cdot d + \left(-c \cdot b\right)\right)\\ \mathbf{elif}\;d \leq 5.5 \cdot 10^{-89}:\\ \;\;\;\;\frac{b - \frac{d}{c} \cdot a}{c}\\ \mathbf{elif}\;d \leq 1.7 \cdot 10^{-63}:\\ \;\;\;\;\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 8.5 \cdot 10^{+23}:\\ \;\;\;\;\frac{b}{c} + \left(-\frac{d}{c \cdot c} \cdot a\right)\\ \mathbf{else}:\\ \;\;\;\;\left(c \cdot \frac{1}{d}\right) \cdot \frac{b}{d} + \left(-\frac{a}{d}\right)\\ \end{array} \]
Alternative 3
Error13.5
Cost1556
\[\begin{array}{l} t_0 := \frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -1.85 \cdot 10^{+127}:\\ \;\;\;\;\frac{\frac{c}{d} \cdot b + \left(-a\right)}{d}\\ \mathbf{elif}\;d \leq -6 \cdot 10^{-113}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 7.2 \cdot 10^{-90}:\\ \;\;\;\;\frac{b - \frac{d}{c} \cdot a}{c}\\ \mathbf{elif}\;d \leq 1.75 \cdot 10^{-63}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 4.8 \cdot 10^{+20}:\\ \;\;\;\;\frac{b}{c} + \left(-\frac{d}{c \cdot c} \cdot a\right)\\ \mathbf{else}:\\ \;\;\;\;\left(c \cdot \frac{1}{d}\right) \cdot \frac{b}{d} + \left(-\frac{a}{d}\right)\\ \end{array} \]
Alternative 4
Error15.6
Cost1424
\[\begin{array}{l} \mathbf{if}\;d \leq -1.2 \cdot 10^{+59}:\\ \;\;\;\;\frac{\frac{b}{d} \cdot c + \left(-a\right)}{d}\\ \mathbf{elif}\;d \leq -1.35 \cdot 10^{-59}:\\ \;\;\;\;\frac{b - \frac{a}{c} \cdot d}{c}\\ \mathbf{elif}\;d \leq -7.6 \cdot 10^{-84}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;d \leq 4.2 \cdot 10^{+20}:\\ \;\;\;\;\frac{b - \frac{d}{c} \cdot a}{c}\\ \mathbf{else}:\\ \;\;\;\;\left(c \cdot \frac{1}{d}\right) \cdot \frac{b}{d} + \left(-\frac{a}{d}\right)\\ \end{array} \]
Alternative 5
Error15.6
Cost1168
\[\begin{array}{l} t_0 := \frac{\frac{b}{d} \cdot c + \left(-a\right)}{d}\\ \mathbf{if}\;d \leq -1.2 \cdot 10^{+59}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -2.8 \cdot 10^{-60}:\\ \;\;\;\;\frac{b - \frac{a}{c} \cdot d}{c}\\ \mathbf{elif}\;d \leq -7.6 \cdot 10^{-84}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;d \leq 5.4 \cdot 10^{+25}:\\ \;\;\;\;\frac{b - \frac{d}{c} \cdot a}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 6
Error17.0
Cost1104
\[\begin{array}{l} t_0 := \frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{if}\;d \leq -1.35 \cdot 10^{+59}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -4.2 \cdot 10^{-60}:\\ \;\;\;\;\frac{b - \frac{a}{c} \cdot d}{c}\\ \mathbf{elif}\;d \leq -7.6 \cdot 10^{-84}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.2 \cdot 10^{+22}:\\ \;\;\;\;\frac{b - \frac{d}{c} \cdot a}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 7
Error18.9
Cost840
\[\begin{array}{l} t_0 := -\frac{a}{d}\\ \mathbf{if}\;d \leq -2 \cdot 10^{+59}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 7.2 \cdot 10^{+22}:\\ \;\;\;\;\frac{b - \frac{a}{c} \cdot d}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Error18.4
Cost840
\[\begin{array}{l} t_0 := -\frac{a}{d}\\ \mathbf{if}\;d \leq -2.5 \cdot 10^{+59}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.44 \cdot 10^{+23}:\\ \;\;\;\;\frac{b - \frac{d}{c} \cdot a}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 9
Error24.6
Cost520
\[\begin{array}{l} t_0 := -\frac{a}{d}\\ \mathbf{if}\;d \leq -7.2 \cdot 10^{-113}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 7.5 \cdot 10^{+39}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 10
Error36.9
Cost192
\[\frac{b}{c} \]

Error

Reproduce

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