Complex division, real part

?

Percentage Accurate: 62.6% → 84.9%
Time: 16.2s
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}\;d \leq -6 \cdot 10^{+74}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{elif}\;d \leq -4.4 \cdot 10^{-102}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq 2.2 \cdot 10^{-203}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot \frac{b}{c}}{c}\\ \mathbf{elif}\;d \leq 1.45 \cdot 10^{+174}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \left(b + \frac{c}{\frac{d}{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 (<= d -6e+74)
     (+ (/ b d) (* (/ c d) (/ a d)))
     (if (<= d -4.4e-102)
       t_1
       (if (<= d 2.2e-203)
         (+ (/ a c) (/ (* d (/ b c)) c))
         (if (<= d 1.45e+174) t_1 (* t_0 (+ b (/ c (/ 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);
	double t_1 = t_0 * (fma(a, c, (d * b)) / hypot(c, d));
	double tmp;
	if (d <= -6e+74) {
		tmp = (b / d) + ((c / d) * (a / d));
	} else if (d <= -4.4e-102) {
		tmp = t_1;
	} else if (d <= 2.2e-203) {
		tmp = (a / c) + ((d * (b / c)) / c);
	} else if (d <= 1.45e+174) {
		tmp = t_1;
	} else {
		tmp = t_0 * (b + (c / (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(1.0 / hypot(c, d))
	t_1 = Float64(t_0 * Float64(fma(a, c, Float64(d * b)) / hypot(c, d)))
	tmp = 0.0
	if (d <= -6e+74)
		tmp = Float64(Float64(b / d) + Float64(Float64(c / d) * Float64(a / d)));
	elseif (d <= -4.4e-102)
		tmp = t_1;
	elseif (d <= 2.2e-203)
		tmp = Float64(Float64(a / c) + Float64(Float64(d * Float64(b / c)) / c));
	elseif (d <= 1.45e+174)
		tmp = t_1;
	else
		tmp = Float64(t_0 * Float64(b + Float64(c / 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[(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[d, -6e+74], N[(N[(b / d), $MachinePrecision] + N[(N[(c / d), $MachinePrecision] * N[(a / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, -4.4e-102], t$95$1, If[LessEqual[d, 2.2e-203], N[(N[(a / c), $MachinePrecision] + N[(N[(d * N[(b / c), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 1.45e+174], t$95$1, N[(t$95$0 * N[(b + N[(c / N[(d / a), $MachinePrecision]), $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)}\\
t_1 := t_0 \cdot \frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{if}\;d \leq -6 \cdot 10^{+74}:\\
\;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\

\mathbf{elif}\;d \leq -4.4 \cdot 10^{-102}:\\
\;\;\;\;t_1\\

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

\mathbf{elif}\;d \leq 1.45 \cdot 10^{+174}:\\
\;\;\;\;t_1\\

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


\end{array}

Local Percentage Accuracy?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Herbie found 12 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Target

Original62.6%
Target99.3%
Herbie84.9%
\[\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 < -6e74

    1. Initial program 47.5%

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{c}{d}, \frac{a}{d}, \frac{b}{d}\right)} \]
      Step-by-step derivation

      [Start]84.0

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

      +-commutative [=>]84.0

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

      unpow2 [=>]84.0

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

      times-frac [=>]88.6

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

      fma-def [=>]88.6

      \[ \color{blue}{\mathsf{fma}\left(\frac{c}{d}, \frac{a}{d}, \frac{b}{d}\right)} \]
    4. Applied egg-rr88.6%

      \[\leadsto \color{blue}{\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}} \]
      Step-by-step derivation

      [Start]88.6

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

      fma-udef [=>]88.6

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

      +-commutative [=>]88.6

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

    if -6e74 < d < -4.40000000000000026e-102 or 2.2e-203 < d < 1.45e174

    1. Initial program 80.2%

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

      [Start]80.2

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

      *-un-lft-identity [=>]80.2

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

      add-sqr-sqrt [=>]80.2

      \[ \frac{1 \cdot \left(a \cdot c + b \cdot d\right)}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}} \]

      times-frac [=>]80.2

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

      hypot-def [=>]80.3

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

      fma-def [=>]80.3

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

      hypot-def [=>]91.4

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

    if -4.40000000000000026e-102 < d < 2.2e-203

    1. Initial program 66.4%

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

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

      \[\leadsto \color{blue}{\frac{a}{c} + \frac{d \cdot b}{c \cdot c}} \]
      Step-by-step derivation

      [Start]75.2

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

      unpow2 [=>]75.2

      \[ \frac{a}{c} + \frac{d \cdot b}{\color{blue}{c \cdot c}} \]
    4. Taylor expanded in d around 0 75.2%

      \[\leadsto \frac{a}{c} + \color{blue}{\frac{d \cdot b}{{c}^{2}}} \]
    5. Simplified75.4%

      \[\leadsto \frac{a}{c} + \color{blue}{d \cdot \frac{b}{c \cdot c}} \]
      Step-by-step derivation

      [Start]75.2

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

      unpow2 [=>]75.2

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

      associate-*r/ [<=]75.4

      \[ \frac{a}{c} + \color{blue}{d \cdot \frac{b}{c \cdot c}} \]
    6. Applied egg-rr90.9%

      \[\leadsto \frac{a}{c} + \color{blue}{\frac{\frac{b}{c} \cdot d}{c}} \]
      Step-by-step derivation

      [Start]75.4

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

      *-commutative [=>]75.4

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

      associate-/r* [=>]84.4

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

      associate-*l/ [=>]90.9

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

    if 1.45e174 < d

    1. Initial program 33.1%

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

      \[\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)}} \]
      Step-by-step derivation

      [Start]33.1

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

      *-un-lft-identity [=>]33.1

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

      add-sqr-sqrt [=>]33.1

      \[ \frac{1 \cdot \left(a \cdot c + b \cdot d\right)}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}} \]

      times-frac [=>]33.1

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

      hypot-def [=>]33.1

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

      fma-def [=>]33.1

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

      hypot-def [=>]56.6

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

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

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\left(b + \frac{c}{\frac{d}{a}}\right)} \]
      Step-by-step derivation

      [Start]76.2

      \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(b + \frac{c \cdot a}{d}\right) \]

      associate-/l* [=>]93.1

      \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(b + \color{blue}{\frac{c}{\frac{d}{a}}}\right) \]
  3. Recombined 4 regimes into one program.
  4. Final simplification91.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \leq -6 \cdot 10^{+74}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{elif}\;d \leq -4.4 \cdot 10^{-102}:\\ \;\;\;\;\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 2.2 \cdot 10^{-203}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot \frac{b}{c}}{c}\\ \mathbf{elif}\;d \leq 1.45 \cdot 10^{+174}:\\ \;\;\;\;\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 \left(b + \frac{c}{\frac{d}{a}}\right)\\ \end{array} \]

Alternatives

Alternative 1
Accuracy82.0%
Cost2000
\[\begin{array}{l} t_0 := d \cdot d + c \cdot c\\ t_1 := \frac{d}{\frac{t_0}{b}} + \frac{c}{\frac{t_0}{a}}\\ t_2 := \frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{if}\;c \leq -9 \cdot 10^{+118}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;c \leq -2.05 \cdot 10^{-45}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq 4 \cdot 10^{-67}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{elif}\;c \leq 4 \cdot 10^{+153}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 2
Accuracy81.6%
Cost1488
\[\begin{array}{l} t_0 := \frac{d \cdot b + c \cdot a}{d \cdot d + c \cdot c}\\ t_1 := \frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{if}\;d \leq -7.3 \cdot 10^{+71}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -2.5 \cdot 10^{-102}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 3.4 \cdot 10^{-34}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot \frac{b}{c}}{c}\\ \mathbf{elif}\;d \leq 2.6 \cdot 10^{+108}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 3
Accuracy74.9%
Cost1233
\[\begin{array}{l} t_0 := \frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{if}\;d \leq -1.52 \cdot 10^{+53}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -3.9 \cdot 10^{+39}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq -8.5 \cdot 10^{-87} \lor \neg \left(d \leq 3.8 \cdot 10^{+26}\right):\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot \frac{b}{c}}{c}\\ \end{array} \]
Alternative 4
Accuracy75.1%
Cost1232
\[\begin{array}{l} t_0 := \frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{if}\;d \leq -4.4 \cdot 10^{+53}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -1.2 \cdot 10^{+43}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq -1.55 \cdot 10^{-85}:\\ \;\;\;\;\frac{b}{d} + a \cdot \frac{c}{d \cdot d}\\ \mathbf{elif}\;d \leq 6.5 \cdot 10^{+31}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot \frac{b}{c}}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Accuracy75.0%
Cost1232
\[\begin{array}{l} t_0 := \frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{if}\;d \leq -2.9 \cdot 10^{+53}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq -4.4 \cdot 10^{+43}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;d \leq -7.8 \cdot 10^{-87}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{\frac{d \cdot d}{c}}\\ \mathbf{elif}\;d \leq 6.5 \cdot 10^{+35}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot \frac{b}{c}}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 6
Accuracy67.7%
Cost969
\[\begin{array}{l} \mathbf{if}\;c \leq -1 \cdot 10^{-21} \lor \neg \left(c \leq 2.05 \cdot 10^{-47}\right):\\ \;\;\;\;\frac{a}{c} + d \cdot \frac{b}{c \cdot c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 7
Accuracy69.8%
Cost969
\[\begin{array}{l} \mathbf{if}\;d \leq -8.6 \cdot 10^{-51} \lor \neg \left(d \leq 3 \cdot 10^{+27}\right):\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \end{array} \]
Alternative 8
Accuracy70.1%
Cost969
\[\begin{array}{l} \mathbf{if}\;d \leq -8.6 \cdot 10^{-51} \lor \neg \left(d \leq 1.05 \cdot 10^{+27}\right):\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{d \cdot \frac{b}{c}}{c}\\ \end{array} \]
Alternative 9
Accuracy62.7%
Cost456
\[\begin{array}{l} \mathbf{if}\;d \leq -1.65 \cdot 10^{-82}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 24000000000:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 10
Accuracy42.4%
Cost324
\[\begin{array}{l} \mathbf{if}\;d \leq -5.2 \cdot 10^{+93}:\\ \;\;\;\;\frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 11
Accuracy42.4%
Cost192
\[\frac{a}{c} \]

Reproduce?

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