home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / glibc-1.06 / sysdeps / ieee754 / printf_fp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-13  |  2.7 KB  |  125 lines

  1. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library 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. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <stdio.h>
  21. #include <math.h>
  22. #include <printf.h>
  23. #include "ieee754.h"
  24.  
  25. #define    outchar(x)                                  \
  26.   do                                          \
  27.     {                                          \
  28.       register CONST int outc = (x);                          \
  29.       if (putc (outc, s) == EOF)                          \
  30.     return -1;                                  \
  31.       else                                      \
  32.     ++done;                                      \
  33.     } while (0)
  34.  
  35. #ifndef    HANDLE_SPECIAL
  36.  
  37. #ifdef    __GNUC__
  38.  __inline
  39. #endif
  40. static int
  41. DEFUN(special_ieee754, (s, info, fpnum),
  42.       FILE *s AND CONST struct printf_info *info AND LONG_DOUBLE fpnum)
  43. {
  44.   int is_neg;
  45.   CONST char *string;
  46.  
  47.   if (__isnan ((double) fpnum))
  48.     {
  49.       string = "NaN";
  50.       is_neg = 0;
  51.     }
  52.   else if (__isinf ((double) fpnum))
  53.     {
  54.       string = "Inf";
  55.       is_neg = fpnum < 0;
  56.     }
  57.   else
  58.     return 0;
  59.  
  60.   {
  61.     size_t done = 0;
  62.     int width = info->prec > info->width ? info->prec : info->width;
  63.  
  64.     if (is_neg || info->showsign || info->space)
  65.       --width;
  66.     width -= 3;
  67.  
  68.     if (!info->left)
  69.       while (width-- > 0)
  70.     outchar (' ');
  71.  
  72.     if (is_neg)
  73.       outchar ('-');
  74.     else if (info->showsign)
  75.       outchar ('+');
  76.     else if (info->space)
  77.       outchar (' ');
  78.  
  79.     {
  80.       register size_t len = 3;
  81.       while (len-- > 0)
  82.     outchar (*string++);
  83.     }
  84.  
  85.     if (info->left)
  86.       while (width-- > 0)
  87.     outchar (' ');
  88.  
  89.     return done;
  90.   }
  91. }
  92.  
  93. #define    HANDLE_SPECIAL(done, s, info, fpnum)                      \
  94.   {                                          \
  95.     int more_done = special_ieee754 (s, info, fpnum);                  \
  96.     if (more_done == -1)                              \
  97.       return -1;                                  \
  98.     else if (more_done != 0)                              \
  99.       return done + more_done;                              \
  100.   }
  101.  
  102. #endif
  103.  
  104. #ifndef    IS_NEGATIVE
  105.  
  106. #ifdef    __GNUC__
  107. __inline
  108. #endif
  109. static int
  110. DEFUN(is_neg_ieee754, (num), register LONG_DOUBLE num)
  111. {
  112.   union ieee754_double u;
  113.  
  114.   u.d = (double) num;
  115.   return u.ieee.negative;
  116. }
  117.  
  118. #define    IS_NEGATIVE(num)    is_neg_ieee754 (num)
  119.  
  120. #endif
  121.  
  122. #undef    outchar
  123.  
  124. #include <../sysdeps/generic/printf_fp.c>
  125.