| Alternative 1 | |
|---|---|
| Accuracy | 85.5% |
| Cost | 14288 |

(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 -2.1e+160)
(/ (- (* (/ c d) b) a) d)
(if (<= d -4.5e-130)
t_0
(if (<= d 1.6e-79)
(/ (- b (* a (/ d c))) c)
(if (<= d 3.6e+37) t_0 (/ (- (* c (/ b 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 <= -2.1e+160) {
tmp = (((c / d) * b) - a) / d;
} else if (d <= -4.5e-130) {
tmp = t_0;
} else if (d <= 1.6e-79) {
tmp = (b - (a * (d / c))) / c;
} else if (d <= 3.6e+37) {
tmp = t_0;
} else {
tmp = ((c * (b / d)) - a) / d;
}
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 = (1.0 / Math.hypot(c, d)) * (((c * b) - (d * a)) / Math.hypot(c, d));
double tmp;
if (d <= -2.1e+160) {
tmp = (((c / d) * b) - a) / d;
} else if (d <= -4.5e-130) {
tmp = t_0;
} else if (d <= 1.6e-79) {
tmp = (b - (a * (d / c))) / c;
} else if (d <= 3.6e+37) {
tmp = t_0;
} else {
tmp = ((c * (b / d)) - a) / d;
}
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 = (1.0 / math.hypot(c, d)) * (((c * b) - (d * a)) / math.hypot(c, d)) tmp = 0 if d <= -2.1e+160: tmp = (((c / d) * b) - a) / d elif d <= -4.5e-130: tmp = t_0 elif d <= 1.6e-79: tmp = (b - (a * (d / c))) / c elif d <= 3.6e+37: tmp = t_0 else: tmp = ((c * (b / 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 <= -2.1e+160) tmp = Float64(Float64(Float64(Float64(c / d) * b) - a) / d); elseif (d <= -4.5e-130) tmp = t_0; elseif (d <= 1.6e-79) tmp = Float64(Float64(b - Float64(a * Float64(d / c))) / c); elseif (d <= 3.6e+37) tmp = t_0; else tmp = Float64(Float64(Float64(c * Float64(b / d)) - a) / d); 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 = (1.0 / hypot(c, d)) * (((c * b) - (d * a)) / hypot(c, d)); tmp = 0.0; if (d <= -2.1e+160) tmp = (((c / d) * b) - a) / d; elseif (d <= -4.5e-130) tmp = t_0; elseif (d <= 1.6e-79) tmp = (b - (a * (d / c))) / c; elseif (d <= 3.6e+37) tmp = t_0; else tmp = ((c * (b / d)) - a) / d; 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[(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, -2.1e+160], N[(N[(N[(N[(c / d), $MachinePrecision] * b), $MachinePrecision] - a), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[d, -4.5e-130], t$95$0, If[LessEqual[d, 1.6e-79], N[(N[(b - N[(a * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 3.6e+37], t$95$0, N[(N[(N[(c * N[(b / d), $MachinePrecision]), $MachinePrecision] - a), $MachinePrecision] / d), $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 -2.1 \cdot 10^{+160}:\\
\;\;\;\;\frac{\frac{c}{d} \cdot b - a}{d}\\
\mathbf{elif}\;d \leq -4.5 \cdot 10^{-130}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;d \leq 1.6 \cdot 10^{-79}:\\
\;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\
\mathbf{elif}\;d \leq 3.6 \cdot 10^{+37}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{c \cdot \frac{b}{d} - a}{d}\\
\end{array}
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
Results
| Original | 62.6% |
|---|---|
| Target | 99.3% |
| Herbie | 85.5% |
if d < -2.09999999999999997e160Initial program 21.3%
Taylor expanded in c around 0 84.0%
Simplified96.9%
[Start]84.0% | \[ -1 \cdot \frac{a}{d} + \frac{c \cdot b}{{d}^{2}}
\] |
|---|---|
+-commutative [=>]84.0% | \[ \color{blue}{\frac{c \cdot b}{{d}^{2}} + -1 \cdot \frac{a}{d}}
\] |
mul-1-neg [=>]84.0% | \[ \frac{c \cdot b}{{d}^{2}} + \color{blue}{\left(-\frac{a}{d}\right)}
\] |
unsub-neg [=>]84.0% | \[ \color{blue}{\frac{c \cdot b}{{d}^{2}} - \frac{a}{d}}
\] |
unpow2 [=>]84.0% | \[ \frac{c \cdot b}{\color{blue}{d \cdot d}} - \frac{a}{d}
\] |
times-frac [=>]96.9% | \[ \color{blue}{\frac{c}{d} \cdot \frac{b}{d}} - \frac{a}{d}
\] |
Applied egg-rr96.9%
[Start]96.9% | \[ \frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}
\] |
|---|---|
associate-*r/ [=>]96.9% | \[ \color{blue}{\frac{\frac{c}{d} \cdot b}{d}} - \frac{a}{d}
\] |
sub-div [=>]96.9% | \[ \color{blue}{\frac{\frac{c}{d} \cdot b - a}{d}}
\] |
if -2.09999999999999997e160 < d < -4.5e-130 or 1.59999999999999994e-79 < d < 3.59999999999999998e37Initial program 77.0%
Applied egg-rr86.6%
[Start]77.0% | \[ \frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\] |
|---|---|
*-un-lft-identity [=>]77.0% | \[ \frac{\color{blue}{1 \cdot \left(b \cdot c - a \cdot d\right)}}{c \cdot c + d \cdot d}
\] |
add-sqr-sqrt [=>]77.0% | \[ \frac{1 \cdot \left(b \cdot c - a \cdot d\right)}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}
\] |
times-frac [=>]77.0% | \[ \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}}
\] |
hypot-def [=>]77.0% | \[ \frac{1}{\color{blue}{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}
\] |
hypot-def [=>]86.6% | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{b \cdot c - a \cdot d}{\color{blue}{\mathsf{hypot}\left(c, d\right)}}
\] |
if -4.5e-130 < d < 1.59999999999999994e-79Initial program 68.9%
Applied egg-rr82.9%
[Start]68.9% | \[ \frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\] |
|---|---|
*-un-lft-identity [=>]68.9% | \[ \frac{\color{blue}{1 \cdot \left(b \cdot c - a \cdot d\right)}}{c \cdot c + d \cdot d}
\] |
add-sqr-sqrt [=>]68.9% | \[ \frac{1 \cdot \left(b \cdot c - a \cdot d\right)}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}
\] |
times-frac [=>]68.8% | \[ \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}}
\] |
hypot-def [=>]68.8% | \[ \frac{1}{\color{blue}{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}
\] |
hypot-def [=>]82.9% | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{b \cdot c - a \cdot d}{\color{blue}{\mathsf{hypot}\left(c, d\right)}}
\] |
Taylor expanded in c around inf 85.5%
Simplified94.5%
[Start]85.5% | \[ -1 \cdot \frac{a \cdot d}{{c}^{2}} + \frac{b}{c}
\] |
|---|---|
+-commutative [=>]85.5% | \[ \color{blue}{\frac{b}{c} + -1 \cdot \frac{a \cdot d}{{c}^{2}}}
\] |
mul-1-neg [=>]85.5% | \[ \frac{b}{c} + \color{blue}{\left(-\frac{a \cdot d}{{c}^{2}}\right)}
\] |
unsub-neg [=>]85.5% | \[ \color{blue}{\frac{b}{c} - \frac{a \cdot d}{{c}^{2}}}
\] |
unpow2 [=>]85.5% | \[ \frac{b}{c} - \frac{a \cdot d}{\color{blue}{c \cdot c}}
\] |
associate-/r* [=>]93.5% | \[ \frac{b}{c} - \color{blue}{\frac{\frac{a \cdot d}{c}}{c}}
\] |
associate-/l* [=>]94.5% | \[ \frac{b}{c} - \frac{\color{blue}{\frac{a}{\frac{c}{d}}}}{c}
\] |
Applied egg-rr95.6%
[Start]94.5% | \[ \frac{b}{c} - \frac{\frac{a}{\frac{c}{d}}}{c}
\] |
|---|---|
*-un-lft-identity [=>]94.5% | \[ \color{blue}{1 \cdot \left(\frac{b}{c} - \frac{\frac{a}{\frac{c}{d}}}{c}\right)}
\] |
sub-div [=>]95.6% | \[ 1 \cdot \color{blue}{\frac{b - \frac{a}{\frac{c}{d}}}{c}}
\] |
Simplified95.6%
[Start]95.6% | \[ 1 \cdot \frac{b - \frac{a}{\frac{c}{d}}}{c}
\] |
|---|---|
*-lft-identity [=>]95.6% | \[ \color{blue}{\frac{b - \frac{a}{\frac{c}{d}}}{c}}
\] |
associate-/l* [<=]94.6% | \[ \frac{b - \color{blue}{\frac{a \cdot d}{c}}}{c}
\] |
associate-*r/ [<=]95.6% | \[ \frac{b - \color{blue}{a \cdot \frac{d}{c}}}{c}
\] |
if 3.59999999999999998e37 < d Initial program 48.7%
Applied egg-rr63.3%
[Start]48.7% | \[ \frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\] |
|---|---|
*-un-lft-identity [=>]48.7% | \[ \frac{\color{blue}{1 \cdot \left(b \cdot c - a \cdot d\right)}}{c \cdot c + d \cdot d}
\] |
add-sqr-sqrt [=>]48.7% | \[ \frac{1 \cdot \left(b \cdot c - a \cdot d\right)}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}
\] |
times-frac [=>]48.7% | \[ \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}}
\] |
hypot-def [=>]48.7% | \[ \frac{1}{\color{blue}{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{b \cdot c - a \cdot d}{\sqrt{c \cdot c + d \cdot d}}
\] |
hypot-def [=>]63.3% | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{b \cdot c - a \cdot d}{\color{blue}{\mathsf{hypot}\left(c, d\right)}}
\] |
Taylor expanded in c around 0 84.0%
Simplified92.2%
[Start]84.0% | \[ -1 \cdot \frac{a}{d} + \frac{c \cdot b}{{d}^{2}}
\] |
|---|---|
neg-mul-1 [<=]84.0% | \[ \color{blue}{\left(-\frac{a}{d}\right)} + \frac{c \cdot b}{{d}^{2}}
\] |
*-commutative [=>]84.0% | \[ \left(-\frac{a}{d}\right) + \frac{\color{blue}{b \cdot c}}{{d}^{2}}
\] |
unpow2 [=>]84.0% | \[ \left(-\frac{a}{d}\right) + \frac{b \cdot c}{\color{blue}{d \cdot d}}
\] |
times-frac [=>]90.6% | \[ \left(-\frac{a}{d}\right) + \color{blue}{\frac{b}{d} \cdot \frac{c}{d}}
\] |
*-commutative [<=]90.6% | \[ \left(-\frac{a}{d}\right) + \color{blue}{\frac{c}{d} \cdot \frac{b}{d}}
\] |
+-commutative [<=]90.6% | \[ \color{blue}{\frac{c}{d} \cdot \frac{b}{d} + \left(-\frac{a}{d}\right)}
\] |
unsub-neg [=>]90.6% | \[ \color{blue}{\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}}
\] |
associate-*l/ [=>]92.2% | \[ \color{blue}{\frac{c \cdot \frac{b}{d}}{d}} - \frac{a}{d}
\] |
div-sub [<=]92.2% | \[ \color{blue}{\frac{c \cdot \frac{b}{d} - a}{d}}
\] |
Final simplification92.3%
| Alternative 1 | |
|---|---|
| Accuracy | 85.5% |
| Cost | 14288 |
| Alternative 2 | |
|---|---|
| Accuracy | 83.1% |
| Cost | 1488 |
| Alternative 3 | |
|---|---|
| Accuracy | 71.9% |
| Cost | 841 |
| Alternative 4 | |
|---|---|
| Accuracy | 77.5% |
| Cost | 841 |
| Alternative 5 | |
|---|---|
| Accuracy | 77.3% |
| Cost | 840 |
| Alternative 6 | |
|---|---|
| Accuracy | 63.0% |
| Cost | 521 |
| Alternative 7 | |
|---|---|
| Accuracy | 42.4% |
| Cost | 192 |
herbie shell --seed 2023272
(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))))