Average Error: 26.0 → 10.1
Time: 17.5s
Precision: binary64
Cost: 20560
\[\frac{a \cdot c + b \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{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{if}\;c \leq -4.57134307228816 \cdot 10^{+58}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;c \leq -1 \cdot 10^{-115}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq 10^{-130}:\\ \;\;\;\;\frac{b}{d} + \frac{a \cdot \frac{c}{d}}{d}\\ \mathbf{elif}\;c \leq 2.281947855931773 \cdot 10^{+159}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \mathsf{fma}\left(\frac{d}{c}, b, a\right)\\ \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (let* ((t_0 (/ 1.0 (hypot c d)))
        (t_1 (* t_0 (/ (fma a c (* d b)) (hypot c d)))))
   (if (<= c -4.57134307228816e+58)
     (+ (/ a c) (* (/ d c) (/ b c)))
     (if (<= c -1e-115)
       t_1
       (if (<= c 1e-130)
         (+ (/ b d) (/ (* a (/ c d)) d))
         (if (<= c 2.281947855931773e+159) t_1 (* t_0 (fma (/ d c) b a))))))))
double code(double a, double b, double c, double d) {
	return ((a * c) + (b * 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 * (fma(a, c, (d * b)) / hypot(c, d));
	double tmp;
	if (c <= -4.57134307228816e+58) {
		tmp = (a / c) + ((d / c) * (b / c));
	} else if (c <= -1e-115) {
		tmp = t_1;
	} else if (c <= 1e-130) {
		tmp = (b / d) + ((a * (c / d)) / d);
	} else if (c <= 2.281947855931773e+159) {
		tmp = t_1;
	} else {
		tmp = t_0 * fma((d / c), b, a);
	}
	return tmp;
}
function code(a, b, c, d)
	return Float64(Float64(Float64(a * c) + Float64(b * 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(fma(a, c, Float64(d * b)) / hypot(c, d)))
	tmp = 0.0
	if (c <= -4.57134307228816e+58)
		tmp = Float64(Float64(a / c) + Float64(Float64(d / c) * Float64(b / c)));
	elseif (c <= -1e-115)
		tmp = t_1;
	elseif (c <= 1e-130)
		tmp = Float64(Float64(b / d) + Float64(Float64(a * Float64(c / d)) / d));
	elseif (c <= 2.281947855931773e+159)
		tmp = t_1;
	else
		tmp = Float64(t_0 * fma(Float64(d / c), b, a));
	end
	return tmp
end
code[a_, b_, c_, d_] := N[(N[(N[(a * c), $MachinePrecision] + N[(b * 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[(a * c + N[(d * b), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -4.57134307228816e+58], N[(N[(a / c), $MachinePrecision] + N[(N[(d / c), $MachinePrecision] * N[(b / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, -1e-115], t$95$1, If[LessEqual[c, 1e-130], N[(N[(b / d), $MachinePrecision] + N[(N[(a * N[(c / d), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 2.281947855931773e+159], t$95$1, N[(t$95$0 * N[(N[(d / c), $MachinePrecision] * b + a), $MachinePrecision]), $MachinePrecision]]]]]]]
\frac{a \cdot c + b \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{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{if}\;c \leq -4.57134307228816 \cdot 10^{+58}:\\
\;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\

\mathbf{elif}\;c \leq -1 \cdot 10^{-115}:\\
\;\;\;\;t_1\\

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

\mathbf{elif}\;c \leq 2.281947855931773 \cdot 10^{+159}:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;t_0 \cdot \mathsf{fma}\left(\frac{d}{c}, b, a\right)\\


\end{array}

Error

Target

Original26.0
Target0.5
Herbie10.1
\[\begin{array}{l} \mathbf{if}\;\left|d\right| < \left|c\right|:\\ \;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c + d \cdot \frac{d}{c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d + c \cdot \frac{c}{d}}\\ \end{array} \]

Derivation

  1. Split input into 4 regimes
  2. if c < -4.57134307228816e58

    1. Initial program 35.6

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

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)} \]
      Proof
      (fma.f64 (/.f64 d c) (/.f64 b c) (/.f64 a c)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= fma-def_binary64 (+.f64 (*.f64 (/.f64 d c) (/.f64 b c)) (/.f64 a c))): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= times-frac_binary64 (/.f64 (*.f64 d b) (*.f64 c c))) (/.f64 a c)): 45 points increase in error, 14 points decrease in error
      (+.f64 (/.f64 (*.f64 d b) (Rewrite<= unpow2_binary64 (pow.f64 c 2))) (/.f64 a c)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= +-commutative_binary64 (+.f64 (/.f64 a c) (/.f64 (*.f64 d b) (pow.f64 c 2)))): 0 points increase in error, 0 points decrease in error
    5. Applied egg-rr12.1

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

    if -4.57134307228816e58 < c < -1.0000000000000001e-115 or 1.0000000000000001e-130 < c < 2.281947855931773e159

    1. Initial program 16.6

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

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

    if -1.0000000000000001e-115 < c < 1.0000000000000001e-130

    1. Initial program 22.3

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

      \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}} \]
    3. Applied egg-rr12.9

      \[\leadsto \color{blue}{\frac{1}{\mathsf{hypot}\left(c, d\right) \cdot \frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, c, b \cdot d\right)}}} \]
    4. Taylor expanded in c around 0 10.6

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{c \cdot a}{{d}^{2}}} \]
    5. Simplified7.9

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{\frac{c}{d}}{d} \cdot a} \]
      Proof
      (+.f64 (/.f64 b d) (*.f64 (/.f64 (/.f64 c d) d) a)): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 b d) (*.f64 (Rewrite<= associate-/r*_binary64 (/.f64 c (*.f64 d d))) a)): 22 points increase in error, 6 points decrease in error
      (+.f64 (/.f64 b d) (*.f64 (/.f64 c (Rewrite<= unpow2_binary64 (pow.f64 d 2))) a)): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 b d) (Rewrite<= associate-/r/_binary64 (/.f64 c (/.f64 (pow.f64 d 2) a)))): 8 points increase in error, 12 points decrease in error
      (+.f64 (/.f64 b d) (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 c a) (pow.f64 d 2)))): 25 points increase in error, 10 points decrease in error
    6. Applied egg-rr7.1

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

    if 2.281947855931773e159 < c

    1. Initial program 45.1

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

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

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\mathsf{fma}\left(\frac{d}{c}, b, a\right)} \]
      Proof
      (fma.f64 (/.f64 d c) b a): 0 points increase in error, 0 points decrease in error
      (Rewrite<= fma-def_binary64 (+.f64 (*.f64 (/.f64 d c) b) a)): 0 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite=> associate-*l/_binary64 (/.f64 (*.f64 d b) c)) a): 25 points increase in error, 28 points decrease in error
      (Rewrite<= +-commutative_binary64 (+.f64 a (/.f64 (*.f64 d b) c))): 0 points increase in error, 0 points decrease in error
  3. Recombined 4 regimes into one program.
  4. Final simplification10.1

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \leq -4.57134307228816 \cdot 10^{+58}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;c \leq -1 \cdot 10^{-115}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \leq 10^{-130}:\\ \;\;\;\;\frac{b}{d} + \frac{a \cdot \frac{c}{d}}{d}\\ \mathbf{elif}\;c \leq 2.281947855931773 \cdot 10^{+159}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \mathsf{fma}\left(\frac{d}{c}, b, a\right)\\ \end{array} \]

Alternatives

Alternative 1
Error10.2
Cost20560
\[\begin{array}{l} t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot \frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, c, d \cdot b\right)}}\\ \mathbf{if}\;c \leq -4.57134307228816 \cdot 10^{+58}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;c \leq -1 \cdot 10^{-115}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 10^{-130}:\\ \;\;\;\;\frac{b}{d} + \frac{a \cdot \frac{c}{d}}{d}\\ \mathbf{elif}\;c \leq 2.281947855931773 \cdot 10^{+159}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \mathsf{fma}\left(\frac{d}{c}, b, a\right)\\ \end{array} \]
Alternative 2
Error11.3
Cost13968
\[\begin{array}{l} t_0 := \frac{d \cdot b + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -4.57134307228816 \cdot 10^{+58}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;c \leq -1 \cdot 10^{-115}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 10^{-130}:\\ \;\;\;\;\frac{b}{d} + \frac{a \cdot \frac{c}{d}}{d}\\ \mathbf{elif}\;c \leq 7.2010048922993756 \cdot 10^{+81}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \mathsf{fma}\left(\frac{d}{c}, b, a\right)\\ \end{array} \]
Alternative 3
Error11.4
Cost1488
\[\begin{array}{l} t_0 := \frac{d \cdot b + c \cdot a}{c \cdot c + d \cdot d}\\ t_1 := \frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{if}\;c \leq -4.57134307228816 \cdot 10^{+58}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -1 \cdot 10^{-115}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 10^{-130}:\\ \;\;\;\;\frac{b}{d} + \frac{a \cdot \frac{c}{d}}{d}\\ \mathbf{elif}\;c \leq 5.346374350427911 \cdot 10^{+74}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 4
Error20.6
Cost1232
\[\begin{array}{l} t_0 := \frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{if}\;d \leq -7.558892591188873 \cdot 10^{+161}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -1.4308316382960976 \cdot 10^{+60}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -925777994571.0144:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 1.917726867369815 \cdot 10^{+76}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 5
Error16.9
Cost968
\[\begin{array}{l} t_0 := \frac{b}{d} + \frac{a \cdot \frac{c}{d}}{d}\\ \mathbf{if}\;d \leq -1.8151233042098852 \cdot 10^{-55}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.917726867369815 \cdot 10^{+76}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 6
Error16.8
Cost968
\[\begin{array}{l} \mathbf{if}\;d \leq -1.8151233042098852 \cdot 10^{-55}:\\ \;\;\;\;\frac{b}{d} + \frac{a \cdot \frac{c}{d}}{d}\\ \mathbf{elif}\;d \leq 1.917726867369815 \cdot 10^{+76}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \end{array} \]
Alternative 7
Error24.7
Cost720
\[\begin{array}{l} \mathbf{if}\;d \leq -3.395567488801678 \cdot 10^{+109}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -1.0363538935864317 \cdot 10^{+67}:\\ \;\;\;\;\frac{\frac{b}{c}}{\frac{c}{d}}\\ \mathbf{elif}\;d \leq -1.8151233042098852 \cdot 10^{-55}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 1.917726867369815 \cdot 10^{+76}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 8
Error25.6
Cost720
\[\begin{array}{l} \mathbf{if}\;d \leq -7.145997256351953 \cdot 10^{+173}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq -9.781394454418386 \cdot 10^{+74}:\\ \;\;\;\;\frac{c}{d \cdot \frac{d}{a}}\\ \mathbf{elif}\;d \leq -1.8151233042098852 \cdot 10^{-55}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 1.917726867369815 \cdot 10^{+76}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 9
Error22.9
Cost584
\[\begin{array}{l} \mathbf{if}\;c \leq -1.4263770608205756 \cdot 10^{-28}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 26419729.609084405:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{c}{a}}\\ \end{array} \]
Alternative 10
Error22.9
Cost456
\[\begin{array}{l} \mathbf{if}\;c \leq -1.4263770608205756 \cdot 10^{-28}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 1.0595451483748359 \cdot 10^{-11}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 11
Error58.6
Cost192
\[\frac{a}{d} \]
Alternative 12
Error38.2
Cost192
\[\frac{b}{d} \]

Error

Reproduce

herbie shell --seed 2022317 
(FPCore (a b c d)
  :name "Complex division, real part"
  :precision binary64

  :herbie-target
  (if (< (fabs d) (fabs c)) (/ (+ a (* b (/ d c))) (+ c (* d (/ d c)))) (/ (+ b (* a (/ c d))) (+ d (* c (/ c d)))))

  (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))