| Alternative 1 | |
|---|---|
| Error | 12.8 |
| Cost | 20304 |
(FPCore (a b c d) :precision binary64 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (* (/ 1.0 (hypot c d)) (/ (fma a c (* d b)) (hypot c d)))))
(if (<= d -8.2e+133)
(* (+ b (/ c (/ d a))) (/ -1.0 (hypot c d)))
(if (<= d -1.45e-92)
t_0
(if (<= d 1.35e-200)
(+ (/ a c) (/ b (* c (/ c d))))
(if (<= d 3.9e+136) t_0 (+ (/ b d) (/ (* a (/ c d)) d))))))))double code(double a, double b, double c, double d) {
return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
double code(double a, double b, double c, double d) {
double t_0 = (1.0 / hypot(c, d)) * (fma(a, c, (d * b)) / hypot(c, d));
double tmp;
if (d <= -8.2e+133) {
tmp = (b + (c / (d / a))) * (-1.0 / hypot(c, d));
} else if (d <= -1.45e-92) {
tmp = t_0;
} else if (d <= 1.35e-200) {
tmp = (a / c) + (b / (c * (c / d)));
} else if (d <= 3.9e+136) {
tmp = t_0;
} else {
tmp = (b / d) + ((a * (c / d)) / d);
}
return tmp;
}
function code(a, b, c, d) return Float64(Float64(Float64(a * c) + Float64(b * 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(fma(a, c, Float64(d * b)) / hypot(c, d))) tmp = 0.0 if (d <= -8.2e+133) tmp = Float64(Float64(b + Float64(c / Float64(d / a))) * Float64(-1.0 / hypot(c, d))); elseif (d <= -1.45e-92) tmp = t_0; elseif (d <= 1.35e-200) tmp = Float64(Float64(a / c) + Float64(b / Float64(c * Float64(c / d)))); elseif (d <= 3.9e+136) tmp = t_0; else tmp = Float64(Float64(b / d) + Float64(Float64(a * Float64(c / d)) / d)); end return tmp end
code[a_, b_, c_, d_] := N[(N[(N[(a * c), $MachinePrecision] + N[(b * 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[(a * c + N[(d * b), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -8.2e+133], N[(N[(b + N[(c / N[(d / a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(-1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, -1.45e-92], t$95$0, If[LessEqual[d, 1.35e-200], N[(N[(a / c), $MachinePrecision] + N[(b / N[(c * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 3.9e+136], t$95$0, N[(N[(b / d), $MachinePrecision] + N[(N[(a * N[(c / d), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision]), $MachinePrecision]]]]]]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{if}\;d \leq -8.2 \cdot 10^{+133}:\\
\;\;\;\;\left(b + \frac{c}{\frac{d}{a}}\right) \cdot \frac{-1}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{elif}\;d \leq -1.45 \cdot 10^{-92}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;d \leq 1.35 \cdot 10^{-200}:\\
\;\;\;\;\frac{a}{c} + \frac{b}{c \cdot \frac{c}{d}}\\
\mathbf{elif}\;d \leq 3.9 \cdot 10^{+136}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{d} + \frac{a \cdot \frac{c}{d}}{d}\\
\end{array}
| Original | 26.4 |
|---|---|
| Target | 0.5 |
| Herbie | 10.0 |
if d < -8.20000000000000008e133Initial program 42.1
Applied egg-rr28.0
Taylor expanded in d around -inf 11.0
Simplified6.9
[Start]11.0 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(-1 \cdot b + -1 \cdot \frac{c \cdot a}{d}\right)
\] |
|---|---|
distribute-lft-out [=>]11.0 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\left(-1 \cdot \left(b + \frac{c \cdot a}{d}\right)\right)}
\] |
associate-/l* [=>]6.9 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(-1 \cdot \left(b + \color{blue}{\frac{c}{\frac{d}{a}}}\right)\right)
\] |
if -8.20000000000000008e133 < d < -1.44999999999999992e-92 or 1.3500000000000001e-200 < d < 3.90000000000000019e136Initial program 18.2
Applied egg-rr12.3
if -1.44999999999999992e-92 < d < 1.3500000000000001e-200Initial program 22.0
Applied egg-rr34.2
Simplified34.2
[Start]34.2 | \[ \frac{\frac{{\left(a \cdot c\right)}^{2}}{a \cdot c - b \cdot d} - \frac{{\left(b \cdot d\right)}^{2}}{a \cdot c - b \cdot d}}{c \cdot c + d \cdot d}
\] |
|---|---|
div-sub [<=]34.2 | \[ \frac{\color{blue}{\frac{{\left(a \cdot c\right)}^{2} - {\left(b \cdot d\right)}^{2}}{a \cdot c - b \cdot d}}}{c \cdot c + d \cdot d}
\] |
*-rgt-identity [<=]34.2 | \[ \frac{\frac{\color{blue}{\left({\left(a \cdot c\right)}^{2} - {\left(b \cdot d\right)}^{2}\right) \cdot 1}}{a \cdot c - b \cdot d}}{c \cdot c + d \cdot d}
\] |
associate-*r/ [<=]34.2 | \[ \frac{\color{blue}{\left({\left(a \cdot c\right)}^{2} - {\left(b \cdot d\right)}^{2}\right) \cdot \frac{1}{a \cdot c - b \cdot d}}}{c \cdot c + d \cdot d}
\] |
*-rgt-identity [<=]34.2 | \[ \frac{\color{blue}{\left(\left({\left(a \cdot c\right)}^{2} - {\left(b \cdot d\right)}^{2}\right) \cdot 1\right)} \cdot \frac{1}{a \cdot c - b \cdot d}}{c \cdot c + d \cdot d}
\] |
*-commutative [=>]34.2 | \[ \frac{\color{blue}{\left(1 \cdot \left({\left(a \cdot c\right)}^{2} - {\left(b \cdot d\right)}^{2}\right)\right)} \cdot \frac{1}{a \cdot c - b \cdot d}}{c \cdot c + d \cdot d}
\] |
associate-*l* [=>]34.2 | \[ \frac{\color{blue}{1 \cdot \left(\left({\left(a \cdot c\right)}^{2} - {\left(b \cdot d\right)}^{2}\right) \cdot \frac{1}{a \cdot c - b \cdot d}\right)}}{c \cdot c + d \cdot d}
\] |
metadata-eval [<=]34.2 | \[ \frac{\color{blue}{\frac{-1}{-1}} \cdot \left(\left({\left(a \cdot c\right)}^{2} - {\left(b \cdot d\right)}^{2}\right) \cdot \frac{1}{a \cdot c - b \cdot d}\right)}{c \cdot c + d \cdot d}
\] |
associate-*r/ [=>]34.2 | \[ \frac{\frac{-1}{-1} \cdot \color{blue}{\frac{\left({\left(a \cdot c\right)}^{2} - {\left(b \cdot d\right)}^{2}\right) \cdot 1}{a \cdot c - b \cdot d}}}{c \cdot c + d \cdot d}
\] |
*-rgt-identity [=>]34.2 | \[ \frac{\frac{-1}{-1} \cdot \frac{\color{blue}{{\left(a \cdot c\right)}^{2} - {\left(b \cdot d\right)}^{2}}}{a \cdot c - b \cdot d}}{c \cdot c + d \cdot d}
\] |
times-frac [<=]34.2 | \[ \frac{\color{blue}{\frac{-1 \cdot \left({\left(a \cdot c\right)}^{2} - {\left(b \cdot d\right)}^{2}\right)}{-1 \cdot \left(a \cdot c - b \cdot d\right)}}}{c \cdot c + d \cdot d}
\] |
neg-mul-1 [<=]34.2 | \[ \frac{\frac{-1 \cdot \left({\left(a \cdot c\right)}^{2} - {\left(b \cdot d\right)}^{2}\right)}{\color{blue}{-\left(a \cdot c - b \cdot d\right)}}}{c \cdot c + d \cdot d}
\] |
Taylor expanded in d around 0 9.7
Simplified11.1
[Start]9.7 | \[ \frac{a}{c} + \frac{d \cdot b}{{c}^{2}}
\] |
|---|---|
*-commutative [=>]9.7 | \[ \frac{a}{c} + \frac{\color{blue}{b \cdot d}}{{c}^{2}}
\] |
associate-/l* [=>]11.1 | \[ \frac{a}{c} + \color{blue}{\frac{b}{\frac{{c}^{2}}{d}}}
\] |
unpow2 [=>]11.1 | \[ \frac{a}{c} + \frac{b}{\frac{\color{blue}{c \cdot c}}{d}}
\] |
Applied egg-rr7.6
if 3.90000000000000019e136 < d Initial program 43.9
Applied egg-rr29.4
Taylor expanded in d around -inf 47.5
Simplified47.3
[Start]47.5 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(-1 \cdot b + -1 \cdot \frac{c \cdot a}{d}\right)
\] |
|---|---|
distribute-lft-out [=>]47.5 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \color{blue}{\left(-1 \cdot \left(b + \frac{c \cdot a}{d}\right)\right)}
\] |
associate-/l* [=>]47.3 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \left(-1 \cdot \left(b + \color{blue}{\frac{c}{\frac{d}{a}}}\right)\right)
\] |
Taylor expanded in d around -inf 17.0
Simplified9.1
[Start]17.0 | \[ \frac{b}{d} + \frac{c \cdot a}{{d}^{2}}
\] |
|---|---|
unpow2 [=>]17.0 | \[ \frac{b}{d} + \frac{c \cdot a}{\color{blue}{d \cdot d}}
\] |
associate-/r* [=>]13.5 | \[ \frac{b}{d} + \color{blue}{\frac{\frac{c \cdot a}{d}}{d}}
\] |
*-commutative [=>]13.5 | \[ \frac{b}{d} + \frac{\frac{\color{blue}{a \cdot c}}{d}}{d}
\] |
associate-*r/ [<=]9.1 | \[ \frac{b}{d} + \frac{\color{blue}{a \cdot \frac{c}{d}}}{d}
\] |
Final simplification10.0
| Alternative 1 | |
|---|---|
| Error | 12.8 |
| Cost | 20304 |
| Alternative 2 | |
|---|---|
| Error | 12.8 |
| Cost | 13768 |
| Alternative 3 | |
|---|---|
| Error | 12.8 |
| Cost | 7300 |
| Alternative 4 | |
|---|---|
| Error | 12.2 |
| Cost | 1488 |
| Alternative 5 | |
|---|---|
| Error | 21.2 |
| Cost | 1240 |
| Alternative 6 | |
|---|---|
| Error | 21.1 |
| Cost | 1240 |
| Alternative 7 | |
|---|---|
| Error | 21.2 |
| Cost | 1240 |
| Alternative 8 | |
|---|---|
| Error | 16.2 |
| Cost | 1233 |
| Alternative 9 | |
|---|---|
| Error | 16.1 |
| Cost | 1232 |
| Alternative 10 | |
|---|---|
| Error | 24.4 |
| Cost | 976 |
| Alternative 11 | |
|---|---|
| Error | 23.5 |
| Cost | 456 |
| Alternative 12 | |
|---|---|
| Error | 37.3 |
| Cost | 324 |
| Alternative 13 | |
|---|---|
| Error | 37.5 |
| Cost | 192 |
herbie shell --seed 2023054
(FPCore (a b c d)
:name "Complex division, real part"
:precision binary64
:herbie-target
(if (< (fabs d) (fabs c)) (/ (+ a (* b (/ d c))) (+ c (* d (/ d c)))) (/ (+ b (* a (/ c d))) (+ d (* c (/ c d)))))
(/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))