| Alternative 1 | |
|---|---|
| Error | 12.7 |
| Cost | 7700 |
(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)) (/ (- (* c b) (* d a)) (hypot c d)))))
(if (<= d -4e+152)
(/ (- (* c (/ b d)) a) d)
(if (<= d -1.7e-25)
t_0
(if (<= d 1.4e-78)
(/ (- b (/ (* d a) c)) c)
(if (<= d 8.8e+36)
t_0
(if (<= d 6.8e+76)
(/ a (/ (fma d d (* c c)) (- d)))
(if (<= d 3.2e+140) t_0 (- (* (/ b d) (/ c d)) (/ a 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 = (1.0 / hypot(c, d)) * (((c * b) - (d * a)) / hypot(c, d));
double tmp;
if (d <= -4e+152) {
tmp = ((c * (b / d)) - a) / d;
} else if (d <= -1.7e-25) {
tmp = t_0;
} else if (d <= 1.4e-78) {
tmp = (b - ((d * a) / c)) / c;
} else if (d <= 8.8e+36) {
tmp = t_0;
} else if (d <= 6.8e+76) {
tmp = a / (fma(d, d, (c * c)) / -d);
} else if (d <= 3.2e+140) {
tmp = t_0;
} else {
tmp = ((b / d) * (c / d)) - (a / 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(1.0 / hypot(c, d)) * Float64(Float64(Float64(c * b) - Float64(d * a)) / hypot(c, d))) tmp = 0.0 if (d <= -4e+152) tmp = Float64(Float64(Float64(c * Float64(b / d)) - a) / d); elseif (d <= -1.7e-25) tmp = t_0; elseif (d <= 1.4e-78) tmp = Float64(Float64(b - Float64(Float64(d * a) / c)) / c); elseif (d <= 8.8e+36) tmp = t_0; elseif (d <= 6.8e+76) tmp = Float64(a / Float64(fma(d, d, Float64(c * c)) / Float64(-d))); elseif (d <= 3.2e+140) tmp = t_0; else tmp = Float64(Float64(Float64(b / d) * Float64(c / d)) - Float64(a / d)); 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[(N[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(N[(N[(c * b), $MachinePrecision] - N[(d * a), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -4e+152], N[(N[(N[(c * N[(b / d), $MachinePrecision]), $MachinePrecision] - a), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[d, -1.7e-25], t$95$0, If[LessEqual[d, 1.4e-78], N[(N[(b - N[(N[(d * a), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 8.8e+36], t$95$0, If[LessEqual[d, 6.8e+76], N[(a / N[(N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision] / (-d)), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 3.2e+140], t$95$0, N[(N[(N[(b / d), $MachinePrecision] * N[(c / d), $MachinePrecision]), $MachinePrecision] - N[(a / d), $MachinePrecision]), $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)} \cdot \frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{if}\;d \leq -4 \cdot 10^{+152}:\\
\;\;\;\;\frac{c \cdot \frac{b}{d} - a}{d}\\
\mathbf{elif}\;d \leq -1.7 \cdot 10^{-25}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;d \leq 1.4 \cdot 10^{-78}:\\
\;\;\;\;\frac{b - \frac{d \cdot a}{c}}{c}\\
\mathbf{elif}\;d \leq 8.8 \cdot 10^{+36}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;d \leq 6.8 \cdot 10^{+76}:\\
\;\;\;\;\frac{a}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{-d}}\\
\mathbf{elif}\;d \leq 3.2 \cdot 10^{+140}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\
\end{array}
| Original | 26.2 |
|---|---|
| Target | 0.4 |
| Herbie | 11.2 |
if d < -4.0000000000000002e152Initial program 45.5
Taylor expanded in c around 0 15.0
Simplified6.6
[Start]15.0 | \[ -1 \cdot \frac{a}{d} + \frac{c \cdot b}{{d}^{2}}
\] |
|---|---|
+-commutative [=>]15.0 | \[ \color{blue}{\frac{c \cdot b}{{d}^{2}} + -1 \cdot \frac{a}{d}}
\] |
mul-1-neg [=>]15.0 | \[ \frac{c \cdot b}{{d}^{2}} + \color{blue}{\left(-\frac{a}{d}\right)}
\] |
unsub-neg [=>]15.0 | \[ \color{blue}{\frac{c \cdot b}{{d}^{2}} - \frac{a}{d}}
\] |
*-commutative [<=]15.0 | \[ \frac{\color{blue}{b \cdot c}}{{d}^{2}} - \frac{a}{d}
\] |
unpow2 [=>]15.0 | \[ \frac{b \cdot c}{\color{blue}{d \cdot d}} - \frac{a}{d}
\] |
times-frac [=>]6.6 | \[ \color{blue}{\frac{b}{d} \cdot \frac{c}{d}} - \frac{a}{d}
\] |
Taylor expanded in b around 0 15.0
Simplified6.5
[Start]15.0 | \[ -1 \cdot \frac{a}{d} + \frac{c \cdot b}{{d}^{2}}
\] |
|---|---|
+-commutative [=>]15.0 | \[ \color{blue}{\frac{c \cdot b}{{d}^{2}} + -1 \cdot \frac{a}{d}}
\] |
*-commutative [=>]15.0 | \[ \frac{\color{blue}{b \cdot c}}{{d}^{2}} + -1 \cdot \frac{a}{d}
\] |
unpow2 [=>]15.0 | \[ \frac{b \cdot c}{\color{blue}{d \cdot d}} + -1 \cdot \frac{a}{d}
\] |
associate-/l* [=>]13.5 | \[ \color{blue}{\frac{b}{\frac{d \cdot d}{c}}} + -1 \cdot \frac{a}{d}
\] |
mul-1-neg [=>]13.5 | \[ \frac{b}{\frac{d \cdot d}{c}} + \color{blue}{\left(-\frac{a}{d}\right)}
\] |
sub-neg [<=]13.5 | \[ \color{blue}{\frac{b}{\frac{d \cdot d}{c}} - \frac{a}{d}}
\] |
associate-/l* [<=]15.0 | \[ \color{blue}{\frac{b \cdot c}{d \cdot d}} - \frac{a}{d}
\] |
associate-/r* [=>]11.6 | \[ \color{blue}{\frac{\frac{b \cdot c}{d}}{d}} - \frac{a}{d}
\] |
div-sub [<=]11.6 | \[ \color{blue}{\frac{\frac{b \cdot c}{d} - a}{d}}
\] |
*-commutative [<=]11.6 | \[ \frac{\frac{\color{blue}{c \cdot b}}{d} - a}{d}
\] |
associate-*r/ [<=]6.5 | \[ \frac{\color{blue}{c \cdot \frac{b}{d}} - a}{d}
\] |
if -4.0000000000000002e152 < d < -1.70000000000000001e-25 or 1.40000000000000012e-78 < d < 8.80000000000000002e36 or 6.7999999999999994e76 < d < 3.20000000000000011e140Initial program 18.0
Applied egg-rr13.0
if -1.70000000000000001e-25 < d < 1.40000000000000012e-78Initial program 19.7
Taylor expanded in c around inf 14.9
Simplified13.4
[Start]14.9 | \[ -1 \cdot \frac{a \cdot d}{{c}^{2}} + \frac{b}{c}
\] |
|---|---|
+-commutative [=>]14.9 | \[ \color{blue}{\frac{b}{c} + -1 \cdot \frac{a \cdot d}{{c}^{2}}}
\] |
mul-1-neg [=>]14.9 | \[ \frac{b}{c} + \color{blue}{\left(-\frac{a \cdot d}{{c}^{2}}\right)}
\] |
unsub-neg [=>]14.9 | \[ \color{blue}{\frac{b}{c} - \frac{a \cdot d}{{c}^{2}}}
\] |
*-commutative [=>]14.9 | \[ \frac{b}{c} - \frac{\color{blue}{d \cdot a}}{{c}^{2}}
\] |
unpow2 [=>]14.9 | \[ \frac{b}{c} - \frac{d \cdot a}{\color{blue}{c \cdot c}}
\] |
times-frac [=>]13.4 | \[ \frac{b}{c} - \color{blue}{\frac{d}{c} \cdot \frac{a}{c}}
\] |
Applied egg-rr11.8
if 8.80000000000000002e36 < d < 6.7999999999999994e76Initial program 17.4
Taylor expanded in b around 0 31.1
Simplified24.3
[Start]31.1 | \[ -1 \cdot \frac{a \cdot d}{{d}^{2} + {c}^{2}}
\] |
|---|---|
associate-*r/ [=>]31.1 | \[ \color{blue}{\frac{-1 \cdot \left(a \cdot d\right)}{{d}^{2} + {c}^{2}}}
\] |
mul-1-neg [=>]31.1 | \[ \frac{\color{blue}{-a \cdot d}}{{d}^{2} + {c}^{2}}
\] |
distribute-rgt-neg-out [<=]31.1 | \[ \frac{\color{blue}{a \cdot \left(-d\right)}}{{d}^{2} + {c}^{2}}
\] |
associate-/l* [=>]24.3 | \[ \color{blue}{\frac{a}{\frac{{d}^{2} + {c}^{2}}{-d}}}
\] |
unpow2 [=>]24.3 | \[ \frac{a}{\frac{\color{blue}{d \cdot d} + {c}^{2}}{-d}}
\] |
fma-def [=>]24.3 | \[ \frac{a}{\frac{\color{blue}{\mathsf{fma}\left(d, d, {c}^{2}\right)}}{-d}}
\] |
unpow2 [=>]24.3 | \[ \frac{a}{\frac{\mathsf{fma}\left(d, d, \color{blue}{c \cdot c}\right)}{-d}}
\] |
if 3.20000000000000011e140 < d Initial program 43.6
Taylor expanded in c around 0 15.0
Simplified7.3
[Start]15.0 | \[ -1 \cdot \frac{a}{d} + \frac{c \cdot b}{{d}^{2}}
\] |
|---|---|
+-commutative [=>]15.0 | \[ \color{blue}{\frac{c \cdot b}{{d}^{2}} + -1 \cdot \frac{a}{d}}
\] |
mul-1-neg [=>]15.0 | \[ \frac{c \cdot b}{{d}^{2}} + \color{blue}{\left(-\frac{a}{d}\right)}
\] |
unsub-neg [=>]15.0 | \[ \color{blue}{\frac{c \cdot b}{{d}^{2}} - \frac{a}{d}}
\] |
*-commutative [<=]15.0 | \[ \frac{\color{blue}{b \cdot c}}{{d}^{2}} - \frac{a}{d}
\] |
unpow2 [=>]15.0 | \[ \frac{b \cdot c}{\color{blue}{d \cdot d}} - \frac{a}{d}
\] |
times-frac [=>]7.3 | \[ \color{blue}{\frac{b}{d} \cdot \frac{c}{d}} - \frac{a}{d}
\] |
Final simplification11.2
| Alternative 1 | |
|---|---|
| Error | 12.7 |
| Cost | 7700 |
| Alternative 2 | |
|---|---|
| Error | 12.7 |
| Cost | 7700 |
| Alternative 3 | |
|---|---|
| Error | 13.8 |
| Cost | 1752 |
| Alternative 4 | |
|---|---|
| Error | 15.7 |
| Cost | 1232 |
| Alternative 5 | |
|---|---|
| Error | 15.8 |
| Cost | 1232 |
| Alternative 6 | |
|---|---|
| Error | 20.1 |
| Cost | 1106 |
| Alternative 7 | |
|---|---|
| Error | 19.7 |
| Cost | 1105 |
| Alternative 8 | |
|---|---|
| Error | 15.3 |
| Cost | 1105 |
| Alternative 9 | |
|---|---|
| Error | 15.7 |
| Cost | 1104 |
| Alternative 10 | |
|---|---|
| Error | 23.7 |
| Cost | 786 |
| Alternative 11 | |
|---|---|
| Error | 38.1 |
| Cost | 192 |
herbie shell --seed 2023066
(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))))