| Alternative 1 | |
|---|---|
| Accuracy | 72.8% |
| Cost | 20700 |
(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 -2.7e+58)
(* (/ d (hypot d c)) (/ b (hypot d c)))
(if (<= d -9.5e-224)
t_0
(if (<= d -3.4e-299)
(/ (+ a (/ b (/ c d))) c)
(if (<= d 9.2e+76) t_0 (+ (/ b d) (/ c (/ (* d d) a)))))))))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 <= -2.7e+58) {
tmp = (d / hypot(d, c)) * (b / hypot(d, c));
} else if (d <= -9.5e-224) {
tmp = t_0;
} else if (d <= -3.4e-299) {
tmp = (a + (b / (c / d))) / c;
} else if (d <= 9.2e+76) {
tmp = t_0;
} else {
tmp = (b / d) + (c / ((d * d) / a));
}
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 <= -2.7e+58) tmp = Float64(Float64(d / hypot(d, c)) * Float64(b / hypot(d, c))); elseif (d <= -9.5e-224) tmp = t_0; elseif (d <= -3.4e-299) tmp = Float64(Float64(a + Float64(b / Float64(c / d))) / c); elseif (d <= 9.2e+76) tmp = t_0; else tmp = Float64(Float64(b / d) + Float64(c / Float64(Float64(d * d) / a))); 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, -2.7e+58], N[(N[(d / N[Sqrt[d ^ 2 + c ^ 2], $MachinePrecision]), $MachinePrecision] * N[(b / N[Sqrt[d ^ 2 + c ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, -9.5e-224], t$95$0, If[LessEqual[d, -3.4e-299], N[(N[(a + N[(b / N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 9.2e+76], t$95$0, N[(N[(b / d), $MachinePrecision] + N[(c / N[(N[(d * d), $MachinePrecision] / a), $MachinePrecision]), $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 -2.7 \cdot 10^{+58}:\\
\;\;\;\;\frac{d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{b}{\mathsf{hypot}\left(d, c\right)}\\
\mathbf{elif}\;d \leq -9.5 \cdot 10^{-224}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;d \leq -3.4 \cdot 10^{-299}:\\
\;\;\;\;\frac{a + \frac{b}{\frac{c}{d}}}{c}\\
\mathbf{elif}\;d \leq 9.2 \cdot 10^{+76}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{d} + \frac{c}{\frac{d \cdot d}{a}}\\
\end{array}
| Original | 55.3% |
|---|---|
| Target | 93.7% |
| Herbie | 76.4% |
if d < -2.7000000000000001e58Initial program 36.4%
Taylor expanded in a around 0 35.0%
Applied egg-rr82.4%
[Start]35.0 | \[ \frac{d \cdot b}{c \cdot c + d \cdot d}
\] |
|---|---|
add-sqr-sqrt [=>]35.0 | \[ \frac{d \cdot b}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}
\] |
times-frac [=>]48.6 | \[ \color{blue}{\frac{d}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b}{\sqrt{c \cdot c + d \cdot d}}}
\] |
+-commutative [=>]48.6 | \[ \frac{d}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}} \cdot \frac{b}{\sqrt{c \cdot c + d \cdot d}}
\] |
hypot-def [=>]48.6 | \[ \frac{d}{\color{blue}{\mathsf{hypot}\left(d, c\right)}} \cdot \frac{b}{\sqrt{c \cdot c + d \cdot d}}
\] |
+-commutative [=>]48.6 | \[ \frac{d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{b}{\sqrt{\color{blue}{d \cdot d + c \cdot c}}}
\] |
hypot-def [=>]82.4 | \[ \frac{d}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{b}{\color{blue}{\mathsf{hypot}\left(d, c\right)}}
\] |
if -2.7000000000000001e58 < d < -9.5000000000000003e-224 or -3.3999999999999998e-299 < d < 9.20000000000000005e76Initial program 73.5%
Applied egg-rr84.8%
[Start]73.5 | \[ \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\] |
|---|---|
*-un-lft-identity [=>]73.5 | \[ \frac{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}{c \cdot c + d \cdot d}
\] |
add-sqr-sqrt [=>]73.5 | \[ \frac{1 \cdot \left(a \cdot c + b \cdot d\right)}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}
\] |
times-frac [=>]73.5 | \[ \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}
\] |
hypot-def [=>]73.5 | \[ \frac{1}{\color{blue}{\mathsf{hypot}\left(c, d\right)}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}
\] |
fma-def [=>]73.5 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{\color{blue}{\mathsf{fma}\left(a, c, b \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d}}
\] |
hypot-def [=>]84.8 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\color{blue}{\mathsf{hypot}\left(c, d\right)}}
\] |
if -9.5000000000000003e-224 < d < -3.3999999999999998e-299Initial program 55.0%
Simplified55.0%
[Start]55.0 | \[ \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\] |
|---|---|
fma-def [=>]55.0 | \[ \frac{\color{blue}{\mathsf{fma}\left(a, c, b \cdot d\right)}}{c \cdot c + d \cdot d}
\] |
fma-def [=>]55.0 | \[ \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\color{blue}{\mathsf{fma}\left(c, c, d \cdot d\right)}}
\] |
Taylor expanded in c around inf 55.0%
Simplified55.0%
[Start]55.0 | \[ \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{{c}^{2}}
\] |
|---|---|
unpow2 [=>]55.0 | \[ \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\color{blue}{c \cdot c}}
\] |
Applied egg-rr57.9%
[Start]55.0 | \[ \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{c \cdot c}
\] |
|---|---|
add-log-exp [=>]6.1 | \[ \color{blue}{\log \left(e^{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{c \cdot c}}\right)}
\] |
*-un-lft-identity [=>]6.1 | \[ \log \color{blue}{\left(1 \cdot e^{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{c \cdot c}}\right)}
\] |
log-prod [=>]6.1 | \[ \color{blue}{\log 1 + \log \left(e^{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{c \cdot c}}\right)}
\] |
metadata-eval [=>]6.1 | \[ \color{blue}{0} + \log \left(e^{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{c \cdot c}}\right)
\] |
add-log-exp [<=]55.0 | \[ 0 + \color{blue}{\frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{c \cdot c}}
\] |
div-inv [=>]54.8 | \[ 0 + \color{blue}{\mathsf{fma}\left(a, c, b \cdot d\right) \cdot \frac{1}{c \cdot c}}
\] |
pow2 [=>]54.8 | \[ 0 + \mathsf{fma}\left(a, c, b \cdot d\right) \cdot \frac{1}{\color{blue}{{c}^{2}}}
\] |
pow-flip [=>]57.9 | \[ 0 + \mathsf{fma}\left(a, c, b \cdot d\right) \cdot \color{blue}{{c}^{\left(-2\right)}}
\] |
metadata-eval [=>]57.9 | \[ 0 + \mathsf{fma}\left(a, c, b \cdot d\right) \cdot {c}^{\color{blue}{-2}}
\] |
Simplified89.9%
[Start]57.9 | \[ 0 + \mathsf{fma}\left(a, c, b \cdot d\right) \cdot {c}^{-2}
\] |
|---|---|
+-lft-identity [=>]57.9 | \[ \color{blue}{\mathsf{fma}\left(a, c, b \cdot d\right) \cdot {c}^{-2}}
\] |
*-commutative [<=]57.9 | \[ \color{blue}{{c}^{-2} \cdot \mathsf{fma}\left(a, c, b \cdot d\right)}
\] |
metadata-eval [<=]57.9 | \[ {c}^{\color{blue}{\left(2 \cdot -1\right)}} \cdot \mathsf{fma}\left(a, c, b \cdot d\right)
\] |
pow-sqr [<=]57.7 | \[ \color{blue}{\left({c}^{-1} \cdot {c}^{-1}\right)} \cdot \mathsf{fma}\left(a, c, b \cdot d\right)
\] |
unpow-1 [=>]57.7 | \[ \left(\color{blue}{\frac{1}{c}} \cdot {c}^{-1}\right) \cdot \mathsf{fma}\left(a, c, b \cdot d\right)
\] |
unpow-1 [=>]57.7 | \[ \left(\frac{1}{c} \cdot \color{blue}{\frac{1}{c}}\right) \cdot \mathsf{fma}\left(a, c, b \cdot d\right)
\] |
associate-*l* [=>]64.5 | \[ \color{blue}{\frac{1}{c} \cdot \left(\frac{1}{c} \cdot \mathsf{fma}\left(a, c, b \cdot d\right)\right)}
\] |
associate-*l/ [=>]64.7 | \[ \frac{1}{c} \cdot \color{blue}{\frac{1 \cdot \mathsf{fma}\left(a, c, b \cdot d\right)}{c}}
\] |
*-lft-identity [=>]64.7 | \[ \frac{1}{c} \cdot \frac{\color{blue}{\mathsf{fma}\left(a, c, b \cdot d\right)}}{c}
\] |
associate-*l/ [=>]65.1 | \[ \color{blue}{\frac{1 \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{c}}{c}}
\] |
associate-*r/ [=>]65.1 | \[ \frac{\color{blue}{\frac{1 \cdot \mathsf{fma}\left(a, c, b \cdot d\right)}{c}}}{c}
\] |
associate-*l/ [<=]65.0 | \[ \frac{\color{blue}{\frac{1}{c} \cdot \mathsf{fma}\left(a, c, b \cdot d\right)}}{c}
\] |
fma-udef [=>]65.0 | \[ \frac{\frac{1}{c} \cdot \color{blue}{\left(a \cdot c + b \cdot d\right)}}{c}
\] |
*-commutative [<=]65.0 | \[ \frac{\frac{1}{c} \cdot \left(a \cdot c + \color{blue}{d \cdot b}\right)}{c}
\] |
distribute-rgt-in [=>]65.0 | \[ \frac{\color{blue}{\left(a \cdot c\right) \cdot \frac{1}{c} + \left(d \cdot b\right) \cdot \frac{1}{c}}}{c}
\] |
associate-*l* [=>]89.8 | \[ \frac{\color{blue}{a \cdot \left(c \cdot \frac{1}{c}\right)} + \left(d \cdot b\right) \cdot \frac{1}{c}}{c}
\] |
rgt-mult-inverse [=>]89.9 | \[ \frac{a \cdot \color{blue}{1} + \left(d \cdot b\right) \cdot \frac{1}{c}}{c}
\] |
*-rgt-identity [=>]89.9 | \[ \frac{\color{blue}{a} + \left(d \cdot b\right) \cdot \frac{1}{c}}{c}
\] |
associate-*r/ [=>]89.9 | \[ \frac{a + \color{blue}{\frac{\left(d \cdot b\right) \cdot 1}{c}}}{c}
\] |
*-rgt-identity [=>]89.9 | \[ \frac{a + \frac{\color{blue}{d \cdot b}}{c}}{c}
\] |
*-commutative [=>]89.9 | \[ \frac{a + \frac{\color{blue}{b \cdot d}}{c}}{c}
\] |
associate-/l* [=>]89.9 | \[ \frac{a + \color{blue}{\frac{b}{\frac{c}{d}}}}{c}
\] |
if 9.20000000000000005e76 < d Initial program 46.7%
Simplified46.7%
[Start]46.7 | \[ \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\] |
|---|---|
fma-def [=>]46.7 | \[ \frac{\color{blue}{\mathsf{fma}\left(a, c, b \cdot d\right)}}{c \cdot c + d \cdot d}
\] |
fma-def [=>]46.7 | \[ \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\color{blue}{\mathsf{fma}\left(c, c, d \cdot d\right)}}
\] |
Taylor expanded in c around 0 79.6%
Simplified85.1%
[Start]79.6 | \[ \frac{b}{d} + \frac{c \cdot a}{{d}^{2}}
\] |
|---|---|
unpow2 [=>]79.6 | \[ \frac{b}{d} + \frac{c \cdot a}{\color{blue}{d \cdot d}}
\] |
associate-/l* [=>]85.1 | \[ \frac{b}{d} + \color{blue}{\frac{c}{\frac{d \cdot d}{a}}}
\] |
Final simplification84.7%
| Alternative 1 | |
|---|---|
| Accuracy | 72.8% |
| Cost | 20700 |
| Alternative 2 | |
|---|---|
| Accuracy | 72.7% |
| Cost | 14168 |
| Alternative 3 | |
|---|---|
| Accuracy | 76.0% |
| Cost | 1488 |
| Alternative 4 | |
|---|---|
| Accuracy | 71.0% |
| Cost | 1232 |
| Alternative 5 | |
|---|---|
| Accuracy | 70.9% |
| Cost | 1232 |
| Alternative 6 | |
|---|---|
| Accuracy | 70.3% |
| Cost | 1106 |
| Alternative 7 | |
|---|---|
| Accuracy | 70.7% |
| Cost | 1105 |
| Alternative 8 | |
|---|---|
| Accuracy | 66.3% |
| Cost | 841 |
| Alternative 9 | |
|---|---|
| Accuracy | 60.7% |
| Cost | 456 |
| Alternative 10 | |
|---|---|
| Accuracy | 39.1% |
| Cost | 192 |
herbie shell --seed 2023157
(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))))