]> www.ginac.de Git - cln.git/blob - src/real/format-output/cl_fmt_scaleexp.cc
404b71ea3f0d7e1949f211c2795992c03214da56
[cln.git] / src / real / format-output / cl_fmt_scaleexp.cc
1 // format_scale_exponent().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 CL_PROVIDE(cl_fmt_scaleexp)
7
8 // Specification.
9 #include "cl_format.h"
10
11
12 // Implementation.
13
14 #include "cln/real.h"
15 #include "cln/integer.h"
16 #include "cln/float.h"
17 #include "cl_F.h"
18 #include "cl_SF.h"
19 #include "cl_FF.h"
20 #include "cl_DF.h"
21 #include "cl_LF.h"
22
23 namespace cln {
24
25 // NOTE: This may introduce roundoff-errors, through the use of *, /, expt.
26 // But this doesn't matter since format_float_to_string() works with
27 // exact integers, starting with integer_decode_float().
28
29 // For a floating point format f, five characteristic numbers:
30 struct float_format_params {
31         cl_F zero;      // cl_float(0,f)
32         cl_F one;       // cl_float(1,f)
33         cl_F ten;       // cl_float(10,f)
34         cl_F tenth;     // cl_float(1/10,f)
35         cl_F lg2;       // log(10,2), as needed (max. 32 bits)
36 // Constructor:
37         float_format_params (cl_F a, cl_F b, cl_F c, cl_F d, cl_F e)
38                 : zero(a), one(b), ten(c), tenth(d), lg2(e) {}
39 };
40
41 static const cl_RA tenth = (cl_RA)"1/10";
42 static const cl_SF SF_zero = cl_RA_to_SF(0);
43 static const cl_SF SF_one = cl_RA_to_SF(1);
44 static const cl_SF SF_ten = cl_RA_to_SF(10);
45 static const cl_SF SF_tenth = cl_RA_to_SF(tenth);
46 static const cl_FF FF_zero = cl_RA_to_FF(0);
47 static const cl_FF FF_one = cl_RA_to_FF(1);
48 static const cl_FF FF_ten = cl_RA_to_FF(10);
49 static const cl_FF FF_tenth = cl_RA_to_FF(tenth);
50 static const cl_DF DF_zero = cl_RA_to_DF(0);
51 static const cl_DF DF_one = cl_RA_to_DF(1);
52 static const cl_DF DF_ten = cl_RA_to_DF(10);
53 static const cl_DF DF_tenth = cl_RA_to_DF(tenth);
54 static const cl_SF SF_lg2 = (cl_SF)"0.30103";
55 static const cl_DF DF_lg2 = (cl_DF)"0.30102999566";
56
57 static const float_format_params get_float_params (const cl_F& arg)
58 {
59         floattypecase(arg
60         ,       return float_format_params(SF_zero,SF_one,SF_ten,SF_tenth,SF_lg2);
61         ,       return float_format_params(FF_zero,FF_one,FF_ten,FF_tenth,SF_lg2);
62         ,       return float_format_params(DF_zero,DF_one,DF_ten,DF_tenth,SF_lg2);
63         ,       var uintC len = TheLfloat(arg)->len;
64                 return float_format_params(
65                         cl_I_to_LF(0,len),
66                         cl_I_to_LF(1,len),
67                         cl_I_to_LF(10,len),
68                         cl_RA_to_LF(tenth,len),
69                         DF_lg2 // lg2 wird mit 32 Bit Genauigkeit gebraucht
70                        );
71         );
72 }
73
74 const decoded_float format_scale_exponent (const cl_F& arg)
75 {
76         // Get float format parameters.
77         var const float_format_params params = get_float_params(arg);
78         var const cl_F& zero = params.zero;
79         var const cl_F& one = params.one;
80         var const cl_F& ten = params.ten;
81         var const cl_F& tenth = params.tenth;
82         var const cl_F& lg2 = params.lg2;
83         // Decode arg.
84         if (zerop(arg))
85                 return decoded_float(zero,0,one);
86         var cl_F abs_arg = abs(arg);
87         var decoded_float decoded = decode_float(abs_arg);
88         var cl_I& expon = decoded.exponent;
89         var cl_I expon10a = truncate1(expon*lg2); // nicht round, um Überlauf zu vermeiden
90         var cl_F signif10a = abs_arg / expt(ten,expon10a);
91         // Maybe need to increment expon10.
92         var cl_I expon10b = expon10a;
93         var cl_F signif10b = signif10a;
94         {
95                 var cl_F tenpow = ten;
96                 until (signif10b < one) {
97                         expon10b = expon10b + 1;
98                         signif10b = signif10a / tenpow;
99                         tenpow = tenpow * ten;
100                 }
101         }
102         // Maybe need to decrement expon10.
103         var cl_I expon10c = expon10b;
104         var cl_F signif10c = signif10b;
105         {
106                 var cl_F tenpow = ten;
107                 until (signif10c >= tenth) {
108                         expon10c = expon10c - 1;
109                         signif10c = signif10b * tenpow;
110                         tenpow = tenpow * ten;
111                 }
112         }
113         return decoded_float(signif10c,expon10c,float_sign(arg));
114 }
115
116 }  // namespace cln
117
118 CL_PROVIDE_END(cl_fmt_scaleexp)