Complex division, real part

?

Percentage Accurate: 61.7% → 86.3%
Time: 14.0s
Precision: binary64
Cost: 21961

?

\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;t_0 \leq -\infty \lor \neg \left(t_0 \leq 2 \cdot 10^{+303}\right):\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\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 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))))
   (if (or (<= t_0 (- INFINITY)) (not (<= t_0 2e+303)))
     (/ (fma (/ d c) b a) c)
     (/ (/ (fma a c (* b d)) (hypot c d)) (hypot c 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 t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d));
	double tmp;
	if ((t_0 <= -((double) INFINITY)) || !(t_0 <= 2e+303)) {
		tmp = fma((d / c), b, a) / c;
	} else {
		tmp = (fma(a, c, (b * d)) / hypot(c, d)) / hypot(c, 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)
	t_0 = Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d)))
	tmp = 0.0
	if ((t_0 <= Float64(-Inf)) || !(t_0 <= 2e+303))
		tmp = Float64(fma(Float64(d / c), b, a) / c);
	else
		tmp = Float64(Float64(fma(a, c, Float64(b * d)) / hypot(c, d)) / hypot(c, 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_] := Block[{t$95$0 = N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, (-Infinity)], N[Not[LessEqual[t$95$0, 2e+303]], $MachinePrecision]], N[(N[(N[(d / c), $MachinePrecision] * b + a), $MachinePrecision] / c), $MachinePrecision], 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]]]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\
\mathbf{if}\;t_0 \leq -\infty \lor \neg \left(t_0 \leq 2 \cdot 10^{+303}\right):\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{c}\\

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


\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 11 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

Original61.7%
Target99.3%
Herbie86.3%
\[\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 or 2e303 < (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d)))

    1. Initial program 21.7%

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

      [Start]21.7%

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

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

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

      add-sqr-sqrt [=>]21.7%

      \[ \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 [=>]21.7%

      \[ \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 [=>]21.7%

      \[ \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 [=>]21.7%

      \[ \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 [=>]30.5%

      \[ \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 -inf 30.0%

      \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\left(-1 \cdot \frac{d \cdot b}{c} + -1 \cdot a\right)} \]
    4. Taylor expanded in c around -inf 61.8%

      \[\leadsto \color{blue}{\frac{-1}{c}} \cdot \left(-1 \cdot \frac{d \cdot b}{c} + -1 \cdot a\right) \]
    5. Applied egg-rr21.7%

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

      [Start]61.8%

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

      expm1-log1p-u [=>]32.0%

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

      expm1-udef [=>]20.4%

      \[ \color{blue}{e^{\mathsf{log1p}\left(\frac{-1}{c} \cdot \left(-1 \cdot \frac{d \cdot b}{c} + -1 \cdot a\right)\right)} - 1} \]
    6. Simplified70.0%

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

      [Start]21.7%

      \[ e^{\mathsf{log1p}\left(\frac{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{c}\right)} - 1 \]

      expm1-def [=>]38.6%

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

      expm1-log1p [=>]70.0%

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

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

    1. Initial program 79.5%

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

      \[\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]79.5%

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

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

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

      add-sqr-sqrt [=>]79.5%

      \[ \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 [=>]79.5%

      \[ \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 [=>]79.5%

      \[ \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 [=>]79.5%

      \[ \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 [=>]97.9%

      \[ \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-rr98.0%

      \[\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]97.9%

      \[ \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/ [=>]98.0%

      \[ \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 [<=]98.0%

      \[ \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)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification88.3%

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

Alternatives

Alternative 1
Accuracy86.3%
Cost21961
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;t_0 \leq -\infty \lor \neg \left(t_0 \leq 2 \cdot 10^{+303}\right):\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]
Alternative 2
Accuracy82.4%
Cost7568
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -1.3 \cdot 10^{+87}:\\ \;\;\;\;\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)\\ \mathbf{elif}\;c \leq -1.12 \cdot 10^{-128}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 2.65 \cdot 10^{-148}:\\ \;\;\;\;\frac{b}{d} + a \cdot \frac{\frac{c}{d}}{d}\\ \mathbf{elif}\;c \leq 7.2 \cdot 10^{+59}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a + \frac{d}{\frac{c}{b}}}{\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]
Alternative 3
Accuracy82.7%
Cost7568
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -8 \cdot 10^{+86}:\\ \;\;\;\;\frac{\left(-a\right) - d \cdot \frac{b}{c}}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \leq -9.5 \cdot 10^{-129}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 5.8 \cdot 10^{-146}:\\ \;\;\;\;\frac{b}{d} + a \cdot \frac{\frac{c}{d}}{d}\\ \mathbf{elif}\;c \leq 6.2 \cdot 10^{+54}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a + \frac{d}{\frac{c}{b}}}{\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]
Alternative 4
Accuracy82.0%
Cost7376
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ t_1 := \frac{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{c}\\ \mathbf{if}\;c \leq -7.6 \cdot 10^{+87}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -2.7 \cdot 10^{-128}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.3 \cdot 10^{-146}:\\ \;\;\;\;\frac{b}{d} + a \cdot \frac{\frac{c}{d}}{d}\\ \mathbf{elif}\;c \leq 6 \cdot 10^{+55}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 5
Accuracy82.0%
Cost7376
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -2.8 \cdot 10^{+87}:\\ \;\;\;\;\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)\\ \mathbf{elif}\;c \leq -2.25 \cdot 10^{-128}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 1.22 \cdot 10^{-144}:\\ \;\;\;\;\frac{b}{d} + a \cdot \frac{\frac{c}{d}}{d}\\ \mathbf{elif}\;c \leq 3.9 \cdot 10^{+54}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{c}\\ \end{array} \]
Alternative 6
Accuracy82.3%
Cost1488
\[\begin{array}{l} t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\ t_1 := \left(a + \frac{d}{\frac{c}{b}}\right) \cdot \frac{1}{c}\\ \mathbf{if}\;c \leq -8 \cdot 10^{+86}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq -1.02 \cdot 10^{-129}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 2 \cdot 10^{-144}:\\ \;\;\;\;\frac{b}{d} + a \cdot \frac{\frac{c}{d}}{d}\\ \mathbf{elif}\;c \leq 1.15 \cdot 10^{+66}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 7
Accuracy67.8%
Cost1100
\[\begin{array}{l} t_0 := \left(a + \frac{d}{\frac{c}{b}}\right) \cdot \frac{1}{c}\\ \mathbf{if}\;c \leq -6 \cdot 10^{-90}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 10^{-175}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 2.2 \cdot 10^{-94}:\\ \;\;\;\;\frac{a \cdot c}{d} \cdot \frac{1}{d}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Accuracy61.1%
Cost972
\[\begin{array}{l} \mathbf{if}\;c \leq -4.8 \cdot 10^{-31}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 7.8 \cdot 10^{-176}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 7.5 \cdot 10^{-92}:\\ \;\;\;\;\frac{a \cdot c}{d} \cdot \frac{1}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 9
Accuracy76.4%
Cost969
\[\begin{array}{l} \mathbf{if}\;c \leq -2.6 \cdot 10^{-30} \lor \neg \left(c \leq 3.7 \cdot 10^{-65}\right):\\ \;\;\;\;\left(a + \frac{d}{\frac{c}{b}}\right) \cdot \frac{1}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + a \cdot \frac{\frac{c}{d}}{d}\\ \end{array} \]
Alternative 10
Accuracy62.1%
Cost456
\[\begin{array}{l} \mathbf{if}\;d \leq -1.45 \cdot 10^{-56}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 1.95 \cdot 10^{+127}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 11
Accuracy43.3%
Cost192
\[\frac{a}{c} \]

Reproduce?

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