?

Average Error: 26.5 → 0.7
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{d}{\mathsf{hypot}\left(c, d\right)} \cdot b + \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)) (+ (* (/ d (hypot c d)) b) (/ 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)) * (((d / hypot(c, d)) * b) + (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)) * (((d / Math.hypot(c, d)) * b) + (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)) * (((d / math.hypot(c, d)) * b) + (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(Float64(d / hypot(c, d)) * b) + 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)) * (((d / hypot(c, d)) * b) + (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[(N[(d / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * b), $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{d}{\mathsf{hypot}\left(c, d\right)} \cdot b + \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.5
Target0.5
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.5

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

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

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

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

    \[ \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. Applied egg-rr0.7

    \[\leadsto \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\color{blue}{\frac{d}{\mathsf{hypot}\left(c, d\right)} \cdot b} + \frac{a}{\frac{\mathsf{hypot}\left(c, d\right)}{c}}\right) \]
  6. Final simplification0.7

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

Alternatives

Alternative 1
Error9.4
Cost20560
\[\begin{array}{l} t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right)}\\ t_1 := t_0 \cdot \frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}\\ \mathbf{if}\;c \leq -1.3 \cdot 10^{+147}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c \cdot \frac{c}{d}}\\ \mathbf{elif}\;c \leq -1.1 \cdot 10^{-191}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;c \leq 1.25 \cdot 10^{-112}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{elif}\;c \leq 2.55 \cdot 10^{+71}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \left(\frac{d}{\mathsf{hypot}\left(c, d\right)} \cdot b + a\right)\\ \end{array} \]
Alternative 2
Error11.4
Cost14028
\[\begin{array}{l} \mathbf{if}\;c \leq -1.12 \cdot 10^{+138}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c \cdot \frac{c}{d}}\\ \mathbf{elif}\;c \leq -2.8 \cdot 10^{-125}:\\ \;\;\;\;\frac{d \cdot b + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{elif}\;c \leq 9.6 \cdot 10^{-105}:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{d}{\mathsf{hypot}\left(c, d\right)} \cdot b + a\right)\\ \end{array} \]
Alternative 3
Error12.4
Cost1488
\[\begin{array}{l} t_0 := \frac{d \cdot b + c \cdot a}{c \cdot c + d \cdot d}\\ \mathbf{if}\;c \leq -1.12 \cdot 10^{+139}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c \cdot \frac{c}{d}}\\ \mathbf{elif}\;c \leq -1.32 \cdot 10^{-123}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 4.1 \cdot 10^{-73}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \mathbf{elif}\;c \leq 8.6 \cdot 10^{+127}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{1}{c} \cdot \left(d \cdot \frac{b}{c}\right)\\ \end{array} \]
Alternative 4
Error15.7
Cost1232
\[\begin{array}{l} t_0 := \frac{a}{c} + \frac{d}{c \cdot \frac{c}{b}}\\ \mathbf{if}\;c \leq -1.1 \cdot 10^{+26}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 6.8 \cdot 10^{-20}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + \frac{c \cdot a}{d}\right)\\ \mathbf{elif}\;c \leq 1.55 \cdot 10^{+32}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;c \leq 9 \cdot 10^{+73}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \end{array} \]
Alternative 5
Error15.7
Cost1232
\[\begin{array}{l} \mathbf{if}\;c \leq -8 \cdot 10^{+28}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{c \cdot \frac{c}{b}}\\ \mathbf{elif}\;c \leq 3.35 \cdot 10^{-18}:\\ \;\;\;\;\frac{1}{d} \cdot \left(b + \frac{c \cdot a}{d}\right)\\ \mathbf{elif}\;c \leq 1.08 \cdot 10^{+32}:\\ \;\;\;\;\frac{a}{c} + \frac{d}{\frac{c \cdot c}{b}}\\ \mathbf{elif}\;c \leq 1.72 \cdot 10^{+75}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \end{array} \]
Alternative 6
Error15.1
Cost1096
\[\begin{array}{l} \mathbf{if}\;d \leq -43000000:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{elif}\;d \leq 6.8 \cdot 10^{-25}:\\ \;\;\;\;\frac{a}{c} + \frac{1}{c} \cdot \left(d \cdot \frac{b}{c}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \end{array} \]
Alternative 7
Error18.5
Cost969
\[\begin{array}{l} \mathbf{if}\;d \leq -9000000 \lor \neg \left(d \leq 1.9 \cdot 10^{-24}\right):\\ \;\;\;\;\frac{1}{d} \cdot \left(b + \frac{c \cdot a}{d}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c + \frac{d \cdot d}{c}}\\ \end{array} \]
Alternative 8
Error16.7
Cost969
\[\begin{array}{l} \mathbf{if}\;d \leq -430000000 \lor \neg \left(d \leq 2.65 \cdot 10^{-24}\right):\\ \;\;\;\;\frac{1}{d} \cdot \left(b + \frac{c \cdot a}{d}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c} \cdot \frac{d}{c}\\ \end{array} \]
Alternative 9
Error16.6
Cost969
\[\begin{array}{l} \mathbf{if}\;d \leq -2800000 \lor \neg \left(d \leq 1.9 \cdot 10^{-24}\right):\\ \;\;\;\;\frac{1}{d} \cdot \left(b + \frac{c \cdot a}{d}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c \cdot \frac{c}{d}}\\ \end{array} \]
Alternative 10
Error15.3
Cost969
\[\begin{array}{l} \mathbf{if}\;d \leq -1650000 \lor \neg \left(d \leq 1.55 \cdot 10^{-25}\right):\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c \cdot \frac{c}{d}}\\ \end{array} \]
Alternative 11
Error15.3
Cost968
\[\begin{array}{l} \mathbf{if}\;d \leq -1250000000:\\ \;\;\;\;\frac{b}{d} + \frac{c}{d} \cdot \frac{a}{d}\\ \mathbf{elif}\;d \leq 2.5 \cdot 10^{-24}:\\ \;\;\;\;\frac{a}{c} + \frac{b}{c \cdot \frac{c}{d}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d} + \frac{c \cdot \frac{a}{d}}{d}\\ \end{array} \]
Alternative 12
Error20.2
Cost840
\[\begin{array}{l} \mathbf{if}\;d \leq -4 \cdot 10^{+46}:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 5.6 \cdot 10^{+28}:\\ \;\;\;\;\frac{a}{c + \frac{d \cdot d}{c}}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 13
Error22.7
Cost456
\[\begin{array}{l} \mathbf{if}\;d \leq -3900000000:\\ \;\;\;\;\frac{b}{d}\\ \mathbf{elif}\;d \leq 5.2 \cdot 10^{-17}:\\ \;\;\;\;\frac{a}{c}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{d}\\ \end{array} \]
Alternative 14
Error36.7
Cost324
\[\begin{array}{l} \mathbf{if}\;d \leq -2 \cdot 10^{+158}:\\ \;\;\;\;\frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{c}\\ \end{array} \]
Alternative 15
Error37.2
Cost192
\[\frac{a}{c} \]

Error

Reproduce?

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