Average Error: 25.9 → 1.1
Time: 15.9s
Precision: binary64
Cost: 20352
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(c \cdot \frac{b}{\mathsf{hypot}\left(c, d\right)} - \frac{d}{\mathsf{hypot}\left(c, d\right)} \cdot a\right) \]
(FPCore (a b c d)
 :precision binary64
 (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (* (/ 1.0 (hypot c d)) (- (* c (/ b (hypot c d))) (* (/ d (hypot c d)) a))))
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 (1.0 / hypot(c, d)) * ((c * (b / hypot(c, d))) - ((d / hypot(c, d)) * a));
}
public static double code(double a, double b, double c, double d) {
	return ((b * c) - (a * d)) / ((c * c) + (d * d));
}
public static double code(double a, double b, double c, double d) {
	return (1.0 / Math.hypot(c, d)) * ((c * (b / Math.hypot(c, d))) - ((d / Math.hypot(c, d)) * a));
}
def code(a, b, c, d):
	return ((b * c) - (a * d)) / ((c * c) + (d * d))
def code(a, b, c, d):
	return (1.0 / math.hypot(c, d)) * ((c * (b / math.hypot(c, d))) - ((d / math.hypot(c, d)) * a))
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 Float64(Float64(1.0 / hypot(c, d)) * Float64(Float64(c * Float64(b / hypot(c, d))) - Float64(Float64(d / hypot(c, d)) * a)))
end
function tmp = code(a, b, c, d)
	tmp = ((b * c) - (a * d)) / ((c * c) + (d * d));
end
function tmp = code(a, b, c, d)
	tmp = (1.0 / hypot(c, d)) * ((c * (b / hypot(c, d))) - ((d / hypot(c, d)) * a));
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[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(N[(c * N[(b / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - N[(N[(d / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(c \cdot \frac{b}{\mathsf{hypot}\left(c, d\right)} - \frac{d}{\mathsf{hypot}\left(c, d\right)} \cdot a\right)

Error

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original25.9
Target0.4
Herbie1.1
\[\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 25.9

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

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

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

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

Alternatives

Alternative 1
Error1.5
Cost20352
\[\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(c \cdot \frac{b}{\mathsf{hypot}\left(c, d\right)} - d \cdot \frac{a}{\mathsf{hypot}\left(c, d\right)}\right) \]
Alternative 2
Error11.1
Cost14160
\[\begin{array}{l} t_0 := \frac{c \cdot b - d \cdot a}{c \cdot c + d \cdot d}\\ t_1 := \frac{1}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{if}\;c \leq -3.5 \cdot 10^{+46}:\\ \;\;\;\;t_1 \cdot \left(\frac{a}{\frac{c}{d}} - b\right)\\ \mathbf{elif}\;c \leq -4 \cdot 10^{-141}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 9.5 \cdot 10^{-148}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{elif}\;c \leq 1.12 \cdot 10^{-74}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1 \cdot \left(b - \frac{d}{\mathsf{hypot}\left(c, d\right)} \cdot a\right)\\ \end{array} \]
Alternative 3
Error11.1
Cost14160
\[\begin{array}{l} t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right)}\\ t_1 := c \cdot b - d \cdot a\\ \mathbf{if}\;c \leq -7.2 \cdot 10^{+48}:\\ \;\;\;\;t_0 \cdot \left(\frac{a}{\frac{c}{d}} - b\right)\\ \mathbf{elif}\;c \leq -1.35 \cdot 10^{-140}:\\ \;\;\;\;\frac{1}{{\left(\mathsf{hypot}\left(c, d\right)\right)}^{2}} \cdot t_1\\ \mathbf{elif}\;c \leq 2.3 \cdot 10^{-148}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{elif}\;c \leq 3.7 \cdot 10^{-75}:\\ \;\;\;\;\frac{t_1}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \left(b - \frac{d}{\mathsf{hypot}\left(c, d\right)} \cdot a\right)\\ \end{array} \]
Alternative 4
Error9.6
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)}\\ \mathbf{if}\;c \leq -6.8 \cdot 10^{+156}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;c \leq -4.6 \cdot 10^{-141}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.18 \cdot 10^{-291}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{elif}\;c \leq 2 \cdot 10^{+96}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(b - \frac{d}{\mathsf{hypot}\left(c, d\right)} \cdot a\right)\\ \end{array} \]
Alternative 5
Error12.3
Cost7632
\[\begin{array}{l} t_0 := \frac{c \cdot b - d \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -8.5 \cdot 10^{+48}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;c \leq -1.65 \cdot 10^{-140}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 4 \cdot 10^{-151}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{elif}\;c \leq 1.9 \cdot 10^{+57}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{a}{\frac{c}{d}} - b}{-\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]
Alternative 6
Error12.2
Cost7632
\[\begin{array}{l} t_0 := \frac{c \cdot b - d \cdot a}{c \cdot c + d \cdot d}\\ t_1 := \frac{a}{\frac{c}{d}} - b\\ \mathbf{if}\;c \leq -8.5 \cdot 10^{+48}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot t_1\\ \mathbf{elif}\;c \leq -8.5 \cdot 10^{-141}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 4.5 \cdot 10^{-148}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{elif}\;c \leq 2.4 \cdot 10^{+57}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{t_1}{-\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]
Alternative 7
Error12.3
Cost7568
\[\begin{array}{l} t_0 := \frac{c \cdot b - d \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -2.2 \cdot 10^{+48}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;c \leq -2.15 \cdot 10^{-141}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 3.1 \cdot 10^{-149}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{elif}\;c \leq 2.4 \cdot 10^{+57}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b - d \cdot \frac{a}{c}}{\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]
Alternative 8
Error12.4
Cost1488
\[\begin{array}{l} t_0 := \frac{c \cdot b - d \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -8.5 \cdot 10^{+48}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;c \leq -4.6 \cdot 10^{-141}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 3.5 \cdot 10^{-149}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{elif}\;c \leq 2.3 \cdot 10^{+57}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c} - \frac{\frac{a}{c}}{\frac{c}{d}}\\ \end{array} \]
Alternative 9
Error20.4
Cost1232
\[\begin{array}{l} t_0 := \frac{-a}{d}\\ \mathbf{if}\;c \leq -7 \cdot 10^{-68}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;c \leq 3.2 \cdot 10^{-147}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 5.3 \cdot 10^{-120}:\\ \;\;\;\;b \cdot \frac{c}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 5 \cdot 10^{+54}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{c} \cdot \left(b - \frac{d}{\frac{c}{a}}\right)\\ \end{array} \]
Alternative 10
Error20.0
Cost968
\[\begin{array}{l} \mathbf{if}\;c \leq -1.28 \cdot 10^{-67}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;c \leq 1.2 \cdot 10^{+56}:\\ \;\;\;\;\frac{-a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{c} \cdot \left(b - \frac{d}{\frac{c}{a}}\right)\\ \end{array} \]
Alternative 11
Error16.6
Cost968
\[\begin{array}{l} \mathbf{if}\;c \leq -1.4 \cdot 10^{-68}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;c \leq 6.5 \cdot 10^{+54}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{c} \cdot \left(b - \frac{d}{\frac{c}{a}}\right)\\ \end{array} \]
Alternative 12
Error16.7
Cost968
\[\begin{array}{l} \mathbf{if}\;c \leq -1.28 \cdot 10^{-67}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;c \leq 6 \cdot 10^{+54}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \end{array} \]
Alternative 13
Error16.7
Cost968
\[\begin{array}{l} \mathbf{if}\;c \leq -1.36 \cdot 10^{-68}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;c \leq 1.1 \cdot 10^{+55}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c} - \frac{\frac{a}{c}}{\frac{c}{d}}\\ \end{array} \]
Alternative 14
Error20.0
Cost841
\[\begin{array}{l} \mathbf{if}\;c \leq -1.15 \cdot 10^{-67} \lor \neg \left(c \leq 1.06 \cdot 10^{+56}\right):\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{-a}{d}\\ \end{array} \]
Alternative 15
Error23.3
Cost520
\[\begin{array}{l} \mathbf{if}\;c \leq -1.15 \cdot 10^{+42}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 4.4 \cdot 10^{+56}:\\ \;\;\;\;\frac{-a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 16
Error55.3
Cost456
\[\begin{array}{l} \mathbf{if}\;c \leq -4.4 \cdot 10^{+133}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 2.2 \cdot 10^{+171}:\\ \;\;\;\;\frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 17
Error34.8
Cost456
\[\begin{array}{l} \mathbf{if}\;d \leq -4.5 \cdot 10^{+141}:\\ \;\;\;\;\frac{a}{d}\\ \mathbf{elif}\;d \leq 1.96 \cdot 10^{+214}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{d}\\ \end{array} \]
Alternative 18
Error58.8
Cost192
\[\frac{a}{c} \]

Error

Reproduce

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