| Alternative 1 | |
|---|---|
| Error | 10.8 |
| Cost | 13904 |
(FPCore (a b c d) :precision binary64 (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (+ (* c c) (* d d))) (t_1 (/ 1.0 (hypot c d))))
(if (<= c -1.25e+59)
(* t_1 (- (/ a (/ c d)) b))
(if (<= c -8e-185)
(* t_1 (/ (- (* c b) (* d a)) (hypot c d)))
(if (<= c 6.4e-121)
(- (* b (/ (/ c d) d)) (/ a d))
(if (<= c 6.4e+69)
(- (* b (/ c t_0)) (/ a (/ t_0 d)))
(* (/ c (hypot c d)) (/ b (hypot c d)))))))))double code(double a, double b, double c, double d) {
return ((b * c) - (a * d)) / ((c * c) + (d * d));
}
double code(double a, double b, double c, double d) {
double t_0 = (c * c) + (d * d);
double t_1 = 1.0 / hypot(c, d);
double tmp;
if (c <= -1.25e+59) {
tmp = t_1 * ((a / (c / d)) - b);
} else if (c <= -8e-185) {
tmp = t_1 * (((c * b) - (d * a)) / hypot(c, d));
} else if (c <= 6.4e-121) {
tmp = (b * ((c / d) / d)) - (a / d);
} else if (c <= 6.4e+69) {
tmp = (b * (c / t_0)) - (a / (t_0 / d));
} else {
tmp = (c / hypot(c, d)) * (b / hypot(c, d));
}
return tmp;
}
public static double code(double a, double b, double c, double d) {
return ((b * c) - (a * d)) / ((c * c) + (d * d));
}
public static double code(double a, double b, double c, double d) {
double t_0 = (c * c) + (d * d);
double t_1 = 1.0 / Math.hypot(c, d);
double tmp;
if (c <= -1.25e+59) {
tmp = t_1 * ((a / (c / d)) - b);
} else if (c <= -8e-185) {
tmp = t_1 * (((c * b) - (d * a)) / Math.hypot(c, d));
} else if (c <= 6.4e-121) {
tmp = (b * ((c / d) / d)) - (a / d);
} else if (c <= 6.4e+69) {
tmp = (b * (c / t_0)) - (a / (t_0 / d));
} else {
tmp = (c / Math.hypot(c, d)) * (b / Math.hypot(c, d));
}
return tmp;
}
def code(a, b, c, d): return ((b * c) - (a * d)) / ((c * c) + (d * d))
def code(a, b, c, d): t_0 = (c * c) + (d * d) t_1 = 1.0 / math.hypot(c, d) tmp = 0 if c <= -1.25e+59: tmp = t_1 * ((a / (c / d)) - b) elif c <= -8e-185: tmp = t_1 * (((c * b) - (d * a)) / math.hypot(c, d)) elif c <= 6.4e-121: tmp = (b * ((c / d) / d)) - (a / d) elif c <= 6.4e+69: tmp = (b * (c / t_0)) - (a / (t_0 / d)) else: tmp = (c / math.hypot(c, d)) * (b / math.hypot(c, d)) return tmp
function code(a, b, c, d) return Float64(Float64(Float64(b * c) - Float64(a * d)) / Float64(Float64(c * c) + Float64(d * d))) end
function code(a, b, c, d) t_0 = Float64(Float64(c * c) + Float64(d * d)) t_1 = Float64(1.0 / hypot(c, d)) tmp = 0.0 if (c <= -1.25e+59) tmp = Float64(t_1 * Float64(Float64(a / Float64(c / d)) - b)); elseif (c <= -8e-185) tmp = Float64(t_1 * Float64(Float64(Float64(c * b) - Float64(d * a)) / hypot(c, d))); elseif (c <= 6.4e-121) tmp = Float64(Float64(b * Float64(Float64(c / d) / d)) - Float64(a / d)); elseif (c <= 6.4e+69) tmp = Float64(Float64(b * Float64(c / t_0)) - Float64(a / Float64(t_0 / d))); else tmp = Float64(Float64(c / hypot(c, d)) * Float64(b / hypot(c, d))); end return tmp end
function tmp = code(a, b, c, d) tmp = ((b * c) - (a * d)) / ((c * c) + (d * d)); end
function tmp_2 = code(a, b, c, d) t_0 = (c * c) + (d * d); t_1 = 1.0 / hypot(c, d); tmp = 0.0; if (c <= -1.25e+59) tmp = t_1 * ((a / (c / d)) - b); elseif (c <= -8e-185) tmp = t_1 * (((c * b) - (d * a)) / hypot(c, d)); elseif (c <= 6.4e-121) tmp = (b * ((c / d) / d)) - (a / d); elseif (c <= 6.4e+69) tmp = (b * (c / t_0)) - (a / (t_0 / d)); else tmp = (c / hypot(c, d)) * (b / hypot(c, d)); end tmp_2 = tmp; end
code[a_, b_, c_, d_] := N[(N[(N[(b * c), $MachinePrecision] - N[(a * 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[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -1.25e+59], N[(t$95$1 * N[(N[(a / N[(c / d), $MachinePrecision]), $MachinePrecision] - b), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, -8e-185], N[(t$95$1 * N[(N[(N[(c * b), $MachinePrecision] - N[(d * a), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 6.4e-121], N[(N[(b * N[(N[(c / d), $MachinePrecision] / d), $MachinePrecision]), $MachinePrecision] - N[(a / d), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 6.4e+69], N[(N[(b * N[(c / t$95$0), $MachinePrecision]), $MachinePrecision] - N[(a / N[(t$95$0 / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(c / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(b / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := c \cdot c + d \cdot d\\
t_1 := \frac{1}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{if}\;c \leq -1.25 \cdot 10^{+59}:\\
\;\;\;\;t_1 \cdot \left(\frac{a}{\frac{c}{d}} - b\right)\\
\mathbf{elif}\;c \leq -8 \cdot 10^{-185}:\\
\;\;\;\;t_1 \cdot \frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{elif}\;c \leq 6.4 \cdot 10^{-121}:\\
\;\;\;\;b \cdot \frac{\frac{c}{d}}{d} - \frac{a}{d}\\
\mathbf{elif}\;c \leq 6.4 \cdot 10^{+69}:\\
\;\;\;\;b \cdot \frac{c}{t_0} - \frac{a}{\frac{t_0}{d}}\\
\mathbf{else}:\\
\;\;\;\;\frac{c}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{b}{\mathsf{hypot}\left(c, d\right)}\\
\end{array}
Results
| Original | 25.9 |
|---|---|
| Target | 0.4 |
| Herbie | 10.5 |
if c < -1.2499999999999999e59Initial program 35.4
Applied egg-rr24.6
Taylor expanded in c around -inf 14.4
Simplified11.3
[Start]14.4 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{a \cdot d}{c} + -1 \cdot b\right)
\] |
|---|---|
associate-/l* [=>]11.3 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\color{blue}{\frac{a}{\frac{c}{d}}} + -1 \cdot b\right)
\] |
mul-1-neg [=>]11.3 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{a}{\frac{c}{d}} + \color{blue}{\left(-b\right)}\right)
\] |
if -1.2499999999999999e59 < c < -7.9999999999999999e-185Initial program 14.2
Applied egg-rr9.5
if -7.9999999999999999e-185 < c < 6.40000000000000038e-121Initial program 22.6
Applied egg-rr22.8
Simplified22.8
[Start]22.8 | \[ \frac{\left(b \cdot c - a \cdot d\right) + \left(\left(\mathsf{fma}\left(-d, a, a \cdot d\right) + \mathsf{fma}\left(-d, a, a \cdot d\right)\right) + \left(\mathsf{fma}\left(-d, a, a \cdot d\right) + \mathsf{fma}\left(-d, a, a \cdot d\right)\right)\right)}{c \cdot c + d \cdot d}
\] |
|---|---|
*-commutative [=>]22.8 | \[ \frac{\left(\color{blue}{c \cdot b} - a \cdot d\right) + \left(\left(\mathsf{fma}\left(-d, a, a \cdot d\right) + \mathsf{fma}\left(-d, a, a \cdot d\right)\right) + \left(\mathsf{fma}\left(-d, a, a \cdot d\right) + \mathsf{fma}\left(-d, a, a \cdot d\right)\right)\right)}{c \cdot c + d \cdot d}
\] |
count-2 [=>]22.8 | \[ \frac{\left(c \cdot b - a \cdot d\right) + \color{blue}{2 \cdot \left(\mathsf{fma}\left(-d, a, a \cdot d\right) + \mathsf{fma}\left(-d, a, a \cdot d\right)\right)}}{c \cdot c + d \cdot d}
\] |
count-2 [=>]22.8 | \[ \frac{\left(c \cdot b - a \cdot d\right) + 2 \cdot \color{blue}{\left(2 \cdot \mathsf{fma}\left(-d, a, a \cdot d\right)\right)}}{c \cdot c + d \cdot d}
\] |
Taylor expanded in d around inf 9.1
Simplified6.1
[Start]9.1 | \[ \left(4 \cdot \frac{a + -1 \cdot a}{d} + \frac{c \cdot b}{{d}^{2}}\right) - \frac{a}{d}
\] |
|---|---|
+-commutative [=>]9.1 | \[ \color{blue}{\left(\frac{c \cdot b}{{d}^{2}} + 4 \cdot \frac{a + -1 \cdot a}{d}\right)} - \frac{a}{d}
\] |
associate--l+ [=>]9.1 | \[ \color{blue}{\frac{c \cdot b}{{d}^{2}} + \left(4 \cdot \frac{a + -1 \cdot a}{d} - \frac{a}{d}\right)}
\] |
associate-*r/ [=>]9.1 | \[ \frac{c \cdot b}{{d}^{2}} + \left(\color{blue}{\frac{4 \cdot \left(a + -1 \cdot a\right)}{d}} - \frac{a}{d}\right)
\] |
div-sub [<=]9.1 | \[ \frac{c \cdot b}{{d}^{2}} + \color{blue}{\frac{4 \cdot \left(a + -1 \cdot a\right) - a}{d}}
\] |
distribute-rgt1-in [=>]9.1 | \[ \frac{c \cdot b}{{d}^{2}} + \frac{4 \cdot \color{blue}{\left(\left(-1 + 1\right) \cdot a\right)} - a}{d}
\] |
metadata-eval [=>]9.1 | \[ \frac{c \cdot b}{{d}^{2}} + \frac{4 \cdot \left(\color{blue}{0} \cdot a\right) - a}{d}
\] |
mul0-lft [=>]9.1 | \[ \frac{c \cdot b}{{d}^{2}} + \frac{4 \cdot \color{blue}{0} - a}{d}
\] |
metadata-eval [=>]9.1 | \[ \frac{c \cdot b}{{d}^{2}} + \frac{\color{blue}{0} - a}{d}
\] |
neg-sub0 [<=]9.1 | \[ \frac{c \cdot b}{{d}^{2}} + \frac{\color{blue}{-a}}{d}
\] |
distribute-frac-neg [=>]9.1 | \[ \frac{c \cdot b}{{d}^{2}} + \color{blue}{\left(-\frac{a}{d}\right)}
\] |
sub-neg [<=]9.1 | \[ \color{blue}{\frac{c \cdot b}{{d}^{2}} - \frac{a}{d}}
\] |
associate-*l/ [<=]10.1 | \[ \color{blue}{\frac{c}{{d}^{2}} \cdot b} - \frac{a}{d}
\] |
*-commutative [=>]10.1 | \[ \color{blue}{b \cdot \frac{c}{{d}^{2}}} - \frac{a}{d}
\] |
unpow2 [=>]10.1 | \[ b \cdot \frac{c}{\color{blue}{d \cdot d}} - \frac{a}{d}
\] |
associate-/r* [=>]6.1 | \[ b \cdot \color{blue}{\frac{\frac{c}{d}}{d}} - \frac{a}{d}
\] |
if 6.40000000000000038e-121 < c < 6.3999999999999997e69Initial program 15.7
Applied egg-rr11.0
Taylor expanded in b around 0 15.7
Simplified12.3
[Start]15.7 | \[ \frac{c \cdot b}{{d}^{2} + {c}^{2}} + -1 \cdot \frac{a \cdot d}{{d}^{2} + {c}^{2}}
\] |
|---|---|
+-commutative [=>]15.7 | \[ \color{blue}{-1 \cdot \frac{a \cdot d}{{d}^{2} + {c}^{2}} + \frac{c \cdot b}{{d}^{2} + {c}^{2}}}
\] |
associate-/l* [=>]13.7 | \[ -1 \cdot \color{blue}{\frac{a}{\frac{{d}^{2} + {c}^{2}}{d}}} + \frac{c \cdot b}{{d}^{2} + {c}^{2}}
\] |
associate-*r/ [=>]13.7 | \[ \color{blue}{\frac{-1 \cdot a}{\frac{{d}^{2} + {c}^{2}}{d}}} + \frac{c \cdot b}{{d}^{2} + {c}^{2}}
\] |
mul-1-neg [=>]13.7 | \[ \frac{\color{blue}{-a}}{\frac{{d}^{2} + {c}^{2}}{d}} + \frac{c \cdot b}{{d}^{2} + {c}^{2}}
\] |
+-commutative [=>]13.7 | \[ \frac{-a}{\frac{\color{blue}{{c}^{2} + {d}^{2}}}{d}} + \frac{c \cdot b}{{d}^{2} + {c}^{2}}
\] |
unpow2 [=>]13.7 | \[ \frac{-a}{\frac{\color{blue}{c \cdot c} + {d}^{2}}{d}} + \frac{c \cdot b}{{d}^{2} + {c}^{2}}
\] |
unpow2 [=>]13.7 | \[ \frac{-a}{\frac{c \cdot c + \color{blue}{d \cdot d}}{d}} + \frac{c \cdot b}{{d}^{2} + {c}^{2}}
\] |
associate-/l* [=>]14.1 | \[ \frac{-a}{\frac{c \cdot c + d \cdot d}{d}} + \color{blue}{\frac{c}{\frac{{d}^{2} + {c}^{2}}{b}}}
\] |
associate-/r/ [=>]12.3 | \[ \frac{-a}{\frac{c \cdot c + d \cdot d}{d}} + \color{blue}{\frac{c}{{d}^{2} + {c}^{2}} \cdot b}
\] |
+-commutative [=>]12.3 | \[ \frac{-a}{\frac{c \cdot c + d \cdot d}{d}} + \frac{c}{\color{blue}{{c}^{2} + {d}^{2}}} \cdot b
\] |
unpow2 [=>]12.3 | \[ \frac{-a}{\frac{c \cdot c + d \cdot d}{d}} + \frac{c}{\color{blue}{c \cdot c} + {d}^{2}} \cdot b
\] |
unpow2 [=>]12.3 | \[ \frac{-a}{\frac{c \cdot c + d \cdot d}{d}} + \frac{c}{c \cdot c + \color{blue}{d \cdot d}} \cdot b
\] |
if 6.3999999999999997e69 < c Initial program 38.8
Taylor expanded in b around inf 41.4
Simplified41.4
[Start]41.4 | \[ \frac{c \cdot b}{{d}^{2} + {c}^{2}}
\] |
|---|---|
unpow2 [=>]41.4 | \[ \frac{c \cdot b}{\color{blue}{d \cdot d} + {c}^{2}}
\] |
unpow2 [=>]41.4 | \[ \frac{c \cdot b}{d \cdot d + \color{blue}{c \cdot c}}
\] |
Applied egg-rr13.8
Final simplification10.5
| Alternative 1 | |
|---|---|
| Error | 10.8 |
| Cost | 13904 |
| Alternative 2 | |
|---|---|
| Error | 10.0 |
| Cost | 7300 |
| Alternative 3 | |
|---|---|
| Error | 10.1 |
| Cost | 2000 |
| Alternative 4 | |
|---|---|
| Error | 11.8 |
| Cost | 1872 |
| Alternative 5 | |
|---|---|
| Error | 15.7 |
| Cost | 1496 |
| Alternative 6 | |
|---|---|
| Error | 11.8 |
| Cost | 1488 |
| Alternative 7 | |
|---|---|
| Error | 16.0 |
| Cost | 1234 |
| Alternative 8 | |
|---|---|
| Error | 15.0 |
| Cost | 1232 |
| Alternative 9 | |
|---|---|
| Error | 15.1 |
| Cost | 969 |
| Alternative 10 | |
|---|---|
| Error | 15.3 |
| Cost | 968 |
| Alternative 11 | |
|---|---|
| Error | 19.1 |
| Cost | 841 |
| Alternative 12 | |
|---|---|
| Error | 22.8 |
| Cost | 521 |
| Alternative 13 | |
|---|---|
| Error | 37.3 |
| Cost | 192 |
herbie shell --seed 2022354
(FPCore (a b c d)
:name "Complex division, imag part"
:precision binary64
:herbie-target
(if (< (fabs d) (fabs c)) (/ (- b (* a (/ d c))) (+ c (* d (/ d c)))) (/ (+ (- a) (* b (/ c d))) (+ d (* c (/ c d)))))
(/ (- (* b c) (* a d)) (+ (* c c) (* d d))))