(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))))
(if (<= c -3.9e+155)
(fma (/ d c) (/ b c) (/ a c))
(if (<= c -3.7e-27)
t_0
(if (<= c 1e-284)
(fma (/ c d) (/ a d) (/ b d))
(if (<= c 1.25e+110)
t_0
(* (/ 1.0 (hypot c d)) (fma (/ d c) b 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 = (fma(a, c, (d * b)) / hypot(c, d)) / hypot(c, d);
double tmp;
if (c <= -3.9e+155) {
tmp = fma((d / c), (b / c), (a / c));
} else if (c <= -3.7e-27) {
tmp = t_0;
} else if (c <= 1e-284) {
tmp = fma((c / d), (a / d), (b / d));
} else if (c <= 1.25e+110) {
tmp = t_0;
} else {
tmp = (1.0 / hypot(c, d)) * fma((d / c), b, 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(fma(a, c, Float64(d * b)) / hypot(c, d)) / hypot(c, d)) tmp = 0.0 if (c <= -3.9e+155) tmp = fma(Float64(d / c), Float64(b / c), Float64(a / c)); elseif (c <= -3.7e-27) tmp = t_0; elseif (c <= 1e-284) tmp = fma(Float64(c / d), Float64(a / d), Float64(b / d)); elseif (c <= 1.25e+110) tmp = t_0; else tmp = Float64(Float64(1.0 / hypot(c, d)) * fma(Float64(d / c), b, 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[(N[(a * c + N[(d * b), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -3.9e+155], N[(N[(d / c), $MachinePrecision] * N[(b / c), $MachinePrecision] + N[(a / c), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, -3.7e-27], t$95$0, If[LessEqual[c, 1e-284], N[(N[(c / d), $MachinePrecision] * N[(a / d), $MachinePrecision] + N[(b / d), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 1.25e+110], t$95$0, N[(N[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(N[(d / c), $MachinePrecision] * b + a), $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)}\\
\mathbf{if}\;c \leq -3.9 \cdot 10^{+155}:\\
\;\;\;\;\mathsf{fma}\left(\frac{d}{c}, \frac{b}{c}, \frac{a}{c}\right)\\
\mathbf{elif}\;c \leq -3.7 \cdot 10^{-27}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;c \leq 10^{-284}:\\
\;\;\;\;\mathsf{fma}\left(\frac{c}{d}, \frac{a}{d}, \frac{b}{d}\right)\\
\mathbf{elif}\;c \leq 1.25 \cdot 10^{+110}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \mathsf{fma}\left(\frac{d}{c}, b, a\right)\\
\end{array}




Bits error versus a




Bits error versus b




Bits error versus c




Bits error versus d
| Original | 26.3 |
|---|---|
| Target | 0.5 |
| Herbie | 11.1 |
if c < -3.8999999999999998e155Initial program 45.5
Simplified45.5
Applied egg-rr29.5
Taylor expanded in c around inf 13.5
Simplified6.3
if -3.8999999999999998e155 < c < -3.70000000000000029e-27 or 1.00000000000000004e-284 < c < 1.24999999999999995e110Initial program 18.5
Simplified18.5
Applied egg-rr12.3
Applied egg-rr12.5
Applied egg-rr12.1
if -3.70000000000000029e-27 < c < 1.00000000000000004e-284Initial program 20.3
Simplified20.3
Applied egg-rr11.7
Taylor expanded in c around 0 14.5
Simplified12.9
if 1.24999999999999995e110 < c Initial program 40.8
Simplified40.8
Applied egg-rr27.3
Taylor expanded in c around inf 13.5
Simplified9.7
Final simplification11.1
herbie shell --seed 2022166
(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))))