?

Average Error: 26.3 → 1.1
Time: 20.6s
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

Original26.3
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 26.3

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

    \[\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
Error12.6
Cost14560
\[\begin{array}{l} t_0 := c \cdot b - d \cdot a\\ t_1 := c \cdot c + d \cdot d\\ t_2 := \frac{c \cdot \frac{b}{d} - a}{d}\\ t_3 := \frac{b - \frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{if}\;d \leq -2.25 \cdot 10^{+157}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;d \leq -6.8 \cdot 10^{-107}:\\ \;\;\;\;\frac{t_0}{t_1}\\ \mathbf{elif}\;d \leq 1.55 \cdot 10^{-118}:\\ \;\;\;\;\mathsf{fma}\left(-1, \frac{\frac{a}{\frac{c}{d}}}{c}, \frac{b}{c}\right)\\ \mathbf{elif}\;d \leq 2.25 \cdot 10^{+35}:\\ \;\;\;\;\frac{\mathsf{fma}\left(c, b, d \cdot \left(-a\right)\right)}{t_1}\\ \mathbf{elif}\;d \leq 1.1 \cdot 10^{+46}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;d \leq 3.2 \cdot 10^{+68}:\\ \;\;\;\;\frac{t_0}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\ \mathbf{elif}\;d \leq 3.1 \cdot 10^{+91}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{elif}\;d \leq 4.6 \cdot 10^{+145}:\\ \;\;\;\;t_3\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 2
Error9.9
Cost14552
\[\begin{array}{l} t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right)}\\ t_1 := t_0 \cdot \frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{if}\;d \leq -4 \cdot 10^{+167}:\\ \;\;\;\;\frac{c \cdot \frac{b}{d} - a}{d}\\ \mathbf{elif}\;d \leq -2.5 \cdot 10^{-111}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 8.8 \cdot 10^{-116}:\\ \;\;\;\;\mathsf{fma}\left(-1, \frac{\frac{a}{\frac{c}{d}}}{c}, \frac{b}{c}\right)\\ \mathbf{elif}\;d \leq 2.1 \cdot 10^{+36}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 3.6 \cdot 10^{+57}:\\ \;\;\;\;\frac{b - \frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \leq 5 \cdot 10^{+91}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \left(\frac{c}{\frac{d}{b}} - a\right)\\ \end{array} \]
Alternative 3
Error12.2
Cost7828
\[\begin{array}{l} t_0 := \frac{c \cdot b - d \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -2.25 \cdot 10^{+157}:\\ \;\;\;\;\frac{c \cdot \frac{b}{d} - a}{d}\\ \mathbf{elif}\;d \leq -4.3 \cdot 10^{-110}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.28 \cdot 10^{-117}:\\ \;\;\;\;\mathsf{fma}\left(-1, \frac{\frac{a}{\frac{c}{d}}}{c}, \frac{b}{c}\right)\\ \mathbf{elif}\;d \leq 3.2 \cdot 10^{+68}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.1 \cdot 10^{+91}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{c}{\frac{d}{b}} - a\right)\\ \end{array} \]
Alternative 4
Error12.2
Cost7828
\[\begin{array}{l} t_0 := c \cdot b - d \cdot a\\ \mathbf{if}\;d \leq -2.25 \cdot 10^{+157}:\\ \;\;\;\;\frac{c \cdot \frac{b}{d} - a}{d}\\ \mathbf{elif}\;d \leq -6.5 \cdot 10^{-109}:\\ \;\;\;\;\frac{t_0}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 7.5 \cdot 10^{-119}:\\ \;\;\;\;\mathsf{fma}\left(-1, \frac{\frac{a}{\frac{c}{d}}}{c}, \frac{b}{c}\right)\\ \mathbf{elif}\;d \leq 3.2 \cdot 10^{+68}:\\ \;\;\;\;\frac{t_0}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\ \mathbf{elif}\;d \leq 3.1 \cdot 10^{+91}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{c}{\frac{d}{b}} - a\right)\\ \end{array} \]
Alternative 5
Error12.5
Cost7500
\[\begin{array}{l} t_0 := \frac{c \cdot b - d \cdot a}{c \cdot c + d \cdot d}\\ t_1 := \frac{c \cdot \frac{b}{d} - a}{d}\\ \mathbf{if}\;d \leq -2.25 \cdot 10^{+157}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -4.4 \cdot 10^{-110}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 2.7 \cdot 10^{-117}:\\ \;\;\;\;\mathsf{fma}\left(-1, \frac{\frac{a}{\frac{c}{d}}}{c}, \frac{b}{c}\right)\\ \mathbf{elif}\;d \leq 3.2 \cdot 10^{+68}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 4.2 \cdot 10^{+103}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 6
Error19.2
Cost1632
\[\begin{array}{l} t_0 := \frac{b}{c} - d \cdot \frac{a}{c \cdot c}\\ t_1 := \frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \mathbf{if}\;d \leq -1.8 \cdot 10^{+97}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -750000000000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -1 \cdot 10^{-44}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;d \leq -7.5 \cdot 10^{-189}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.2 \cdot 10^{-190}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;d \leq 1.75 \cdot 10^{-42}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.2 \cdot 10^{+68}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 3.6 \cdot 10^{+103}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot \frac{b}{d} - a}{d}\\ \end{array} \]
Alternative 7
Error17.3
Cost1496
\[\begin{array}{l} t_0 := \frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ t_1 := \frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \mathbf{if}\;d \leq -1.8 \cdot 10^{+97}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -51000000000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -1.8 \cdot 10^{-42}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;d \leq 3.6 \cdot 10^{-45}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.2 \cdot 10^{+68}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 3.6 \cdot 10^{+103}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot \frac{b}{d} - a}{d}\\ \end{array} \]
Alternative 8
Error17.3
Cost1496
\[\begin{array}{l} t_0 := \frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{if}\;d \leq -1.8 \cdot 10^{+97}:\\ \;\;\;\;\left(\frac{c}{\frac{d}{b}} - a\right) \cdot \frac{1}{d}\\ \mathbf{elif}\;d \leq -17000000000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -7.2 \cdot 10^{-50}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;d \leq 1.75 \cdot 10^{-42}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.75 \cdot 10^{+68}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \mathbf{elif}\;d \leq 3.6 \cdot 10^{+103}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot \frac{b}{d} - a}{d}\\ \end{array} \]
Alternative 9
Error12.9
Cost1488
\[\begin{array}{l} t_0 := \frac{c \cdot b - d \cdot a}{c \cdot c + d \cdot d}\\ t_1 := \frac{c \cdot \frac{b}{d} - a}{d}\\ t_2 := \frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{if}\;d \leq -2.25 \cdot 10^{+157}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -1.45 \cdot 10^{-142}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.6 \cdot 10^{-121}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;d \leq 3.2 \cdot 10^{+68}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.6 \cdot 10^{+103}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 10
Error20.8
Cost1371
\[\begin{array}{l} \mathbf{if}\;d \leq -1.8 \cdot 10^{+97} \lor \neg \left(d \leq -40000000000 \lor \neg \left(d \leq -1.7 \cdot 10^{-28}\right) \land \left(d \leq 1.55 \cdot 10^{-53} \lor \neg \left(d \leq 3.2 \cdot 10^{+68}\right) \land d \leq 3.6 \cdot 10^{+103}\right)\right):\\ \;\;\;\;\frac{c \cdot \frac{b}{d} - a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 11
Error20.8
Cost1369
\[\begin{array}{l} t_0 := \frac{c \cdot \frac{b}{d} - a}{d}\\ \mathbf{if}\;d \leq -1.8 \cdot 10^{+97}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -490000000000:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;d \leq -3 \cdot 10^{-43}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;d \leq 2.8 \cdot 10^{-54} \lor \neg \left(d \leq 3.2 \cdot 10^{+68}\right) \land d \leq 3.6 \cdot 10^{+103}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 12
Error20.9
Cost1368
\[\begin{array}{l} t_0 := \frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \mathbf{if}\;d \leq -1.8 \cdot 10^{+97}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -850000000000:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;d \leq -9.5 \cdot 10^{-43}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;d \leq 3.4 \cdot 10^{-54}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;d \leq 3.2 \cdot 10^{+68}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.6 \cdot 10^{+103}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{c \cdot \frac{b}{d} - a}{d}\\ \end{array} \]
Alternative 13
Error24.7
Cost786
\[\begin{array}{l} \mathbf{if}\;d \leq -1.8 \cdot 10^{+97} \lor \neg \left(d \leq 4.1 \cdot 10^{-53} \lor \neg \left(d \leq 100000000\right) \land d \leq 3.6 \cdot 10^{+103}\right):\\ \;\;\;\;\frac{-a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 14
Error36.2
Cost324
\[\begin{array}{l} \mathbf{if}\;d \leq -1.4 \cdot 10^{+131}:\\ \;\;\;\;\frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 15
Error57.0
Cost192
\[\frac{a}{d} \]

Error

Reproduce?

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