?

Average Accuracy: 59.2% → 98.9%
Time: 13.9s
Precision: binary64
Cost: 20352

?

\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
\[\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{d}} + \frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{c}}\right) \]
(FPCore (a b c d)
 :precision binary64
 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (* (/ 1.0 (hypot c d)) (+ (/ b (/ (hypot c d) d)) (/ a (/ (hypot c d) c)))))
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 (1.0 / hypot(c, d)) * ((b / (hypot(c, d) / d)) + (a / (hypot(c, d) / c)));
}
public static double code(double a, double b, double c, double d) {
	return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
public static double code(double a, double b, double c, double d) {
	return (1.0 / Math.hypot(c, d)) * ((b / (Math.hypot(c, d) / d)) + (a / (Math.hypot(c, d) / c)));
}
def code(a, b, c, d):
	return ((a * c) + (b * d)) / ((c * c) + (d * d))
def code(a, b, c, d):
	return (1.0 / math.hypot(c, d)) * ((b / (math.hypot(c, d) / d)) + (a / (math.hypot(c, d) / c)))
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(Float64(1.0 / hypot(c, d)) * Float64(Float64(b / Float64(hypot(c, d) / d)) + Float64(a / Float64(hypot(c, d) / c))))
end
function tmp = code(a, b, c, d)
	tmp = ((a * c) + (b * d)) / ((c * c) + (d * d));
end
function tmp = code(a, b, c, d)
	tmp = (1.0 / hypot(c, d)) * ((b / (hypot(c, d) / d)) + (a / (hypot(c, d) / c)));
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[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(N[(b / N[(N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision] / d), $MachinePrecision]), $MachinePrecision] + N[(a / N[(N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{d}} + \frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{c}}\right)

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original59.2%
Target99.4%
Herbie98.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. Initial program 59.2%

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

    \[\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]59.2

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

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

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

    add-sqr-sqrt [=>]59.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 [=>]59.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 [=>]59.2

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

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

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

    \[\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]74.0

    \[ \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.8

    \[ \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.8

    \[ \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.8

    \[ \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.8

    \[ \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.9

    \[ \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.9

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

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

    \[ \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. Simplified98.9%

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

    [Start]74.0

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

    \[ \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.6

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

    associate-/l* [=>]98.9

    \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{d}} + \color{blue}{\frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{c}}}\right) \]
  5. Final simplification98.9%

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

Alternatives

Alternative 1
Accuracy88.8%
Cost22089
\[\begin{array}{l} t_0 := \frac{c \cdot a + d \cdot b}{c \cdot c + d \cdot d}\\ t_1 := \frac{1}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{if}\;t_0 \leq -\infty \lor \neg \left(t_0 \leq 5 \cdot 10^{+259}\right):\\ \;\;\;\;t_1 \cdot \left(\frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{d}} + a\right)\\ \mathbf{else}:\\ \;\;\;\;t_1 \cdot \frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]
Alternative 2
Accuracy77.8%
Cost1224
\[\begin{array}{l} \mathbf{if}\;d \leq -1.1 \cdot 10^{+116}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;d \leq -1.4 \cdot 10^{-48}:\\ \;\;\;\;\frac{c \cdot a + d \cdot b}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;d \leq 206000000:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d}{\frac{c}{b}}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d \cdot \frac{d}{a}}\\ \end{array} \]
Alternative 3
Accuracy70.3%
Cost969
\[\begin{array}{l} \mathbf{if}\;d \leq -6 \cdot 10^{+33} \lor \neg \left(d \leq 64000000000\right):\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{c} \cdot \left(a + \frac{b}{\frac{c}{d}}\right)\\ \end{array} \]
Alternative 4
Accuracy70.0%
Cost969
\[\begin{array}{l} \mathbf{if}\;d \leq -1.8 \cdot 10^{+32} \lor \neg \left(d \leq 18000000000\right):\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d}{\frac{c}{b}}}{c}\\ \end{array} \]
Alternative 5
Accuracy76.3%
Cost969
\[\begin{array}{l} \mathbf{if}\;d \leq -2.2 \cdot 10^{+19} \lor \neg \left(d \leq 72\right):\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d}{\frac{c}{b}}}{c}\\ \end{array} \]
Alternative 6
Accuracy75.8%
Cost968
\[\begin{array}{l} \mathbf{if}\;d \leq -2.15 \cdot 10^{+19}:\\ \;\;\;\;\frac{b}{d} + \frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{elif}\;d \leq 185:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d}{\frac{c}{b}}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d \cdot \frac{d}{a}}\\ \end{array} \]
Alternative 7
Accuracy63.4%
Cost844
\[\begin{array}{l} \mathbf{if}\;c \leq -8.6 \cdot 10^{+19}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 1.05 \cdot 10^{-6}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;c \leq 2.3 \cdot 10^{+34}:\\ \;\;\;\;\frac{a}{d} \cdot \frac{c}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 8
Accuracy64.0%
Cost456
\[\begin{array}{l} \mathbf{if}\;c \leq -2.5 \cdot 10^{+19}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 4.05 \cdot 10^{+34}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 9
Accuracy41.8%
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

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