Complex division, real part

?

Percentage Accurate: 70.1% → 88.8%
Time: 20.7s
Precision: binary64
Cost: 20932

?

\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} \mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \leq \infty:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \end{array} \]
(FPCore (a b c d)
 :precision binary64
 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (if (<= (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))) INFINITY)
   (/ (/ (fma a c (* b d)) (hypot c d)) (hypot c d))
   (+ (/ b d) (* (/ c d) (/ a d)))))
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 tmp;
	if ((((a * c) + (b * d)) / ((c * c) + (d * d))) <= ((double) INFINITY)) {
		tmp = (fma(a, c, (b * d)) / hypot(c, d)) / hypot(c, d);
	} else {
		tmp = (b / d) + ((c / d) * (a / d));
	}
	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)
	tmp = 0.0
	if (Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))) <= Inf)
		tmp = Float64(Float64(fma(a, c, Float64(b * d)) / hypot(c, d)) / hypot(c, d));
	else
		tmp = Float64(Float64(b / d) + Float64(Float64(c / d) * Float64(a / d)));
	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_] := If[LessEqual[N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], Infinity], N[(N[(N[(a * c + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision], N[(N[(b / d), $MachinePrecision] + N[(N[(c / d), $MachinePrecision] * N[(a / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
\mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \leq \infty:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\

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


\end{array}

Local Percentage Accuracy vs ?

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 10 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Bogosity?

Bogosity

Target

Original70.1%
Target99.4%
Herbie88.8%
\[\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 2 regimes
  2. if (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) < +inf.0

    1. Initial program 83.2%

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

      [Start]83.2%

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

      *-un-lft-identity [=>]83.2%

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

      add-sqr-sqrt [=>]83.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 [=>]83.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 [=>]83.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 [=>]83.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 [=>]96.1%

      \[ \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. Applied egg-rr96.3%

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

      [Start]96.1%

      \[ \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)} \]

      associate-*l/ [=>]96.3%

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

      *-un-lft-identity [<=]96.3%

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

    if +inf.0 < (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d)))

    1. Initial program 0.0%

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

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

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

      [Start]55.7%

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

      unpow2 [=>]55.7%

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

      times-frac [=>]58.9%

      \[ \frac{b}{d} + \color{blue}{\frac{c}{d} \cdot \frac{a}{d}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification91.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \leq \infty:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \end{array} \]

Alternatives

Alternative 1
Accuracy88.8%
Cost20932
\[\begin{array}{l} \mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \leq \infty:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \end{array} \]
Alternative 2
Accuracy86.6%
Cost1356
\[\begin{array}{l} \mathbf{if}\;c \leq -12500000000000:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;c \leq 1.9 \cdot 10^{-105}:\\ \;\;\;\;\frac{b}{d} + \frac{a \cdot \frac{c}{d}}{d}\\ \mathbf{elif}\;c \leq 4.5 \cdot 10^{+15}:\\ \;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{\frac{c \cdot c}{b}}\\ \end{array} \]
Alternative 3
Accuracy75.0%
Cost1232
\[\begin{array}{l} t_0 := \frac{b}{d + \frac{c \cdot c}{d}}\\ t_1 := \frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{if}\;c \leq -12500000000000:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -6.2 \cdot 10^{-180}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -2.95 \cdot 10^{-230}:\\ \;\;\;\;\frac{a \cdot c}{d \cdot d}\\ \mathbf{elif}\;c \leq 1.35 \cdot 10^{-71}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 4
Accuracy74.7%
Cost1232
\[\begin{array}{l} t_0 := \frac{b}{d + \frac{c \cdot c}{d}}\\ \mathbf{if}\;c \leq -12500000000000:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;c \leq -3.6 \cdot 10^{-179}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -2.95 \cdot 10^{-230}:\\ \;\;\;\;\frac{a \cdot c}{d \cdot d}\\ \mathbf{elif}\;c \leq 1.1 \cdot 10^{-71}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{\frac{c \cdot c}{b}}\\ \end{array} \]
Alternative 5
Accuracy72.2%
Cost1104
\[\begin{array}{l} t_0 := \frac{b}{d + \frac{c \cdot c}{d}}\\ \mathbf{if}\;c \leq -2 \cdot 10^{+14}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -1.45 \cdot 10^{-179}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq -2.95 \cdot 10^{-230}:\\ \;\;\;\;\frac{a \cdot c}{d \cdot d}\\ \mathbf{elif}\;c \leq 1.4 \cdot 10^{+24}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 6
Accuracy81.9%
Cost968
\[\begin{array}{l} \mathbf{if}\;c \leq -12500000000000:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;c \leq 7.5 \cdot 10^{-86}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{\frac{c \cdot c}{b}}\\ \end{array} \]
Alternative 7
Accuracy82.9%
Cost968
\[\begin{array}{l} \mathbf{if}\;c \leq -12500000000000:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c} \cdot \frac{b}{c}\\ \mathbf{elif}\;c \leq 1.45 \cdot 10^{-87}:\\ \;\;\;\;\frac{b}{d} + \frac{a \cdot \frac{c}{d}}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{\frac{c \cdot c}{b}}\\ \end{array} \]
Alternative 8
Accuracy67.6%
Cost844
\[\begin{array}{l} \mathbf{if}\;c \leq -1.6 \cdot 10^{+14}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -6.8 \cdot 10^{-180}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq -2.95 \cdot 10^{-230}:\\ \;\;\;\;\frac{a \cdot c}{d \cdot d}\\ \mathbf{elif}\;c \leq 8.5 \cdot 10^{-62}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 9
Accuracy69.9%
Cost456
\[\begin{array}{l} \mathbf{if}\;c \leq -12500000000000:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 1.38 \cdot 10^{-61}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 10
Accuracy43.1%
Cost192
\[\frac{a}{c} \]

Reproduce?

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