Average Error: 26.7 → 1.4
Time: 19.5s
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{\frac{-d}{\frac{\mathsf{hypot}\left(d, c\right)}{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(Float64(-d) / Float64(hypot(d, c) / 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[(N[Sqrt[d ^ 2 + c ^ 2], $MachinePrecision] / a), $MachinePrecision]), $MachinePrecision] / N[Sqrt[d ^ 2 + c ^ 2], $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{\frac{-d}{\frac{\mathsf{hypot}\left(d, c\right)}{a}}}{\mathsf{hypot}\left(d, c\right)}\right)

Error

Target

Original26.7
Target0.5
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.7

    \[\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. 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{\frac{d}{\frac{\mathsf{hypot}\left(d, c\right)}{a}}}{\mathsf{hypot}\left(d, c\right)}}\right) \]
  5. Final simplification1.4

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

Alternatives

Alternative 1
Error7.9
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 -122006031482371650:\\ \;\;\;\;\mathsf{fma}\left(t_0, t_1, \frac{a}{\mathsf{hypot}\left(d, c\right)}\right)\\ \mathbf{elif}\;d \leq 7.395863073945087 \cdot 10^{+50}:\\ \;\;\;\;\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}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \end{array} \]
Alternative 2
Error8.9
Cost26500
\[\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 -8941568.424361361:\\ \;\;\;\;\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{a}{\mathsf{hypot}\left(d, c\right)}\right)\\ \mathbf{elif}\;d \leq -1 \cdot 10^{-180}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 10^{-160}:\\ \;\;\;\;\frac{b}{c} - \frac{a}{c \cdot \frac{c}{d}}\\ \mathbf{elif}\;d \leq 1.5586786786398142 \cdot 10^{+46}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \end{array} \]
Alternative 3
Error10.7
Cost14160
\[\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 := \frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{if}\;d \leq -4.348610485428097 \cdot 10^{+35}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -1 \cdot 10^{-180}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 10^{-160}:\\ \;\;\;\;\frac{b}{c} - \frac{a}{c \cdot \frac{c}{d}}\\ \mathbf{elif}\;d \leq 1.5586786786398142 \cdot 10^{+46}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 4
Error12.4
Cost13832
\[\begin{array}{l} t_0 := \frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{if}\;d \leq -122006031482371650:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -1 \cdot 10^{-160}:\\ \;\;\;\;\frac{\mathsf{fma}\left(d, -a, c \cdot b\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}\\ \mathbf{elif}\;d \leq 7 \cdot 10^{-133}:\\ \;\;\;\;\mathsf{fma}\left(a \cdot \frac{-d}{c}, \frac{1}{c}, \frac{b}{c}\right)\\ \mathbf{elif}\;d \leq 1.5586786786398142 \cdot 10^{+46}:\\ \;\;\;\;\frac{\mathsf{fma}\left(-d, a, c \cdot b\right)}{d \cdot d + c \cdot c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Error12.4
Cost7824
\[\begin{array}{l} t_0 := d \cdot d + c \cdot c\\ t_1 := \frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{if}\;d \leq -122006031482371650:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -1 \cdot 10^{-160}:\\ \;\;\;\;\frac{c \cdot b - d \cdot a}{t_0}\\ \mathbf{elif}\;d \leq 7 \cdot 10^{-133}:\\ \;\;\;\;\mathsf{fma}\left(a \cdot \frac{-d}{c}, \frac{1}{c}, \frac{b}{c}\right)\\ \mathbf{elif}\;d \leq 1.5586786786398142 \cdot 10^{+46}:\\ \;\;\;\;\frac{\mathsf{fma}\left(-d, a, c \cdot b\right)}{t_0}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 6
Error12.4
Cost7564
\[\begin{array}{l} t_0 := \frac{c \cdot b - d \cdot a}{d \cdot d + c \cdot c}\\ t_1 := \frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{if}\;d \leq -122006031482371650:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -1 \cdot 10^{-160}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 7 \cdot 10^{-133}:\\ \;\;\;\;\mathsf{fma}\left(a \cdot \frac{-d}{c}, \frac{1}{c}, \frac{b}{c}\right)\\ \mathbf{elif}\;d \leq 1.5586786786398142 \cdot 10^{+46}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 7
Error17.0
Cost1496
\[\begin{array}{l} t_0 := \frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{if}\;d \leq -122006031482371650:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -4.1758521565422204 \cdot 10^{-48}:\\ \;\;\;\;\frac{d \cdot \left(-a\right)}{d \cdot d + c \cdot c}\\ \mathbf{elif}\;d \leq -1.998814742675197 \cdot 10^{-75}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -4.6 \cdot 10^{-135}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{elif}\;d \leq -1.8 \cdot 10^{-162}:\\ \;\;\;\;\frac{-a}{d}\\ \mathbf{elif}\;d \leq 7.605581508364986 \cdot 10^{-74}:\\ \;\;\;\;\frac{b}{c} - \frac{a}{c \cdot \frac{c}{d}}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Error12.6
Cost1488
\[\begin{array}{l} t_0 := \frac{c \cdot b - d \cdot a}{d \cdot d + c \cdot c}\\ t_1 := \frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{if}\;d \leq -122006031482371650:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -1 \cdot 10^{-160}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 7 \cdot 10^{-133}:\\ \;\;\;\;\frac{b}{c} - \frac{a}{c \cdot \frac{c}{d}}\\ \mathbf{elif}\;d \leq 1.5586786786398142 \cdot 10^{+46}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 9
Error16.3
Cost1232
\[\begin{array}{l} \mathbf{if}\;c \leq -1.2088374622742729 \cdot 10^{+45}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c \cdot \frac{c}{a}}\\ \mathbf{elif}\;c \leq 3.337487325098142 \cdot 10^{-58}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;c \leq 7.916160279112307 \cdot 10^{+49}:\\ \;\;\;\;\frac{b}{c} - \frac{a}{c \cdot \frac{c}{d}}\\ \mathbf{elif}\;c \leq 4.9522731660807546 \cdot 10^{+101}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b - d \cdot \frac{a}{c}}{c}\\ \end{array} \]
Alternative 10
Error16.3
Cost1232
\[\begin{array}{l} \mathbf{if}\;c \leq -1.2088374622742729 \cdot 10^{+45}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c \cdot \frac{c}{a}}\\ \mathbf{elif}\;c \leq 3.337487325098142 \cdot 10^{-58}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;c \leq 7.916160279112307 \cdot 10^{+49}:\\ \;\;\;\;\frac{b}{c} - \frac{a}{c \cdot \frac{c}{d}}\\ \mathbf{elif}\;c \leq 4.9522731660807546 \cdot 10^{+101}:\\ \;\;\;\;c \cdot \frac{\frac{b}{d}}{d} - \frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b - d \cdot \frac{a}{c}}{c}\\ \end{array} \]
Alternative 11
Error20.1
Cost1104
\[\begin{array}{l} t_0 := \frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{if}\;c \leq -1.2088374622742729 \cdot 10^{+45}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 3.337487325098142 \cdot 10^{-58}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 2.6689993466329684 \cdot 10^{-5}:\\ \;\;\;\;\frac{d \cdot \left(-a\right)}{c \cdot c}\\ \mathbf{elif}\;c \leq 627949049897513100:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.151460632922877 \cdot 10^{+60}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 1.65 \cdot 10^{+106}:\\ \;\;\;\;\frac{-a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 12
Error16.0
Cost1104
\[\begin{array}{l} t_0 := \frac{b - d \cdot \frac{a}{c}}{c}\\ \mathbf{if}\;c \leq -1.2088374622742729 \cdot 10^{+45}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 6.555413221717854 \cdot 10^{-39}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;c \leq 1.151460632922877 \cdot 10^{+60}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 4.9522731660807546 \cdot 10^{+101}:\\ \;\;\;\;\frac{-a}{d}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 13
Error16.2
Cost1104
\[\begin{array}{l} t_0 := \frac{b - d \cdot \frac{a}{c}}{c}\\ \mathbf{if}\;c \leq -1.2088374622742729 \cdot 10^{+45}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 3.337487325098142 \cdot 10^{-58}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;c \leq 1.151460632922877 \cdot 10^{+60}:\\ \;\;\;\;\frac{b}{c} - \frac{a}{c \cdot \frac{c}{d}}\\ \mathbf{elif}\;c \leq 4.9522731660807546 \cdot 10^{+101}:\\ \;\;\;\;\frac{-a}{d}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 14
Error16.5
Cost1104
\[\begin{array}{l} \mathbf{if}\;c \leq -1.2088374622742729 \cdot 10^{+45}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c \cdot \frac{c}{a}}\\ \mathbf{elif}\;c \leq 3.337487325098142 \cdot 10^{-58}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;c \leq 1.151460632922877 \cdot 10^{+60}:\\ \;\;\;\;\frac{b}{c} - \frac{a}{c \cdot \frac{c}{d}}\\ \mathbf{elif}\;c \leq 4.9522731660807546 \cdot 10^{+101}:\\ \;\;\;\;\frac{-a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b - d \cdot \frac{a}{c}}{c}\\ \end{array} \]
Alternative 15
Error24.3
Cost916
\[\begin{array}{l} t_0 := \frac{-a}{d}\\ \mathbf{if}\;c \leq -1.2088374622742729 \cdot 10^{+45}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 3.337487325098142 \cdot 10^{-58}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 2.6689993466329684 \cdot 10^{-5}:\\ \;\;\;\;\frac{d \cdot \left(-a\right)}{c \cdot c}\\ \mathbf{elif}\;c \leq 1.151460632922877 \cdot 10^{+60}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 1.65 \cdot 10^{+106}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 16
Error23.8
Cost784
\[\begin{array}{l} t_0 := \frac{-a}{d}\\ \mathbf{if}\;c \leq -1.2088374622742729 \cdot 10^{+45}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 3.337487325098142 \cdot 10^{-58}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.151460632922877 \cdot 10^{+60}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 1.65 \cdot 10^{+106}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 17
Error37.4
Cost192
\[\frac{b}{c} \]

Error

Reproduce

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