?

Average Error: 25.9 → 9.4
Time: 14.0s
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)}\\ t_2 := \frac{c}{\frac{d}{b}} - a\\ \mathbf{if}\;d \leq -8.8 \cdot 10^{+167}:\\ \;\;\;\;\frac{t_2}{d}\\ \mathbf{elif}\;d \leq -4.5 \cdot 10^{-125}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 3.6 \cdot 10^{-174}:\\ \;\;\;\;\mathsf{fma}\left(-1, \frac{\frac{a}{\frac{c}{d}}}{c}, \frac{b}{c}\right)\\ \mathbf{elif}\;d \leq 6 \cdot 10^{+115}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot t_2\\ \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))))
        (t_2 (- (/ c (/ d b)) a)))
   (if (<= d -8.8e+167)
     (/ t_2 d)
     (if (<= d -4.5e-125)
       t_1
       (if (<= d 3.6e-174)
         (fma -1.0 (/ (/ a (/ c d)) c) (/ b c))
         (if (<= d 6e+115) t_1 (* t_0 t_2)))))))
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 t_2 = (c / (d / b)) - a;
	double tmp;
	if (d <= -8.8e+167) {
		tmp = t_2 / d;
	} else if (d <= -4.5e-125) {
		tmp = t_1;
	} else if (d <= 3.6e-174) {
		tmp = fma(-1.0, ((a / (c / d)) / c), (b / c));
	} else if (d <= 6e+115) {
		tmp = t_1;
	} else {
		tmp = t_0 * t_2;
	}
	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)))
	t_2 = Float64(Float64(c / Float64(d / b)) - a)
	tmp = 0.0
	if (d <= -8.8e+167)
		tmp = Float64(t_2 / d);
	elseif (d <= -4.5e-125)
		tmp = t_1;
	elseif (d <= 3.6e-174)
		tmp = fma(-1.0, Float64(Float64(a / Float64(c / d)) / c), Float64(b / c));
	elseif (d <= 6e+115)
		tmp = t_1;
	else
		tmp = Float64(t_0 * t_2);
	end
	return 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]}, Block[{t$95$2 = N[(N[(c / N[(d / b), $MachinePrecision]), $MachinePrecision] - a), $MachinePrecision]}, If[LessEqual[d, -8.8e+167], N[(t$95$2 / d), $MachinePrecision], If[LessEqual[d, -4.5e-125], t$95$1, If[LessEqual[d, 3.6e-174], N[(-1.0 * N[(N[(a / N[(c / d), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision] + N[(b / c), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 6e+115], t$95$1, N[(t$95$0 * t$95$2), $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)}\\
t_2 := \frac{c}{\frac{d}{b}} - a\\
\mathbf{if}\;d \leq -8.8 \cdot 10^{+167}:\\
\;\;\;\;\frac{t_2}{d}\\

\mathbf{elif}\;d \leq -4.5 \cdot 10^{-125}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;d \leq 3.6 \cdot 10^{-174}:\\
\;\;\;\;\mathsf{fma}\left(-1, \frac{\frac{a}{\frac{c}{d}}}{c}, \frac{b}{c}\right)\\

\mathbf{elif}\;d \leq 6 \cdot 10^{+115}:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;t_0 \cdot t_2\\


\end{array}

Error?

Target

Original25.9
Target0.5
Herbie9.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. Split input into 4 regimes
  2. if d < -8.80000000000000013e167

    1. Initial program 44.7

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

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

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

      [Start]14.4

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

      +-commutative [=>]14.4

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

      mul-1-neg [=>]14.4

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

      unsub-neg [=>]14.4

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

      *-commutative [<=]14.4

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

      unpow2 [=>]14.4

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

      times-frac [=>]7.0

      \[ \color{blue}{\frac{b}{d} \cdot \frac{c}{d}} - \frac{a}{d} \]
    4. Taylor expanded in b around 0 14.4

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

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

      [Start]14.4

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

      +-commutative [=>]14.4

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

      *-commutative [=>]14.4

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

      unpow2 [=>]14.4

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

      associate-/l* [=>]13.0

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

      mul-1-neg [=>]13.0

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

      sub-neg [<=]13.0

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

      associate-/l* [<=]14.4

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

      associate-/r* [=>]11.5

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

      div-sub [<=]11.5

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

      *-commutative [<=]11.5

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

      associate-/l* [=>]6.5

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

    if -8.80000000000000013e167 < d < -4.50000000000000012e-125 or 3.59999999999999999e-174 < d < 6.0000000000000001e115

    1. Initial program 17.7

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

    if -4.50000000000000012e-125 < d < 3.59999999999999999e-174

    1. Initial program 23.1

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

      \[\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 inf 8.4

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

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

      [Start]8.4

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

      fma-def [=>]8.4

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

      unpow2 [=>]8.4

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

      associate-/r* [=>]5.3

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

      associate-/l* [=>]5.2

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

    if 6.0000000000000001e115 < d

    1. Initial program 40.9

      \[\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 14.9

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

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

      [Start]14.9

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

      +-commutative [=>]14.9

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

      mul-1-neg [=>]14.9

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

      associate-/l* [=>]9.8

      \[ \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 simplification9.4

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -8.8 \cdot 10^{+167}:\\ \;\;\;\;\frac{\frac{c}{\frac{d}{b}} - a}{d}\\ \mathbf{elif}\;d \leq -4.5 \cdot 10^{-125}:\\ \;\;\;\;\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 3.6 \cdot 10^{-174}:\\ \;\;\;\;\mathsf{fma}\left(-1, \frac{\frac{a}{\frac{c}{d}}}{c}, \frac{b}{c}\right)\\ \mathbf{elif}\;d \leq 6 \cdot 10^{+115}:\\ \;\;\;\;\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
Error11.9
Cost7696
\[\begin{array}{l} t_0 := \frac{c \cdot b - d \cdot a}{c \cdot c + d \cdot d}\\ t_1 := \frac{c}{\frac{d}{b}} - a\\ \mathbf{if}\;d \leq -2.55 \cdot 10^{+60}:\\ \;\;\;\;\frac{t_1}{d}\\ \mathbf{elif}\;d \leq -1.95 \cdot 10^{-60}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.2 \cdot 10^{-154}:\\ \;\;\;\;\mathsf{fma}\left(-1, \frac{\frac{a}{\frac{c}{d}}}{c}, \frac{b}{c}\right)\\ \mathbf{elif}\;d \leq 5.8 \cdot 10^{+115}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot t_1\\ \end{array} \]
Alternative 2
Error12.0
Cost7500
\[\begin{array}{l} t_0 := \frac{c \cdot b - d \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -2.55 \cdot 10^{+60}:\\ \;\;\;\;\frac{\frac{c}{\frac{d}{b}} - a}{d}\\ \mathbf{elif}\;d \leq -2.25 \cdot 10^{-60}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 4.9 \cdot 10^{-157}:\\ \;\;\;\;\mathsf{fma}\left(-1, \frac{\frac{a}{\frac{c}{d}}}{c}, \frac{b}{c}\right)\\ \mathbf{elif}\;d \leq 1.3 \cdot 10^{+115}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \end{array} \]
Alternative 3
Error12.0
Cost1488
\[\begin{array}{l} t_0 := \frac{c \cdot b - d \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -2.4 \cdot 10^{+59}:\\ \;\;\;\;\frac{\frac{c}{\frac{d}{b}} - a}{d}\\ \mathbf{elif}\;d \leq -1.15 \cdot 10^{-60}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 2.35 \cdot 10^{-153}:\\ \;\;\;\;\frac{b - \frac{d \cdot a}{c}}{c}\\ \mathbf{elif}\;d \leq 7 \cdot 10^{+115}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \end{array} \]
Alternative 4
Error15.5
Cost1232
\[\begin{array}{l} t_0 := \frac{\frac{c}{\frac{d}{b}} - a}{d}\\ \mathbf{if}\;d \leq -2.6 \cdot 10^{-60}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.6 \cdot 10^{-34}:\\ \;\;\;\;\frac{b - \frac{d \cdot a}{c}}{c}\\ \mathbf{elif}\;d \leq 2.7 \cdot 10^{+17}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 7.2 \cdot 10^{+33}:\\ \;\;\;\;\frac{b - d \cdot \frac{a}{c}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \end{array} \]
Alternative 5
Error19.6
Cost1105
\[\begin{array}{l} t_0 := \frac{b - d \cdot \frac{a}{c}}{c}\\ \mathbf{if}\;c \leq -7.2 \cdot 10^{+179}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -2 \cdot 10^{+153}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d}\\ \mathbf{elif}\;c \leq -4.5 \cdot 10^{-43} \lor \neg \left(c \leq 3.6 \cdot 10^{-54}\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{-a}{d}\\ \end{array} \]
Alternative 6
Error15.4
Cost1105
\[\begin{array}{l} t_0 := \frac{\frac{c}{\frac{d}{b}} - a}{d}\\ \mathbf{if}\;d \leq -2.7 \cdot 10^{-60}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 7 \cdot 10^{-24}:\\ \;\;\;\;\frac{b - \frac{d \cdot a}{c}}{c}\\ \mathbf{elif}\;d \leq 9.5 \cdot 10^{+15} \lor \neg \left(d \leq 7.2 \cdot 10^{+33}\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b - d \cdot \frac{a}{c}}{c}\\ \end{array} \]
Alternative 7
Error24.8
Cost840
\[\begin{array}{l} \mathbf{if}\;c \leq -7.2 \cdot 10^{+179}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq -1.05 \cdot 10^{+122}:\\ \;\;\;\;\frac{1}{d} \cdot \left(c \cdot \frac{b}{d}\right)\\ \mathbf{elif}\;c \leq -2.25 \cdot 10^{-20}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 9.5 \cdot 10^{+58}:\\ \;\;\;\;\frac{-a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 8
Error24.8
Cost784
\[\begin{array}{l} \mathbf{if}\;c \leq -7.2 \cdot 10^{+179}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq -1.3 \cdot 10^{+123}:\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d}\\ \mathbf{elif}\;c \leq -6.5 \cdot 10^{-21}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 1.1 \cdot 10^{+59}:\\ \;\;\;\;\frac{-a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 9
Error23.4
Cost521
\[\begin{array}{l} \mathbf{if}\;c \leq -3.5 \cdot 10^{-31} \lor \neg \left(c \leq 1.2 \cdot 10^{+59}\right):\\ \;\;\;\;\frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{-a}{d}\\ \end{array} \]
Alternative 10
Error37.2
Cost192
\[\frac{b}{c} \]

Error

Reproduce?

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