| Alternative 1 | |
|---|---|
| Error | 11.9 |
| Cost | 7696 |
(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 (/ 1.0 (hypot c d)))
(t_1 (* t_0 (/ (- (* c b) (* d a)) (hypot c d))))
(t_2 (- (/ c (/ d b)) a)))
(if (<= d -8.8e+167)
(/ t_2 d)
(if (<= d -4.5e-125)
t_1
(if (<= d 3.6e-174)
(fma -1.0 (/ (/ a (/ c d)) c) (/ b c))
(if (<= d 6e+115) t_1 (* t_0 t_2)))))))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 = 1.0 / hypot(c, d);
double t_1 = t_0 * (((c * b) - (d * a)) / hypot(c, d));
double t_2 = (c / (d / b)) - a;
double tmp;
if (d <= -8.8e+167) {
tmp = t_2 / d;
} else if (d <= -4.5e-125) {
tmp = t_1;
} else if (d <= 3.6e-174) {
tmp = fma(-1.0, ((a / (c / d)) / c), (b / c));
} else if (d <= 6e+115) {
tmp = t_1;
} else {
tmp = t_0 * t_2;
}
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(1.0 / hypot(c, d)) t_1 = Float64(t_0 * Float64(Float64(Float64(c * b) - Float64(d * a)) / hypot(c, d))) t_2 = Float64(Float64(c / Float64(d / b)) - a) tmp = 0.0 if (d <= -8.8e+167) tmp = Float64(t_2 / d); elseif (d <= -4.5e-125) tmp = t_1; elseif (d <= 3.6e-174) tmp = fma(-1.0, Float64(Float64(a / Float64(c / d)) / c), Float64(b / c)); elseif (d <= 6e+115) tmp = t_1; else tmp = Float64(t_0 * t_2); end return 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[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(t$95$0 * N[(N[(N[(c * b), $MachinePrecision] - N[(d * a), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[(c / N[(d / b), $MachinePrecision]), $MachinePrecision] - a), $MachinePrecision]}, If[LessEqual[d, -8.8e+167], N[(t$95$2 / d), $MachinePrecision], If[LessEqual[d, -4.5e-125], t$95$1, If[LessEqual[d, 3.6e-174], N[(-1.0 * N[(N[(a / N[(c / d), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision] + N[(b / c), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 6e+115], t$95$1, N[(t$95$0 * t$95$2), $MachinePrecision]]]]]]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right)}\\
t_1 := t_0 \cdot \frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(c, d\right)}\\
t_2 := \frac{c}{\frac{d}{b}} - a\\
\mathbf{if}\;d \leq -8.8 \cdot 10^{+167}:\\
\;\;\;\;\frac{t_2}{d}\\
\mathbf{elif}\;d \leq -4.5 \cdot 10^{-125}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;d \leq 3.6 \cdot 10^{-174}:\\
\;\;\;\;\mathsf{fma}\left(-1, \frac{\frac{a}{\frac{c}{d}}}{c}, \frac{b}{c}\right)\\
\mathbf{elif}\;d \leq 6 \cdot 10^{+115}:\\
\;\;\;\;t_1\\
\mathbf{else}:\\
\;\;\;\;t_0 \cdot t_2\\
\end{array}
| Original | 25.9 |
|---|---|
| Target | 0.5 |
| Herbie | 9.4 |
if d < -8.80000000000000013e167Initial program 44.7
Taylor expanded in c around 0 14.4
Simplified7.0
[Start]14.4 | \[ -1 \cdot \frac{a}{d} + \frac{c \cdot b}{{d}^{2}}
\] |
|---|---|
+-commutative [=>]14.4 | \[ \color{blue}{\frac{c \cdot b}{{d}^{2}} + -1 \cdot \frac{a}{d}}
\] |
mul-1-neg [=>]14.4 | \[ \frac{c \cdot b}{{d}^{2}} + \color{blue}{\left(-\frac{a}{d}\right)}
\] |
unsub-neg [=>]14.4 | \[ \color{blue}{\frac{c \cdot b}{{d}^{2}} - \frac{a}{d}}
\] |
*-commutative [<=]14.4 | \[ \frac{\color{blue}{b \cdot c}}{{d}^{2}} - \frac{a}{d}
\] |
unpow2 [=>]14.4 | \[ \frac{b \cdot c}{\color{blue}{d \cdot d}} - \frac{a}{d}
\] |
times-frac [=>]7.0 | \[ \color{blue}{\frac{b}{d} \cdot \frac{c}{d}} - \frac{a}{d}
\] |
Taylor expanded in b around 0 14.4
Simplified6.5
[Start]14.4 | \[ -1 \cdot \frac{a}{d} + \frac{c \cdot b}{{d}^{2}}
\] |
|---|---|
+-commutative [=>]14.4 | \[ \color{blue}{\frac{c \cdot b}{{d}^{2}} + -1 \cdot \frac{a}{d}}
\] |
*-commutative [=>]14.4 | \[ \frac{\color{blue}{b \cdot c}}{{d}^{2}} + -1 \cdot \frac{a}{d}
\] |
unpow2 [=>]14.4 | \[ \frac{b \cdot c}{\color{blue}{d \cdot d}} + -1 \cdot \frac{a}{d}
\] |
associate-/l* [=>]13.0 | \[ \color{blue}{\frac{b}{\frac{d \cdot d}{c}}} + -1 \cdot \frac{a}{d}
\] |
mul-1-neg [=>]13.0 | \[ \frac{b}{\frac{d \cdot d}{c}} + \color{blue}{\left(-\frac{a}{d}\right)}
\] |
sub-neg [<=]13.0 | \[ \color{blue}{\frac{b}{\frac{d \cdot d}{c}} - \frac{a}{d}}
\] |
associate-/l* [<=]14.4 | \[ \color{blue}{\frac{b \cdot c}{d \cdot d}} - \frac{a}{d}
\] |
associate-/r* [=>]11.5 | \[ \color{blue}{\frac{\frac{b \cdot c}{d}}{d}} - \frac{a}{d}
\] |
div-sub [<=]11.5 | \[ \color{blue}{\frac{\frac{b \cdot c}{d} - a}{d}}
\] |
*-commutative [<=]11.5 | \[ \frac{\frac{\color{blue}{c \cdot b}}{d} - a}{d}
\] |
associate-/l* [=>]6.5 | \[ \frac{\color{blue}{\frac{c}{\frac{d}{b}}} - a}{d}
\] |
if -8.80000000000000013e167 < d < -4.50000000000000012e-125 or 3.59999999999999999e-174 < d < 6.0000000000000001e115Initial program 17.7
Applied egg-rr12.0
if -4.50000000000000012e-125 < d < 3.59999999999999999e-174Initial program 23.1
Applied egg-rr12.7
Taylor expanded in c around inf 8.4
Simplified5.2
[Start]8.4 | \[ -1 \cdot \frac{a \cdot d}{{c}^{2}} + \frac{b}{c}
\] |
|---|---|
fma-def [=>]8.4 | \[ \color{blue}{\mathsf{fma}\left(-1, \frac{a \cdot d}{{c}^{2}}, \frac{b}{c}\right)}
\] |
unpow2 [=>]8.4 | \[ \mathsf{fma}\left(-1, \frac{a \cdot d}{\color{blue}{c \cdot c}}, \frac{b}{c}\right)
\] |
associate-/r* [=>]5.3 | \[ \mathsf{fma}\left(-1, \color{blue}{\frac{\frac{a \cdot d}{c}}{c}}, \frac{b}{c}\right)
\] |
associate-/l* [=>]5.2 | \[ \mathsf{fma}\left(-1, \frac{\color{blue}{\frac{a}{\frac{c}{d}}}}{c}, \frac{b}{c}\right)
\] |
if 6.0000000000000001e115 < d Initial program 40.9
Applied egg-rr27.3
Taylor expanded in c around 0 14.9
Simplified9.8
[Start]14.9 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{c \cdot b}{d} + -1 \cdot a\right)
\] |
|---|---|
+-commutative [=>]14.9 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\left(-1 \cdot a + \frac{c \cdot b}{d}\right)}
\] |
mul-1-neg [=>]14.9 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\color{blue}{\left(-a\right)} + \frac{c \cdot b}{d}\right)
\] |
associate-/l* [=>]9.8 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\left(-a\right) + \color{blue}{\frac{c}{\frac{d}{b}}}\right)
\] |
Final simplification9.4
| Alternative 1 | |
|---|---|
| Error | 11.9 |
| Cost | 7696 |
| Alternative 2 | |
|---|---|
| Error | 12.0 |
| Cost | 7500 |
| Alternative 3 | |
|---|---|
| Error | 12.0 |
| Cost | 1488 |
| Alternative 4 | |
|---|---|
| Error | 15.5 |
| Cost | 1232 |
| Alternative 5 | |
|---|---|
| Error | 19.6 |
| Cost | 1105 |
| Alternative 6 | |
|---|---|
| Error | 15.4 |
| Cost | 1105 |
| Alternative 7 | |
|---|---|
| Error | 24.8 |
| Cost | 840 |
| Alternative 8 | |
|---|---|
| Error | 24.8 |
| Cost | 784 |
| Alternative 9 | |
|---|---|
| Error | 23.4 |
| Cost | 521 |
| Alternative 10 | |
|---|---|
| Error | 37.2 |
| Cost | 192 |
herbie shell --seed 2023034
(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))))