| Alternative 1 | |
|---|---|
| Error | 12.0 |
| Cost | 7172 |
(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 b) (* d a)) (hypot c d)) (hypot c d))))
(if (<= c -4.6e+139)
(/ (- b (* d (/ a c))) c)
(if (<= c -1.7e-157)
t_0
(if (<= c 1.6e-58)
(/ (- a (* b (/ c d))) (- d))
(if (<= c 1.9e+148) t_0 (- (/ b c) (* (/ a c) (/ d c)))))))))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 * b) - (d * a)) / hypot(c, d)) / hypot(c, d);
double tmp;
if (c <= -4.6e+139) {
tmp = (b - (d * (a / c))) / c;
} else if (c <= -1.7e-157) {
tmp = t_0;
} else if (c <= 1.6e-58) {
tmp = (a - (b * (c / d))) / -d;
} else if (c <= 1.9e+148) {
tmp = t_0;
} else {
tmp = (b / c) - ((a / c) * (d / c));
}
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 * b) - (d * a)) / Math.hypot(c, d)) / Math.hypot(c, d);
double tmp;
if (c <= -4.6e+139) {
tmp = (b - (d * (a / c))) / c;
} else if (c <= -1.7e-157) {
tmp = t_0;
} else if (c <= 1.6e-58) {
tmp = (a - (b * (c / d))) / -d;
} else if (c <= 1.9e+148) {
tmp = t_0;
} else {
tmp = (b / c) - ((a / c) * (d / c));
}
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 * b) - (d * a)) / math.hypot(c, d)) / math.hypot(c, d) tmp = 0 if c <= -4.6e+139: tmp = (b - (d * (a / c))) / c elif c <= -1.7e-157: tmp = t_0 elif c <= 1.6e-58: tmp = (a - (b * (c / d))) / -d elif c <= 1.9e+148: tmp = t_0 else: tmp = (b / c) - ((a / c) * (d / c)) 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(Float64(c * b) - Float64(d * a)) / hypot(c, d)) / hypot(c, d)) tmp = 0.0 if (c <= -4.6e+139) tmp = Float64(Float64(b - Float64(d * Float64(a / c))) / c); elseif (c <= -1.7e-157) tmp = t_0; elseif (c <= 1.6e-58) tmp = Float64(Float64(a - Float64(b * Float64(c / d))) / Float64(-d)); elseif (c <= 1.9e+148) tmp = t_0; else tmp = Float64(Float64(b / c) - Float64(Float64(a / c) * Float64(d / c))); 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 * b) - (d * a)) / hypot(c, d)) / hypot(c, d); tmp = 0.0; if (c <= -4.6e+139) tmp = (b - (d * (a / c))) / c; elseif (c <= -1.7e-157) tmp = t_0; elseif (c <= 1.6e-58) tmp = (a - (b * (c / d))) / -d; elseif (c <= 1.9e+148) tmp = t_0; else tmp = (b / c) - ((a / c) * (d / c)); 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[(N[(c * b), $MachinePrecision] - N[(d * a), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -4.6e+139], N[(N[(b - N[(d * N[(a / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[c, -1.7e-157], t$95$0, If[LessEqual[c, 1.6e-58], N[(N[(a - N[(b * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / (-d)), $MachinePrecision], If[LessEqual[c, 1.9e+148], t$95$0, N[(N[(b / c), $MachinePrecision] - N[(N[(a / c), $MachinePrecision] * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{\frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{if}\;c \leq -4.6 \cdot 10^{+139}:\\
\;\;\;\;\frac{b - d \cdot \frac{a}{c}}{c}\\
\mathbf{elif}\;c \leq -1.7 \cdot 10^{-157}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;c \leq 1.6 \cdot 10^{-58}:\\
\;\;\;\;\frac{a - b \cdot \frac{c}{d}}{-d}\\
\mathbf{elif}\;c \leq 1.9 \cdot 10^{+148}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{c} - \frac{a}{c} \cdot \frac{d}{c}\\
\end{array}
Results
| Original | 26.7 |
|---|---|
| Target | 0.3 |
| Herbie | 9.8 |
if c < -4.6e139Initial program 43.6
Taylor expanded in c around inf 13.4
Simplified6.5
[Start]13.4 | \[ -1 \cdot \frac{a \cdot d}{{c}^{2}} + \frac{b}{c}
\] |
|---|---|
+-commutative [=>]13.4 | \[ \color{blue}{\frac{b}{c} + -1 \cdot \frac{a \cdot d}{{c}^{2}}}
\] |
mul-1-neg [=>]13.4 | \[ \frac{b}{c} + \color{blue}{\left(-\frac{a \cdot d}{{c}^{2}}\right)}
\] |
unsub-neg [=>]13.4 | \[ \color{blue}{\frac{b}{c} - \frac{a \cdot d}{{c}^{2}}}
\] |
*-commutative [=>]13.4 | \[ \frac{b}{c} - \frac{\color{blue}{d \cdot a}}{{c}^{2}}
\] |
unpow2 [=>]13.4 | \[ \frac{b}{c} - \frac{d \cdot a}{\color{blue}{c \cdot c}}
\] |
times-frac [=>]6.5 | \[ \frac{b}{c} - \color{blue}{\frac{d}{c} \cdot \frac{a}{c}}
\] |
Taylor expanded in b around 0 13.4
Simplified6.3
[Start]13.4 | \[ -1 \cdot \frac{a \cdot d}{{c}^{2}} + \frac{b}{c}
\] |
|---|---|
mul-1-neg [=>]13.4 | \[ \color{blue}{\left(-\frac{a \cdot d}{{c}^{2}}\right)} + \frac{b}{c}
\] |
*-commutative [<=]13.4 | \[ \left(-\frac{\color{blue}{d \cdot a}}{{c}^{2}}\right) + \frac{b}{c}
\] |
unpow2 [=>]13.4 | \[ \left(-\frac{d \cdot a}{\color{blue}{c \cdot c}}\right) + \frac{b}{c}
\] |
associate-*l/ [<=]12.1 | \[ \left(-\color{blue}{\frac{d}{c \cdot c} \cdot a}\right) + \frac{b}{c}
\] |
+-commutative [<=]12.1 | \[ \color{blue}{\frac{b}{c} + \left(-\frac{d}{c \cdot c} \cdot a\right)}
\] |
sub-neg [<=]12.1 | \[ \color{blue}{\frac{b}{c} - \frac{d}{c \cdot c} \cdot a}
\] |
associate-*l/ [=>]13.4 | \[ \frac{b}{c} - \color{blue}{\frac{d \cdot a}{c \cdot c}}
\] |
associate-/r* [=>]11.1 | \[ \frac{b}{c} - \color{blue}{\frac{\frac{d \cdot a}{c}}{c}}
\] |
div-sub [<=]11.1 | \[ \color{blue}{\frac{b - \frac{d \cdot a}{c}}{c}}
\] |
associate-*r/ [<=]6.3 | \[ \frac{b - \color{blue}{d \cdot \frac{a}{c}}}{c}
\] |
if -4.6e139 < c < -1.69999999999999989e-157 or 1.6e-58 < c < 1.8999999999999999e148Initial program 18.0
Applied egg-rr13.1
Applied egg-rr13.0
if -1.69999999999999989e-157 < c < 1.6e-58Initial program 22.0
Applied egg-rr12.4
Taylor expanded in c around 0 31.9
Simplified31.9
[Start]31.9 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\frac{c \cdot b}{d} + -1 \cdot a\right)
\] |
|---|---|
+-commutative [=>]31.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 [=>]31.9 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\color{blue}{\left(-a\right)} + \frac{c \cdot b}{d}\right)
\] |
associate-/l* [=>]32.3 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\left(-a\right) + \color{blue}{\frac{c}{\frac{d}{b}}}\right)
\] |
associate-/r/ [=>]31.9 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(\left(-a\right) + \color{blue}{\frac{c}{d} \cdot b}\right)
\] |
Taylor expanded in c around 0 8.1
Applied egg-rr9.0
Simplified7.9
[Start]9.0 | \[ \frac{a - \frac{c}{\frac{d}{b}}}{-d}
\] |
|---|---|
associate-/r/ [=>]7.9 | \[ \frac{a - \color{blue}{\frac{c}{d} \cdot b}}{-d}
\] |
*-commutative [=>]7.9 | \[ \frac{a - \color{blue}{b \cdot \frac{c}{d}}}{-d}
\] |
if 1.8999999999999999e148 < c Initial program 45.0
Taylor expanded in c around inf 15.6
Simplified7.9
[Start]15.6 | \[ -1 \cdot \frac{a \cdot d}{{c}^{2}} + \frac{b}{c}
\] |
|---|---|
+-commutative [=>]15.6 | \[ \color{blue}{\frac{b}{c} + -1 \cdot \frac{a \cdot d}{{c}^{2}}}
\] |
mul-1-neg [=>]15.6 | \[ \frac{b}{c} + \color{blue}{\left(-\frac{a \cdot d}{{c}^{2}}\right)}
\] |
unsub-neg [=>]15.6 | \[ \color{blue}{\frac{b}{c} - \frac{a \cdot d}{{c}^{2}}}
\] |
*-commutative [=>]15.6 | \[ \frac{b}{c} - \frac{\color{blue}{d \cdot a}}{{c}^{2}}
\] |
unpow2 [=>]15.6 | \[ \frac{b}{c} - \frac{d \cdot a}{\color{blue}{c \cdot c}}
\] |
times-frac [=>]7.9 | \[ \frac{b}{c} - \color{blue}{\frac{d}{c} \cdot \frac{a}{c}}
\] |
Final simplification9.8
| Alternative 1 | |
|---|---|
| Error | 12.0 |
| Cost | 7172 |
| Alternative 2 | |
|---|---|
| Error | 12.1 |
| Cost | 1488 |
| Alternative 3 | |
|---|---|
| Error | 15.3 |
| Cost | 1232 |
| Alternative 4 | |
|---|---|
| Error | 15.2 |
| Cost | 1232 |
| Alternative 5 | |
|---|---|
| Error | 14.7 |
| Cost | 1168 |
| Alternative 6 | |
|---|---|
| Error | 15.2 |
| Cost | 1168 |
| Alternative 7 | |
|---|---|
| Error | 16.6 |
| Cost | 905 |
| Alternative 8 | |
|---|---|
| Error | 18.1 |
| Cost | 841 |
| Alternative 9 | |
|---|---|
| Error | 22.2 |
| Cost | 520 |
| Alternative 10 | |
|---|---|
| Error | 34.7 |
| Cost | 456 |
| Alternative 11 | |
|---|---|
| Error | 57.1 |
| Cost | 192 |
herbie shell --seed 2023018
(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))))