Average Error: 26.1 → 10.3
Time: 12.5s
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)} \cdot \frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(c, d\right)}\\ t_1 := \frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{if}\;c \leq -5.5 \cdot 10^{+88}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -6.2 \cdot 10^{-71}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 2.2 \cdot 10^{-60}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;c \leq 1.55 \cdot 10^{+130}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \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)) (/ (- (* c b) (* d a)) (hypot c d))))
        (t_1 (- (/ b c) (* (/ d c) (/ a c)))))
   (if (<= c -5.5e+88)
     t_1
     (if (<= c -6.2e-71)
       t_0
       (if (<= c 2.2e-60)
         (/ (- (/ (* c b) d) a) d)
         (if (<= c 1.55e+130) t_0 t_1))))))
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)) * (((c * b) - (d * a)) / hypot(c, d));
	double t_1 = (b / c) - ((d / c) * (a / c));
	double tmp;
	if (c <= -5.5e+88) {
		tmp = t_1;
	} else if (c <= -6.2e-71) {
		tmp = t_0;
	} else if (c <= 2.2e-60) {
		tmp = (((c * b) / d) - a) / d;
	} else if (c <= 1.55e+130) {
		tmp = t_0;
	} else {
		tmp = t_1;
	}
	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)) * (((c * b) - (d * a)) / Math.hypot(c, d));
	double t_1 = (b / c) - ((d / c) * (a / c));
	double tmp;
	if (c <= -5.5e+88) {
		tmp = t_1;
	} else if (c <= -6.2e-71) {
		tmp = t_0;
	} else if (c <= 2.2e-60) {
		tmp = (((c * b) / d) - a) / d;
	} else if (c <= 1.55e+130) {
		tmp = t_0;
	} else {
		tmp = t_1;
	}
	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)) * (((c * b) - (d * a)) / math.hypot(c, d))
	t_1 = (b / c) - ((d / c) * (a / c))
	tmp = 0
	if c <= -5.5e+88:
		tmp = t_1
	elif c <= -6.2e-71:
		tmp = t_0
	elif c <= 2.2e-60:
		tmp = (((c * b) / d) - a) / d
	elif c <= 1.55e+130:
		tmp = t_0
	else:
		tmp = t_1
	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(1.0 / hypot(c, d)) * Float64(Float64(Float64(c * b) - Float64(d * a)) / hypot(c, d)))
	t_1 = Float64(Float64(b / c) - Float64(Float64(d / c) * Float64(a / c)))
	tmp = 0.0
	if (c <= -5.5e+88)
		tmp = t_1;
	elseif (c <= -6.2e-71)
		tmp = t_0;
	elseif (c <= 2.2e-60)
		tmp = Float64(Float64(Float64(Float64(c * b) / d) - a) / d);
	elseif (c <= 1.55e+130)
		tmp = t_0;
	else
		tmp = t_1;
	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)) * (((c * b) - (d * a)) / hypot(c, d));
	t_1 = (b / c) - ((d / c) * (a / c));
	tmp = 0.0;
	if (c <= -5.5e+88)
		tmp = t_1;
	elseif (c <= -6.2e-71)
		tmp = t_0;
	elseif (c <= 2.2e-60)
		tmp = (((c * b) / d) - a) / d;
	elseif (c <= 1.55e+130)
		tmp = t_0;
	else
		tmp = t_1;
	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[(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]}, Block[{t$95$1 = N[(N[(b / c), $MachinePrecision] - N[(N[(d / c), $MachinePrecision] * N[(a / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -5.5e+88], t$95$1, If[LessEqual[c, -6.2e-71], t$95$0, If[LessEqual[c, 2.2e-60], N[(N[(N[(N[(c * b), $MachinePrecision] / d), $MachinePrecision] - a), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[c, 1.55e+130], t$95$0, t$95$1]]]]]]
\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)} \cdot \frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(c, d\right)}\\
t_1 := \frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\
\mathbf{if}\;c \leq -5.5 \cdot 10^{+88}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;c \leq -6.2 \cdot 10^{-71}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;c \leq 1.55 \cdot 10^{+130}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;t_1\\


\end{array}

Error

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original26.1
Target0.4
Herbie10.3
\[\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 3 regimes
  2. if c < -5.5e88 or 1.55e130 < c

    1. Initial program 39.5

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

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

      \[\leadsto \color{blue}{\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}} \]
      Proof
      (-.f64 (/.f64 b c) (*.f64 (/.f64 d c) (/.f64 a c))): 0 points increase in error, 0 points decrease in error
      (-.f64 (/.f64 b c) (Rewrite<= times-frac_binary64 (/.f64 (*.f64 d a) (*.f64 c c)))): 0 points increase in error, 1 points decrease in error
      (-.f64 (/.f64 b c) (/.f64 (Rewrite<= *-commutative_binary64 (*.f64 a d)) (*.f64 c c))): 0 points increase in error, 7 points decrease in error
      (-.f64 (/.f64 b c) (/.f64 (*.f64 a d) (Rewrite<= unpow2_binary64 (pow.f64 c 2)))): 7 points increase in error, 0 points decrease in error
      (Rewrite<= unsub-neg_binary64 (+.f64 (/.f64 b c) (neg.f64 (/.f64 (*.f64 a d) (pow.f64 c 2))))): 0 points increase in error, 1 points decrease in error
      (+.f64 (/.f64 b c) (Rewrite<= mul-1-neg_binary64 (*.f64 -1 (/.f64 (*.f64 a d) (pow.f64 c 2))))): 1 points increase in error, 0 points decrease in error
      (Rewrite<= +-commutative_binary64 (+.f64 (*.f64 -1 (/.f64 (*.f64 a d) (pow.f64 c 2))) (/.f64 b c))): 0 points increase in error, 7 points decrease in error

    if -5.5e88 < c < -6.20000000000000004e-71 or 2.1999999999999999e-60 < c < 1.55e130

    1. Initial program 16.2

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

      \[\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 -6.20000000000000004e-71 < c < 2.1999999999999999e-60

    1. Initial program 21.4

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Simplified21.4

      \[\leadsto \color{blue}{\frac{b \cdot c - a \cdot d}{\mathsf{fma}\left(c, c, d \cdot d\right)}} \]
      Proof
      (/.f64 (-.f64 (*.f64 b c) (*.f64 a d)) (fma.f64 c c (*.f64 d d))): 0 points increase in error, 0 points decrease in error
      (/.f64 (-.f64 (*.f64 b c) (*.f64 a d)) (Rewrite<= fma-def_binary64 (+.f64 (*.f64 c c) (*.f64 d d)))): 2 points increase in error, 0 points decrease in error
    3. Taylor expanded in c around 0 12.6

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

      \[\leadsto \color{blue}{\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}} \]
      Proof
      (-.f64 (*.f64 (/.f64 b d) (/.f64 c d)) (/.f64 a d)): 0 points increase in error, 0 points decrease in error
      (-.f64 (Rewrite<= times-frac_binary64 (/.f64 (*.f64 b c) (*.f64 d d))) (/.f64 a d)): 0 points increase in error, 0 points decrease in error
      (-.f64 (/.f64 (Rewrite=> *-commutative_binary64 (*.f64 c b)) (*.f64 d d)) (/.f64 a d)): 7 points increase in error, 0 points decrease in error
      (-.f64 (/.f64 (*.f64 c b) (Rewrite<= unpow2_binary64 (pow.f64 d 2))) (/.f64 a d)): 0 points increase in error, 7 points decrease in error
      (Rewrite<= unsub-neg_binary64 (+.f64 (/.f64 (*.f64 c b) (pow.f64 d 2)) (neg.f64 (/.f64 a d)))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 (*.f64 c b) (pow.f64 d 2)) (Rewrite<= mul-1-neg_binary64 (*.f64 -1 (/.f64 a d)))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= +-commutative_binary64 (+.f64 (*.f64 -1 (/.f64 a d)) (/.f64 (*.f64 c b) (pow.f64 d 2)))): 7 points increase in error, 0 points decrease in error
    5. Applied egg-rr10.0

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -5.5 \cdot 10^{+88}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{elif}\;c \leq -6.2 \cdot 10^{-71}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \leq 2.2 \cdot 10^{-60}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;c \leq 1.55 \cdot 10^{+130}:\\ \;\;\;\;\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{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \end{array} \]

Alternatives

Alternative 1
Error12.5
Cost7760
\[\begin{array}{l} t_0 := c \cdot b - d \cdot a\\ \mathbf{if}\;c \leq -1.25 \cdot 10^{+77}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{elif}\;c \leq -5.3 \cdot 10^{-71}:\\ \;\;\;\;\frac{t_0}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 1.95 \cdot 10^{-63}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;c \leq 2.35 \cdot 10^{+33}:\\ \;\;\;\;\frac{t_0}{\mathsf{fma}\left(c, c, d \cdot d\right)}\\ \mathbf{elif}\;c \leq 2.8 \cdot 10^{+79}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b - d \cdot \frac{a}{c}}{c}\\ \end{array} \]
Alternative 2
Error12.4
Cost1488
\[\begin{array}{l} t_0 := \frac{c \cdot b - d \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -6 \cdot 10^{+77}:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{elif}\;c \leq -1.6 \cdot 10^{-70}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 4 \cdot 10^{-62}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;c \leq 3.6 \cdot 10^{+31}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 3.2 \cdot 10^{+76}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b - d \cdot \frac{a}{c}}{c}\\ \end{array} \]
Alternative 3
Error15.1
Cost1233
\[\begin{array}{l} t_0 := \frac{b - d \cdot \frac{a}{c}}{c}\\ \mathbf{if}\;c \leq -980000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 5.6 \cdot 10^{-51}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;c \leq 5.2 \cdot 10^{+30} \lor \neg \left(c \leq 6.8 \cdot 10^{+80}\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \end{array} \]
Alternative 4
Error15.1
Cost1232
\[\begin{array}{l} t_0 := \frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{if}\;c \leq -1360000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 5.6 \cdot 10^{-51}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;c \leq 3.2 \cdot 10^{+31}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.3 \cdot 10^{+83}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b - d \cdot \frac{a}{c}}{c}\\ \end{array} \]
Alternative 5
Error15.2
Cost1232
\[\begin{array}{l} \mathbf{if}\;c \leq -1120000:\\ \;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\ \mathbf{elif}\;c \leq 5.8 \cdot 10^{-59}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;c \leq 10^{+31}:\\ \;\;\;\;\frac{c}{\frac{c \cdot c + d \cdot d}{b}}\\ \mathbf{elif}\;c \leq 3.55 \cdot 10^{+75}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b - d \cdot \frac{a}{c}}{c}\\ \end{array} \]
Alternative 6
Error23.4
Cost1108
\[\begin{array}{l} t_0 := \frac{-a}{d}\\ \mathbf{if}\;c \leq -7.2 \cdot 10^{+44}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 4.6 \cdot 10^{-51}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.7 \cdot 10^{+32}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 4.8 \cdot 10^{+58}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 3.8 \cdot 10^{+89}:\\ \;\;\;\;\frac{b}{d} \cdot \frac{c}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 7
Error15.4
Cost1106
\[\begin{array}{l} \mathbf{if}\;c \leq -1750000 \lor \neg \left(c \leq 1.4 \cdot 10^{-52}\right) \land \left(c \leq 3.9 \cdot 10^{+31} \lor \neg \left(c \leq 3.5 \cdot 10^{+78}\right)\right):\\ \;\;\;\;\frac{b - d \cdot \frac{a}{c}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{c}{\frac{d}{b}} - a}{d}\\ \end{array} \]
Alternative 8
Error15.1
Cost1105
\[\begin{array}{l} t_0 := \frac{b - d \cdot \frac{a}{c}}{c}\\ \mathbf{if}\;c \leq -520000:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 5.6 \cdot 10^{-51}:\\ \;\;\;\;\frac{\frac{c \cdot b}{d} - a}{d}\\ \mathbf{elif}\;c \leq 8.2 \cdot 10^{+31} \lor \neg \left(c \leq 1.6 \cdot 10^{+82}\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{c}{\frac{d}{b}} - a}{d}\\ \end{array} \]
Alternative 9
Error18.1
Cost841
\[\begin{array}{l} \mathbf{if}\;c \leq -6.2 \cdot 10^{-8} \lor \neg \left(c \leq 1.95 \cdot 10^{-53}\right):\\ \;\;\;\;\frac{b - d \cdot \frac{a}{c}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{-a}{d}\\ \end{array} \]
Alternative 10
Error22.4
Cost520
\[\begin{array}{l} \mathbf{if}\;c \leq -8.5 \cdot 10^{+44}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 5.6 \cdot 10^{-51}:\\ \;\;\;\;\frac{-a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 11
Error36.0
Cost324
\[\begin{array}{l} \mathbf{if}\;d \leq 2.1 \cdot 10^{+110}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{d}\\ \end{array} \]
Alternative 12
Error57.0
Cost192
\[\frac{a}{d} \]

Error

Reproduce

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