Average Error: 26.2 → 9.5
Time: 12.0s
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)} \cdot \frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{if}\;d \leq -5 \cdot 10^{+114}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{elif}\;d \leq -4.8 \cdot 10^{-115}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.15 \cdot 10^{-134}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{\frac{c}{d}}}{c}\\ \mathbf{elif}\;d \leq 8 \cdot 10^{+76}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{\frac{c}{d}}{\frac{d}{a}}\\ \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)) (/ (fma a c (* d b)) (hypot c d)))))
   (if (<= d -5e+114)
     (+ (/ b d) (* (/ c d) (/ a d)))
     (if (<= d -4.8e-115)
       t_0
       (if (<= d 1.15e-134)
         (+ (/ a c) (/ (/ b (/ c d)) c))
         (if (<= d 8e+76) t_0 (+ (/ b d) (/ (/ c d) (/ d 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)) * (fma(a, c, (d * b)) / hypot(c, d));
	double tmp;
	if (d <= -5e+114) {
		tmp = (b / d) + ((c / d) * (a / d));
	} else if (d <= -4.8e-115) {
		tmp = t_0;
	} else if (d <= 1.15e-134) {
		tmp = (a / c) + ((b / (c / d)) / c);
	} else if (d <= 8e+76) {
		tmp = t_0;
	} else {
		tmp = (b / d) + ((c / d) / (d / 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(Float64(1.0 / hypot(c, d)) * Float64(fma(a, c, Float64(d * b)) / hypot(c, d)))
	tmp = 0.0
	if (d <= -5e+114)
		tmp = Float64(Float64(b / d) + Float64(Float64(c / d) * Float64(a / d)));
	elseif (d <= -4.8e-115)
		tmp = t_0;
	elseif (d <= 1.15e-134)
		tmp = Float64(Float64(a / c) + Float64(Float64(b / Float64(c / d)) / c));
	elseif (d <= 8e+76)
		tmp = t_0;
	else
		tmp = Float64(Float64(b / d) + Float64(Float64(c / d) / Float64(d / 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[(N[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(N[(a * c + N[(d * b), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -5e+114], N[(N[(b / d), $MachinePrecision] + N[(N[(c / d), $MachinePrecision] * N[(a / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, -4.8e-115], t$95$0, If[LessEqual[d, 1.15e-134], N[(N[(a / c), $MachinePrecision] + N[(N[(b / N[(c / d), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 8e+76], t$95$0, N[(N[(b / d), $MachinePrecision] + N[(N[(c / d), $MachinePrecision] / N[(d / a), $MachinePrecision]), $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)} \cdot \frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{if}\;d \leq -5 \cdot 10^{+114}:\\
\;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\

\mathbf{elif}\;d \leq -4.8 \cdot 10^{-115}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;d \leq 8 \cdot 10^{+76}:\\
\;\;\;\;t_0\\

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


\end{array}

Error

Target

Original26.2
Target0.5
Herbie9.5
\[\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 d < -5.0000000000000001e114

    1. Initial program 40.8

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

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

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

    if -5.0000000000000001e114 < d < -4.80000000000000042e-115 or 1.15e-134 < d < 8.0000000000000004e76

    1. Initial program 16.0

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

    if -4.80000000000000042e-115 < d < 1.15e-134

    1. Initial program 23.6

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
    2. Applied egg-rr13.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. Taylor expanded in c around inf 30.7

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\left(a + \frac{d}{\frac{c}{b}}\right)} \]
      Proof
      (+.f64 a (/.f64 d (/.f64 c b))): 0 points increase in error, 0 points decrease in error
      (+.f64 a (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 d b) c))): 26 points increase in error, 20 points decrease in error
    5. Taylor expanded in c around inf 10.7

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

      \[\leadsto \color{blue}{\frac{a}{c} + \frac{\frac{b}{\frac{c}{d}}}{c}} \]
      Proof
      (+.f64 (/.f64 a c) (/.f64 (/.f64 b (/.f64 c d)) c)): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 a c) (/.f64 (Rewrite<= associate-/l*_binary64 (/.f64 (*.f64 b d) c)) c)): 19 points increase in error, 9 points decrease in error
      (+.f64 (/.f64 a c) (/.f64 (/.f64 (Rewrite<= *-commutative_binary64 (*.f64 d b)) c) c)): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 a c) (Rewrite<= associate-/r*_binary64 (/.f64 (*.f64 d b) (*.f64 c c)))): 24 points increase in error, 10 points decrease in error
      (+.f64 (/.f64 a c) (/.f64 (*.f64 d b) (Rewrite<= unpow2_binary64 (pow.f64 c 2)))): 0 points increase in error, 0 points decrease in error

    if 8.0000000000000004e76 < d

    1. Initial program 37.1

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

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

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}} \]
      Proof
      (+.f64 (/.f64 b d) (*.f64 (/.f64 c d) (/.f64 a d))): 0 points increase in error, 0 points decrease in error
      (+.f64 (/.f64 b d) (Rewrite<= times-frac_binary64 (/.f64 (*.f64 c a) (*.f64 d d)))): 38 points increase in error, 13 points decrease in error
      (+.f64 (/.f64 b d) (/.f64 (*.f64 c a) (Rewrite<= unpow2_binary64 (pow.f64 d 2)))): 0 points increase in error, 0 points decrease in error
    4. Applied egg-rr10.5

      \[\leadsto \frac{b}{d} + \color{blue}{\frac{\frac{c}{d}}{\frac{d}{a}}} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification9.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -5 \cdot 10^{+114}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{elif}\;d \leq -4.8 \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}\;d \leq 1.15 \cdot 10^{-134}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{\frac{c}{d}}}{c}\\ \mathbf{elif}\;d \leq 8 \cdot 10^{+76}:\\ \;\;\;\;\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{b}{d} + \frac{\frac{c}{d}}{\frac{d}{a}}\\ \end{array} \]

Alternatives

Alternative 1
Error14.8
Cost1492
\[\begin{array}{l} \mathbf{if}\;d \leq -3.45 \cdot 10^{+45}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{elif}\;d \leq -1.28 \cdot 10^{-17}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;d \leq -5.4 \cdot 10^{-48}:\\ \;\;\;\;\frac{b}{d} + a \cdot \frac{c}{d \cdot d}\\ \mathbf{elif}\;d \leq 1.15 \cdot 10^{-15}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{\frac{c}{d}}}{c}\\ \mathbf{elif}\;d \leq 1.45 \cdot 10^{+76}:\\ \;\;\;\;\frac{d}{\frac{c}{\frac{b}{c}} + \frac{d}{\frac{b}{d}}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{\frac{c}{d}}{\frac{d}{a}}\\ \end{array} \]
Alternative 2
Error11.2
Cost1488
\[\begin{array}{l} t_0 := \frac{d \cdot b + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;d \leq -1.5 \cdot 10^{+116}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{elif}\;d \leq -2.4 \cdot 10^{-136}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 2.9 \cdot 10^{-128}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{\frac{c}{d}}}{c}\\ \mathbf{elif}\;d \leq 8.8 \cdot 10^{+75}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{\frac{c}{d}}{\frac{d}{a}}\\ \end{array} \]
Alternative 3
Error23.4
Cost1364
\[\begin{array}{l} t_0 := a \cdot \frac{c}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -8 \cdot 10^{+164}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -3.7 \cdot 10^{+87}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq -1.22 \cdot 10^{-62}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.65 \cdot 10^{-135}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 1.62 \cdot 10^{+72}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 4
Error20.4
Cost1232
\[\begin{array}{l} t_0 := \frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{if}\;c \leq -1.5 \cdot 10^{+121}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -7.1 \cdot 10^{+87}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq -1.36 \cdot 10^{-62}:\\ \;\;\;\;a \cdot \frac{c}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 1.32 \cdot 10^{-102}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Error17.1
Cost1232
\[\begin{array}{l} t_0 := \frac{b}{d} + a \cdot \frac{c}{d \cdot d}\\ \mathbf{if}\;d \leq -1.7 \cdot 10^{+36}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -8.8 \cdot 10^{-17}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;d \leq -6.5 \cdot 10^{-49}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 8.8 \cdot 10^{-14}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{\frac{c}{d}}}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 6
Error14.9
Cost1232
\[\begin{array}{l} t_0 := \frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{if}\;d \leq -8.6 \cdot 10^{+43}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -1.2 \cdot 10^{-17}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;d \leq -1.08 \cdot 10^{-47}:\\ \;\;\;\;\frac{b}{d} + a \cdot \frac{c}{d \cdot d}\\ \mathbf{elif}\;d \leq 9 \cdot 10^{-14}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{\frac{c}{d}}}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 7
Error14.9
Cost1232
\[\begin{array}{l} \mathbf{if}\;d \leq -9.5 \cdot 10^{+41}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{elif}\;d \leq -1.28 \cdot 10^{-17}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;d \leq -1.08 \cdot 10^{-47}:\\ \;\;\;\;\frac{b}{d} + a \cdot \frac{c}{d \cdot d}\\ \mathbf{elif}\;d \leq 1.05 \cdot 10^{-13}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{\frac{c}{d}}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{\frac{a}{d}}{\frac{d}{c}}\\ \end{array} \]
Alternative 8
Error23.1
Cost456
\[\begin{array}{l} \mathbf{if}\;d \leq -3.9 \cdot 10^{+36}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 3.9 \cdot 10^{-14}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 9
Error37.7
Cost192
\[\frac{a}{c} \]

Error

Reproduce

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