Average Error: 26.0 → 0.7
Time: 15.3s
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

Original26.0
Target0.4
Herbie0.7
\[\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 26.0

    \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \]
  2. Applied egg-rr16.8

    \[\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)}} \]
  3. Applied egg-rr16.8

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

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

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

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

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

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

    \[\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
Error10.0
Cost20296
\[\begin{array}{l} t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{if}\;c \leq -1.16 \cdot 10^{+157}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;c \leq -1.7 \cdot 10^{-103}:\\ \;\;\;\;t_0 \cdot \frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{elif}\;c \leq 8.2 \cdot 10^{-107}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + a \cdot \left(c \cdot \frac{1}{d}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \left(\frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{d}} + a\right)\\ \end{array} \]
Alternative 2
Error10.0
Cost20296
\[\begin{array}{l} t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{if}\;c \leq -1.3 \cdot 10^{+157}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;c \leq -2.5 \cdot 10^{-97}:\\ \;\;\;\;\frac{t_0}{\frac{\mathsf{hypot}\left(c, d\right)}{\mathsf{fma}\left(a, c, d \cdot b\right)}}\\ \mathbf{elif}\;c \leq 4.9 \cdot 10^{-104}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + a \cdot \left(c \cdot \frac{1}{d}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \left(\frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{d}} + a\right)\\ \end{array} \]
Alternative 3
Error10.6
Cost14028
\[\begin{array}{l} \mathbf{if}\;c \leq -9.8 \cdot 10^{+67}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d}{\frac{c}{b}}}{c}\\ \mathbf{elif}\;c \leq -1.15 \cdot 10^{-104}:\\ \;\;\;\;\frac{d \cdot b + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 2.65 \cdot 10^{-104}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + a \cdot \left(c \cdot \frac{1}{d}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{d}} + a\right)\\ \end{array} \]
Alternative 4
Error11.9
Cost1488
\[\begin{array}{l} t_0 := \frac{d \cdot b + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -6 \cdot 10^{+68}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d}{\frac{c}{b}}}{c}\\ \mathbf{elif}\;c \leq -1.25 \cdot 10^{-101}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 3.9 \cdot 10^{-67}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + a \cdot \left(c \cdot \frac{1}{d}\right)\right)\\ \mathbf{elif}\;c \leq 8 \cdot 10^{+113}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{b}{c}}{\frac{c}{d}}\\ \end{array} \]
Alternative 5
Error15.2
Cost1096
\[\begin{array}{l} \mathbf{if}\;c \leq -8.2 \cdot 10^{-47}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d}{\frac{c}{b}}}{c}\\ \mathbf{elif}\;c \leq 2.25 \cdot 10^{-51}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + a \cdot \left(c \cdot \frac{1}{d}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b \cdot \frac{d}{c}}{c}\\ \end{array} \]
Alternative 6
Error15.4
Cost969
\[\begin{array}{l} \mathbf{if}\;c \leq -5.3 \cdot 10^{-49} \lor \neg \left(c \leq 9.6 \cdot 10^{-49}\right):\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + \frac{c \cdot a}{d}\right)\\ \end{array} \]
Alternative 7
Error15.5
Cost969
\[\begin{array}{l} \mathbf{if}\;c \leq -2.65 \cdot 10^{-48} \lor \neg \left(c \leq 4.5 \cdot 10^{-49}\right):\\ \;\;\;\;\frac{a}{c} + \frac{b \cdot \frac{d}{c}}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + \frac{c \cdot a}{d}\right)\\ \end{array} \]
Alternative 8
Error19.2
Cost968
\[\begin{array}{l} \mathbf{if}\;c \leq -8.5 \cdot 10^{+71}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 52000:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + \frac{c \cdot a}{d}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 9
Error15.6
Cost968
\[\begin{array}{l} \mathbf{if}\;c \leq -1.25 \cdot 10^{-47}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \mathbf{elif}\;c \leq 9.5 \cdot 10^{-49}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + \frac{c \cdot a}{d}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c \cdot \frac{c}{b}}\\ \end{array} \]
Alternative 10
Error15.3
Cost968
\[\begin{array}{l} \mathbf{if}\;c \leq -3 \cdot 10^{-46}:\\ \;\;\;\;\frac{a}{c} + \frac{\frac{d}{\frac{c}{b}}}{c}\\ \mathbf{elif}\;c \leq 1.36 \cdot 10^{-59}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + \frac{c \cdot a}{d}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b \cdot \frac{d}{c}}{c}\\ \end{array} \]
Alternative 11
Error19.6
Cost841
\[\begin{array}{l} \mathbf{if}\;c \leq -9.4 \cdot 10^{+70} \lor \neg \left(c \leq 28000\right):\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b + c \cdot \frac{a}{d}}{d}\\ \end{array} \]
Alternative 12
Error23.9
Cost456
\[\begin{array}{l} \mathbf{if}\;c \leq -5 \cdot 10^{-67}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{elif}\;c \leq 3600:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 13
Error37.2
Cost192
\[\frac{a}{c} \]

Error

Reproduce

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