Average Error: 26.0 → 1.1
Time: 19.5s
Precision: binary64
Cost: 35208
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{-a}{\mathsf{hypot}\left(d, c\right)}\right)\\ t_1 := b \cdot c - a \cdot d\\ t_2 := \frac{t_1}{c \cdot c + d \cdot d}\\ \mathbf{if}\;t_2 \leq -4 \cdot 10^{+248}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;t_2 \leq 5 \cdot 10^{+271}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{t_1}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \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
         (fma
          (/ c (hypot c d))
          (/ b (hypot c d))
          (* (/ d (hypot d c)) (/ (- a) (hypot d c)))))
        (t_1 (- (* b c) (* a d)))
        (t_2 (/ t_1 (+ (* c c) (* d d)))))
   (if (<= t_2 -4e+248)
     t_0
     (if (<= t_2 5e+271) (* (/ 1.0 (hypot c d)) (/ t_1 (hypot c d))) t_0))))
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 = fma((c / hypot(c, d)), (b / hypot(c, d)), ((d / hypot(d, c)) * (-a / hypot(d, c))));
	double t_1 = (b * c) - (a * d);
	double t_2 = t_1 / ((c * c) + (d * d));
	double tmp;
	if (t_2 <= -4e+248) {
		tmp = t_0;
	} else if (t_2 <= 5e+271) {
		tmp = (1.0 / hypot(c, d)) * (t_1 / hypot(c, d));
	} else {
		tmp = t_0;
	}
	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 = fma(Float64(c / hypot(c, d)), Float64(b / hypot(c, d)), Float64(Float64(d / hypot(d, c)) * Float64(Float64(-a) / hypot(d, c))))
	t_1 = Float64(Float64(b * c) - Float64(a * d))
	t_2 = Float64(t_1 / Float64(Float64(c * c) + Float64(d * d)))
	tmp = 0.0
	if (t_2 <= -4e+248)
		tmp = t_0;
	elseif (t_2 <= 5e+271)
		tmp = Float64(Float64(1.0 / hypot(c, d)) * Float64(t_1 / hypot(c, d)));
	else
		tmp = t_0;
	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[(N[(c / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(b / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] + N[(N[(d / N[Sqrt[d ^ 2 + c ^ 2], $MachinePrecision]), $MachinePrecision] * N[((-a) / N[Sqrt[d ^ 2 + c ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(b * c), $MachinePrecision] - N[(a * d), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(t$95$1 / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$2, -4e+248], t$95$0, If[LessEqual[t$95$2, 5e+271], N[(N[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(t$95$1 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$0]]]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{-a}{\mathsf{hypot}\left(d, c\right)}\right)\\
t_1 := b \cdot c - a \cdot d\\
t_2 := \frac{t_1}{c \cdot c + d \cdot d}\\
\mathbf{if}\;t_2 \leq -4 \cdot 10^{+248}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;t_2 \leq 5 \cdot 10^{+271}:\\
\;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{t_1}{\mathsf{hypot}\left(c, d\right)}\\

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


\end{array}

Error

Target

Original26.0
Target0.4
Herbie1.1
\[\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))) < -4.00000000000000018e248 or 5.0000000000000003e271 < (/.f64 (-.f64 (*.f64 b c) (*.f64 a d)) (+.f64 (*.f64 c c) (*.f64 d d)))

    1. Initial program 59.7

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, -\frac{a \cdot d}{{\left(\mathsf{hypot}\left(c, d\right)\right)}^{2}}\right)} \]
    3. Applied egg-rr1.8

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

    if -4.00000000000000018e248 < (/.f64 (-.f64 (*.f64 b c) (*.f64 a d)) (+.f64 (*.f64 c c) (*.f64 d d))) < 5.0000000000000003e271

    1. Initial program 11.7

      \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
    2. Applied egg-rr0.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)}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification1.1

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \leq -4 \cdot 10^{+248}:\\ \;\;\;\;\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{-a}{\mathsf{hypot}\left(d, c\right)}\right)\\ \mathbf{elif}\;\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \leq 5 \cdot 10^{+271}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{-a}{\mathsf{hypot}\left(d, c\right)}\right)\\ \end{array} \]

Alternatives

Alternative 1
Error6.9
Cost33552
\[\begin{array}{l} t_0 := \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{a \cdot \left(-d\right)}{{\left(\mathsf{hypot}\left(c, d\right)\right)}^{2}}\right)\\ t_1 := \frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{if}\;d \leq -7.146207147472019 \cdot 10^{+125}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -1 \cdot 10^{-150}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 5 \cdot 10^{-163}:\\ \;\;\;\;\frac{c \cdot \frac{b}{c}}{c} - \frac{\frac{a}{\frac{c}{d}}}{c}\\ \mathbf{elif}\;d \leq 7.44935694106442 \cdot 10^{+145}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 2
Error1.7
Cost33152
\[\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{\frac{-d}{\mathsf{hypot}\left(d, c\right)}}{\frac{\mathsf{hypot}\left(d, c\right)}{a}}\right) \]
Alternative 3
Error7.9
Cost27216
\[\begin{array}{l} t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}\\ t_1 := \frac{c}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{if}\;c \leq -2.2459430675363694 \cdot 10^{+66}:\\ \;\;\;\;\mathsf{fma}\left(t_1, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{a}{c} \cdot \frac{-d}{c}\right)\\ \mathbf{elif}\;c \leq -1 \cdot 10^{-180}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 10^{-185}:\\ \;\;\;\;\frac{\frac{b \cdot c}{d} - a}{d}\\ \mathbf{elif}\;c \leq 2.6889481674883298 \cdot 10^{+35}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(t_1, \frac{b}{c}, \frac{d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{-a}{\mathsf{hypot}\left(d, c\right)}\right)\\ \end{array} \]
Alternative 4
Error9.2
Cost20356
\[\begin{array}{l} t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{if}\;c \leq -2.2459430675363694 \cdot 10^{+66}:\\ \;\;\;\;\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{a}{c} \cdot \frac{-d}{c}\right)\\ \mathbf{elif}\;c \leq -1 \cdot 10^{-180}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 10^{-185}:\\ \;\;\;\;\frac{\frac{b \cdot c}{d} - a}{d}\\ \mathbf{elif}\;c \leq 2.502016278307326 \cdot 10^{+61}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c} - \frac{\frac{d}{\frac{c}{a}}}{c}\\ \end{array} \]
Alternative 5
Error9.8
Cost14288
\[\begin{array}{l} t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{b \cdot c - a \cdot d}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{if}\;c \leq -8.43376747347395 \cdot 10^{+71}:\\ \;\;\;\;\frac{b}{c} - \frac{\frac{d}{c}}{\frac{c}{a}}\\ \mathbf{elif}\;c \leq -1 \cdot 10^{-180}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 10^{-185}:\\ \;\;\;\;\frac{\frac{b \cdot c}{d} - a}{d}\\ \mathbf{elif}\;c \leq 2.502016278307326 \cdot 10^{+61}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c} - \frac{\frac{d}{\frac{c}{a}}}{c}\\ \end{array} \]
Alternative 6
Error15.3
Cost1488
\[\begin{array}{l} t_0 := \frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{if}\;d \leq -5.755640880864757 \cdot 10^{+77}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -3.449980886049424 \cdot 10^{+53}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;d \leq -1694614927.6689146:\\ \;\;\;\;c \cdot \frac{\frac{b}{d}}{d} - \frac{a}{d}\\ \mathbf{elif}\;d \leq 1.873608231729959:\\ \;\;\;\;\frac{c \cdot \frac{b}{c}}{c} - \frac{\frac{a}{\frac{c}{d}}}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 7
Error12.4
Cost1488
\[\begin{array}{l} t_0 := \frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}\\ t_1 := \frac{b}{c} - \frac{\frac{d}{c}}{\frac{c}{a}}\\ \mathbf{if}\;c \leq -89842096553.5778:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -1 \cdot 10^{-100}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 10^{-191}:\\ \;\;\;\;\frac{\frac{b \cdot c}{d} - a}{d}\\ \mathbf{elif}\;c \leq 1.3960910347479708 \cdot 10^{+36}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 8
Error15.6
Cost968
\[\begin{array}{l} t_0 := \frac{b}{c} - \frac{\frac{d}{c}}{\frac{c}{a}}\\ \mathbf{if}\;c \leq -2.7253377423111406 \cdot 10^{-12}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 4.484607801108024 \cdot 10^{-82}:\\ \;\;\;\;\frac{\frac{b \cdot c}{d} - a}{d}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 9
Error15.6
Cost968
\[\begin{array}{l} \mathbf{if}\;c \leq -2.7253377423111406 \cdot 10^{-12}:\\ \;\;\;\;\frac{b}{c} - \frac{\frac{d}{c}}{\frac{c}{a}}\\ \mathbf{elif}\;c \leq 4.484607801108024 \cdot 10^{-82}:\\ \;\;\;\;\frac{\frac{b \cdot c}{d} - a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c} - \frac{\frac{d}{\frac{c}{a}}}{c}\\ \end{array} \]
Alternative 10
Error20.3
Cost840
\[\begin{array}{l} \mathbf{if}\;c \leq -2.7253377423111406 \cdot 10^{-12}:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 4.484607801108024 \cdot 10^{-82}:\\ \;\;\;\;\frac{\frac{b \cdot c}{d} - a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 11
Error15.6
Cost840
\[\begin{array}{l} t_0 := \frac{b - d \cdot \frac{a}{c}}{c}\\ \mathbf{if}\;c \leq -2.7253377423111406 \cdot 10^{-12}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 4.484607801108024 \cdot 10^{-82}:\\ \;\;\;\;\frac{\frac{b \cdot c}{d} - a}{d}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 12
Error23.8
Cost520
\[\begin{array}{l} \mathbf{if}\;c \leq -9.197655707976395:\\ \;\;\;\;\frac{b}{c}\\ \mathbf{elif}\;c \leq 4.484607801108024 \cdot 10^{-82}:\\ \;\;\;\;\frac{-a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 13
Error36.8
Cost192
\[\frac{b}{c} \]

Error

Reproduce

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