1 // format_scale_exponent().
6 CL_PROVIDE(cl_fmt_scaleexp)
15 #include "cln/integer.h"
16 #include "cln/float.h"
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().
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)
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) {}
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";
57 static const float_format_params get_float_params (const cl_F& 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(
68 cl_RA_to_LF(tenth,len),
69 DF_lg2 // lg2 wird mit 32 Bit Genauigkeit gebraucht
74 const decoded_float format_scale_exponent (const cl_F& arg)
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;
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;
95 var cl_F tenpow = ten;
96 until (signif10b < one) {
97 expon10b = expon10b + 1;
98 signif10b = signif10a / tenpow;
99 tenpow = tenpow * ten;
102 // Maybe need to decrement expon10.
103 var cl_I expon10c = expon10b;
104 var cl_F signif10c = signif10b;
106 var cl_F tenpow = ten;
107 until (signif10c >= tenth) {
108 expon10c = expon10c - 1;
109 signif10c = signif10b * tenpow;
110 tenpow = tenpow * ten;
113 return decoded_float(signif10c,expon10c,float_sign(arg));
118 CL_PROVIDE_END(cl_fmt_scaleexp)