Average Error: 26.6 → 1.5
Time: 17.4s
Precision: binary64
Cost: 33152
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{-a}{\mathsf{hypot}\left(d, c\right)}\right) \]
(FPCore (a b c d)
 :precision binary64
 (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (fma
  (/ c (hypot c d))
  (/ b (hypot c d))
  (* (/ d (hypot d c)) (/ (- a) (hypot d c)))))
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) {
	return fma((c / hypot(c, d)), (b / hypot(c, d)), ((d / hypot(d, c)) * (-a / hypot(d, c))));
}
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)
	return fma(Float64(c / hypot(c, d)), Float64(b / hypot(c, d)), Float64(Float64(d / hypot(d, c)) * Float64(Float64(-a) / hypot(d, c))))
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_] := N[(N[(c / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(b / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] + N[(N[(d / N[Sqrt[d ^ 2 + c ^ 2], $MachinePrecision]), $MachinePrecision] * N[((-a) / N[Sqrt[d ^ 2 + c ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{-a}{\mathsf{hypot}\left(d, c\right)}\right)

Error

Target

Original26.6
Target0.4
Herbie1.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. Initial program 26.6

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

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

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

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

Alternatives

Alternative 1
Error6.2
Cost33288
\[\begin{array}{l} t_0 := \frac{c}{\mathsf{hypot}\left(c, d\right)}\\ t_1 := \frac{b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{if}\;d \leq -4.1 \cdot 10^{+117}:\\ \;\;\;\;\mathsf{fma}\left(t_0, t_1, \frac{-a}{d}\right)\\ \mathbf{elif}\;d \leq 8.4 \cdot 10^{+43}:\\ \;\;\;\;\mathsf{fma}\left(t_0, t_1, \frac{d \cdot \left(-a\right)}{{\left(\mathsf{hypot}\left(c, d\right)\right)}^{2}}\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(t_0, t_1, \frac{-a}{\mathsf{hypot}\left(d, c\right)}\right)\\ \end{array} \]
Alternative 2
Error7.5
Cost20996
\[\begin{array}{l} t_0 := c \cdot b - d \cdot a\\ \mathbf{if}\;\frac{t_0}{c \cdot c + d \cdot d} \leq 5 \cdot 10^{+250}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{t_0}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{-a}{d}\right)\\ \end{array} \]
Alternative 3
Error9.1
Cost14408
\[\begin{array}{l} \mathbf{if}\;d \leq -6.5 \cdot 10^{+45}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{elif}\;d \leq 8 \cdot 10^{+128}:\\ \;\;\;\;\frac{c}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{b}{\mathsf{hypot}\left(c, d\right)} - d \cdot \frac{a}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{b}{d}, \frac{c}{d}, \frac{-a}{d}\right)\\ \end{array} \]
Alternative 4
Error11.0
Cost14288
\[\begin{array}{l} t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{if}\;d \leq -7 \cdot 10^{+113}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{elif}\;d \leq -4.1 \cdot 10^{-112}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 2.3 \cdot 10^{-166}:\\ \;\;\;\;\frac{b}{c} - \frac{a}{\frac{c \cdot c}{d}}\\ \mathbf{elif}\;d \leq 2 \cdot 10^{+73}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{b}{d}, \frac{c}{d}, \frac{-a}{d}\right)\\ \end{array} \]
Alternative 5
Error14.5
Cost7436
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := d \cdot \frac{a}{t_0}\\ \mathbf{if}\;d \leq -4.25 \cdot 10^{+43}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{elif}\;d \leq 9.6 \cdot 10^{-136}:\\ \;\;\;\;\frac{b}{c} - t_1\\ \mathbf{elif}\;d \leq 2 \cdot 10^{+73}:\\ \;\;\;\;\frac{c \cdot b}{t_0} - t_1\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{b}{d}, \frac{c}{d}, \frac{-a}{d}\right)\\ \end{array} \]
Alternative 6
Error14.6
Cost1868
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := d \cdot \frac{a}{t_0}\\ t_2 := \frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{if}\;d \leq -1.42 \cdot 10^{+44}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;d \leq 9.6 \cdot 10^{-136}:\\ \;\;\;\;\frac{b}{c} - t_1\\ \mathbf{elif}\;d \leq 2 \cdot 10^{+73}:\\ \;\;\;\;\frac{c \cdot b}{t_0} - t_1\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 7
Error17.4
Cost1496
\[\begin{array}{l} t_0 := \frac{b}{c} - d \cdot \frac{a}{c \cdot c}\\ t_1 := \frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{if}\;d \leq -1.8 \cdot 10^{+36}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -7.2 \cdot 10^{-17}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -1.25 \cdot 10^{-50}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 8.4 \cdot 10^{-44}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.26 \cdot 10^{-26}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;d \leq 9.8 \cdot 10^{-14}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot \frac{b}{d} - a}{d}\\ \end{array} \]
Alternative 8
Error16.8
Cost1496
\[\begin{array}{l} t_0 := \frac{b}{c} - \frac{a}{\frac{c \cdot c}{d}}\\ t_1 := \frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{if}\;d \leq -9.6 \cdot 10^{+40}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -1.22 \cdot 10^{-17}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -4.1 \cdot 10^{-50}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 1.9 \cdot 10^{-42}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 9.2 \cdot 10^{-27}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;d \leq 1.05 \cdot 10^{-13}:\\ \;\;\;\;\frac{b}{c} - d \cdot \frac{a}{c \cdot c}\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot \frac{b}{d} - a}{d}\\ \end{array} \]
Alternative 9
Error16.8
Cost1496
\[\begin{array}{l} t_0 := \frac{b}{c} - \frac{a}{\frac{c \cdot c}{d}}\\ t_1 := \frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{if}\;d \leq -4 \cdot 10^{+37}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -6.5 \cdot 10^{-17}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -1.65 \cdot 10^{-50}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 1.96 \cdot 10^{-42}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 9.5 \cdot 10^{-27}:\\ \;\;\;\;\frac{b}{d \cdot \frac{d}{c}} - \frac{a}{d}\\ \mathbf{elif}\;d \leq 3.4 \cdot 10^{-14}:\\ \;\;\;\;\frac{b}{c} - d \cdot \frac{a}{c \cdot c}\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot \frac{b}{d} - a}{d}\\ \end{array} \]
Alternative 10
Error15.8
Cost1488
\[\begin{array}{l} t_0 := \frac{b}{c} - d \cdot \frac{a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -1.45 \cdot 10^{+44}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{elif}\;d \leq 1.96 \cdot 10^{-42}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 7.6 \cdot 10^{-27}:\\ \;\;\;\;\frac{b}{d \cdot \frac{d}{c}} - \frac{a}{d}\\ \mathbf{elif}\;d \leq 1.1 \cdot 10^{-13}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot \frac{b}{d} - a}{d}\\ \end{array} \]
Alternative 11
Error14.5
Cost1356
\[\begin{array}{l} t_0 := c \cdot c + d \cdot d\\ t_1 := \frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{if}\;d \leq -7.2 \cdot 10^{+39}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 8.2 \cdot 10^{-134}:\\ \;\;\;\;\frac{b}{c} - d \cdot \frac{a}{t_0}\\ \mathbf{elif}\;d \leq 9 \cdot 10^{+72}:\\ \;\;\;\;\frac{c \cdot b - d \cdot a}{t_0}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 12
Error19.0
Cost1104
\[\begin{array}{l} t_0 := \frac{c \cdot \frac{b}{d} - a}{d}\\ \mathbf{if}\;d \leq -5.4 \cdot 10^{+39}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 4.9 \cdot 10^{-45}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;d \leq 1.26 \cdot 10^{-26}:\\ \;\;\;\;\frac{b \cdot \frac{c}{d} - a}{d}\\ \mathbf{elif}\;d \leq 3.3 \cdot 10^{-14}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 13
Error19.0
Cost1104
\[\begin{array}{l} t_0 := \frac{c \cdot \frac{b}{d} - a}{d}\\ \mathbf{if}\;d \leq -1.8 \cdot 10^{+37}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 2.3 \cdot 10^{-44}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;d \leq 7.6 \cdot 10^{-27}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;d \leq 1.06 \cdot 10^{-14}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 14
Error19.2
Cost1104
\[\begin{array}{l} \mathbf{if}\;d \leq -8.2 \cdot 10^{+40}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{elif}\;d \leq 3.6 \cdot 10^{-43}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;d \leq 10^{-26}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;d \leq 9.8 \cdot 10^{-14}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot \frac{b}{d} - a}{d}\\ \end{array} \]
Alternative 15
Error19.7
Cost840
\[\begin{array}{l} \mathbf{if}\;c \leq -2.25 \cdot 10^{+121}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 1.05 \cdot 10^{+88}:\\ \;\;\;\;\frac{c \cdot \frac{b}{d} - a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 16
Error22.8
Cost520
\[\begin{array}{l} t_0 := \frac{-a}{d}\\ \mathbf{if}\;d \leq -1 \cdot 10^{+36}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1150000:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 17
Error37.7
Cost192
\[\frac{b}{c} \]

Error

Reproduce

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