Debian Codegraph @

src/c/printer/float_to_digits.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * float_to_digits.d: floating point printer functions
6 *
7 * Copyright (c) 2010 Juan Jose Garcia Ripoll
8 *
9 * See file 'LICENSE' for the copyright details.
10 *
11 */
12
13 #define ECL_INCLUDE_MATH_H
14 #include <float.h>
15 #include <ecl/ecl.h>
16 #include <ecl/ecl-inl.h>
17
18 /*
19 * The following algorithm is based on Burger, Dybvig (1996)
20 * (available at http://www.cs.indiana.edu/~dyb/pubs/FP-Printing-PLDI96.pdf)
21 */
22
23 #define PRINT_BASE ecl_make_fixnum(10)
24 #define EXPT_RADIX(x) ecl_ash(ecl_make_fixnum(1),x)
25
26 typedef struct {
27 cl_object r;
28 cl_object s;
29 cl_object mm;
30 cl_object mp;
31 bool high_ok;
32 bool low_ok;
33 } float_approx;
34
35 static cl_object
36 times2(cl_object x)
37 {
38 return ecl_plus(x, x);
39 }
40
41 static float_approx *
42 setup(cl_object number, float_approx *approx)
43 {
44 cl_object f = cl_integer_decode_float(number);
45 cl_fixnum e = ecl_fixnum(VALUES(1)), min_e;
46 bool limit_f = 0;
47 switch (ecl_t_of(number)) {
48 case t_singlefloat:
49 min_e = FLT_MIN_EXP;
50 limit_f = (number->SF.SFVAL ==
51 ldexpf(FLT_RADIX, FLT_MANT_DIG-1));
52 break;
53 case t_doublefloat:
54 min_e = DBL_MIN_EXP;
55 limit_f = (number->DF.DFVAL ==
56 ldexp(FLT_RADIX, DBL_MANT_DIG-1));
57 break;
58 case t_longfloat:
59 min_e = LDBL_MIN_EXP;
60 limit_f = (number->longfloat.value ==
61 ldexpl(FLT_RADIX, LDBL_MANT_DIG-1));
62 break;
63 default:
64 break;
65 }
66 approx->low_ok = approx->high_ok = 0;
67 if (e > 0) {
68 cl_object be = EXPT_RADIX(e);
69 if (limit_f) {
70 cl_object be1 = ecl_times(be, ecl_make_fixnum(FLT_RADIX));
71 approx->r = times2(ecl_times(f, be1));
72 approx->s = ecl_make_fixnum(FLT_RADIX*2);
73 approx->mm = be;
74 approx->mp = be1;
75 } else {
76 approx->r = times2(ecl_times(f, be));
77 approx->s = ecl_make_fixnum(2);
78 approx->mm = be;
79 approx->mp = be;
80 }
81 } else if (!limit_f || (e == min_e)) {
82 approx->r = times2(f);
83 approx->s = times2(EXPT_RADIX(-e));
84 approx->mp = ecl_make_fixnum(1);
85 approx->mm = ecl_make_fixnum(1);
86 } else {
87 approx->r = times2(ecl_make_fixnum(FLT_RADIX));
88 approx->s = times2(EXPT_RADIX(1-e));
89 approx->mp = ecl_make_fixnum(FLT_RADIX);
90 approx->mm = ecl_make_fixnum(1);
91 }
92 return approx;
93 }
94
95 static cl_fixnum
96 scale(float_approx *approx)
97 {
98 cl_fixnum k = 0;
99 cl_object x = ecl_plus(approx->r, approx->mp);
100 int sign;
101 do {
102 sign = ecl_number_compare(x, approx->s);
103 if (approx->high_ok) {
104 if (sign < 0)
105 break;
106 } else {
107 if (sign <= 0)
108 break;
109 }
110 approx->s = ecl_times(approx->s, PRINT_BASE);
111 k++;
112 } while(1);
113 do {
114 x = ecl_times(x, PRINT_BASE);
115 sign = ecl_number_compare(x, approx->s);
116 if (approx->high_ok) {
117 if (sign >= 0)
118 break;
119 } else {
120 if (sign > 0)
121 break;
122 }
123 k--;
124 approx->r = ecl_times(approx->r, PRINT_BASE);
125 approx->mm = ecl_times(approx->mm, PRINT_BASE);
126 approx->mp = ecl_times(approx->mp, PRINT_BASE);
127 } while(1);
128 return k;
129 }
130
131 static cl_object
132 generate(cl_object digits, float_approx *approx)
133 {
134 cl_object d, x;
135 cl_fixnum digit;
136 bool tc1, tc2;
137 do {
138 d = ecl_truncate2(ecl_times(approx->r, PRINT_BASE), approx->s);
139 approx->r = VALUES(1);
140 approx->mp = ecl_times(approx->mp, PRINT_BASE);
141 approx->mm = ecl_times(approx->mm, PRINT_BASE);
142 tc1 = approx->low_ok?
143 ecl_lowereq(approx->r, approx->mm) :
144 ecl_lower(approx->r, approx->mm);
145 x = ecl_plus(approx->r, approx->mp);
146 tc2 = approx->high_ok?
147 ecl_greatereq(x, approx->s) :
148 ecl_greater(x, approx->s);
149 if (tc1 || tc2) {
150 break;
151 }
152 ecl_string_push_extend(digits, ecl_digit_char(ecl_fixnum(d), 10));
153 } while (1);
154 if (tc2 && !tc1) {
155 digit = ecl_fixnum(d) + 1;
156 } else if (tc1 && !tc2) {
157 digit = ecl_fixnum(d);
158 } else if (ecl_lower(times2(approx->r), approx->s)) {
159 digit = ecl_fixnum(d);
160 } else {
161 digit = ecl_fixnum(d) + 1;
162 }
163 ecl_string_push_extend(digits, ecl_digit_char(digit, 10));
164 return digits;
165 }
166
167 static void
168 change_precision(float_approx *approx, cl_object position, cl_object relativep)
169 {
170 if (Null(position))
171 return;
172 if (!Null(relativep)) {
173 cl_object k = ecl_make_fixnum(0);
174 cl_object l = ecl_make_fixnum(1);
175 while (ecl_lower(ecl_times(approx->s, l),
176 ecl_plus(approx->r, approx->mp))) {
177 k = ecl_one_plus(k);
178 l = ecl_times(l, PRINT_BASE);
179 }
180 position = ecl_minus(k, position);
181 {
182 cl_object e1 = cl_expt(PRINT_BASE, position);
183 cl_object e2 = ecl_divide(e1, ecl_make_fixnum(2));
184 if (ecl_greatereq(ecl_plus(approx->r, ecl_times(approx->s, e1)),
185 ecl_times(approx->s, e2)))
186 position = ecl_one_minus(position);
187 }
188 }
189 {
190 cl_object x = ecl_times(approx->s, cl_expt(PRINT_BASE, position));
191 cl_object e = ecl_divide(x, ecl_make_fixnum(2));
192 cl_object low = cl_max(2, approx->mm, e);
193 cl_object high = cl_max(2, approx->mp, e);
194 if (ecl_lowereq(approx->mm, low)) {
195 approx->mm = low;
196 approx->low_ok = 1;
197 }
198 if (ecl_lowereq(approx->mp, high)) {
199 approx->mp = high;
200 approx->high_ok = 1;
201 }
202 }
203 }
204
205 /*
206 * si_float_to_digits: create a correctly rounded approximation for a
207 * floating point number in the form of a string of (base 10) digits.
208 * Zero digits at the end or beginning of the string are omitted.
209 *
210 * Arguments:
211 *
212 * - digits: an adjustable base-string to store the digits in or NIL
213 * to allocate a new base-string
214 * - number: the floating point number to approximate
215 * - position: specifies the number of digits to generate. If position
216 * is NIL create as many digits as necessary to reconstruct the
217 * number without error; otherwise:
218 * - if relativep is false, position specifies the position relative
219 * to the decimal point up to which to generate digits. Example: a
220 * number of the form (d_n)...(d_0).(d_{-1})...*10^0 is translated
221 * into a string (d_n)...(d_{position}) and then rounded. Here,
222 * (d_i) is the digit at position i relative to the decimal point.
223 * - if replativep is true, the number is normalized such that the
224 * decimal point lies behind the first digit and position
225 * specifies the number of digits to generate after this shifted
226 * decimal point. Example: for k >= 0, a number of the form
227 * (d_1).(d_2)...(d_n)*10^(-k) is translated into a string
228 * (d_1)(d_2)...(d_{position+1}) and then rounded irrespective of
229 * the value of k. For k < 0, the result is a string of the form
230 * (d_1)(d_2)..(d_{position-k+1}) (k leading zeros are omitted).
231 * - relativep: a generalized boolean
232 *
233 * Return values:
234 *
235 * - k: a scale factor such that the string of digits multiplied with
236 * 10^k is (up to rounding) equal to number. To be precise, for
237 * return values k and (d_1)...(d_n), number ≅ 10^(k-n)*int((d_1)...(d_n)).
238 * - digits: the created digits
239 */
240
241 cl_object
242 si_float_to_digits(cl_object digits, cl_object number, cl_object position,
243 cl_object relativep)
244 {
245 cl_fixnum k;
246 float_approx approx[1];
247 setup(number, approx);
248 change_precision(approx, position, relativep);
249 k = scale(approx);
250 if (Null(digits))
251 digits = si_make_vector(@'base-char', ecl_make_fixnum(10),
252 ECL_T /* adjustable */,
253 ecl_make_fixnum(0) /* fill pointer */,
254 ECL_NIL /* displacement */,
255 ECL_NIL /* displ. offset */);
256 generate(digits, approx);
257 @(return ecl_make_fixnum(k) digits);
258 }