| Alternative 1 | |
|---|---|
| Error | 13.3 |
| Cost | 14036 |
(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 (- (* b c) (* d a))) (t_1 (/ (- (* (/ b d) c) a) d)))
(if (<= d -3.3e+128)
t_1
(if (<= d -7.8e-114)
(* (/ 1.0 (fma c c (* d d))) t_0)
(if (<= d 5.2e-90)
(/ (- b (* a (/ d c))) c)
(if (<= d 1.5e-61)
(/ t_0 (+ (* d d) (* c c)))
(if (<= d 5.1e+22)
(/ (/ 1.0 (hypot c d)) (/ (/ (hypot c d) b) c))
t_1)))))))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 = (b * c) - (d * a);
double t_1 = (((b / d) * c) - a) / d;
double tmp;
if (d <= -3.3e+128) {
tmp = t_1;
} else if (d <= -7.8e-114) {
tmp = (1.0 / fma(c, c, (d * d))) * t_0;
} else if (d <= 5.2e-90) {
tmp = (b - (a * (d / c))) / c;
} else if (d <= 1.5e-61) {
tmp = t_0 / ((d * d) + (c * c));
} else if (d <= 5.1e+22) {
tmp = (1.0 / hypot(c, d)) / ((hypot(c, d) / b) / c);
} else {
tmp = t_1;
}
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(b * c) - Float64(d * a)) t_1 = Float64(Float64(Float64(Float64(b / d) * c) - a) / d) tmp = 0.0 if (d <= -3.3e+128) tmp = t_1; elseif (d <= -7.8e-114) tmp = Float64(Float64(1.0 / fma(c, c, Float64(d * d))) * t_0); elseif (d <= 5.2e-90) tmp = Float64(Float64(b - Float64(a * Float64(d / c))) / c); elseif (d <= 1.5e-61) tmp = Float64(t_0 / Float64(Float64(d * d) + Float64(c * c))); elseif (d <= 5.1e+22) tmp = Float64(Float64(1.0 / hypot(c, d)) / Float64(Float64(hypot(c, d) / b) / c)); else tmp = t_1; 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[(b * c), $MachinePrecision] - N[(d * a), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(N[(b / d), $MachinePrecision] * c), $MachinePrecision] - a), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -3.3e+128], t$95$1, If[LessEqual[d, -7.8e-114], N[(N[(1.0 / N[(c * c + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * t$95$0), $MachinePrecision], If[LessEqual[d, 5.2e-90], N[(N[(b - N[(a * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 1.5e-61], N[(t$95$0 / N[(N[(d * d), $MachinePrecision] + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 5.1e+22], N[(N[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] / N[(N[(N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision] / b), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision], t$95$1]]]]]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := b \cdot c - d \cdot a\\
t_1 := \frac{\frac{b}{d} \cdot c - a}{d}\\
\mathbf{if}\;d \leq -3.3 \cdot 10^{+128}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;d \leq -7.8 \cdot 10^{-114}:\\
\;\;\;\;\frac{1}{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot t_0\\
\mathbf{elif}\;d \leq 5.2 \cdot 10^{-90}:\\
\;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\
\mathbf{elif}\;d \leq 1.5 \cdot 10^{-61}:\\
\;\;\;\;\frac{t_0}{d \cdot d + c \cdot c}\\
\mathbf{elif}\;d \leq 5.1 \cdot 10^{+22}:\\
\;\;\;\;\frac{\frac{1}{\mathsf{hypot}\left(c, d\right)}}{\frac{\frac{\mathsf{hypot}\left(c, d\right)}{b}}{c}}\\
\mathbf{else}:\\
\;\;\;\;t_1\\
\end{array}
| Original | 25.8 |
|---|---|
| Target | 0.4 |
| Herbie | 13.3 |
if d < -3.3000000000000001e128 or 5.1000000000000002e22 < d Initial program 36.5
Taylor expanded in c around 0 18.1
Simplified12.4
[Start]18.1 | \[ -1 \cdot \frac{a}{d} + \frac{c \cdot b}{{d}^{2}}
\] |
|---|---|
+-commutative [=>]18.1 | \[ \color{blue}{\frac{c \cdot b}{{d}^{2}} + -1 \cdot \frac{a}{d}}
\] |
mul-1-neg [=>]18.1 | \[ \frac{c \cdot b}{{d}^{2}} + \color{blue}{\left(-\frac{a}{d}\right)}
\] |
unsub-neg [=>]18.1 | \[ \color{blue}{\frac{c \cdot b}{{d}^{2}} - \frac{a}{d}}
\] |
*-commutative [=>]18.1 | \[ \frac{\color{blue}{b \cdot c}}{{d}^{2}} - \frac{a}{d}
\] |
unpow2 [=>]18.1 | \[ \frac{b \cdot c}{\color{blue}{d \cdot d}} - \frac{a}{d}
\] |
times-frac [=>]12.4 | \[ \color{blue}{\frac{b}{d} \cdot \frac{c}{d}} - \frac{a}{d}
\] |
Applied egg-rr12.1
if -3.3000000000000001e128 < d < -7.80000000000000003e-114Initial program 17.2
Applied egg-rr17.2
if -7.80000000000000003e-114 < d < 5.2000000000000001e-90Initial program 21.5
Taylor expanded in c around inf 11.8
Simplified10.5
[Start]11.8 | \[ -1 \cdot \frac{a \cdot d}{{c}^{2}} + \frac{b}{c}
\] |
|---|---|
+-commutative [=>]11.8 | \[ \color{blue}{\frac{b}{c} + -1 \cdot \frac{a \cdot d}{{c}^{2}}}
\] |
mul-1-neg [=>]11.8 | \[ \frac{b}{c} + \color{blue}{\left(-\frac{a \cdot d}{{c}^{2}}\right)}
\] |
unsub-neg [=>]11.8 | \[ \color{blue}{\frac{b}{c} - \frac{a \cdot d}{{c}^{2}}}
\] |
*-commutative [=>]11.8 | \[ \frac{b}{c} - \frac{\color{blue}{d \cdot a}}{{c}^{2}}
\] |
unpow2 [=>]11.8 | \[ \frac{b}{c} - \frac{d \cdot a}{\color{blue}{c \cdot c}}
\] |
times-frac [=>]10.5 | \[ \frac{b}{c} - \color{blue}{\frac{d}{c} \cdot \frac{a}{c}}
\] |
Applied egg-rr8.5
if 5.2000000000000001e-90 < d < 1.50000000000000006e-61Initial program 14.1
if 1.50000000000000006e-61 < d < 5.1000000000000002e22Initial program 14.6
Taylor expanded in b around inf 38.8
Applied egg-rr37.9
Simplified27.6
[Start]37.9 | \[ \frac{1}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \cdot \frac{c}{\frac{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}{b}}
\] |
|---|---|
associate-*l/ [=>]37.8 | \[ \color{blue}{\frac{1 \cdot \frac{c}{\frac{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}{b}}}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}}
\] |
*-lft-identity [=>]37.8 | \[ \frac{\color{blue}{\frac{c}{\frac{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}{b}}}}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}
\] |
associate-/r/ [=>]37.3 | \[ \frac{\color{blue}{\frac{c}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \cdot b}}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}
\] |
associate-*r/ [<=]37.9 | \[ \color{blue}{\frac{c}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \cdot \frac{b}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}}
\] |
*-commutative [=>]37.9 | \[ \color{blue}{\frac{b}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}} \cdot \frac{c}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}}
\] |
fma-udef [=>]37.9 | \[ \frac{b}{\sqrt{\color{blue}{c \cdot c + d \cdot d}}} \cdot \frac{c}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}
\] |
hypot-def [=>]37.9 | \[ \frac{b}{\color{blue}{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{c}{\sqrt{\mathsf{fma}\left(c, c, d \cdot d\right)}}
\] |
fma-udef [=>]37.9 | \[ \frac{b}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{c}{\sqrt{\color{blue}{c \cdot c + d \cdot d}}}
\] |
hypot-def [=>]27.6 | \[ \frac{b}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{c}{\color{blue}{\mathsf{hypot}\left(c, d\right)}}
\] |
Applied egg-rr27.9
Final simplification13.3
| Alternative 1 | |
|---|---|
| Error | 13.3 |
| Cost | 14036 |
| Alternative 2 | |
|---|---|
| Error | 13.3 |
| Cost | 7624 |
| Alternative 3 | |
|---|---|
| Error | 13.3 |
| Cost | 1488 |
| Alternative 4 | |
|---|---|
| Error | 18.5 |
| Cost | 841 |
| Alternative 5 | |
|---|---|
| Error | 15.2 |
| Cost | 841 |
| Alternative 6 | |
|---|---|
| Error | 15.4 |
| Cost | 840 |
| Alternative 7 | |
|---|---|
| Error | 24.7 |
| Cost | 521 |
| Alternative 8 | |
|---|---|
| Error | 36.9 |
| Cost | 192 |
herbie shell --seed 2023010
(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))))