]> www.ginac.de Git - cln.git/blob - src/float/misc/cl_F_shortenrel.cc
Initial revision
[cln.git] / src / float / misc / cl_F_shortenrel.cc
1 // cl_F_shortenrelative().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_F.h"
8
9
10 // Implementation.
11
12 #include "cl_LF.h"
13 #include "cl_abort.h"
14
15 const cl_F cl_F_shortenrelative (const cl_F& x, const cl_F& y)
16 {
17         // Methode:
18         // x = 0.0 -> Precision egal, return x.
19         // ex := float_exponent(x), ey := float_exponent(y).
20         // dx := float_digits(x), dy := float_digits(y).
21         // 1 ulp(x) = 2^(ex-dx), 1 ulp(y) = 2^(ey-dy).
22         // Falls ex-dx < ey-dy, x von Precision dx auf dy-ey+ex verkürzen.
23         var sintL ey = float_exponent(y);
24         var sintL dy = float_precision(y);
25         if (dy==0) // zerop(y) ?
26                 cl_abort();
27         var sintL ex = float_exponent(x);
28         var sintL dx = float_precision(x);
29         if (dx==0) // zerop(x) ?
30                 return x;
31         var sintL d = ex - ey;
32         if (ex>=0 && ey<0 && d<0) // d overflow?
33                 return x;
34         if (ex<0 && ey>=0 && d>=0) // d underflow?
35                 return cl_F_to_SF(x);
36         if (d >= dx - dy)
37                 return x;
38         var uintL new_dx = dy + d;
39         floatformatcase(new_dx
40         ,       return cl_F_to_SF(x);
41         ,       return cl_F_to_FF(x);
42         ,       return cl_F_to_DF(x);
43         ,       if (intDsize*len < (uintL)dx)
44                         return shorten(The(cl_LF)(x),len);
45                 else
46                         return x;
47         );
48 }