?

Average Error: 27.0 → 10.7
Time: 13.9s
Precision: binary64
Cost: 14288

?

\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\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 -2.2 \cdot 10^{+151}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{d}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \leq -1.55 \cdot 10^{-121}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 1.08 \cdot 10^{-153}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{elif}\;d \leq 3.1 \cdot 10^{+118}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \left(\frac{c}{\frac{d}{b}} - a\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 (/ 1.0 (hypot c d)))
        (t_1 (* t_0 (/ (- (* c b) (* d a)) (hypot c d)))))
   (if (<= d -2.2e+151)
     (* (/ (- a) (hypot d c)) (/ d (hypot d c)))
     (if (<= d -1.55e-121)
       t_1
       (if (<= d 1.08e-153)
         (- (/ b c) (* (/ d c) (/ a c)))
         (if (<= d 3.1e+118) t_1 (* t_0 (- (/ c (/ d b)) 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) {
	double t_0 = 1.0 / hypot(c, d);
	double t_1 = t_0 * (((c * b) - (d * a)) / hypot(c, d));
	double tmp;
	if (d <= -2.2e+151) {
		tmp = (-a / hypot(d, c)) * (d / hypot(d, c));
	} else if (d <= -1.55e-121) {
		tmp = t_1;
	} else if (d <= 1.08e-153) {
		tmp = (b / c) - ((d / c) * (a / c));
	} else if (d <= 3.1e+118) {
		tmp = t_1;
	} else {
		tmp = t_0 * ((c / (d / b)) - a);
	}
	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 = 1.0 / Math.hypot(c, d);
	double t_1 = t_0 * (((c * b) - (d * a)) / Math.hypot(c, d));
	double tmp;
	if (d <= -2.2e+151) {
		tmp = (-a / Math.hypot(d, c)) * (d / Math.hypot(d, c));
	} else if (d <= -1.55e-121) {
		tmp = t_1;
	} else if (d <= 1.08e-153) {
		tmp = (b / c) - ((d / c) * (a / c));
	} else if (d <= 3.1e+118) {
		tmp = t_1;
	} else {
		tmp = t_0 * ((c / (d / b)) - a);
	}
	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 = 1.0 / math.hypot(c, d)
	t_1 = t_0 * (((c * b) - (d * a)) / math.hypot(c, d))
	tmp = 0
	if d <= -2.2e+151:
		tmp = (-a / math.hypot(d, c)) * (d / math.hypot(d, c))
	elif d <= -1.55e-121:
		tmp = t_1
	elif d <= 1.08e-153:
		tmp = (b / c) - ((d / c) * (a / c))
	elif d <= 3.1e+118:
		tmp = t_1
	else:
		tmp = t_0 * ((c / (d / b)) - a)
	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(1.0 / hypot(c, d))
	t_1 = Float64(t_0 * Float64(Float64(Float64(c * b) - Float64(d * a)) / hypot(c, d)))
	tmp = 0.0
	if (d <= -2.2e+151)
		tmp = Float64(Float64(Float64(-a) / hypot(d, c)) * Float64(d / hypot(d, c)));
	elseif (d <= -1.55e-121)
		tmp = t_1;
	elseif (d <= 1.08e-153)
		tmp = Float64(Float64(b / c) - Float64(Float64(d / c) * Float64(a / c)));
	elseif (d <= 3.1e+118)
		tmp = t_1;
	else
		tmp = Float64(t_0 * Float64(Float64(c / Float64(d / b)) - a));
	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 = 1.0 / hypot(c, d);
	t_1 = t_0 * (((c * b) - (d * a)) / hypot(c, d));
	tmp = 0.0;
	if (d <= -2.2e+151)
		tmp = (-a / hypot(d, c)) * (d / hypot(d, c));
	elseif (d <= -1.55e-121)
		tmp = t_1;
	elseif (d <= 1.08e-153)
		tmp = (b / c) - ((d / c) * (a / c));
	elseif (d <= 3.1e+118)
		tmp = t_1;
	else
		tmp = t_0 * ((c / (d / b)) - a);
	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[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(t$95$0 * N[(N[(N[(c * b), $MachinePrecision] - N[(d * a), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -2.2e+151], N[(N[((-a) / N[Sqrt[d ^ 2 + c ^ 2], $MachinePrecision]), $MachinePrecision] * N[(d / N[Sqrt[d ^ 2 + c ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, -1.55e-121], t$95$1, If[LessEqual[d, 1.08e-153], N[(N[(b / c), $MachinePrecision] - N[(N[(d / c), $MachinePrecision] * N[(a / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 3.1e+118], t$95$1, N[(t$95$0 * N[(N[(c / N[(d / b), $MachinePrecision]), $MachinePrecision] - a), $MachinePrecision]), $MachinePrecision]]]]]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\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 -2.2 \cdot 10^{+151}:\\
\;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{d}{\mathsf{hypot}\left(d, c\right)}\\

\mathbf{elif}\;d \leq -1.55 \cdot 10^{-121}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;d \leq 1.08 \cdot 10^{-153}:\\
\;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\

\mathbf{elif}\;d \leq 3.1 \cdot 10^{+118}:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;t_0 \cdot \left(\frac{c}{\frac{d}{b}} - a\right)\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original27.0
Target0.5
Herbie10.7
\[\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 4 regimes
  2. if d < -2.20000000000000007e151

    1. Initial program 45.1

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Taylor expanded in b around 0 45.0

      \[\leadsto \color{blue}{-1 \cdot \frac{a \cdot d}{{d}^{2} + {c}^{2}}} \]
    3. Simplified44.0

      \[\leadsto \color{blue}{\frac{a}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{-d}}} \]
      Proof

      [Start]45.0

      \[ -1 \cdot \frac{a \cdot d}{{d}^{2} + {c}^{2}} \]

      associate-*r/ [=>]45.0

      \[ \color{blue}{\frac{-1 \cdot \left(a \cdot d\right)}{{d}^{2} + {c}^{2}}} \]

      mul-1-neg [=>]45.0

      \[ \frac{\color{blue}{-a \cdot d}}{{d}^{2} + {c}^{2}} \]

      distribute-rgt-neg-out [<=]45.0

      \[ \frac{\color{blue}{a \cdot \left(-d\right)}}{{d}^{2} + {c}^{2}} \]

      associate-/l* [=>]44.0

      \[ \color{blue}{\frac{a}{\frac{{d}^{2} + {c}^{2}}{-d}}} \]

      unpow2 [=>]44.0

      \[ \frac{a}{\frac{\color{blue}{d \cdot d} + {c}^{2}}{-d}} \]

      unpow2 [=>]44.0

      \[ \frac{a}{\frac{d \cdot d + \color{blue}{c \cdot c}}{-d}} \]

      fma-udef [<=]44.0

      \[ \frac{a}{\frac{\color{blue}{\mathsf{fma}\left(d, d, c \cdot c\right)}}{-d}} \]
    4. Applied egg-rr10.8

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

    if -2.20000000000000007e151 < d < -1.5499999999999999e-121 or 1.07999999999999996e-153 < d < 3.09999999999999986e118

    1. Initial program 17.3

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Applied egg-rr12.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)}} \]

    if -1.5499999999999999e-121 < d < 1.07999999999999996e-153

    1. Initial program 24.1

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Taylor expanded in c around inf 10.2

      \[\leadsto \color{blue}{-1 \cdot \frac{a \cdot d}{{c}^{2}} + \frac{b}{c}} \]
    3. Simplified9.1

      \[\leadsto \color{blue}{\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}} \]
      Proof

      [Start]10.2

      \[ -1 \cdot \frac{a \cdot d}{{c}^{2}} + \frac{b}{c} \]

      +-commutative [=>]10.2

      \[ \color{blue}{\frac{b}{c} + -1 \cdot \frac{a \cdot d}{{c}^{2}}} \]

      mul-1-neg [=>]10.2

      \[ \frac{b}{c} + \color{blue}{\left(-\frac{a \cdot d}{{c}^{2}}\right)} \]

      unsub-neg [=>]10.2

      \[ \color{blue}{\frac{b}{c} - \frac{a \cdot d}{{c}^{2}}} \]

      *-commutative [=>]10.2

      \[ \frac{b}{c} - \frac{\color{blue}{d \cdot a}}{{c}^{2}} \]

      unpow2 [=>]10.2

      \[ \frac{b}{c} - \frac{d \cdot a}{\color{blue}{c \cdot c}} \]

      times-frac [=>]9.1

      \[ \frac{b}{c} - \color{blue}{\frac{d}{c} \cdot \frac{a}{c}} \]

    if 3.09999999999999986e118 < d

    1. Initial program 42.0

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Applied egg-rr27.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. Taylor expanded in c around 0 13.1

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

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

      [Start]13.1

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

      +-commutative [=>]13.1

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

      mul-1-neg [=>]13.1

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

      associate-/l* [=>]8.4

      \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\left(-a\right) + \color{blue}{\frac{c}{\frac{d}{b}}}\right) \]
  3. Recombined 4 regimes into one program.
  4. Final simplification10.7

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -2.2 \cdot 10^{+151}:\\ \;\;\;\;\frac{-a}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{d}{\mathsf{hypot}\left(d, c\right)}\\ \mathbf{elif}\;d \leq -1.55 \cdot 10^{-121}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;d \leq 1.08 \cdot 10^{-153}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{elif}\;d \leq 3.1 \cdot 10^{+118}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{c}{\frac{d}{b}} - a\right)\\ \end{array} \]

Alternatives

Alternative 1
Error12.9
Cost1736
\[\begin{array}{l} t_0 := d \cdot d + c \cdot c\\ \mathbf{if}\;c \leq -1.45 \cdot 10^{+144}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{elif}\;c \leq -1.95 \cdot 10^{-32}:\\ \;\;\;\;b \cdot \frac{c}{t_0} - \frac{d \cdot a}{t_0}\\ \mathbf{elif}\;c \leq 2.7 \cdot 10^{-114}:\\ \;\;\;\;b \cdot \frac{c}{d \cdot d} - \frac{a}{d}\\ \mathbf{elif}\;c \leq 1.05 \cdot 10^{+44}:\\ \;\;\;\;\frac{c \cdot b - d \cdot a}{t_0}\\ \mathbf{else}:\\ \;\;\;\;\frac{b - \frac{d}{\frac{c}{a}}}{c}\\ \end{array} \]
Alternative 2
Error14.8
Cost1488
\[\begin{array}{l} t_0 := d \cdot d + c \cdot c\\ \mathbf{if}\;c \leq -1 \cdot 10^{+144}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{elif}\;c \leq -820000:\\ \;\;\;\;b \cdot \frac{c}{t_0}\\ \mathbf{elif}\;c \leq 10^{-122}:\\ \;\;\;\;b \cdot \frac{c}{d \cdot d} - \frac{a}{d}\\ \mathbf{elif}\;c \leq 1.65 \cdot 10^{+43}:\\ \;\;\;\;\frac{c \cdot b - d \cdot a}{t_0}\\ \mathbf{else}:\\ \;\;\;\;\frac{b - \frac{d}{\frac{c}{a}}}{c}\\ \end{array} \]
Alternative 3
Error17.6
Cost1300
\[\begin{array}{l} t_0 := b \cdot \frac{c}{d \cdot d + c \cdot c}\\ t_1 := \frac{b - \frac{d}{\frac{c}{a}}}{c}\\ t_2 := \frac{a}{\left(-d\right) - \frac{c \cdot c}{d}}\\ \mathbf{if}\;c \leq -1.1 \cdot 10^{+144}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -7800000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 2.8 \cdot 10^{-107}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;c \leq 9 \cdot 10^{-53}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 2.95 \cdot 10^{+31}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 4
Error16.5
Cost1100
\[\begin{array}{l} t_0 := \frac{b - \frac{d}{\frac{c}{a}}}{c}\\ \mathbf{if}\;c \leq -1.02 \cdot 10^{+144}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -2500000000:\\ \;\;\;\;b \cdot \frac{c}{d \cdot d + c \cdot c}\\ \mathbf{elif}\;c \leq 2.95 \cdot 10^{+31}:\\ \;\;\;\;b \cdot \frac{c}{d \cdot d} - \frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Error16.5
Cost1100
\[\begin{array}{l} \mathbf{if}\;c \leq -4 \cdot 10^{+144}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{elif}\;c \leq -190000000:\\ \;\;\;\;b \cdot \frac{c}{d \cdot d + c \cdot c}\\ \mathbf{elif}\;c \leq 1.15 \cdot 10^{+32}:\\ \;\;\;\;b \cdot \frac{c}{d \cdot d} - \frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b - \frac{d}{\frac{c}{a}}}{c}\\ \end{array} \]
Alternative 6
Error16.9
Cost905
\[\begin{array}{l} \mathbf{if}\;c \leq -760000000 \lor \neg \left(c \leq 1.08 \cdot 10^{+33}\right):\\ \;\;\;\;\frac{b - \frac{d}{\frac{c}{a}}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\left(-d\right) - \frac{c \cdot c}{d}}\\ \end{array} \]
Alternative 7
Error18.9
Cost841
\[\begin{array}{l} \mathbf{if}\;c \leq -680000 \lor \neg \left(c \leq 0.12\right):\\ \;\;\;\;\frac{b - \frac{d}{\frac{c}{a}}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{-a}{d}\\ \end{array} \]
Alternative 8
Error23.0
Cost520
\[\begin{array}{l} \mathbf{if}\;c \leq -100000000:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 7.9 \cdot 10^{+31}:\\ \;\;\;\;\frac{-a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 9
Error34.9
Cost456
\[\begin{array}{l} \mathbf{if}\;d \leq -8.5 \cdot 10^{+199}:\\ \;\;\;\;\frac{a}{d}\\ \mathbf{elif}\;d \leq 5.5 \cdot 10^{+160}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{d}\\ \end{array} \]
Alternative 10
Error56.8
Cost192
\[\frac{a}{d} \]

Error

Reproduce?

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