?

Average Accuracy: 58.8% → 99.2%
Time: 17.7s
Precision: binary64
Cost: 26496

?

\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\frac{\mathsf{fma}\left(b, \frac{d}{\mathsf{hypot}\left(c, d\right)}, \frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{c}}\right)}{\mathsf{hypot}\left(c, d\right)} \]
(FPCore (a b c d)
 :precision binary64
 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (/ (fma b (/ d (hypot c d)) (/ a (/ (hypot c d) c))) (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) {
	return fma(b, (d / hypot(c, d)), (a / (hypot(c, d) / c))) / hypot(c, d);
}
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)
	return Float64(fma(b, Float64(d / hypot(c, d)), Float64(a / Float64(hypot(c, d) / c))) / hypot(c, d))
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_] := N[(N[(b * N[(d / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] + N[(a / N[(N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\frac{\mathsf{fma}\left(b, \frac{d}{\mathsf{hypot}\left(c, d\right)}, \frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{c}}\right)}{\mathsf{hypot}\left(c, d\right)}

Error?

Target

Original58.8%
Target99.3%
Herbie99.2%
\[\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. Initial program 58.8%

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

    [Start]58.8

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

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

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

    add-sqr-sqrt [=>]58.8

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

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

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

    \[ \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 [=>]73.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)}} \]
  3. Applied egg-rr73.4%

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

    [Start]73.4

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

    div-inv [=>]73.3

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

    *-commutative [<=]73.3

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

    fma-udef [=>]73.3

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

    distribute-lft-in [=>]73.3

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

    associate-*l/ [=>]73.3

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

    *-un-lft-identity [<=]73.3

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

    associate-*l/ [=>]73.4

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

    *-un-lft-identity [<=]73.4

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

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

    [Start]73.4

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

    +-commutative [=>]73.4

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

    associate-/l* [=>]85.0

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

    *-commutative [<=]85.0

    \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{d}} + \frac{\color{blue}{c \cdot a}}{\mathsf{hypot}\left(c, d\right)}\right) \]
  5. Applied egg-rr99.2%

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

    [Start]85.0

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

    associate-/r/ [<=]84.4

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

    clear-num [<=]85.2

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

    div-inv [=>]85.2

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

    fma-def [=>]85.2

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

    clear-num [<=]85.3

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

    *-commutative [=>]85.3

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

    associate-/l* [=>]99.2

    \[ \frac{\mathsf{fma}\left(b, \frac{d}{\mathsf{hypot}\left(c, d\right)}, \color{blue}{\frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{c}}}\right)}{\mathsf{hypot}\left(c, d\right)} \]
  6. Final simplification99.2%

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

Alternatives

Alternative 1
Accuracy88.5%
Cost22988
\[\begin{array}{l} t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right)}\\ t_1 := b \cdot d + c \cdot a\\ t_2 := \frac{t_1}{c \cdot c + d \cdot d}\\ \mathbf{if}\;t_2 \leq -\infty:\\ \;\;\;\;t_0 \cdot \left(a + \frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}\right)\\ \mathbf{elif}\;t_2 \leq -1 \cdot 10^{-102}:\\ \;\;\;\;\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}\\ \mathbf{elif}\;t_2 \leq 2 \cdot 10^{+262}:\\ \;\;\;\;t_0 \cdot \frac{t_1}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\mathsf{fma}\left(b, 1, \frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{c}}\right)}{\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]
Alternative 2
Accuracy98.9%
Cost20352
\[\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{d}} + a \cdot \frac{c}{\mathsf{hypot}\left(c, d\right)}\right) \]
Alternative 3
Accuracy87.9%
Cost16844
\[\begin{array}{l} t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right)}\\ t_1 := t_0 \cdot \left(a + \frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}\right)\\ t_2 := b \cdot d + c \cdot a\\ t_3 := \frac{t_2}{c \cdot c + d \cdot d}\\ \mathbf{if}\;t_3 \leq -\infty:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_3 \leq -1 \cdot 10^{-102}:\\ \;\;\;\;\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{fma}\left(c, c, d \cdot d\right)}\\ \mathbf{elif}\;t_3 \leq 2 \cdot 10^{+262}:\\ \;\;\;\;t_0 \cdot \frac{t_2}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 4
Accuracy82.5%
Cost14028
\[\begin{array}{l} \mathbf{if}\;c \leq -1.4 \cdot 10^{+38}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;c \leq -1.22 \cdot 10^{-67}:\\ \;\;\;\;\frac{b \cdot d + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 1.6 \cdot 10^{-131}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(a + \frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}\right)\\ \end{array} \]
Alternative 5
Accuracy79.6%
Cost7436
\[\begin{array}{l} \mathbf{if}\;d \leq -0.65:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \mathbf{elif}\;d \leq 3 \cdot 10^{-132}:\\ \;\;\;\;\frac{1}{c} \cdot \left(a + \frac{b \cdot d}{c}\right)\\ \mathbf{elif}\;d \leq 3.4 \cdot 10^{+46}:\\ \;\;\;\;\frac{b \cdot d + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b + \frac{c}{\frac{d}{a}}}{\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]
Alternative 6
Accuracy79.4%
Cost1356
\[\begin{array}{l} \mathbf{if}\;d \leq -0.56:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \mathbf{elif}\;d \leq 5.1 \cdot 10^{-133}:\\ \;\;\;\;\frac{1}{c} \cdot \left(a + \frac{b \cdot d}{c}\right)\\ \mathbf{elif}\;d \leq 4.5 \cdot 10^{+55}:\\ \;\;\;\;\frac{b \cdot d + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \end{array} \]
Alternative 7
Accuracy76.5%
Cost1232
\[\begin{array}{l} t_0 := \frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \mathbf{if}\;d \leq -0.6:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 4.6 \cdot 10^{-78}:\\ \;\;\;\;\frac{1}{c} \cdot \left(a + \frac{b \cdot d}{c}\right)\\ \mathbf{elif}\;d \leq 4.6 \cdot 10^{+40}:\\ \;\;\;\;\frac{d}{\frac{c \cdot c + d \cdot d}{b}}\\ \mathbf{elif}\;d \leq 7.2 \cdot 10^{+45}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Accuracy76.5%
Cost1232
\[\begin{array}{l} t_0 := \frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \mathbf{if}\;d \leq -0.56:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 4.6 \cdot 10^{-78}:\\ \;\;\;\;\frac{1}{c} \cdot \left(a + \frac{b \cdot d}{c}\right)\\ \mathbf{elif}\;d \leq 8.8 \cdot 10^{+40}:\\ \;\;\;\;\frac{b \cdot d}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 2.3 \cdot 10^{+44}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 9
Accuracy71.1%
Cost969
\[\begin{array}{l} \mathbf{if}\;d \leq -1.32 \cdot 10^{+16} \lor \neg \left(d \leq 3 \cdot 10^{+44}\right):\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{c} \cdot \left(a + \frac{b \cdot d}{c}\right)\\ \end{array} \]
Alternative 10
Accuracy76.7%
Cost969
\[\begin{array}{l} \mathbf{if}\;d \leq -0.6 \lor \neg \left(d \leq 1.24 \cdot 10^{+45}\right):\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{c} \cdot \left(a + \frac{b \cdot d}{c}\right)\\ \end{array} \]
Alternative 11
Accuracy76.9%
Cost969
\[\begin{array}{l} \mathbf{if}\;d \leq -0.6 \lor \neg \left(d \leq 3.9 \cdot 10^{+46}\right):\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{c} \cdot \left(a + \frac{b \cdot d}{c}\right)\\ \end{array} \]
Alternative 12
Accuracy63.0%
Cost844
\[\begin{array}{l} \mathbf{if}\;c \leq -2.6 \cdot 10^{-64}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq -5.6 \cdot 10^{-223}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq -4.8 \cdot 10^{-239}:\\ \;\;\;\;\frac{a}{d \cdot \frac{d}{c}}\\ \mathbf{elif}\;c \leq 1.4 \cdot 10^{-16}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 13
Accuracy64.1%
Cost456
\[\begin{array}{l} \mathbf{if}\;d \leq -8.6 \cdot 10^{+17}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 5 \cdot 10^{+44}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 14
Accuracy41.6%
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

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