| Alternative 1 | |
|---|---|
| Error | 13.8 |
| Cost | 13768 |
(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 d) (/ c d)) (/ a d)))
(t_1 (* (/ 1.0 (hypot c d)) (/ (- (* b c) (* d a)) (hypot c d)))))
(if (<= d -3.3e+33)
t_0
(if (<= d -2e-59)
(- (/ b c) (* (/ d c) (/ a c)))
(if (<= d -1.8e-197)
t_1
(if (<= d 6.9e-152)
(/ (- b (* d (/ a c))) c)
(if (<= d 2.4e+137) t_1 t_0)))))))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 / d) * (c / d)) - (a / d);
double t_1 = (1.0 / hypot(c, d)) * (((b * c) - (d * a)) / hypot(c, d));
double tmp;
if (d <= -3.3e+33) {
tmp = t_0;
} else if (d <= -2e-59) {
tmp = (b / c) - ((d / c) * (a / c));
} else if (d <= -1.8e-197) {
tmp = t_1;
} else if (d <= 6.9e-152) {
tmp = (b - (d * (a / c))) / c;
} else if (d <= 2.4e+137) {
tmp = t_1;
} else {
tmp = t_0;
}
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 = ((b / d) * (c / d)) - (a / d);
double t_1 = (1.0 / Math.hypot(c, d)) * (((b * c) - (d * a)) / Math.hypot(c, d));
double tmp;
if (d <= -3.3e+33) {
tmp = t_0;
} else if (d <= -2e-59) {
tmp = (b / c) - ((d / c) * (a / c));
} else if (d <= -1.8e-197) {
tmp = t_1;
} else if (d <= 6.9e-152) {
tmp = (b - (d * (a / c))) / c;
} else if (d <= 2.4e+137) {
tmp = t_1;
} else {
tmp = t_0;
}
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 = ((b / d) * (c / d)) - (a / d) t_1 = (1.0 / math.hypot(c, d)) * (((b * c) - (d * a)) / math.hypot(c, d)) tmp = 0 if d <= -3.3e+33: tmp = t_0 elif d <= -2e-59: tmp = (b / c) - ((d / c) * (a / c)) elif d <= -1.8e-197: tmp = t_1 elif d <= 6.9e-152: tmp = (b - (d * (a / c))) / c elif d <= 2.4e+137: tmp = t_1 else: tmp = t_0 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(Float64(b / d) * Float64(c / d)) - Float64(a / d)) t_1 = Float64(Float64(1.0 / hypot(c, d)) * Float64(Float64(Float64(b * c) - Float64(d * a)) / hypot(c, d))) tmp = 0.0 if (d <= -3.3e+33) tmp = t_0; elseif (d <= -2e-59) tmp = Float64(Float64(b / c) - Float64(Float64(d / c) * Float64(a / c))); elseif (d <= -1.8e-197) tmp = t_1; elseif (d <= 6.9e-152) tmp = Float64(Float64(b - Float64(d * Float64(a / c))) / c); elseif (d <= 2.4e+137) tmp = t_1; else tmp = t_0; 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 = ((b / d) * (c / d)) - (a / d); t_1 = (1.0 / hypot(c, d)) * (((b * c) - (d * a)) / hypot(c, d)); tmp = 0.0; if (d <= -3.3e+33) tmp = t_0; elseif (d <= -2e-59) tmp = (b / c) - ((d / c) * (a / c)); elseif (d <= -1.8e-197) tmp = t_1; elseif (d <= 6.9e-152) tmp = (b - (d * (a / c))) / c; elseif (d <= 2.4e+137) tmp = t_1; else tmp = t_0; 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[(N[(b / d), $MachinePrecision] * N[(c / d), $MachinePrecision]), $MachinePrecision] - N[(a / d), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(N[(N[(b * c), $MachinePrecision] - N[(d * a), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -3.3e+33], t$95$0, If[LessEqual[d, -2e-59], N[(N[(b / c), $MachinePrecision] - N[(N[(d / c), $MachinePrecision] * N[(a / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, -1.8e-197], t$95$1, If[LessEqual[d, 6.9e-152], N[(N[(b - N[(d * N[(a / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 2.4e+137], t$95$1, t$95$0]]]]]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{b}{d} \cdot \frac{c}{d} - \frac{a}{d}\\
t_1 := \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{b \cdot c - d \cdot a}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{if}\;d \leq -3.3 \cdot 10^{+33}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;d \leq -2 \cdot 10^{-59}:\\
\;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\
\mathbf{elif}\;d \leq -1.8 \cdot 10^{-197}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;d \leq 6.9 \cdot 10^{-152}:\\
\;\;\;\;\frac{b - d \cdot \frac{a}{c}}{c}\\
\mathbf{elif}\;d \leq 2.4 \cdot 10^{+137}:\\
\;\;\;\;t_1\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
Results
| Original | 26.9 |
|---|---|
| Target | 0.5 |
| Herbie | 11.9 |
if d < -3.29999999999999976e33 or 2.39999999999999983e137 < d Initial program 38.9
Simplified38.9
[Start]38.9 | \[ \frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\] |
|---|---|
fma-def [=>]38.9 | \[ \frac{b \cdot c - a \cdot d}{\color{blue}{\mathsf{fma}\left(c, c, d \cdot d\right)}}
\] |
Taylor expanded in c around 0 16.8
Simplified11.1
[Start]16.8 | \[ -1 \cdot \frac{a}{d} + \frac{c \cdot b}{{d}^{2}}
\] |
|---|---|
+-commutative [=>]16.8 | \[ \color{blue}{\frac{c \cdot b}{{d}^{2}} + -1 \cdot \frac{a}{d}}
\] |
mul-1-neg [=>]16.8 | \[ \frac{c \cdot b}{{d}^{2}} + \color{blue}{\left(-\frac{a}{d}\right)}
\] |
unsub-neg [=>]16.8 | \[ \color{blue}{\frac{c \cdot b}{{d}^{2}} - \frac{a}{d}}
\] |
*-commutative [<=]16.8 | \[ \frac{\color{blue}{b \cdot c}}{{d}^{2}} - \frac{a}{d}
\] |
unpow2 [=>]16.8 | \[ \frac{b \cdot c}{\color{blue}{d \cdot d}} - \frac{a}{d}
\] |
times-frac [=>]11.1 | \[ \color{blue}{\frac{b}{d} \cdot \frac{c}{d}} - \frac{a}{d}
\] |
if -3.29999999999999976e33 < d < -2.0000000000000001e-59Initial program 13.4
Taylor expanded in c around inf 32.1
Simplified30.1
[Start]32.1 | \[ -1 \cdot \frac{a \cdot d}{{c}^{2}} + \frac{b}{c}
\] |
|---|---|
+-commutative [=>]32.1 | \[ \color{blue}{\frac{b}{c} + -1 \cdot \frac{a \cdot d}{{c}^{2}}}
\] |
mul-1-neg [=>]32.1 | \[ \frac{b}{c} + \color{blue}{\left(-\frac{a \cdot d}{{c}^{2}}\right)}
\] |
unsub-neg [=>]32.1 | \[ \color{blue}{\frac{b}{c} - \frac{a \cdot d}{{c}^{2}}}
\] |
*-commutative [=>]32.1 | \[ \frac{b}{c} - \frac{\color{blue}{d \cdot a}}{{c}^{2}}
\] |
unpow2 [=>]32.1 | \[ \frac{b}{c} - \frac{d \cdot a}{\color{blue}{c \cdot c}}
\] |
times-frac [=>]30.1 | \[ \frac{b}{c} - \color{blue}{\frac{d}{c} \cdot \frac{a}{c}}
\] |
if -2.0000000000000001e-59 < d < -1.7999999999999999e-197 or 6.90000000000000039e-152 < d < 2.39999999999999983e137Initial program 17.2
Applied egg-rr11.6
if -1.7999999999999999e-197 < d < 6.90000000000000039e-152Initial program 24.8
Taylor expanded in c around inf 25.0
Simplified25.0
[Start]25.0 | \[ \frac{b \cdot c - a \cdot d}{{c}^{2}}
\] |
|---|---|
unpow2 [=>]25.0 | \[ \frac{b \cdot c - a \cdot d}{\color{blue}{c \cdot c}}
\] |
Applied egg-rr46.4
Simplified6.4
[Start]46.4 | \[ e^{\mathsf{log1p}\left(\left(b \cdot c - a \cdot d\right) \cdot {c}^{-2}\right)} - 1
\] |
|---|---|
expm1-def [=>]34.1 | \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\left(b \cdot c - a \cdot d\right) \cdot {c}^{-2}\right)\right)}
\] |
expm1-log1p [=>]25.2 | \[ \color{blue}{\left(b \cdot c - a \cdot d\right) \cdot {c}^{-2}}
\] |
sqr-pow [=>]25.3 | \[ \left(b \cdot c - a \cdot d\right) \cdot \color{blue}{\left({c}^{\left(\frac{-2}{2}\right)} \cdot {c}^{\left(\frac{-2}{2}\right)}\right)}
\] |
associate-*r* [=>]16.5 | \[ \color{blue}{\left(\left(b \cdot c - a \cdot d\right) \cdot {c}^{\left(\frac{-2}{2}\right)}\right) \cdot {c}^{\left(\frac{-2}{2}\right)}}
\] |
metadata-eval [=>]16.5 | \[ \left(\left(b \cdot c - a \cdot d\right) \cdot {c}^{\color{blue}{-1}}\right) \cdot {c}^{\left(\frac{-2}{2}\right)}
\] |
unpow-1 [=>]16.5 | \[ \left(\left(b \cdot c - a \cdot d\right) \cdot \color{blue}{\frac{1}{c}}\right) \cdot {c}^{\left(\frac{-2}{2}\right)}
\] |
associate-*r/ [=>]16.4 | \[ \color{blue}{\frac{\left(b \cdot c - a \cdot d\right) \cdot 1}{c}} \cdot {c}^{\left(\frac{-2}{2}\right)}
\] |
associate-*l/ [<=]16.4 | \[ \color{blue}{\left(\frac{b \cdot c - a \cdot d}{c} \cdot 1\right)} \cdot {c}^{\left(\frac{-2}{2}\right)}
\] |
*-rgt-identity [=>]16.4 | \[ \color{blue}{\frac{b \cdot c - a \cdot d}{c}} \cdot {c}^{\left(\frac{-2}{2}\right)}
\] |
metadata-eval [=>]16.4 | \[ \frac{b \cdot c - a \cdot d}{c} \cdot {c}^{\color{blue}{-1}}
\] |
unpow-1 [=>]16.4 | \[ \frac{b \cdot c - a \cdot d}{c} \cdot \color{blue}{\frac{1}{c}}
\] |
associate-*r/ [=>]16.3 | \[ \color{blue}{\frac{\frac{b \cdot c - a \cdot d}{c} \cdot 1}{c}}
\] |
*-commutative [<=]16.3 | \[ \frac{\color{blue}{1 \cdot \frac{b \cdot c - a \cdot d}{c}}}{c}
\] |
*-lft-identity [=>]16.3 | \[ \frac{\color{blue}{\frac{b \cdot c - a \cdot d}{c}}}{c}
\] |
Final simplification11.9
| Alternative 1 | |
|---|---|
| Error | 13.8 |
| Cost | 13768 |
| Alternative 2 | |
|---|---|
| Error | 13.6 |
| Cost | 7628 |
| Alternative 3 | |
|---|---|
| Error | 13.6 |
| Cost | 1356 |
| Alternative 4 | |
|---|---|
| Error | 15.1 |
| Cost | 969 |
| Alternative 5 | |
|---|---|
| Error | 23.6 |
| Cost | 908 |
| Alternative 6 | |
|---|---|
| Error | 18.7 |
| Cost | 841 |
| Alternative 7 | |
|---|---|
| Error | 15.3 |
| Cost | 841 |
| Alternative 8 | |
|---|---|
| Error | 22.6 |
| Cost | 521 |
| Alternative 9 | |
|---|---|
| Error | 37.9 |
| Cost | 192 |
herbie shell --seed 2023060
(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))))