| Alternative 1 | |
|---|---|
| Error | 17.95% |
| Cost | 14940 |
(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)) a) d))
(t_1 (- (* c b) (* d a)))
(t_2 (/ 1.0 (hypot c d)))
(t_3 (fma d d (* c c))))
(if (<= c -1.4e+138)
(- (/ b c) (* (/ d c) (/ a c)))
(if (<= c -1.85e+60)
(* (/ c (hypot c d)) (/ b (hypot c d)))
(if (<= c -1.05e+37)
(* t_2 (/ (+ t_1 (* 2.0 (* 2.0 (fma (- d) a (* d a))))) (hypot c d)))
(if (<= c -38000000000.0)
t_0
(if (<= c -2.4e-123)
(* t_2 (/ t_1 (hypot c d)))
(if (<= c 2.15e-108)
t_0
(if (<= c 1.15e+142)
(- (* b (/ c t_3)) (* d (/ a t_3)))
(* t_2 (- b (/ (* d a) 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 = ((b / (d / c)) - a) / d;
double t_1 = (c * b) - (d * a);
double t_2 = 1.0 / hypot(c, d);
double t_3 = fma(d, d, (c * c));
double tmp;
if (c <= -1.4e+138) {
tmp = (b / c) - ((d / c) * (a / c));
} else if (c <= -1.85e+60) {
tmp = (c / hypot(c, d)) * (b / hypot(c, d));
} else if (c <= -1.05e+37) {
tmp = t_2 * ((t_1 + (2.0 * (2.0 * fma(-d, a, (d * a))))) / hypot(c, d));
} else if (c <= -38000000000.0) {
tmp = t_0;
} else if (c <= -2.4e-123) {
tmp = t_2 * (t_1 / hypot(c, d));
} else if (c <= 2.15e-108) {
tmp = t_0;
} else if (c <= 1.15e+142) {
tmp = (b * (c / t_3)) - (d * (a / t_3));
} else {
tmp = t_2 * (b - ((d * a) / 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(b / Float64(d / c)) - a) / d) t_1 = Float64(Float64(c * b) - Float64(d * a)) t_2 = Float64(1.0 / hypot(c, d)) t_3 = fma(d, d, Float64(c * c)) tmp = 0.0 if (c <= -1.4e+138) tmp = Float64(Float64(b / c) - Float64(Float64(d / c) * Float64(a / c))); elseif (c <= -1.85e+60) tmp = Float64(Float64(c / hypot(c, d)) * Float64(b / hypot(c, d))); elseif (c <= -1.05e+37) tmp = Float64(t_2 * Float64(Float64(t_1 + Float64(2.0 * Float64(2.0 * fma(Float64(-d), a, Float64(d * a))))) / hypot(c, d))); elseif (c <= -38000000000.0) tmp = t_0; elseif (c <= -2.4e-123) tmp = Float64(t_2 * Float64(t_1 / hypot(c, d))); elseif (c <= 2.15e-108) tmp = t_0; elseif (c <= 1.15e+142) tmp = Float64(Float64(b * Float64(c / t_3)) - Float64(d * Float64(a / t_3))); else tmp = Float64(t_2 * Float64(b - Float64(Float64(d * a) / c))); end return 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 / N[(d / c), $MachinePrecision]), $MachinePrecision] - a), $MachinePrecision] / d), $MachinePrecision]}, Block[{t$95$1 = N[(N[(c * b), $MachinePrecision] - N[(d * a), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$3 = N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -1.4e+138], N[(N[(b / c), $MachinePrecision] - N[(N[(d / c), $MachinePrecision] * N[(a / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, -1.85e+60], N[(N[(c / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(b / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, -1.05e+37], N[(t$95$2 * N[(N[(t$95$1 + N[(2.0 * N[(2.0 * N[((-d) * a + N[(d * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, -38000000000.0], t$95$0, If[LessEqual[c, -2.4e-123], N[(t$95$2 * N[(t$95$1 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 2.15e-108], t$95$0, If[LessEqual[c, 1.15e+142], N[(N[(b * N[(c / t$95$3), $MachinePrecision]), $MachinePrecision] - N[(d * N[(a / t$95$3), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(t$95$2 * N[(b - N[(N[(d * a), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]]]]]]]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{\frac{b}{\frac{d}{c}} - a}{d}\\
t_1 := c \cdot b - d \cdot a\\
t_2 := \frac{1}{\mathsf{hypot}\left(c, d\right)}\\
t_3 := \mathsf{fma}\left(d, d, c \cdot c\right)\\
\mathbf{if}\;c \leq -1.4 \cdot 10^{+138}:\\
\;\;\;\;\frac{b}{c} - \frac{d}{c} \cdot \frac{a}{c}\\
\mathbf{elif}\;c \leq -1.85 \cdot 10^{+60}:\\
\;\;\;\;\frac{c}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{b}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{elif}\;c \leq -1.05 \cdot 10^{+37}:\\
\;\;\;\;t_2 \cdot \frac{t_1 + 2 \cdot \left(2 \cdot \mathsf{fma}\left(-d, a, d \cdot a\right)\right)}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{elif}\;c \leq -38000000000:\\
\;\;\;\;t_0\\
\mathbf{elif}\;c \leq -2.4 \cdot 10^{-123}:\\
\;\;\;\;t_2 \cdot \frac{t_1}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{elif}\;c \leq 2.15 \cdot 10^{-108}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;c \leq 1.15 \cdot 10^{+142}:\\
\;\;\;\;b \cdot \frac{c}{t_3} - d \cdot \frac{a}{t_3}\\
\mathbf{else}:\\
\;\;\;\;t_2 \cdot \left(b - \frac{d \cdot a}{c}\right)\\
\end{array}
| Original | 41.11% |
|---|---|
| Target | 0.66% |
| Herbie | 17.93% |
if c < -1.4e138Initial program 68.52
Taylor expanded in c around inf 22.41
Simplified10.84
[Start]22.41 | \[ -1 \cdot \frac{a \cdot d}{{c}^{2}} + \frac{b}{c}
\] |
|---|---|
+-commutative [=>]22.41 | \[ \color{blue}{\frac{b}{c} + -1 \cdot \frac{a \cdot d}{{c}^{2}}}
\] |
mul-1-neg [=>]22.41 | \[ \frac{b}{c} + \color{blue}{\left(-\frac{a \cdot d}{{c}^{2}}\right)}
\] |
unsub-neg [=>]22.41 | \[ \color{blue}{\frac{b}{c} - \frac{a \cdot d}{{c}^{2}}}
\] |
*-commutative [=>]22.41 | \[ \frac{b}{c} - \frac{\color{blue}{d \cdot a}}{{c}^{2}}
\] |
unpow2 [=>]22.41 | \[ \frac{b}{c} - \frac{d \cdot a}{\color{blue}{c \cdot c}}
\] |
times-frac [=>]10.84 | \[ \frac{b}{c} - \color{blue}{\frac{d}{c} \cdot \frac{a}{c}}
\] |
if -1.4e138 < c < -1.84999999999999994e60Initial program 34.01
Taylor expanded in b around inf 48.53
Applied egg-rr31.47
if -1.84999999999999994e60 < c < -1.0500000000000001e37Initial program 25.29
Applied egg-rr18.27
Applied egg-rr18.46
Simplified18.46
[Start]18.46 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{\left(b \cdot c - a \cdot d\right) + \left(\left(\mathsf{fma}\left(-d, a, a \cdot d\right) + \mathsf{fma}\left(-d, a, a \cdot d\right)\right) + \left(\mathsf{fma}\left(-d, a, a \cdot d\right) + \mathsf{fma}\left(-d, a, a \cdot d\right)\right)\right)}{\mathsf{hypot}\left(c, d\right)}
\] |
|---|---|
*-commutative [=>]18.46 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{\left(\color{blue}{c \cdot b} - a \cdot d\right) + \left(\left(\mathsf{fma}\left(-d, a, a \cdot d\right) + \mathsf{fma}\left(-d, a, a \cdot d\right)\right) + \left(\mathsf{fma}\left(-d, a, a \cdot d\right) + \mathsf{fma}\left(-d, a, a \cdot d\right)\right)\right)}{\mathsf{hypot}\left(c, d\right)}
\] |
count-2 [=>]18.46 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{\left(c \cdot b - a \cdot d\right) + \color{blue}{2 \cdot \left(\mathsf{fma}\left(-d, a, a \cdot d\right) + \mathsf{fma}\left(-d, a, a \cdot d\right)\right)}}{\mathsf{hypot}\left(c, d\right)}
\] |
count-2 [=>]18.46 | \[ \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{\left(c \cdot b - a \cdot d\right) + 2 \cdot \color{blue}{\left(2 \cdot \mathsf{fma}\left(-d, a, a \cdot d\right)\right)}}{\mathsf{hypot}\left(c, d\right)}
\] |
if -1.0500000000000001e37 < c < -3.8e10 or -2.4e-123 < c < 2.15e-108Initial program 33.21
Taylor expanded in c around 0 20.48
Simplified21.72
[Start]20.48 | \[ -1 \cdot \frac{a}{d} + \frac{c \cdot b}{{d}^{2}}
\] |
|---|---|
+-commutative [=>]20.48 | \[ \color{blue}{\frac{c \cdot b}{{d}^{2}} + -1 \cdot \frac{a}{d}}
\] |
mul-1-neg [=>]20.48 | \[ \frac{c \cdot b}{{d}^{2}} + \color{blue}{\left(-\frac{a}{d}\right)}
\] |
unsub-neg [=>]20.48 | \[ \color{blue}{\frac{c \cdot b}{{d}^{2}} - \frac{a}{d}}
\] |
associate-/l* [=>]25.19 | \[ \color{blue}{\frac{c}{\frac{{d}^{2}}{b}}} - \frac{a}{d}
\] |
unpow2 [=>]25.19 | \[ \frac{c}{\frac{\color{blue}{d \cdot d}}{b}} - \frac{a}{d}
\] |
associate-/r/ [=>]21.72 | \[ \color{blue}{\frac{c}{d \cdot d} \cdot b} - \frac{a}{d}
\] |
Applied egg-rr16.07
Taylor expanded in d around 0 20.48
Simplified14.87
[Start]20.48 | \[ -1 \cdot \frac{a}{d} + \frac{c \cdot b}{{d}^{2}}
\] |
|---|---|
unpow2 [=>]20.48 | \[ -1 \cdot \frac{a}{d} + \frac{c \cdot b}{\color{blue}{d \cdot d}}
\] |
associate-*l/ [<=]21.72 | \[ -1 \cdot \frac{a}{d} + \color{blue}{\frac{c}{d \cdot d} \cdot b}
\] |
associate-*r/ [=>]21.72 | \[ \color{blue}{\frac{-1 \cdot a}{d}} + \frac{c}{d \cdot d} \cdot b
\] |
associate-*l/ [<=]21.96 | \[ \color{blue}{\frac{-1}{d} \cdot a} + \frac{c}{d \cdot d} \cdot b
\] |
metadata-eval [<=]21.96 | \[ \frac{\color{blue}{-1}}{d} \cdot a + \frac{c}{d \cdot d} \cdot b
\] |
distribute-neg-frac [<=]21.96 | \[ \color{blue}{\left(-\frac{1}{d}\right)} \cdot a + \frac{c}{d \cdot d} \cdot b
\] |
distribute-lft-neg-out [=>]21.96 | \[ \color{blue}{\left(-\frac{1}{d} \cdot a\right)} + \frac{c}{d \cdot d} \cdot b
\] |
distribute-rgt-neg-out [<=]21.96 | \[ \color{blue}{\frac{1}{d} \cdot \left(-a\right)} + \frac{c}{d \cdot d} \cdot b
\] |
associate-*l/ [=>]20.72 | \[ \frac{1}{d} \cdot \left(-a\right) + \color{blue}{\frac{c \cdot b}{d \cdot d}}
\] |
/-rgt-identity [<=]20.72 | \[ \frac{1}{d} \cdot \left(-a\right) + \frac{c \cdot b}{\color{blue}{\frac{d}{1}} \cdot d}
\] |
associate-/r/ [<=]20.73 | \[ \frac{1}{d} \cdot \left(-a\right) + \frac{c \cdot b}{\color{blue}{\frac{d}{\frac{1}{d}}}}
\] |
associate-/r/ [=>]15.13 | \[ \frac{1}{d} \cdot \left(-a\right) + \color{blue}{\frac{c \cdot b}{d} \cdot \frac{1}{d}}
\] |
*-commutative [=>]15.13 | \[ \frac{1}{d} \cdot \left(-a\right) + \frac{\color{blue}{b \cdot c}}{d} \cdot \frac{1}{d}
\] |
associate-*r/ [<=]15.05 | \[ \frac{1}{d} \cdot \left(-a\right) + \color{blue}{\left(b \cdot \frac{c}{d}\right)} \cdot \frac{1}{d}
\] |
*-commutative [<=]15.05 | \[ \frac{1}{d} \cdot \left(-a\right) + \color{blue}{\frac{1}{d} \cdot \left(b \cdot \frac{c}{d}\right)}
\] |
distribute-lft-in [<=]15.05 | \[ \color{blue}{\frac{1}{d} \cdot \left(\left(-a\right) + b \cdot \frac{c}{d}\right)}
\] |
+-commutative [<=]15.05 | \[ \frac{1}{d} \cdot \color{blue}{\left(b \cdot \frac{c}{d} + \left(-a\right)\right)}
\] |
associate-*l/ [=>]14.78 | \[ \color{blue}{\frac{1 \cdot \left(b \cdot \frac{c}{d} + \left(-a\right)\right)}{d}}
\] |
if -3.8e10 < c < -2.4e-123Initial program 23.87
Applied egg-rr16.87
if 2.15e-108 < c < 1.15000000000000001e142Initial program 27.76
Taylor expanded in b around 0 27.77
Simplified22.38
[Start]27.77 | \[ \frac{c \cdot b}{{d}^{2} + {c}^{2}} + -1 \cdot \frac{a \cdot d}{{d}^{2} + {c}^{2}}
\] |
|---|---|
mul-1-neg [=>]27.77 | \[ \frac{c \cdot b}{{d}^{2} + {c}^{2}} + \color{blue}{\left(-\frac{a \cdot d}{{d}^{2} + {c}^{2}}\right)}
\] |
unsub-neg [=>]27.77 | \[ \color{blue}{\frac{c \cdot b}{{d}^{2} + {c}^{2}} - \frac{a \cdot d}{{d}^{2} + {c}^{2}}}
\] |
associate-/l* [=>]25.75 | \[ \color{blue}{\frac{c}{\frac{{d}^{2} + {c}^{2}}{b}}} - \frac{a \cdot d}{{d}^{2} + {c}^{2}}
\] |
associate-/r/ [=>]22.32 | \[ \color{blue}{\frac{c}{{d}^{2} + {c}^{2}} \cdot b} - \frac{a \cdot d}{{d}^{2} + {c}^{2}}
\] |
unpow2 [=>]22.32 | \[ \frac{c}{\color{blue}{d \cdot d} + {c}^{2}} \cdot b - \frac{a \cdot d}{{d}^{2} + {c}^{2}}
\] |
unpow2 [=>]22.32 | \[ \frac{c}{d \cdot d + \color{blue}{c \cdot c}} \cdot b - \frac{a \cdot d}{{d}^{2} + {c}^{2}}
\] |
fma-udef [<=]22.32 | \[ \frac{c}{\color{blue}{\mathsf{fma}\left(d, d, c \cdot c\right)}} \cdot b - \frac{a \cdot d}{{d}^{2} + {c}^{2}}
\] |
associate-/l* [=>]19.57 | \[ \frac{c}{\mathsf{fma}\left(d, d, c \cdot c\right)} \cdot b - \color{blue}{\frac{a}{\frac{{d}^{2} + {c}^{2}}{d}}}
\] |
associate-/r/ [=>]22.38 | \[ \frac{c}{\mathsf{fma}\left(d, d, c \cdot c\right)} \cdot b - \color{blue}{\frac{a}{{d}^{2} + {c}^{2}} \cdot d}
\] |
unpow2 [=>]22.38 | \[ \frac{c}{\mathsf{fma}\left(d, d, c \cdot c\right)} \cdot b - \frac{a}{\color{blue}{d \cdot d} + {c}^{2}} \cdot d
\] |
unpow2 [=>]22.38 | \[ \frac{c}{\mathsf{fma}\left(d, d, c \cdot c\right)} \cdot b - \frac{a}{d \cdot d + \color{blue}{c \cdot c}} \cdot d
\] |
fma-udef [<=]22.38 | \[ \frac{c}{\mathsf{fma}\left(d, d, c \cdot c\right)} \cdot b - \frac{a}{\color{blue}{\mathsf{fma}\left(d, d, c \cdot c\right)}} \cdot d
\] |
if 1.15000000000000001e142 < c Initial program 68.38
Applied egg-rr45.25
Taylor expanded in c around inf 19.53
Final simplification17.93
| Alternative 1 | |
|---|---|
| Error | 17.95% |
| Cost | 14940 |
| Alternative 2 | |
|---|---|
| Error | 17.3% |
| Cost | 14684 |
| Alternative 3 | |
|---|---|
| Error | 19.27% |
| Cost | 14168 |
| Alternative 4 | |
|---|---|
| Error | 23.69% |
| Cost | 7696 |
| Alternative 5 | |
|---|---|
| Error | 25.91% |
| Cost | 7632 |
| Alternative 6 | |
|---|---|
| Error | 25.85% |
| Cost | 1369 |
| Alternative 7 | |
|---|---|
| Error | 27.28% |
| Cost | 1106 |
| Alternative 8 | |
|---|---|
| Error | 27.14% |
| Cost | 1105 |
| Alternative 9 | |
|---|---|
| Error | 29.5% |
| Cost | 841 |
| Alternative 10 | |
|---|---|
| Error | 35.79% |
| Cost | 520 |
| Alternative 11 | |
|---|---|
| Error | 58.22% |
| Cost | 192 |
herbie shell --seed 2023090
(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))))