?

Average Error: 41.18% → 1.01%
Time: 17.5s
Precision: binary64
Cost: 22409

?

\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := c \cdot b - d \cdot a\\ t_1 := \frac{t_0}{c \cdot c + d \cdot d}\\ \mathbf{if}\;t_1 \leq 0 \lor \neg \left(t_1 \leq 5 \cdot 10^{+307}\right):\\ \;\;\;\;\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)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{t_0}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (- (* c b) (* d a))) (t_1 (/ t_0 (+ (* c c) (* d d)))))
   (if (or (<= t_1 0.0) (not (<= t_1 5e+307)))
     (*
      (/ 1.0 (hypot c d))
      (- (* c (/ b (hypot c d))) (* (/ d (hypot c d)) a)))
     (/ (/ t_0 (hypot c d)) (hypot c d)))))
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) {
	double t_0 = (c * b) - (d * a);
	double t_1 = t_0 / ((c * c) + (d * d));
	double tmp;
	if ((t_1 <= 0.0) || !(t_1 <= 5e+307)) {
		tmp = (1.0 / hypot(c, d)) * ((c * (b / hypot(c, d))) - ((d / hypot(c, d)) * a));
	} else {
		tmp = (t_0 / hypot(c, d)) / hypot(c, d);
	}
	return tmp;
}
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) {
	double t_0 = (c * b) - (d * a);
	double t_1 = t_0 / ((c * c) + (d * d));
	double tmp;
	if ((t_1 <= 0.0) || !(t_1 <= 5e+307)) {
		tmp = (1.0 / Math.hypot(c, d)) * ((c * (b / Math.hypot(c, d))) - ((d / Math.hypot(c, d)) * a));
	} else {
		tmp = (t_0 / Math.hypot(c, d)) / Math.hypot(c, d);
	}
	return tmp;
}
def code(a, b, c, d):
	return ((b * c) - (a * d)) / ((c * c) + (d * d))
def code(a, b, c, d):
	t_0 = (c * b) - (d * a)
	t_1 = t_0 / ((c * c) + (d * d))
	tmp = 0
	if (t_1 <= 0.0) or not (t_1 <= 5e+307):
		tmp = (1.0 / math.hypot(c, d)) * ((c * (b / math.hypot(c, d))) - ((d / math.hypot(c, d)) * a))
	else:
		tmp = (t_0 / math.hypot(c, d)) / math.hypot(c, d)
	return tmp
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)
	t_0 = Float64(Float64(c * b) - Float64(d * a))
	t_1 = Float64(t_0 / Float64(Float64(c * c) + Float64(d * d)))
	tmp = 0.0
	if ((t_1 <= 0.0) || !(t_1 <= 5e+307))
		tmp = Float64(Float64(1.0 / hypot(c, d)) * Float64(Float64(c * Float64(b / hypot(c, d))) - Float64(Float64(d / hypot(c, d)) * a)));
	else
		tmp = Float64(Float64(t_0 / hypot(c, d)) / hypot(c, d));
	end
	return tmp
end
function tmp = code(a, b, c, d)
	tmp = ((b * c) - (a * d)) / ((c * c) + (d * d));
end
function tmp_2 = code(a, b, c, d)
	t_0 = (c * b) - (d * a);
	t_1 = t_0 / ((c * c) + (d * d));
	tmp = 0.0;
	if ((t_1 <= 0.0) || ~((t_1 <= 5e+307)))
		tmp = (1.0 / hypot(c, d)) * ((c * (b / hypot(c, d))) - ((d / hypot(c, d)) * a));
	else
		tmp = (t_0 / hypot(c, d)) / hypot(c, d);
	end
	tmp_2 = tmp;
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_] := Block[{t$95$0 = N[(N[(c * b), $MachinePrecision] - N[(d * a), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(t$95$0 / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$1, 0.0], N[Not[LessEqual[t$95$1, 5e+307]], $MachinePrecision]], 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], N[(N[(t$95$0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := c \cdot b - d \cdot a\\
t_1 := \frac{t_0}{c \cdot c + d \cdot d}\\
\mathbf{if}\;t_1 \leq 0 \lor \neg \left(t_1 \leq 5 \cdot 10^{+307}\right):\\
\;\;\;\;\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)\\

\mathbf{else}:\\
\;\;\;\;\frac{\frac{t_0}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original41.18%
Target0.58%
Herbie1.01%
\[\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. Split input into 2 regimes
  2. if (/.f64 (-.f64 (*.f64 b c) (*.f64 a d)) (+.f64 (*.f64 c c) (*.f64 d d))) < -0.0 or 5e307 < (/.f64 (-.f64 (*.f64 b c) (*.f64 a d)) (+.f64 (*.f64 c c) (*.f64 d d)))

    1. Initial program 52.04

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

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

      \[\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)} \]

    if -0.0 < (/.f64 (-.f64 (*.f64 b c) (*.f64 a d)) (+.f64 (*.f64 c c) (*.f64 d d))) < 5e307

    1. Initial program 1.16

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

      \[\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-rr0.46

      \[\leadsto \color{blue}{\frac{\frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification1.01

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{c \cdot b - d \cdot a}{c \cdot c + d \cdot d} \leq 0 \lor \neg \left(\frac{c \cdot b - d \cdot a}{c \cdot c + d \cdot d} \leq 5 \cdot 10^{+307}\right):\\ \;\;\;\;\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)\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]

Alternatives

Alternative 1
Error0.87%
Cost20416
\[\left(\frac{d}{\mathsf{hypot}\left(c, d\right)} \cdot a + \frac{c}{-\mathsf{hypot}\left(c, d\right)} \cdot b\right) \cdot \frac{-1}{\mathsf{hypot}\left(c, d\right)} \]
Alternative 2
Error17.32%
Cost17744
\[\begin{array}{l} t_0 := \frac{c}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{b}{\mathsf{hypot}\left(d, c\right)}\\ t_1 := \frac{c \cdot b - d \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;t_1 \leq -\infty:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_1 \leq -5 \cdot 10^{-316}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_1 \leq 0:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_1 \leq 5 \cdot 10^{+307}:\\ \;\;\;\;t_1\\ \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 3
Error11.16%
Cost15688
\[\begin{array}{l} t_0 := c \cdot b - d \cdot a\\ t_1 := \frac{t_0}{c \cdot c + d \cdot d}\\ \mathbf{if}\;t_1 \leq -\infty:\\ \;\;\;\;\frac{c}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{b}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;t_1 \leq 5 \cdot 10^{+307}:\\ \;\;\;\;\frac{\frac{t_0}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \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 4
Error10.65%
Cost15688
\[\begin{array}{l} t_0 := c \cdot b - d \cdot a\\ t_1 := \frac{t_0}{c \cdot c + d \cdot d}\\ \mathbf{if}\;t_1 \leq -\infty:\\ \;\;\;\;\left(a + \frac{c}{-\mathsf{hypot}\left(c, d\right)} \cdot b\right) \cdot \frac{-1}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;t_1 \leq 5 \cdot 10^{+307}:\\ \;\;\;\;\frac{\frac{t_0}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \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
Error20.13%
Cost13904
\[\begin{array}{l} t_0 := \frac{c}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{b}{\mathsf{hypot}\left(d, c\right)}\\ t_1 := c \cdot c + d \cdot d\\ t_2 := \frac{c \cdot b - d \cdot a}{t_1}\\ \mathbf{if}\;d \leq -6.8 \cdot 10^{+142}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \mathbf{elif}\;d \leq -3.55 \cdot 10^{+75}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -7.2 \cdot 10^{+32}:\\ \;\;\;\;\frac{-a}{\frac{t_1}{d}}\\ \mathbf{elif}\;d \leq -4.4 \cdot 10^{-7}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -1.55 \cdot 10^{-158}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;d \leq 2.8 \cdot 10^{-105}:\\ \;\;\;\;\frac{b - \frac{a}{\frac{c}{d}}}{c}\\ \mathbf{elif}\;d \leq 2.5 \cdot 10^{+89}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{c}{\frac{d}{b}} - a\right)\\ \end{array} \]
Alternative 6
Error18.23%
Cost7696
\[\begin{array}{l} t_0 := \frac{c \cdot b - d \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -6.6 \cdot 10^{+89}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \mathbf{elif}\;d \leq -1.5 \cdot 10^{-158}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 9.8 \cdot 10^{-104}:\\ \;\;\;\;\frac{b - \frac{a}{\frac{c}{d}}}{c}\\ \mathbf{elif}\;d \leq 5.3 \cdot 10^{+94}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{c}{\frac{d}{b}} - a\right)\\ \end{array} \]
Alternative 7
Error18.32%
Cost1488
\[\begin{array}{l} t_0 := \frac{c \cdot b - d \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -3.4 \cdot 10^{+89}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \mathbf{elif}\;d \leq -1.46 \cdot 10^{-158}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 4.4 \cdot 10^{-107}:\\ \;\;\;\;\frac{b - \frac{a}{\frac{c}{d}}}{c}\\ \mathbf{elif}\;d \leq 2.2 \cdot 10^{+95}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\left(\frac{c}{\frac{d}{b}} - a\right) \cdot \frac{1}{d}\\ \end{array} \]
Alternative 8
Error25.77%
Cost1234
\[\begin{array}{l} \mathbf{if}\;c \leq -2.65 \cdot 10^{-13} \lor \neg \left(c \leq 3.6 \cdot 10^{-92} \lor \neg \left(c \leq 0.7\right) \land c \leq 1.25 \cdot 10^{+43}\right):\\ \;\;\;\;\frac{b - d \cdot \frac{a}{c}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \end{array} \]
Alternative 9
Error25.48%
Cost1233
\[\begin{array}{l} t_0 := \frac{b - d \cdot \frac{a}{c}}{c}\\ \mathbf{if}\;c \leq -9.5 \cdot 10^{-15}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 3.6 \cdot 10^{-92}:\\ \;\;\;\;\left(\frac{c}{\frac{d}{b}} - a\right) \cdot \frac{1}{d}\\ \mathbf{elif}\;c \leq 0.46 \lor \neg \left(c \leq 1.45 \cdot 10^{+40}\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \end{array} \]
Alternative 10
Error30.25%
Cost1106
\[\begin{array}{l} \mathbf{if}\;c \leq -1.4 \cdot 10^{-16} \lor \neg \left(c \leq 10^{-78}\right) \land \left(c \leq 0.185 \lor \neg \left(c \leq 7 \cdot 10^{+37}\right)\right):\\ \;\;\;\;\frac{b - d \cdot \frac{a}{c}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{-a}{d}\\ \end{array} \]
Alternative 11
Error37.44%
Cost1050
\[\begin{array}{l} t_0 := \frac{-a}{d}\\ \mathbf{if}\;d \leq -1.25 \cdot 10^{+90}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -1 \cdot 10^{+58}:\\ \;\;\;\;\frac{d}{c} \cdot \frac{a}{-c}\\ \mathbf{elif}\;d \leq -1.16 \cdot 10^{+31} \lor \neg \left(d \leq -1100 \lor \neg \left(d \leq -1.5 \cdot 10^{-93}\right) \land d \leq 2.65 \cdot 10^{-17}\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 12
Error37.56%
Cost1050
\[\begin{array}{l} t_0 := \frac{-a}{d}\\ \mathbf{if}\;d \leq -1.25 \cdot 10^{+90}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -1 \cdot 10^{+58}:\\ \;\;\;\;\frac{\frac{-a}{\frac{c}{d}}}{c}\\ \mathbf{elif}\;d \leq -4.5 \cdot 10^{+31} \lor \neg \left(d \leq -820\right) \land \left(d \leq -5.9 \cdot 10^{-92} \lor \neg \left(d \leq 2.05 \cdot 10^{-13}\right)\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 13
Error37.53%
Cost1050
\[\begin{array}{l} t_0 := \frac{-a}{d}\\ \mathbf{if}\;d \leq -1.25 \cdot 10^{+90}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -1 \cdot 10^{+58}:\\ \;\;\;\;\frac{a \cdot \frac{-d}{c}}{c}\\ \mathbf{elif}\;d \leq -4.5 \cdot 10^{+31} \lor \neg \left(d \leq -2050 \lor \neg \left(d \leq -3.15 \cdot 10^{-93}\right) \land d \leq 1.5 \cdot 10^{-13}\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 14
Error36.13%
Cost520
\[\begin{array}{l} \mathbf{if}\;c \leq -2 \cdot 10^{-19}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 1.5 \cdot 10^{+82}:\\ \;\;\;\;\frac{-a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 15
Error54.1%
Cost456
\[\begin{array}{l} \mathbf{if}\;d \leq -3.3 \cdot 10^{+181}:\\ \;\;\;\;\frac{a}{d}\\ \mathbf{elif}\;d \leq 4.5 \cdot 10^{+202}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{d}\\ \end{array} \]
Alternative 16
Error88.88%
Cost192
\[\frac{a}{d} \]

Error

Reproduce?

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