Remove "Written by Ben Pfaff <blp@gnu.org>" lines everywhere.
[pspp-builds.git] / src / data / data-out.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include "data-out.h"
22
23 #include <ctype.h>
24 #include <float.h>
25 #include <math.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <time.h>
29 #include <gsl/gsl_math.h>
30
31 #include "calendar.h"
32 #include "format.h"
33 #include "settings.h"
34 #include "value.h"
35
36 #include <libpspp/assertion.h>
37 #include <libpspp/float-format.h>
38 #include <libpspp/integer-format.h>
39 #include <libpspp/magic.h>
40 #include <libpspp/message.h>
41 #include <libpspp/misc.h>
42 #include <libpspp/misc.h>
43 #include <libpspp/str.h>
44
45 #include "minmax.h"
46
47 #include "gettext.h"
48 #define _(msgid) gettext (msgid)
49 \f
50 /* A representation of a number that can be quickly rounded to
51    any desired number of decimal places (up to a specified
52    maximum). */
53 struct rounder
54   {
55     char string[64];    /* Magnitude of number with excess precision. */
56     int integer_digits; /* Number of digits before decimal point. */
57     int leading_nines;  /* Number of `9's or `.'s at start of string. */
58     int leading_zeros;  /* Number of `0's or `.'s at start of string. */
59     bool negative;      /* Is the number negative? */
60   };
61
62 static void rounder_init (struct rounder *, double number, int max_decimals);
63 static int rounder_width (const struct rounder *, int decimals,
64                           int *integer_digits, bool *negative);
65 static void rounder_format (const struct rounder *, int decimals,
66                             char *output);
67 \f
68 /* Format of integers in output (SET WIB). */
69 static enum integer_format output_integer_format = INTEGER_NATIVE;
70
71 /* Format of reals in output (SET WRB). */
72 static enum float_format output_float_format = FLOAT_NATIVE_DOUBLE;
73
74 typedef void data_out_converter_func (const union value *,
75                                       const struct fmt_spec *,
76                                       char *);
77 #define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY) \
78         static data_out_converter_func output_##METHOD;
79 #include "format.def"
80
81 static bool output_decimal (const struct rounder *, const struct fmt_spec *,
82                             bool require_affixes, char *);
83 static bool output_scientific (double, const struct fmt_spec *,
84                                bool require_affixes, char *);
85
86 static double power10 (int) PURE_FUNCTION;
87 static double power256 (int) PURE_FUNCTION;
88
89 static void output_infinite (double, const struct fmt_spec *, char *);
90 static void output_missing (const struct fmt_spec *, char *);
91 static void output_overflow (const struct fmt_spec *, char *);
92 static bool output_bcd_integer (double, int digits, char *);
93 static void output_binary_integer (uint64_t, int bytes, enum integer_format,
94                                    char *);
95 static void output_hex (const void *, size_t bytes, char *);
96 \f
97 /* Converts the INPUT value into printable form in the exactly
98    FORMAT->W characters in OUTPUT according to format
99    specification FORMAT.  No null terminator is appended to the
100    buffer.  */
101 void
102 data_out (const union value *input, const struct fmt_spec *format,
103           char *output)
104 {
105   static data_out_converter_func *const converters[FMT_NUMBER_OF_FORMATS] = 
106     {
107 #define FMT(NAME, METHOD, IMIN, OMIN, IO, CATEGORY) output_##METHOD,
108 #include "format.def"
109     };
110
111   assert (fmt_check_output (format));
112
113   converters[format->type] (input, format, output);
114 }
115
116 /* Returns the current output integer format. */
117 enum integer_format
118 data_out_get_integer_format (void) 
119 {
120   return output_integer_format;
121 }
122
123 /* Sets the output integer format to INTEGER_FORMAT. */
124 void
125 data_out_set_integer_format (enum integer_format integer_format) 
126 {
127   output_integer_format = integer_format;
128 }
129
130 /* Returns the current output float format. */
131 enum float_format
132 data_out_get_float_format (void) 
133 {
134   return output_float_format;
135 }
136
137 /* Sets the output float format to FLOAT_FORMAT. */
138 void
139 data_out_set_float_format (enum float_format float_format) 
140 {
141   output_float_format = float_format;
142 }
143 \f
144 /* Main conversion functions. */
145
146 /* Outputs F, COMMA, DOT, DOLLAR, PCT, E, CCA, CCB, CCC, CCD, and
147    CCE formats. */
148 static void
149 output_number (const union value *input, const struct fmt_spec *format,
150                char *output)
151 {
152   double number = input->f;
153
154   if (number == SYSMIS)
155     output_missing (format, output);
156   else if (!gsl_finite (number))
157     output_infinite (number, format, output);
158   else 
159     {
160       if (format->type != FMT_E && fabs (number) < 1.5 * power10 (format->w)) 
161         {
162           struct rounder r;
163           rounder_init (&r, number, format->d);
164
165           if (output_decimal (&r, format, true, output)
166               || output_scientific (number, format, true, output)
167               || output_decimal (&r, format, false, output))
168             return;
169         }
170
171       if (!output_scientific (number, format, false, output))
172         output_overflow (format, output);
173     }
174 }
175
176 /* Outputs N format. */
177 static void
178 output_N (const union value *input, const struct fmt_spec *format,
179           char *output)
180 {
181   double number = input->f * power10 (format->d);
182   if (input->f == SYSMIS || number < 0)
183     output_missing (format, output);
184   else
185     {
186       char buf[128];
187       number = fabs (round (number));
188       if (number < power10 (format->w)
189           && sprintf (buf, "%0*.0f", format->w, number) == format->w)
190         memcpy (output, buf, format->w);
191       else
192         output_overflow (format, output);
193     }
194 }
195
196 /* Outputs Z format. */
197 static void
198 output_Z (const union value *input, const struct fmt_spec *format,
199           char *output)
200 {
201   double number = input->f * power10 (format->d);
202   char buf[128];
203   if (input->f == SYSMIS)
204     output_missing (format, output);
205   else if (fabs (number) >= power10 (format->w)
206            || sprintf (buf, "%0*.0f", format->w,
207                        fabs (round (number))) != format->w)
208     output_overflow (format, output);
209   else
210     {
211       if (number < 0 && strspn (buf, "0") < format->w) 
212         {
213           char *p = &buf[format->w - 1];
214           *p = "}JKLMNOPQR"[*p - '0'];
215         }
216       memcpy (output, buf, format->w); 
217     }
218 }
219
220 /* Outputs P format. */
221 static void
222 output_P (const union value *input, const struct fmt_spec *format,
223           char *output)
224 {
225   if (output_bcd_integer (fabs (input->f * power10 (format->d)),
226                           format->w * 2 - 1, output)
227       && input->f < 0.0)
228     output[format->w - 1] |= 0xd;
229   else
230     output[format->w - 1] |= 0xf;
231 }
232
233 /* Outputs PK format. */
234 static void
235 output_PK (const union value *input, const struct fmt_spec *format,
236            char *output)
237 {
238   output_bcd_integer (input->f * power10 (format->d), format->w * 2, output);
239 }
240
241 /* Outputs IB format. */
242 static void
243 output_IB (const union value *input, const struct fmt_spec *format,
244            char *output)
245 {
246   double number = round (input->f * power10 (format->d));
247   if (input->f == SYSMIS
248       || number >= power256 (format->w) / 2 - 1
249       || number < -power256 (format->w) / 2)
250     memset (output, 0, format->w);
251   else
252     {
253       uint64_t integer = fabs (number);
254       if (number < 0)
255         integer = -integer;
256       output_binary_integer (integer, format->w, output_integer_format,
257                              output);
258     }
259 }
260
261 /* Outputs PIB format. */
262 static void
263 output_PIB (const union value *input, const struct fmt_spec *format,
264             char *output)
265 {
266   double number = round (input->f * power10 (format->d));
267   if (input->f == SYSMIS
268       || number < 0 || number >= power256 (format->w))
269     memset (output, 0, format->w);
270   else
271     output_binary_integer (number, format->w, output_integer_format, output);
272 }
273
274 /* Outputs PIBHEX format. */
275 static void
276 output_PIBHEX (const union value *input, const struct fmt_spec *format,
277                char *output)
278 {
279   double number = round (input->f);
280   if (input->f == SYSMIS)
281     output_missing (format, output);
282   else if (input->f < 0 || number >= power256 (format->w / 2))
283     output_overflow (format, output);
284   else
285     {
286       char tmp[8];
287       output_binary_integer (number, format->w / 2, INTEGER_MSB_FIRST, tmp);
288       output_hex (tmp, format->w / 2, output);
289     }
290 }
291
292 /* Outputs RB format. */
293 static void
294 output_RB (const union value *input, const struct fmt_spec *format,
295            char *output)
296 {
297   double d = input->f;
298   memcpy (output, &d, format->w);
299 }
300
301 /* Outputs RBHEX format. */
302 static void
303 output_RBHEX (const union value *input, const struct fmt_spec *format,
304               char *output)
305 {
306   double d = input->f;
307   output_hex (&d, format->w / 2, output);
308 }
309
310 /* Outputs DATE, ADATE, EDATE, JDATE, SDATE, QYR, MOYR, WKYR,
311    DATETIME, TIME, and DTIME formats. */
312 static void
313 output_date (const union value *input, const struct fmt_spec *format,
314              char *output)
315 {
316   double number = input->f;
317   int year, month, day, yday;
318
319   const char *template = fmt_date_template (format->type);
320   size_t template_width = strlen (template);
321   int excess_width = format->w - template_width;
322
323   char tmp[64];
324   char *p = tmp;
325
326   assert (format->w >= template_width);
327   if (number == SYSMIS)
328     goto missing;
329
330   if (fmt_get_category (format->type) == FMT_CAT_DATE) 
331     {
332       if (number <= 0)
333         goto missing;
334       calendar_offset_to_gregorian (number / 60. / 60. / 24.,
335                                     &year, &month, &day, &yday);
336       number = fmod (number, 60. * 60. * 24.);
337     }
338   else
339     year = month = day = yday = 0;
340
341   while (*template != '\0')
342     {
343       int ch = *template;
344       int count = 1;
345       while (template[count] == ch) 
346         count++;
347       template += count;
348       
349       switch (ch)
350         {
351         case 'd':
352           if (count < 3)
353             p += sprintf (p, "%02d", day);
354           else
355             p += sprintf (p, "%03d", yday);
356           break;
357         case 'm':
358           if (count < 3)
359             p += sprintf (p, "%02d", month);
360           else
361             {
362               static const char *months[12] =
363                 {
364                   "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
365                   "JUL", "AUG", "SEP", "OCT", "NOV", "DEC",
366                 };
367               p = stpcpy (p, months[month - 1]); 
368             }
369           break;
370         case 'y':
371           if (count >= 4 || excess_width >= 2) 
372             {
373               if (year <= 9999)
374                 p += sprintf (p, "%04d", year);
375               else if (format->type == FMT_DATETIME)
376                 p = stpcpy (p, "****");
377               else
378                 goto overflow; 
379             }
380           else 
381             {
382               int offset = year - get_epoch ();
383               if (offset < 0 || offset > 99)
384                 goto overflow;
385               p += sprintf (p, "%02d", abs (year) % 100); 
386             }
387           break;
388         case 'q':
389           p += sprintf (p, "%d", (month - 1) / 3 + 1);
390           break;
391         case 'w':
392           p += sprintf (p, "%2d", (yday - 1) / 7 + 1);
393           break;
394         case 'D':
395           if (number < 0) 
396             *p++ = '-';
397           number = fabs (number); 
398           p += sprintf (p, "%*.0f", count, floor (number / 60. / 60. / 24.));
399           number = fmod (number, 60. * 60. * 24.);
400           break;
401         case 'H':
402           if (number < 0)
403             *p++ = '-';
404           number = fabs (number); 
405           p += sprintf (p, "%0*.0f", count, floor (number / 60. / 60.));
406           number = fmod (number, 60. * 60.);
407           break;
408         case 'M':
409           p += sprintf (p, "%02d", (int) floor (number / 60.));
410           number = fmod (number, 60.);
411           excess_width = format->w - (p - tmp);
412           if (excess_width < 0) 
413             goto overflow;
414           if (excess_width == 3 || excess_width == 4
415               || (excess_width >= 5 && format->d == 0))
416             p += sprintf (p, ":%02d", (int) number);
417           else if (excess_width >= 5)
418             {
419               int d = MIN (format->d, excess_width - 4);
420               int w = d + 3;
421               sprintf (p, ":%0*.*f", w, d, number);
422               if (fmt_decimal_char (FMT_F) != '.') 
423                 {
424                   char *cp = strchr (p, '.');
425                   if (cp != NULL)
426                     *cp = fmt_decimal_char (FMT_F);
427                 }
428               p += strlen (p);
429             }
430           break;
431         case 'X':
432           *p++ = ' ';
433           break;
434         default:
435           assert (count == 1);
436           *p++ = ch;
437           break; 
438         }
439     }
440
441   buf_copy_lpad (output, format->w, tmp, p - tmp);
442   return;
443
444  overflow:
445   output_overflow (format, output);
446   return;
447
448  missing:
449   output_missing (format, output);
450   return;
451 }
452
453 /* Outputs WKDAY format. */
454 static void
455 output_WKDAY (const union value *input, const struct fmt_spec *format,
456               char *output)
457 {
458   static const char *weekdays[7] =
459     {
460       "SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY",
461       "THURSDAY", "FRIDAY", "SATURDAY",
462     };
463
464   if (input->f >= 1 && input->f < 8)
465     buf_copy_str_rpad (output, format->w, weekdays[(int) input->f - 1]);
466   else
467     {
468       if (input->f != SYSMIS)
469         msg (ME, _("Weekday number %f is not between 1 and 7."), input->f);
470       output_missing (format, output);
471     }
472 }
473
474 /* Outputs MONTH format. */
475 static void
476 output_MONTH (const union value *input, const struct fmt_spec *format,
477               char *output)
478 {
479   static const char *months[12] =
480     {
481       "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
482       "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER",
483     };
484
485   if (input->f >= 1 && input->f < 13)
486     buf_copy_str_rpad (output, format->w, months[(int) input->f - 1]);
487   else
488     {
489       if (input->f != SYSMIS)
490         msg (ME, _("Month number %f is not between 1 and 12."), input->f);
491       output_missing (format, output);
492     }
493 }
494
495 /* Outputs A format. */
496 static void
497 output_A (const union value *input, const struct fmt_spec *format,
498           char *output)
499 {
500   memcpy (output, input->s, format->w);
501 }
502
503 /* Outputs AHEX format. */
504 static void
505 output_AHEX (const union value *input, const struct fmt_spec *format,
506              char *output)
507 {
508   output_hex (input->s, format->w, output);
509 }
510 \f
511 /* Decimal and scientific formatting. */
512
513 /* If REQUEST plus the current *WIDTH fits within MAX_WIDTH,
514    increments *WIDTH by REQUEST and return true.
515    Otherwise returns false without changing *WIDTH. */
516 static bool
517 allocate_space (int request, int max_width, int *width)
518 {
519   assert (*width <= max_width);
520   if (request + *width <= max_width) 
521     {
522       *width += request;
523       return true;
524     }
525   else
526     return false;
527 }
528
529 /* Tries to compose the number represented by R, in the style of
530    FORMAT, into OUTPUT.  Returns true if successful, false on
531    failure, which occurs if FORMAT's width is too narrow.  If
532    REQUIRE_AFFIXES is true, then the prefix and suffix specified
533    by FORMAT's style must be included; otherwise, they may be
534    omitted to make the number fit. */
535 static bool
536 output_decimal (const struct rounder *r, const struct fmt_spec *format,
537                 bool require_affixes, char *output)
538 {
539   const struct fmt_number_style *style = fmt_get_style (format->type);
540   int decimals;
541
542   for (decimals = format->d; decimals >= 0; decimals--) 
543     {
544       /* Formatted version of magnitude of NUMBER. */
545       char magnitude[64];
546
547       /* Number of digits in MAGNITUDE's integer and fractional parts. */
548       int integer_digits;
549
550       /* Amount of space within the field width already claimed.
551          Initially this is the width of MAGNITUDE, then it is reduced
552          in stages as space is allocated to prefixes and suffixes and
553          grouping characters. */
554       int width;
555
556       /* Include various decorations? */
557       bool add_neg_prefix;
558       bool add_affixes;
559       bool add_grouping;
560
561       /* Position in output. */
562       char *p;
563
564       /* Make sure there's room for the number's magnitude, plus
565          the negative suffix, plus (if negative) the negative
566          prefix. */
567       width = rounder_width (r, decimals, &integer_digits, &add_neg_prefix);
568       width += ss_length (style->neg_suffix);
569       if (add_neg_prefix)
570         width += ss_length (style->neg_prefix);
571       if (width > format->w)
572         continue;
573
574       /* If there's room for the prefix and suffix, allocate
575          space.  If the affixes are required, but there's no
576          space, give up. */
577       add_affixes = allocate_space (fmt_affix_width (style),
578                                     format->w, &width);
579       if (!add_affixes && require_affixes)
580         continue;
581       
582       /* Check whether we should include grouping characters.
583          We need room for a complete set or we don't insert any at all.
584          We don't include grouping characters if decimal places were
585          requested but they were all dropped. */
586       add_grouping = (style->grouping != 0
587                       && integer_digits > 3
588                       && (format->d == 0 || decimals > 0)
589                       && allocate_space ((integer_digits - 1) / 3,
590                                          format->w, &width));
591
592       /* Format the number's magnitude. */
593       rounder_format (r, decimals, magnitude);
594   
595       /* Assemble number. */
596       p = output;
597       if (format->w > width)
598         p = mempset (p, ' ', format->w - width);
599       if (add_neg_prefix)
600         p = mempcpy (p, ss_data (style->neg_prefix),
601                      ss_length (style->neg_prefix));
602       if (add_affixes)
603         p = mempcpy (p, ss_data (style->prefix), ss_length (style->prefix));
604       if (!add_grouping)
605         p = mempcpy (p, magnitude, integer_digits);
606       else
607         {
608           int i;
609           for (i = 0; i < integer_digits; i++)
610             {
611               if (i > 0 && (integer_digits - i) % 3 == 0)
612                 *p++ = style->grouping;
613               *p++ = magnitude[i];
614             } 
615         }
616       if (decimals > 0)
617         {
618           *p++ = style->decimal;
619           p = mempcpy (p, &magnitude[integer_digits + 1], decimals);
620         }
621       if (add_affixes)
622         p = mempcpy (p, ss_data (style->suffix), ss_length (style->suffix));
623       if (add_neg_prefix)
624         p = mempcpy (p, ss_data (style->neg_suffix),
625                      ss_length (style->neg_suffix));
626       else
627         p = mempset (p, ' ', ss_length (style->neg_suffix));
628       assert (p == output + format->w);
629
630       return true;
631     }
632   return false;
633 }
634
635 /* Formats NUMBER into OUTPUT in scientific notation according to
636    the style of the format specified in FORMAT. */
637 static bool
638 output_scientific (double number, const struct fmt_spec *format,
639                    bool require_affixes, char *output)
640 {
641   const struct fmt_number_style *style = fmt_get_style (format->type);
642   int width;
643   int fraction_width;
644   bool add_affixes;
645   char buf[64], *p;
646
647   /* Allocate minimum required space. */
648   width = 6 + ss_length (style->neg_suffix);
649   if (number < 0)
650     width += ss_length (style->neg_prefix);
651   if (width > format->w)
652     return false;
653
654   /* Check for room for prefix and suffix. */
655   add_affixes = allocate_space (fmt_affix_width (style), format->w, &width);
656   if (require_affixes && !add_affixes)
657     return false;
658
659   /* Figure out number of characters we can use for the fraction,
660      if any.  (If that turns out to be 1, then we'll output a
661      decimal point without any digits following; that's what the
662      # flag does in the call to sprintf, below.) */
663   fraction_width = MIN (MIN (format->d + 1, format->w - width), 16);
664   if (format->type != FMT_E && fraction_width == 1)
665     fraction_width = 0; 
666   width += fraction_width;
667
668   /* Format (except suffix). */
669   p = buf;
670   if (width < format->w)
671     p = mempset (p, ' ', format->w - width);
672   if (number < 0)
673     p = mempcpy (p, ss_data (style->neg_prefix),
674                  ss_length (style->neg_prefix));
675   if (add_affixes)
676     p = mempcpy (p, ss_data (style->prefix), ss_length (style->prefix));
677   if (fraction_width > 0)
678     sprintf (p, "%#.*E", fraction_width - 1, fabs (number));
679   else
680     sprintf (p, "%.0E", fabs (number));
681
682   /* The C locale always uses a period `.' as a decimal point.
683      Translate to comma if necessary. */
684   if (style->decimal != '.')
685     {
686       char *cp = strchr (p, '.');
687       if (cp != NULL)
688         *cp = style->decimal;
689     }
690
691   /* Make exponent have exactly three digits, plus sign. */
692   {
693     char *cp = strchr (p, 'E') + 1;
694     long int exponent = strtol (cp, NULL, 10);
695     if (abs (exponent) > 999) 
696       return false;
697     sprintf (cp, "%+04ld", exponent);
698   }
699
700   /* Add suffixes. */
701   p = strchr (p, '\0');
702   if (add_affixes)
703     p = mempcpy (p, ss_data (style->suffix), ss_length (style->suffix));
704   if (number < 0)
705     p = mempcpy (p, ss_data (style->neg_suffix),
706                  ss_length (style->neg_suffix));
707   else
708     p = mempset (p, ' ', ss_length (style->neg_suffix));
709
710   assert (p == buf + format->w);
711   memcpy (output, buf, format->w);
712
713   return true;
714 }
715 \f
716 #ifndef HAVE_ROUND
717 /* Return X rounded to the nearest integer,
718    rounding ties away from zero. */
719 static double
720 round (double x) 
721 {
722   return x >= 0.0 ? floor (x + .5) : ceil (x - .5);
723 }
724 #endif /* !HAVE_ROUND */
725
726 /* Returns true if the magnitude represented by R should be
727    rounded up when chopped off at DECIMALS decimal places, false
728    if it should be rounded down. */
729 static bool
730 should_round_up (const struct rounder *r, int decimals) 
731 {
732   int digit = r->string[r->integer_digits + decimals + 1];
733   assert (digit >= '0' && digit <= '9');
734   return digit >= '5';
735 }
736
737 /* Initializes R for formatting the magnitude of NUMBER to no
738    more than MAX_DECIMAL decimal places. */
739 static void
740 rounder_init (struct rounder *r, double number, int max_decimals)
741 {
742   assert (fabs (number) < 1e41);
743   assert (max_decimals >= 0 && max_decimals <= 16);
744   if (max_decimals == 0) 
745     {
746       /* Fast path.  No rounding needed.
747
748          We append ".00" to the integer representation because
749          round_up assumes that fractional digits are present.  */
750       sprintf (r->string, "%.0f.00", fabs (round (number)));
751     }
752   else 
753     {
754       /* Slow path.
755          
756          This is more difficult than it really should be because
757          we have to make sure that numbers that are exactly
758          halfway between two representations are always rounded
759          away from zero.  This is not what sprintf normally does
760          (usually it rounds to even), so we have to fake it as
761          best we can, by formatting with extra precision and then
762          doing the rounding ourselves.
763      
764          We take up to two rounds to format numbers.  In the
765          first round, we obtain 2 digits of precision beyond
766          those requested by the user.  If those digits are
767          exactly "50", then in a second round we format with as
768          many digits as are significant in a "double".
769      
770          It might be better to directly implement our own
771          floating-point formatting routine instead of relying on
772          the system's sprintf implementation.  But the classic
773          Steele and White paper on printing floating-point
774          numbers does not hint how to do what we want, and it's
775          not obvious how to change their algorithms to do so.  It
776          would also be a lot of work. */
777       sprintf (r->string, "%.*f", max_decimals + 2, fabs (number));
778       if (!strcmp (r->string + strlen (r->string) - 2, "50"))
779         {
780           int binary_exponent, decimal_exponent, format_decimals;
781           frexp (number, &binary_exponent);
782           decimal_exponent = binary_exponent * 3 / 10;
783           format_decimals = (DBL_DIG + 1) - decimal_exponent;
784           if (format_decimals > max_decimals + 2)
785             sprintf (r->string, "%.*f", format_decimals, fabs (number));
786         }
787     }
788   
789   if (r->string[0] == '0') 
790     memmove (r->string, &r->string[1], strlen (r->string));
791
792   r->leading_zeros = strspn (r->string, "0.");
793   r->leading_nines = strspn (r->string, "9.");
794   r->integer_digits = strchr (r->string, '.') - r->string;
795   r->negative = number < 0;
796 }
797
798 /* Returns the number of characters required to format the
799    magnitude represented by R to DECIMALS decimal places.
800    The return value includes integer digits and a decimal point
801    and fractional digits, if any, but it does not include any
802    negative prefix or suffix or other affixes.
803
804    *INTEGER_DIGITS is set to the number of digits before the
805    decimal point in the output, between 0 and 40.
806
807    If R represents a negative number and its rounded
808    representation would include at least one nonzero digit,
809    *NEGATIVE is set to true; otherwise, it is set to false. */
810 static int
811 rounder_width (const struct rounder *r, int decimals,
812                int *integer_digits, bool *negative) 
813 {
814   /* Calculate base measures. */
815   int width = r->integer_digits;
816   if (decimals > 0)
817     width += decimals + 1;
818   *integer_digits = r->integer_digits;
819   *negative = r->negative;
820
821   /* Rounding can cause adjustments. */
822   if (should_round_up (r, decimals))
823     {
824       /* Rounding up leading 9s adds a new digit (a 1). */
825       if (r->leading_nines >= width) 
826         {
827           width++;
828           ++*integer_digits; 
829         }
830     }
831   else
832     {
833       /* Rounding down. */
834       if (r->leading_zeros >= width) 
835         {
836           /* All digits that remain after rounding are zeros.
837              Therefore we drop the negative sign. */
838           *negative = false;
839           if (r->integer_digits == 0 && decimals == 0) 
840             {
841               /* No digits at all are left.  We need to display
842                  at least a single digit (a zero). */
843               assert (width == 0);
844               width++;
845               *integer_digits = 1;
846             } 
847         }
848     }
849   return width;
850 }
851
852 /* Formats the magnitude represented by R into OUTPUT, rounding
853    to DECIMALS decimal places.  Exactly as many characters as
854    indicated by rounder_width are written.  No terminating null
855    is appended. */
856 static void
857 rounder_format (const struct rounder *r, int decimals, char *output) 
858 {
859   int base_width = r->integer_digits + (decimals > 0 ? decimals + 1 : 0);
860   if (should_round_up (r, decimals)) 
861     {
862       if (r->leading_nines < base_width) 
863         {
864           /* Rounding up.  This is the common case where rounding
865              up doesn't add an extra digit. */
866           char *p;
867           memcpy (output, r->string, base_width);
868           for (p = output + base_width - 1; ; p--) 
869             {
870               assert (p >= output);
871               if (*p == '9')
872                 *p = '0';
873               else if (*p >= '0' && *p <= '8') 
874                 {
875                   (*p)++;
876                   break;
877                 }
878               else
879                 assert (*p == '.');
880             }
881         }
882       else 
883         {
884           /* Rounding up leading 9s causes the result to be a 1
885              followed by a number of 0s, plus a decimal point. */
886           char *p = output;
887           *p++ = '1';
888           p = mempset (p, '0', r->integer_digits);
889           if (decimals > 0) 
890             {
891               *p++ = '.';
892               p = mempset (p, '0', decimals);
893             }
894           assert (p == output + base_width + 1);
895         }
896     }
897   else 
898     {
899       /* Rounding down. */
900       if (r->integer_digits != 0 || decimals != 0) 
901         {
902           /* Common case: just copy the digits. */
903           memcpy (output, r->string, base_width); 
904         }
905       else 
906         {
907           /* No digits remain.  The output is just a zero. */
908           output[0] = '0'; 
909         }
910     }
911 }
912 \f
913 /* Helper functions. */
914
915 /* Returns 10**X. */
916 static double PURE_FUNCTION
917 power10 (int x) 
918 {
919   static const double p[] =
920     {
921       1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
922       1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
923       1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29,
924       1e30, 1e31, 1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38, 1e39,
925       1e40,
926     };
927   return x >= 0 && x < sizeof p / sizeof *p ? p[x] : pow (10.0, x);
928 }
929
930 /* Returns 256**X. */
931 static double PURE_FUNCTION
932 power256 (int x) 
933 {
934   static const double p[] = 
935     {
936       1.0,
937       256.0,
938       65536.0,
939       16777216.0,
940       4294967296.0,
941       1099511627776.0,
942       281474976710656.0,
943       72057594037927936.0,
944       18446744073709551616.0
945     };
946   return x >= 0 && x < sizeof p / sizeof *p ? p[x] : pow (256.0, x);
947 }
948
949 /* Formats non-finite NUMBER into OUTPUT according to the width
950    given in FORMAT. */
951 static void
952 output_infinite (double number, const struct fmt_spec *format, char *output)
953 {
954   assert (!gsl_finite (number));
955   
956   if (format->w >= 3)
957     {
958       const char *s;
959
960       if (isnan (number))
961         s = "NaN";
962       else if (isinf (number))
963         s = number > 0 ? "+Infinity" : "-Infinity";
964       else
965         s = "Unknown";
966
967       buf_copy_str_lpad (output, format->w, s);
968     }
969   else 
970     output_overflow (format, output);
971 }
972
973 /* Formats OUTPUT as a missing value for the given FORMAT. */
974 static void
975 output_missing (const struct fmt_spec *format, char *output)
976 {
977   memset (output, ' ', format->w);
978
979   if (format->type != FMT_N) 
980     {
981       int dot_ofs = (format->type == FMT_PCT ? 2
982                      : format->type == FMT_E ? 5
983                      : 1);
984       output[MAX (0, format->w - format->d - dot_ofs)] = '.'; 
985     }
986   else
987     output[format->w - 1] = '.';
988 }
989
990 /* Formats OUTPUT for overflow given FORMAT. */
991 static void
992 output_overflow (const struct fmt_spec *format, char *output) 
993 {
994   memset (output, '*', format->w);
995 }
996
997 /* Converts the integer part of NUMBER to a packed BCD number
998    with the given number of DIGITS in OUTPUT.  If DIGITS is odd,
999    the least significant nibble of the final byte in OUTPUT is
1000    set to 0.  Returns true if successful, false if NUMBER is not
1001    representable.  On failure, OUTPUT is cleared to all zero
1002    bytes. */
1003 static bool
1004 output_bcd_integer (double number, int digits, char *output) 
1005 {
1006   char decimal[64];
1007
1008   assert (digits < sizeof decimal);
1009   if (number != SYSMIS
1010       && number >= 0.
1011       && number < power10 (digits)
1012       && sprintf (decimal, "%0*.0f", digits, round (number)) == digits)
1013     {
1014       const char *src = decimal;
1015       int i;
1016
1017       for (i = 0; i < digits / 2; i++) 
1018         {
1019           int d0 = *src++ - '0';
1020           int d1 = *src++ - '0';
1021           *output++ = (d0 << 4) + d1; 
1022         }
1023       if (digits % 2)
1024         *output = (*src - '0') << 4;
1025       
1026       return true;
1027     }
1028   else 
1029     {
1030       memset (output, 0, DIV_RND_UP (digits, 2));
1031       return false; 
1032     }
1033 }
1034
1035 /* Writes VALUE to OUTPUT as a BYTES-byte binary integer of the
1036    given INTEGER_FORMAT. */
1037 static void
1038 output_binary_integer (uint64_t value, int bytes,
1039                        enum integer_format integer_format, char *output) 
1040 {
1041   integer_put (value, integer_format, output, bytes);
1042 }
1043
1044 /* Converts the BYTES bytes in DATA to twice as many hexadecimal
1045    digits in OUTPUT. */
1046 static void
1047 output_hex (const void *data_, size_t bytes, char *output) 
1048 {
1049   const uint8_t *data = data_;
1050   size_t i;
1051
1052   for (i = 0; i < bytes; i++)
1053     {
1054       static const char hex_digits[] = "0123456789ABCDEF";
1055       *output++ = hex_digits[data[i] >> 4];
1056       *output++ = hex_digits[data[i] & 15];
1057     }
1058 }