?

Average Accuracy: 59.2% → 79.9%
Time: 14.9s
Precision: binary64
Cost: 14024

?

\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} \mathbf{if}\;d \leq -2.3 \cdot 10^{+131}:\\ \;\;\;\;\frac{\frac{c}{\frac{d}{b}} - a}{d}\\ \mathbf{elif}\;d \leq -1.4 \cdot 10^{-48}:\\ \;\;\;\;\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 20000000000:\\ \;\;\;\;\mathsf{fma}\left(-1, \frac{\frac{d \cdot a}{c}}{c}, \frac{b}{c}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (if (<= d -2.3e+131)
   (/ (- (/ c (/ d b)) a) d)
   (if (<= d -1.4e-48)
     (* (/ 1.0 (hypot c d)) (/ (- (* c b) (* d a)) (hypot c d)))
     (if (<= d 20000000000.0)
       (fma -1.0 (/ (/ (* d a) c) c) (/ b c))
       (- (* (/ b d) (/ c d)) (/ a 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 tmp;
	if (d <= -2.3e+131) {
		tmp = ((c / (d / b)) - a) / d;
	} else if (d <= -1.4e-48) {
		tmp = (1.0 / hypot(c, d)) * (((c * b) - (d * a)) / hypot(c, d));
	} else if (d <= 20000000000.0) {
		tmp = fma(-1.0, (((d * a) / c) / c), (b / c));
	} else {
		tmp = ((b / d) * (c / d)) - (a / 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)
	tmp = 0.0
	if (d <= -2.3e+131)
		tmp = Float64(Float64(Float64(c / Float64(d / b)) - a) / d);
	elseif (d <= -1.4e-48)
		tmp = Float64(Float64(1.0 / hypot(c, d)) * Float64(Float64(Float64(c * b) - Float64(d * a)) / hypot(c, d)));
	elseif (d <= 20000000000.0)
		tmp = fma(-1.0, Float64(Float64(Float64(d * a) / c) / c), Float64(b / c));
	else
		tmp = Float64(Float64(Float64(b / d) * Float64(c / d)) - Float64(a / d));
	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_] := If[LessEqual[d, -2.3e+131], N[(N[(N[(c / N[(d / b), $MachinePrecision]), $MachinePrecision] - a), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[d, -1.4e-48], N[(N[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(N[(N[(c * b), $MachinePrecision] - N[(d * a), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 20000000000.0], N[(-1.0 * N[(N[(N[(d * a), $MachinePrecision] / c), $MachinePrecision] / c), $MachinePrecision] + N[(b / c), $MachinePrecision]), $MachinePrecision], N[(N[(N[(b / d), $MachinePrecision] * N[(c / d), $MachinePrecision]), $MachinePrecision] - N[(a / d), $MachinePrecision]), $MachinePrecision]]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;d \leq -2.3 \cdot 10^{+131}:\\
\;\;\;\;\frac{\frac{c}{\frac{d}{b}} - a}{d}\\

\mathbf{elif}\;d \leq -1.4 \cdot 10^{-48}:\\
\;\;\;\;\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 20000000000:\\
\;\;\;\;\mathsf{fma}\left(-1, \frac{\frac{d \cdot a}{c}}{c}, \frac{b}{c}\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\


\end{array}

Error?

Target

Original59.2%
Target99.3%
Herbie79.9%
\[\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.29999999999999992e131

    1. Initial program 33.2%

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

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

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

      [Start]76.3

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

      +-commutative [=>]76.3

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

      mul-1-neg [=>]76.3

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

      unsub-neg [=>]76.3

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

      *-commutative [<=]76.3

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

      unpow2 [=>]76.3

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

      times-frac [=>]87.3

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

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

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

      [Start]76.3

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

      +-commutative [=>]76.3

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

      *-commutative [=>]76.3

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

      unpow2 [=>]76.3

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

      associate-/l* [=>]77.9

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

      mul-1-neg [=>]77.9

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

      sub-neg [<=]77.9

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

      associate-/l* [<=]76.3

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

      associate-/r* [=>]80.4

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

      div-sub [<=]80.4

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

      *-commutative [<=]80.4

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

      associate-/l* [=>]87.5

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

    if -2.29999999999999992e131 < d < -1.40000000000000002e-48

    1. Initial program 73.0%

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

      [Start]73.0

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

      *-un-lft-identity [=>]73.0

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

      add-sqr-sqrt [=>]73.0

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

      times-frac [=>]73.0

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

      hypot-def [=>]73.0

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

      hypot-def [=>]80.3

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

    if -1.40000000000000002e-48 < d < 2e10

    1. Initial program 69.5%

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Applied egg-rr82.4%

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

      [Start]69.5

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

      *-un-lft-identity [=>]69.5

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

      add-sqr-sqrt [=>]69.5

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

      times-frac [=>]69.5

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

      hypot-def [=>]69.5

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

      hypot-def [=>]82.4

      \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{b \cdot c - a \cdot d}{\color{blue}{\mathsf{hypot}\left(c, d\right)}} \]
    3. Taylor expanded in c around inf 74.3%

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

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

      [Start]74.3

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

      fma-def [=>]74.3

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

      unpow2 [=>]74.3

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

      associate-/r* [=>]78.7

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

      associate-/l* [=>]78.5

      \[ \mathsf{fma}\left(-1, \frac{\color{blue}{\frac{a}{\frac{c}{d}}}}{c}, \frac{b}{c}\right) \]
    5. Taylor expanded in a around 0 78.7%

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

    if 2e10 < d

    1. Initial program 47.9%

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

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

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

      [Start]71.0

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

      +-commutative [=>]71.0

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

      mul-1-neg [=>]71.0

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

      unsub-neg [=>]71.0

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

      *-commutative [<=]71.0

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

      unpow2 [=>]71.0

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

      times-frac [=>]77.2

      \[ \color{blue}{\frac{b}{d} \cdot \frac{c}{d}} - \frac{a}{d} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification79.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -2.3 \cdot 10^{+131}:\\ \;\;\;\;\frac{\frac{c}{\frac{d}{b}} - a}{d}\\ \mathbf{elif}\;d \leq -1.4 \cdot 10^{-48}:\\ \;\;\;\;\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 20000000000:\\ \;\;\;\;\mathsf{fma}\left(-1, \frac{\frac{d \cdot a}{c}}{c}, \frac{b}{c}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy79.3%
Cost7500
\[\begin{array}{l} t_0 := d \cdot d + c \cdot c\\ \mathbf{if}\;d \leq -1.7 \cdot 10^{+119}:\\ \;\;\;\;\frac{\frac{c}{\frac{d}{b}} - a}{d}\\ \mathbf{elif}\;d \leq -1.05 \cdot 10^{-154}:\\ \;\;\;\;\frac{c}{\frac{t_0}{b}} - \frac{a}{\frac{t_0}{d}}\\ \mathbf{elif}\;d \leq 78000:\\ \;\;\;\;\mathsf{fma}\left(-1, \frac{\frac{a}{\frac{c}{d}}}{c}, \frac{b}{c}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \end{array} \]
Alternative 2
Accuracy79.4%
Cost7500
\[\begin{array}{l} t_0 := d \cdot d + c \cdot c\\ \mathbf{if}\;d \leq -1.7 \cdot 10^{+119}:\\ \;\;\;\;\frac{\frac{c}{\frac{d}{b}} - a}{d}\\ \mathbf{elif}\;d \leq -2.05 \cdot 10^{-155}:\\ \;\;\;\;\frac{c}{\frac{t_0}{b}} - \frac{a}{\frac{t_0}{d}}\\ \mathbf{elif}\;d \leq 185000000000:\\ \;\;\;\;\mathsf{fma}\left(-1, \frac{\frac{d \cdot a}{c}}{c}, \frac{b}{c}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \end{array} \]
Alternative 3
Accuracy78.6%
Cost1736
\[\begin{array}{l} t_0 := d \cdot d + c \cdot c\\ \mathbf{if}\;d \leq -1.7 \cdot 10^{+119}:\\ \;\;\;\;\frac{\frac{c}{\frac{d}{b}} - a}{d}\\ \mathbf{elif}\;d \leq -2.15 \cdot 10^{-156}:\\ \;\;\;\;\frac{c}{\frac{t_0}{b}} - \frac{a}{\frac{t_0}{d}}\\ \mathbf{elif}\;d \leq 38000000000:\\ \;\;\;\;\frac{1}{c} \cdot \left(b - d \cdot \frac{a}{c}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \end{array} \]
Alternative 4
Accuracy78.0%
Cost1224
\[\begin{array}{l} \mathbf{if}\;d \leq -2.25 \cdot 10^{+117}:\\ \;\;\;\;\frac{\frac{c}{\frac{d}{b}} - a}{d}\\ \mathbf{elif}\;d \leq -1.6 \cdot 10^{-48}:\\ \;\;\;\;\frac{c \cdot b - d \cdot a}{d \cdot d + c \cdot c}\\ \mathbf{elif}\;d \leq 950000:\\ \;\;\;\;\frac{1}{c} \cdot \left(b - d \cdot \frac{a}{c}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \end{array} \]
Alternative 5
Accuracy70.7%
Cost968
\[\begin{array}{l} \mathbf{if}\;d \leq -3.5 \cdot 10^{-27}:\\ \;\;\;\;\frac{\frac{c}{\frac{d}{b}} - a}{d}\\ \mathbf{elif}\;d \leq 13200000:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \end{array} \]
Alternative 6
Accuracy75.7%
Cost968
\[\begin{array}{l} \mathbf{if}\;d \leq -2 \cdot 10^{+19}:\\ \;\;\;\;\frac{\frac{c}{\frac{d}{b}} - a}{d}\\ \mathbf{elif}\;d \leq 550:\\ \;\;\;\;\frac{b}{c} - \frac{a}{c} \cdot \frac{d}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \end{array} \]
Alternative 7
Accuracy75.9%
Cost968
\[\begin{array}{l} \mathbf{if}\;d \leq -2 \cdot 10^{+19}:\\ \;\;\;\;\frac{\frac{c}{\frac{d}{b}} - a}{d}\\ \mathbf{elif}\;d \leq 2000000:\\ \;\;\;\;\frac{1}{c} \cdot \left(b - d \cdot \frac{a}{c}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \end{array} \]
Alternative 8
Accuracy61.3%
Cost844
\[\begin{array}{l} t_0 := \frac{-a}{d}\\ \mathbf{if}\;c \leq -3.4 \cdot 10^{-40}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq -2 \cdot 10^{-82}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -6.5 \cdot 10^{-188}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;c \leq 9.2 \cdot 10^{+32}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 9
Accuracy70.8%
Cost841
\[\begin{array}{l} \mathbf{if}\;d \leq -4.3 \cdot 10^{-27} \lor \neg \left(d \leq 92000\right):\\ \;\;\;\;\frac{\frac{c}{\frac{d}{b}} - a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 10
Accuracy63.9%
Cost521
\[\begin{array}{l} \mathbf{if}\;d \leq -1.52 \cdot 10^{-46} \lor \neg \left(d \leq 1700\right):\\ \;\;\;\;\frac{-a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 11
Accuracy41.2%
Cost192
\[\frac{b}{c} \]

Error

Reproduce?

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