(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 d c)) (hypot d c)))
(t_1 (fma (/ d c) b a)))
(if (<= c -8.995298531255504e+122)
(/ (- t_1) (hypot d c))
(if (<= c -1.6578507474419048e-168)
t_0
(if (<= c 6.117043132162393e-276)
(fma (/ a d) (/ c d) (/ b d))
(if (<= c 4.7943349601815015e+69) t_0 (/ t_1 (hypot d c))))))))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(d, c)) / hypot(d, c);
double t_1 = fma((d / c), b, a);
double tmp;
if (c <= -8.995298531255504e+122) {
tmp = -t_1 / hypot(d, c);
} else if (c <= -1.6578507474419048e-168) {
tmp = t_0;
} else if (c <= 6.117043132162393e-276) {
tmp = fma((a / d), (c / d), (b / d));
} else if (c <= 4.7943349601815015e+69) {
tmp = t_0;
} else {
tmp = t_1 / hypot(d, c);
}
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(d, c)) / hypot(d, c)) t_1 = fma(Float64(d / c), b, a) tmp = 0.0 if (c <= -8.995298531255504e+122) tmp = Float64(Float64(-t_1) / hypot(d, c)); elseif (c <= -1.6578507474419048e-168) tmp = t_0; elseif (c <= 6.117043132162393e-276) tmp = fma(Float64(a / d), Float64(c / d), Float64(b / d)); elseif (c <= 4.7943349601815015e+69) tmp = t_0; else tmp = Float64(t_1 / hypot(d, c)); 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[d ^ 2 + c ^ 2], $MachinePrecision]), $MachinePrecision] / N[Sqrt[d ^ 2 + c ^ 2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(d / c), $MachinePrecision] * b + a), $MachinePrecision]}, If[LessEqual[c, -8.995298531255504e+122], N[((-t$95$1) / N[Sqrt[d ^ 2 + c ^ 2], $MachinePrecision]), $MachinePrecision], If[LessEqual[c, -1.6578507474419048e-168], t$95$0, If[LessEqual[c, 6.117043132162393e-276], N[(N[(a / d), $MachinePrecision] * N[(c / d), $MachinePrecision] + N[(b / d), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 4.7943349601815015e+69], t$95$0, N[(t$95$1 / N[Sqrt[d ^ 2 + c ^ 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(d, c\right)}}{\mathsf{hypot}\left(d, c\right)}\\
t_1 := \mathsf{fma}\left(\frac{d}{c}, b, a\right)\\
\mathbf{if}\;c \leq -8.995298531255504 \cdot 10^{+122}:\\
\;\;\;\;\frac{-t_1}{\mathsf{hypot}\left(d, c\right)}\\
\mathbf{elif}\;c \leq -1.6578507474419048 \cdot 10^{-168}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;c \leq 6.117043132162393 \cdot 10^{-276}:\\
\;\;\;\;\mathsf{fma}\left(\frac{a}{d}, \frac{c}{d}, \frac{b}{d}\right)\\
\mathbf{elif}\;c \leq 4.7943349601815015 \cdot 10^{+69}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{t_1}{\mathsf{hypot}\left(d, c\right)}\\
\end{array}




Bits error versus a




Bits error versus b




Bits error versus c




Bits error versus d
| Original | 26.2 |
|---|---|
| Target | 0.4 |
| Herbie | 10.1 |
if c < -8.9952985312555037e122Initial program 41.0
Simplified41.0
Applied add-sqr-sqrt_binary6441.0
Applied *-un-lft-identity_binary6441.0
Applied times-frac_binary6441.0
Simplified41.0
Simplified27.5
Applied associate-*r/_binary6427.5
Taylor expanded in c around -inf 11.8
Simplified8.3
if -8.9952985312555037e122 < c < -1.65785074744190476e-168 or 6.11704313216239298e-276 < c < 4.7943349601815015e69Initial program 17.6
Simplified17.6
Applied add-sqr-sqrt_binary6417.6
Applied *-un-lft-identity_binary6417.6
Applied times-frac_binary6417.6
Simplified17.6
Simplified11.4
Applied associate-*l/_binary6411.3
Simplified11.3
if -1.65785074744190476e-168 < c < 6.11704313216239298e-276Initial program 23.3
Simplified23.3
Applied add-sqr-sqrt_binary6423.3
Applied *-un-lft-identity_binary6423.3
Applied times-frac_binary6423.3
Simplified23.3
Simplified13.1
Taylor expanded in d around inf 7.8
Simplified6.2
if 4.7943349601815015e69 < c Initial program 37.6
Simplified37.6
Applied add-sqr-sqrt_binary6437.6
Applied *-un-lft-identity_binary6437.6
Applied times-frac_binary6437.6
Simplified37.6
Simplified25.1
Applied associate-*r/_binary6425.1
Taylor expanded in d around 0 14.2
Simplified10.9
Final simplification10.1
herbie shell --seed 2022129
(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))))