]> www.ginac.de Git - cln.git/blob - src/float/lfloat/misc/cl_LF_shortenwith.cc
2006-04-25 Bruno Haible <bruno@clisp.org>
[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 #include "cln/abort.h"
13
14 #undef MAYBE_INLINE2
15 #define MAYBE_INLINE2 inline
16 #include "cl_LF_precision.cc"
17 #undef MAYBE_INLINE
18 #define MAYBE_INLINE inline
19 #include "cl_LF_exponent.cc"
20
21 namespace cln {
22
23 const cl_LF cl_LF_shortenwith (const cl_LF& x, const cl_LF& y)
24 {
25         // Methode:
26         // x = 0.0 -> Precision egal, return x.
27         // ex := float_exponent(x), dx := float_digits(x), 1 ulp(x) = 2^(ex-dx).
28         // ey := float_exponent(y).
29         // Falls ex-dx < ey, x von Precision dx auf ex-ey verkürzen.
30         var sintL ey = float_exponent(y);
31         var sintL ex = float_exponent(x);
32         var uintC dx = float_precision(x);
33         if (dx==0) // zerop(x) ?
34                 return x;
35         var sintL ulpx = ex - dx;
36         if ((ex<0 && ulpx>=0) // underflow?
37             || (ulpx < ey)
38            ) {  // Now ex-dx < ey, hence ex-ey < dx.
39                 var uintL new_dx;
40                 if (ex < ey)
41                         new_dx = intDsize*LF_minlen;
42                 else if ((new_dx = ex - ey) < intDsize*LF_minlen)
43                         new_dx = intDsize*LF_minlen;
44                 var uintL len = ceiling(new_dx,intDsize);
45                 if (intDsize*len < dx)
46                         return shorten(x,len);
47                 else
48                         return x;
49         } else
50                 return x;
51 }
52
53 }  // namespace cln