(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 (/ (/ (fma a c (* d b)) (hypot c d)) (hypot c d)))
(t_1 (fma (/ d c) b a)))
(if (<= c -1.3e+132)
(/ (- t_1) (hypot c d))
(if (<= c -3.3e-83)
t_0
(if (<= c 1.9e-106)
(fma (/ a d) (/ c d) (/ b d))
(if (<= c 1.08e+55) t_0 (/ t_1 (hypot c 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 = (fma(a, c, (d * b)) / hypot(c, d)) / hypot(c, d);
double t_1 = fma((d / c), b, a);
double tmp;
if (c <= -1.3e+132) {
tmp = -t_1 / hypot(c, d);
} else if (c <= -3.3e-83) {
tmp = t_0;
} else if (c <= 1.9e-106) {
tmp = fma((a / d), (c / d), (b / d));
} else if (c <= 1.08e+55) {
tmp = t_0;
} else {
tmp = t_1 / hypot(c, 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(fma(a, c, Float64(d * b)) / hypot(c, d)) / hypot(c, d)) t_1 = fma(Float64(d / c), b, a) tmp = 0.0 if (c <= -1.3e+132) tmp = Float64(Float64(-t_1) / hypot(c, d)); elseif (c <= -3.3e-83) tmp = t_0; elseif (c <= 1.9e-106) tmp = fma(Float64(a / d), Float64(c / d), Float64(b / d)); elseif (c <= 1.08e+55) tmp = t_0; else tmp = Float64(t_1 / hypot(c, 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[(N[(a * c + N[(d * b), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(d / c), $MachinePrecision] * b + a), $MachinePrecision]}, If[LessEqual[c, -1.3e+132], N[((-t$95$1) / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision], If[LessEqual[c, -3.3e-83], t$95$0, If[LessEqual[c, 1.9e-106], N[(N[(a / d), $MachinePrecision] * N[(c / d), $MachinePrecision] + N[(b / d), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 1.08e+55], t$95$0, N[(t$95$1 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]]]]]]]
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\begin{array}{l}
t_0 := \frac{\frac{\mathsf{fma}\left(a, c, d \cdot b\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\
t_1 := \mathsf{fma}\left(\frac{d}{c}, b, a\right)\\
\mathbf{if}\;c \leq -1.3 \cdot 10^{+132}:\\
\;\;\;\;\frac{-t_1}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{elif}\;c \leq -3.3 \cdot 10^{-83}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;c \leq 1.9 \cdot 10^{-106}:\\
\;\;\;\;\mathsf{fma}\left(\frac{a}{d}, \frac{c}{d}, \frac{b}{d}\right)\\
\mathbf{elif}\;c \leq 1.08 \cdot 10^{+55}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{t_1}{\mathsf{hypot}\left(c, d\right)}\\
\end{array}




Bits error versus a




Bits error versus b




Bits error versus c




Bits error versus d
| Original | 26.0 |
|---|---|
| Target | 0.5 |
| Herbie | 10.3 |
if c < -1.3e132Initial program 42.2
Simplified42.2
Applied egg-rr28.6
Applied egg-rr28.5
Taylor expanded in c around -inf 13.5
Simplified9.0
if -1.3e132 < c < -3.2999999999999999e-83 or 1.9e-106 < c < 1.08000000000000004e55Initial program 15.9
Simplified15.9
Applied egg-rr10.8
Applied egg-rr10.6
if -3.2999999999999999e-83 < c < 1.9e-106Initial program 22.2
Simplified22.2
Applied egg-rr12.6
Taylor expanded in c around 0 11.0
Simplified9.8
if 1.08000000000000004e55 < c Initial program 35.4
Simplified35.4
Applied egg-rr24.1
Applied egg-rr24.0
Taylor expanded in c around inf 14.3
Simplified11.3
Final simplification10.3
herbie shell --seed 2022156
(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))))