| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* |
| 6 |
* Copyright (c) 2010 Juan Jose Garcia Ripoll |
| 7 |
* |
| 8 |
* See file 'LICENSE' for the copyright details. |
| 9 |
* |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <ecl/ecl.h> |
| 13 |
#include <ecl/ecl-inl.h> |
| 14 |
|
| 15 |
/* |
| 16 |
* This is a port of CMUCL's FLOAT-STRING routine which converts a |
| 17 |
* floating point number of arbitrary representation into a text |
| 18 |
* representation which contains the least number of digits for the |
| 19 |
* given precision. |
| 20 |
*/ |
| 21 |
/* Written by Bill Maddox |
| 22 |
* Translated to C by Juan Jose Garcia Ripoll |
| 23 |
* |
| 24 |
* FLONUM-TO-STRING (and its subsidiary function FLOAT-STRING) does most of |
| 25 |
* the work for all printing of floating point numbers in the printer and in |
| 26 |
* FORMAT. It converts a floating point number to a string in a free or |
| 27 |
* fixed format with no exponent. The interpretation of the arguments is as |
| 28 |
* follows: |
| 29 |
* |
| 30 |
* X - The floating point number to convert, which must not be |
| 31 |
* negative. |
| 32 |
* WIDTH - The preferred field width, used to determine the number |
| 33 |
* of fraction digits to produce if the FDIGITS parameter |
| 34 |
* is unspecified or NIL. If the non-fraction digits and the |
| 35 |
* decimal point alone exceed this width, no fraction digits |
| 36 |
* will be produced unless a non-NIL value of FDIGITS has been |
| 37 |
* specified. Field overflow is not considerd an error at this |
| 38 |
* level. |
| 39 |
* FDIGITS - The number of fractional digits to produce. Insignificant |
| 40 |
* trailing zeroes may be introduced as needed. May be |
| 41 |
* unspecified or NIL, in which case as many digits as possible |
| 42 |
* are generated, subject to the constraint that there are no |
| 43 |
* trailing zeroes. |
| 44 |
* SCALE - If this parameter is specified or non-NIL, then the number |
| 45 |
* printed is (* x (expt 10 scale)). This scaling is exact, |
| 46 |
* and cannot lose precision. |
| 47 |
* FMIN - This parameter, if specified or non-NIL, is the minimum |
| 48 |
* number of fraction digits which will be produced, regardless |
| 49 |
* of the value of WIDTH or FDIGITS. This feature is used by |
| 50 |
* the ~E format directive to prevent complete loss of |
| 51 |
* significance in the printed value due to a bogus choice of |
| 52 |
* scale factor. |
| 53 |
* |
| 54 |
* Most of the optional arguments are for the benefit for FORMAT and are not |
| 55 |
* used by the printer. |
| 56 |
* |
| 57 |
* Returns: |
| 58 |
* (VALUES DIGIT-STRING DIGIT-LENGTH LEADING-POINT TRAILING-POINT DECPNT) |
| 59 |
* where the results have the following interpretation: |
| 60 |
* |
| 61 |
* DIGIT-STRING - The decimal representation of X, with decimal point. |
| 62 |
* DIGIT-LENGTH - The length of the string DIGIT-STRING. |
| 63 |
* LEADING-POINT - True if the first character of DIGIT-STRING is the |
| 64 |
* decimal point. |
| 65 |
* TRAILING-POINT - True if the last character of DIGIT-STRING is the |
| 66 |
* decimal point. |
| 67 |
* POINT-POS - The position of the digit preceding the decimal |
| 68 |
* point. Zero indicates point before first digit. |
| 69 |
* |
| 70 |
* NOTE: FLONUM-TO-STRING goes to a lot of trouble to guarantee accuracy. |
| 71 |
* Specifically, the decimal number printed is the closest possible |
| 72 |
* approximation to the true value of the binary number to be printed from |
| 73 |
* among all decimal representations with the same number of digits. In |
| 74 |
* free-format output, i.e. with the number of digits unconstrained, it is |
| 75 |
* guaranteed that all the information is preserved, so that a properly- |
| 76 |
* rounding reader can reconstruct the original binary number, bit-for-bit, |
| 77 |
* from its printed decimal representation. Furthermore, only as many digits |
| 78 |
* as necessary to satisfy this condition will be printed. |
| 79 |
* |
| 80 |
* |
| 81 |
* FLOAT-STRING actually generates the digits for positive numbers. The |
| 82 |
* algorithm is essentially that of algorithm Dragon4 in "How to Print |
| 83 |
* Floating-Point Numbers Accurately" by Steele and White. The current |
| 84 |
* (draft) version of this paper may be found in [CMUC]<steele>tradix.press. |
| 85 |
* DO NOT EVEN THINK OF ATTEMPTING TO UNDERSTAND THIS CODE WITHOUT READING |
| 86 |
* THE PAPER! |
| 87 |
*/ |
| 88 |
|
| 89 |
static bool |
| 90 |
large_mantissa(cl_object r, cl_object mp, cl_object s) |
| 91 |
{ |
| 92 |
return ecl_greatereq(ecl_plus(ecl_ash(r,1), mp), |
| 93 |
ecl_ash(s, 1)); |
| 94 |
} |
| 95 |
|
| 96 |
static cl_fixnum |
| 97 |
assert_floating_point_width(cl_object width) |
| 98 |
{ |
| 99 |
if (!ECL_FIXNUMP(width) || ecl_lower(width,ecl_make_fixnum(1))) { |
| 100 |
FEerror("Invalid number of floating point digits." |
| 101 |
"~%~A~%is not an integer within bounds", |
| 102 |
1, width); |
| 103 |
} |
| 104 |
return ecl_fixnum(width); |
| 105 |
} |
| 106 |
|
| 107 |
static cl_object |
| 108 |
float_string(cl_object digits_string, |
| 109 |
cl_object fraction, cl_object exponent, cl_object precision, |
| 110 |
cl_object width, cl_object fdigits, cl_object scale, cl_object fmin) |
| 111 |
{ |
| 112 |
cl_object r = fraction; |
| 113 |
cl_object s = ecl_make_fixnum(1); |
| 114 |
cl_object mm = s; |
| 115 |
cl_object mp = s; |
| 116 |
cl_fixnum i, k = 0, digits = 0, decpnt = 0, cutoff = 0; |
| 117 |
cl_object u; |
| 118 |
char *buffer; |
| 119 |
bool roundup = 0, cutoffp = 0, low = 0, high = 0; |
| 120 |
|
| 121 |
if (Null(digits_string)) { |
| 122 |
digits_string = si_make_vector(@'base-char', ecl_make_fixnum(10), |
| 123 |
ECL_T /* adjustable */, |
| 124 |
ecl_make_fixnum(0) /* fill pointer */, |
| 125 |
ECL_NIL /* displacement */, |
| 126 |
ECL_NIL /* displ. offset */); |
| 127 |
} |
| 128 |
/* Represent fraction as r/s, error bounds as m+/s and m-/s. |
| 129 |
* Rational arithmetic avoids loss of precision in subsequent |
| 130 |
* calculations. |
| 131 |
*/ |
| 132 |
{ |
| 133 |
int sign = ecl_number_compare(exponent, ecl_make_fixnum(0)); |
| 134 |
if (sign > 0) { |
| 135 |
r = cl_ash(fraction, exponent); |
| 136 |
mm = cl_ash(ecl_make_fixnum(1), exponent); |
| 137 |
mp = mm; |
| 138 |
} else if (sign < 0) { |
| 139 |
s = cl_ash(ecl_make_fixnum(1), ecl_negate(exponent)); |
| 140 |
} |
| 141 |
} |
| 142 |
/* Adjust error bounds m+ and m- for unequal gaps */ |
| 143 |
if (ecl_number_equalp(fraction, cl_ash(ecl_make_fixnum(1), precision))) { |
| 144 |
mp = ecl_ash(mm, 1); |
| 145 |
r = ecl_ash(r, 1); |
| 146 |
s = ecl_ash(s, 1); |
| 147 |
} |
| 148 |
/* Scale value by requested amount and update error bounds */ |
| 149 |
if (!Null(scale)) { |
| 150 |
if (ecl_minusp(scale)) { |
| 151 |
cl_object factor = cl_expt(ecl_make_fixnum(10), |
| 152 |
ecl_negate(scale)); |
| 153 |
s = ecl_times(s, factor); |
| 154 |
} else { |
| 155 |
cl_object factor = cl_expt(ecl_make_fixnum(10), scale); |
| 156 |
r = ecl_times(r, factor); |
| 157 |
mm = ecl_times(mm, factor); |
| 158 |
mp = ecl_times(mp, factor); |
| 159 |
} |
| 160 |
} |
| 161 |
while (ecl_lower(r, ecl_ceiling2(s, ecl_make_fixnum(10)))) { |
| 162 |
k--; |
| 163 |
r = ecl_times(r, ecl_make_fixnum(10)); |
| 164 |
mm = ecl_times(r, ecl_make_fixnum(10)); |
| 165 |
mp = ecl_times(r, ecl_make_fixnum(10)); |
| 166 |
} |
| 167 |
do { |
| 168 |
/* Ensure mantissa (r + m+)/s is smaller than one */ |
| 169 |
while (large_mantissa(r, mp, s)) { |
| 170 |
s = ecl_times(s, ecl_make_fixnum(10)); |
| 171 |
k++; |
| 172 |
} |
| 173 |
/* Determine the number of digits to generate */ |
| 174 |
if (!Null(fdigits)) { |
| 175 |
cutoffp = 1; |
| 176 |
cutoff = assert_floating_point_width(width); |
| 177 |
} else if (!Null(width)) { |
| 178 |
cutoffp = 1; |
| 179 |
cutoff = assert_floating_point_width(width); |
| 180 |
if (k < 0) { |
| 181 |
cutoff = cutoff - 1; |
| 182 |
} else { |
| 183 |
cutoff = cutoff - k + 1; |
| 184 |
} |
| 185 |
} |
| 186 |
/* ... and ensure it is never less than fmin */ |
| 187 |
if (cutoffp) { |
| 188 |
cl_fixnum a, i; |
| 189 |
cl_object y; |
| 190 |
if (!Null(fmin)) { |
| 191 |
cl_fixnum f = assert_floating_point_width(fmin); |
| 192 |
if (cutoff < f) |
| 193 |
cutoff = f; |
| 194 |
} |
| 195 |
/* If we decided to cut off digit generation before precision |
| 196 |
* has been exhausted, rounding the last digit may cause a |
| 197 |
* carry propagation. We can prevent this, preserving |
| 198 |
* left-to-right digit generation, with a few magical |
| 199 |
* adjustments to m- and m+. Of course, correct rounding is |
| 200 |
* also preserved. */ |
| 201 |
a = k - cutoff; |
| 202 |
y = s; |
| 203 |
if (a < 0) { |
| 204 |
for (i = 0, a = -a; i < a; i++) { |
| 205 |
y = ecl_ceiling2(y, ecl_make_fixnum(10)); |
| 206 |
} |
| 207 |
} else { |
| 208 |
for (i = 0, a = -a; i < a; i++) { |
| 209 |
y = ecl_times(y, ecl_make_fixnum(10)); |
| 210 |
} |
| 211 |
} |
| 212 |
mm = cl_max(2, y, mm); |
| 213 |
mp = cl_max(2, y, mp); |
| 214 |
roundup = ecl_number_equalp(mp, y); |
| 215 |
} |
| 216 |
} while (large_mantissa(r, mp, s)); |
| 217 |
/* Zero-fill before fraction if no integer part */ |
| 218 |
if (k < 0) { |
| 219 |
decpnt = digits; |
| 220 |
ecl_string_push_extend(digits_string, '.'); |
| 221 |
for (i = k; i; i++) { |
| 222 |
digits++; |
| 223 |
ecl_string_push_extend(digits_string, '0'); |
| 224 |
} |
| 225 |
} |
| 226 |
/* Generate least significant digits */ |
| 227 |
do { |
| 228 |
int sign; |
| 229 |
if (--k == -1) { |
| 230 |
ecl_string_push_extend(digits_string, '.'); |
| 231 |
decpnt = digits; |
| 232 |
} |
| 233 |
u = ecl_truncate2(ecl_times(r, ecl_make_fixnum(10)), s); |
| 234 |
r = VALUES(1); |
| 235 |
mm = ecl_times(mm, ecl_make_fixnum(10)); |
| 236 |
mp = ecl_times(mp, ecl_make_fixnum(10)); |
| 237 |
low = ecl_lower(ecl_ash(r,1), mm); |
| 238 |
sign = ecl_number_compare(ecl_ash(r,1), ecl_minus(ecl_ash(s,1),mp)); |
| 239 |
high = roundup? (sign >= 0) : (sign > 0); |
| 240 |
/* stop when either precision is exhausted or we have printed as many |
| 241 |
* fraction digits as permitted */ |
| 242 |
if (low || high || (cutoffp && (k + cutoff <= 0))) |
| 243 |
break; |
| 244 |
ecl_string_push_extend(digits_string, ecl_digit_char(ecl_fixnum(u), 10)); |
| 245 |
digits++; |
| 246 |
} while(1); |
| 247 |
/* If cutof occured before first digit, then no digits generated at all */ |
| 248 |
if (!cutoffp || (k + cutoff) >= 0) { |
| 249 |
/* Last digit may need rounding */ |
| 250 |
int digit = ecl_fixnum(u); |
| 251 |
if (low && !high) |
| 252 |
digit = ecl_fixnum(u); |
| 253 |
else if (high && !low) |
| 254 |
digit = ecl_fixnum(u)+1; |
| 255 |
else if (ecl_lower(ecl_ash(r,1), s)) |
| 256 |
digit = ecl_fixnum(u); |
| 257 |
else |
| 258 |
digit = ecl_fixnum(u) + 1; |
| 259 |
ecl_string_push_extend(digits_string, ecl_digit_char(digit, 10)); |
| 260 |
digits++; |
| 261 |
} |
| 262 |
/* Zero-fill after integer part if no fraction */ |
| 263 |
if (k >= 0) { |
| 264 |
for (i = 0; i < k; i++) { |
| 265 |
ecl_string_push_extend(digits_string, '0'); |
| 266 |
digits++; |
| 267 |
} |
| 268 |
ecl_string_push_extend(digits_string, '.'); |
| 269 |
decpnt = digits; |
| 270 |
} |
| 271 |
/* Add trailing zeroes to pad fraction if fdigits needed */ |
| 272 |
if (!Null(fdigits)) { |
| 273 |
cl_fixnum f = assert_floating_point_width(fdigits) - (digits - decpnt); |
| 274 |
for (i = 0; i < f; i++) { |
| 275 |
ecl_string_push_extend(digits_string, '0'); |
| 276 |
digits++; |
| 277 |
} |
| 278 |
} |
| 279 |
/* All done */ |
| 280 |
@(return |
| 281 |
digits_string |
| 282 |
ecl_make_fixnum(1+digits) |
| 283 |
((decpnt == 0)? ECL_T : ECL_NIL) |
| 284 |
((decpnt == digits)? ECL_T : ECL_NIL) |
| 285 |
ecl_make_fixnum(decpnt)); |
| 286 |
} |
| 287 |
|
| 288 |
ecl_def_ct_base_string(str_dot,".",1,static,const); |
| 289 |
|
| 290 |
@(defun ext::float-string (string x &optional width fdigits scale fmin) |
| 291 |
@ |
| 292 |
{ |
| 293 |
if (ecl_zerop(x)) { |
| 294 |
if (Null(fdigits)) { |
| 295 |
cl_object s = cl_make_string(3, ecl_one_plus(fdigits), |
| 296 |
@':initial-element', |
| 297 |
ECL_CODE_CHAR('0')); |
| 298 |
ecl_char_set(s, 0, '.'); |
| 299 |
@(return s cl_length(s) ECL_T cl_zerop(fdigits) ecl_make_fixnum(0)); |
| 300 |
} else { |
| 301 |
@(return str_dot ecl_make_fixnum(1) ECL_T ECL_T ecl_make_fixnum(0)); |
| 302 |
} |
| 303 |
} else { |
| 304 |
cl_object sig = cl_integer_decode_float(x); |
| 305 |
cl_object exp = VALUES(1); |
| 306 |
cl_object precision = cl_float_precision(x); |
| 307 |
cl_object digits = cl_float_digits(x); |
| 308 |
cl_object fudge = ecl_minus(digits, precision); |
| 309 |
cl_object w = Null(width)? ECL_NIL : cl_max(2, width, ecl_make_fixnum(1)); |
| 310 |
return float_string(string, cl_ash(sig, ecl_negate(fudge)), |
| 311 |
ecl_plus(exp, fudge), precision, w, |
| 312 |
fdigits, scale, fmin); |
| 313 |
} |
| 314 |
} |
| 315 |
@) |