Average Error: 26.0 → 1.4
Time: 21.1s
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.0
Target0.4
Herbie1.4
\[\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.0

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

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

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

    \[\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
Error10.6
Cost33552
\[\begin{array}{l} t_0 := \mathsf{fma}\left(d, d, c \cdot c\right)\\ t_1 := \frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{if}\;c \leq -9.328439050051052 \cdot 10^{+150}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -1 \cdot 10^{-150}:\\ \;\;\;\;b \cdot \frac{c}{t_0} - a \cdot \frac{d}{t_0}\\ \mathbf{elif}\;c \leq 8.358364608016383 \cdot 10^{-5}:\\ \;\;\;\;\frac{b \cdot \frac{c}{d} - a}{d}\\ \mathbf{elif}\;c \leq 7.524065297619922 \cdot 10^{+168}:\\ \;\;\;\;\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{a \cdot \left(-d\right)}{{\left(\mathsf{hypot}\left(c, d\right)\right)}^{2}}\right)\\ \mathbf{elif}\;c \leq 1.0718376395457786 \cdot 10^{+195}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq 4.109939024723472 \cdot 10^{+200}:\\ \;\;\;\;\frac{1}{\left(c \cdot \frac{c}{d}\right) \cdot \left(\frac{-1}{a} - \frac{b \cdot b}{{a}^{3}}\right) - \left(\frac{d}{a} + \frac{c}{a} \cdot \frac{b}{a}\right)}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 2
Error1.6
Cost33152
\[\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{\frac{-d}{\mathsf{hypot}\left(c, d\right)}}{\frac{\mathsf{hypot}\left(c, d\right)}{a}}\right) \]
Alternative 3
Error10.9
Cost14808
\[\begin{array}{l} t_0 := \frac{\frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ t_1 := \mathsf{fma}\left(c, c, d \cdot d\right)\\ \mathbf{if}\;d \leq -3.0435373335258623 \cdot 10^{+50}:\\ \;\;\;\;\frac{c \cdot \frac{b}{d} - a}{d}\\ \mathbf{elif}\;d \leq -1 \cdot 10^{-195}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 10^{-190}:\\ \;\;\;\;\frac{b}{c} - \frac{a}{c \cdot \frac{c}{d}}\\ \mathbf{elif}\;d \leq 7.456499424371905 \cdot 10^{+28}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 6.145696025654044 \cdot 10^{+93}:\\ \;\;\;\;\frac{c}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \leq 4.957626207730631 \cdot 10^{+104}:\\ \;\;\;\;c \cdot \frac{b}{t_1} - a \cdot \frac{d}{t_1}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{c}{d}}{\frac{d}{b}} - \frac{a}{d}\\ \end{array} \]
Alternative 4
Error11.1
Cost14296
\[\begin{array}{l} t_0 := \frac{\frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{if}\;d \leq -3.0435373335258623 \cdot 10^{+50}:\\ \;\;\;\;\frac{c \cdot \frac{b}{d} - a}{d}\\ \mathbf{elif}\;d \leq -1 \cdot 10^{-195}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 10^{-190}:\\ \;\;\;\;\frac{b}{c} - \frac{a}{c \cdot \frac{c}{d}}\\ \mathbf{elif}\;d \leq 7.456499424371905 \cdot 10^{+28}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 6.145696025654044 \cdot 10^{+93}:\\ \;\;\;\;\frac{c}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \leq 9.637483085321208 \cdot 10^{+97}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{c \cdot b}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{c}{d}}{\frac{d}{b}} - \frac{a}{d}\\ \end{array} \]
Alternative 5
Error12.4
Cost1488
\[\begin{array}{l} t_0 := \frac{c \cdot b - d \cdot a}{c \cdot c + d \cdot d}\\ t_1 := \frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{if}\;c \leq -4.149810530834531 \cdot 10^{+114}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -1 \cdot 10^{-150}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 8.358364608016383 \cdot 10^{-5}:\\ \;\;\;\;\frac{b \cdot \frac{c}{d} - a}{d}\\ \mathbf{elif}\;c \leq 6.8607979028319614 \cdot 10^{+128}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 6
Error14.9
Cost968
\[\begin{array}{l} t_0 := \frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{if}\;c \leq -3.632484411105255 \cdot 10^{+39}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 8.358364608016383 \cdot 10^{-5}:\\ \;\;\;\;\frac{b \cdot \frac{c}{d} - a}{d}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 7
Error24.0
Cost908
\[\begin{array}{l} \mathbf{if}\;c \leq -5.52445799675944 \cdot 10^{-33}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 2.191442538112069 \cdot 10^{+47}:\\ \;\;\;\;\frac{-a}{d}\\ \mathbf{elif}\;c \leq 6.010144942631681 \cdot 10^{+113}:\\ \;\;\;\;d \cdot \frac{-a}{c \cdot c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 8
Error19.8
Cost840
\[\begin{array}{l} \mathbf{if}\;c \leq -4.275832477588358 \cdot 10^{+115}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 6.010144942631681 \cdot 10^{+113}:\\ \;\;\;\;\frac{b \cdot \frac{c}{d} - a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 9
Error23.1
Cost784
\[\begin{array}{l} t_0 := \frac{-a}{d}\\ \mathbf{if}\;d \leq -9.834115852018582 \cdot 10^{+65}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 4.484683706448499 \cdot 10^{-9}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;d \leq 7.456499424371905 \cdot 10^{+28}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 5.271506439197176 \cdot 10^{+68}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 10
Error37.7
Cost192
\[\frac{b}{c} \]

Error

Reproduce

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