Average Error: 25.8 → 13.3
Time: 13.6s
Precision: binary64
Cost: 14164
\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := b \cdot c - d \cdot a\\ t_1 := \frac{\frac{b}{d} \cdot c - a}{d}\\ \mathbf{if}\;d \leq -3.3 \cdot 10^{+128}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -7.8 \cdot 10^{-114}:\\ \;\;\;\;\frac{1}{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot t_0\\ \mathbf{elif}\;d \leq 5.2 \cdot 10^{-90}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;d \leq 1.5 \cdot 10^{-61}:\\ \;\;\;\;\frac{t_0}{d \cdot d + c \cdot c}\\ \mathbf{elif}\;d \leq 5.1 \cdot 10^{+22}:\\ \;\;\;\;\frac{\frac{1}{\mathsf{hypot}\left(c, d\right)}}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{b}}{c}}\\ \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 (- (* b c) (* d a))) (t_1 (/ (- (* (/ b d) c) a) d)))
   (if (<= d -3.3e+128)
     t_1
     (if (<= d -7.8e-114)
       (* (/ 1.0 (fma c c (* d d))) t_0)
       (if (<= d 5.2e-90)
         (/ (- b (* a (/ d c))) c)
         (if (<= d 1.5e-61)
           (/ t_0 (+ (* d d) (* c c)))
           (if (<= d 5.1e+22)
             (/ (/ 1.0 (hypot c d)) (/ (/ (hypot c d) b) c))
             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 = (b * c) - (d * a);
	double t_1 = (((b / d) * c) - a) / d;
	double tmp;
	if (d <= -3.3e+128) {
		tmp = t_1;
	} else if (d <= -7.8e-114) {
		tmp = (1.0 / fma(c, c, (d * d))) * t_0;
	} else if (d <= 5.2e-90) {
		tmp = (b - (a * (d / c))) / c;
	} else if (d <= 1.5e-61) {
		tmp = t_0 / ((d * d) + (c * c));
	} else if (d <= 5.1e+22) {
		tmp = (1.0 / hypot(c, d)) / ((hypot(c, d) / b) / c);
	} 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(b * c) - Float64(d * a))
	t_1 = Float64(Float64(Float64(Float64(b / d) * c) - a) / d)
	tmp = 0.0
	if (d <= -3.3e+128)
		tmp = t_1;
	elseif (d <= -7.8e-114)
		tmp = Float64(Float64(1.0 / fma(c, c, Float64(d * d))) * t_0);
	elseif (d <= 5.2e-90)
		tmp = Float64(Float64(b - Float64(a * Float64(d / c))) / c);
	elseif (d <= 1.5e-61)
		tmp = Float64(t_0 / Float64(Float64(d * d) + Float64(c * c)));
	elseif (d <= 5.1e+22)
		tmp = Float64(Float64(1.0 / hypot(c, d)) / Float64(Float64(hypot(c, d) / b) / c));
	else
		tmp = t_1;
	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[(b * c), $MachinePrecision] - N[(d * a), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(N[(b / d), $MachinePrecision] * c), $MachinePrecision] - a), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -3.3e+128], t$95$1, If[LessEqual[d, -7.8e-114], N[(N[(1.0 / N[(c * c + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * t$95$0), $MachinePrecision], If[LessEqual[d, 5.2e-90], N[(N[(b - N[(a * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 1.5e-61], N[(t$95$0 / N[(N[(d * d), $MachinePrecision] + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 5.1e+22], N[(N[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] / N[(N[(N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision] / b), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision], t$95$1]]]]]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := b \cdot c - d \cdot a\\
t_1 := \frac{\frac{b}{d} \cdot c - a}{d}\\
\mathbf{if}\;d \leq -3.3 \cdot 10^{+128}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;d \leq -7.8 \cdot 10^{-114}:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot t_0\\

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

\mathbf{elif}\;d \leq 1.5 \cdot 10^{-61}:\\
\;\;\;\;\frac{t_0}{d \cdot d + c \cdot c}\\

\mathbf{elif}\;d \leq 5.1 \cdot 10^{+22}:\\
\;\;\;\;\frac{\frac{1}{\mathsf{hypot}\left(c, d\right)}}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{b}}{c}}\\

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


\end{array}

Error

Target

Original25.8
Target0.4
Herbie13.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 5 regimes
  2. if d < -3.3000000000000001e128 or 5.1000000000000002e22 < d

    1. Initial program 36.5

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

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

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

      [Start]18.1

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

      +-commutative [=>]18.1

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

      mul-1-neg [=>]18.1

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

      unsub-neg [=>]18.1

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

      *-commutative [=>]18.1

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

      unpow2 [=>]18.1

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

      times-frac [=>]12.4

      \[ \color{blue}{\frac{b}{d} \cdot \frac{c}{d}} - \frac{a}{d} \]
    4. Applied egg-rr12.1

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

    if -3.3000000000000001e128 < d < -7.80000000000000003e-114

    1. Initial program 17.2

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

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

    if -7.80000000000000003e-114 < d < 5.2000000000000001e-90

    1. Initial program 21.5

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

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

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

      [Start]11.8

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

      +-commutative [=>]11.8

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

      mul-1-neg [=>]11.8

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

      unsub-neg [=>]11.8

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

      *-commutative [=>]11.8

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

      unpow2 [=>]11.8

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

      times-frac [=>]10.5

      \[ \frac{b}{c} - \color{blue}{\frac{d}{c} \cdot \frac{a}{c}} \]
    4. Applied egg-rr8.5

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

    if 5.2000000000000001e-90 < d < 1.50000000000000006e-61

    1. Initial program 14.1

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

    if 1.50000000000000006e-61 < d < 5.1000000000000002e22

    1. Initial program 14.6

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

      \[\leadsto \frac{\color{blue}{c \cdot b}}{c \cdot c + d \cdot d} \]
    3. Applied egg-rr37.9

      \[\leadsto \color{blue}{\frac{1}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \cdot \frac{c}{\frac{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}{b}}} \]
    4. Simplified27.6

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

      [Start]37.9

      \[ \frac{1}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \cdot \frac{c}{\frac{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}{b}} \]

      associate-*l/ [=>]37.8

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

      *-lft-identity [=>]37.8

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

      associate-/r/ [=>]37.3

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

      associate-*r/ [<=]37.9

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

      *-commutative [=>]37.9

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

      fma-udef [=>]37.9

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

      hypot-def [=>]37.9

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

      fma-udef [=>]37.9

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

      hypot-def [=>]27.6

      \[ \frac{b}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{c}{\color{blue}{\mathsf{hypot}\left(c, d\right)}} \]
    5. Applied egg-rr27.9

      \[\leadsto \color{blue}{\frac{\frac{1}{\mathsf{hypot}\left(c, d\right)}}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{b}}{c}}} \]
  3. Recombined 5 regimes into one program.
  4. Final simplification13.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -3.3 \cdot 10^{+128}:\\ \;\;\;\;\frac{\frac{b}{d} \cdot c - a}{d}\\ \mathbf{elif}\;d \leq -7.8 \cdot 10^{-114}:\\ \;\;\;\;\frac{1}{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot \left(b \cdot c - d \cdot a\right)\\ \mathbf{elif}\;d \leq 5.2 \cdot 10^{-90}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;d \leq 1.5 \cdot 10^{-61}:\\ \;\;\;\;\frac{b \cdot c - d \cdot a}{d \cdot d + c \cdot c}\\ \mathbf{elif}\;d \leq 5.1 \cdot 10^{+22}:\\ \;\;\;\;\frac{\frac{1}{\mathsf{hypot}\left(c, d\right)}}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{b}}{c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{b}{d} \cdot c - a}{d}\\ \end{array} \]

Alternatives

Alternative 1
Error13.3
Cost14036
\[\begin{array}{l} t_0 := b \cdot c - d \cdot a\\ t_1 := \frac{\frac{b}{d} \cdot c - a}{d}\\ \mathbf{if}\;d \leq -7.6 \cdot 10^{+126}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -2.8 \cdot 10^{-113}:\\ \;\;\;\;\frac{1}{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot t_0\\ \mathbf{elif}\;d \leq 4.1 \cdot 10^{-90}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;d \leq 1.45 \cdot 10^{-63}:\\ \;\;\;\;\frac{t_0}{d \cdot d + c \cdot c}\\ \mathbf{elif}\;d \leq 5 \cdot 10^{+22}:\\ \;\;\;\;\frac{b}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{c}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 2
Error13.3
Cost7624
\[\begin{array}{l} t_0 := b \cdot c - d \cdot a\\ t_1 := \frac{\frac{b}{d} \cdot c - a}{d}\\ \mathbf{if}\;d \leq -7.2 \cdot 10^{+126}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -2.1 \cdot 10^{-114}:\\ \;\;\;\;\frac{1}{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot t_0\\ \mathbf{elif}\;d \leq 6.6 \cdot 10^{-90}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;d \leq 1.76 \cdot 10^{-63}:\\ \;\;\;\;\frac{t_0}{d \cdot d + c \cdot c}\\ \mathbf{elif}\;d \leq 10^{+23}:\\ \;\;\;\;\frac{b - \frac{a}{\frac{c}{d}}}{c}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 3
Error13.3
Cost1488
\[\begin{array}{l} t_0 := \frac{b \cdot c - d \cdot a}{d \cdot d + c \cdot c}\\ t_1 := \frac{\frac{b}{d} \cdot c - a}{d}\\ \mathbf{if}\;d \leq -1 \cdot 10^{+127}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -3.3 \cdot 10^{-113}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 2.35 \cdot 10^{-89}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{elif}\;d \leq 1.3 \cdot 10^{-64}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.1 \cdot 10^{+21}:\\ \;\;\;\;\frac{b - \frac{a}{\frac{c}{d}}}{c}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 4
Error18.5
Cost841
\[\begin{array}{l} \mathbf{if}\;d \leq -3.9 \cdot 10^{+59} \lor \neg \left(d \leq 8.8 \cdot 10^{+23}\right):\\ \;\;\;\;-\frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \end{array} \]
Alternative 5
Error15.2
Cost841
\[\begin{array}{l} \mathbf{if}\;d \leq -1.35 \cdot 10^{+59} \lor \neg \left(d \leq 8.6 \cdot 10^{+21}\right):\\ \;\;\;\;\frac{\frac{b}{d} \cdot c - a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \end{array} \]
Alternative 6
Error15.4
Cost840
\[\begin{array}{l} \mathbf{if}\;d \leq -1.3 \cdot 10^{+59}:\\ \;\;\;\;\frac{c}{\frac{d}{\frac{b}{d}}} - \frac{a}{d}\\ \mathbf{elif}\;d \leq 5.7 \cdot 10^{+20}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{b}{d} \cdot c - a}{d}\\ \end{array} \]
Alternative 7
Error24.7
Cost521
\[\begin{array}{l} \mathbf{if}\;d \leq -7.2 \cdot 10^{-113} \lor \neg \left(d \leq 2.35 \cdot 10^{+43}\right):\\ \;\;\;\;-\frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 8
Error36.9
Cost192
\[\frac{b}{c} \]

Error

Reproduce

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