]> www.ginac.de Git - cln.git/blob - src/float/lfloat/misc/cl_LF_shortenwith.cc
* */*: Remove cl_boolean, cl_true, and cl_false in favor of built-in
[cln.git] / src / float / lfloat / misc / cl_LF_shortenwith.cc
1 // cl_LF_shortenwith().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_LF.h"
8
9
10 // Implementation.
11
12 #undef MAYBE_INLINE2
13 #define MAYBE_INLINE2 inline
14 #include "cl_LF_precision.cc"
15 #undef MAYBE_INLINE
16 #define MAYBE_INLINE inline
17 #include "cl_LF_exponent.cc"
18
19 namespace cln {
20
21 const cl_LF cl_LF_shortenwith (const cl_LF& x, const cl_LF& y)
22 {
23         // Methode:
24         // x = 0.0 -> Precision egal, return x.
25         // ex := float_exponent(x), dx := float_digits(x), 1 ulp(x) = 2^(ex-dx).
26         // ey := float_exponent(y).
27         // Falls ex-dx < ey, x von Precision dx auf ex-ey verkürzen.
28         var sintE ey = float_exponent(y);
29         var sintE ex = float_exponent(x);
30         var uintC dx = float_precision(x);
31         if (dx==0) // zerop(x) ?
32                 return x;
33         var sintE ulpx = ex - dx;
34         if ((ex<0 && ulpx>=0) // underflow?
35             || (ulpx < ey)
36            ) {  // Now ex-dx < ey, hence ex-ey < dx.
37                 var uintL new_dx;
38                 if (ex < ey)
39                         new_dx = intDsize*LF_minlen;
40                 else if ((new_dx = ex - ey) < intDsize*LF_minlen)
41                         new_dx = intDsize*LF_minlen;
42                 var uintL len = ceiling(new_dx,intDsize);
43                 if (intDsize*len < dx)
44                         return shorten(x,len);
45                 else
46                         return x;
47         } else
48                 return x;
49 }
50
51 }  // namespace cln