wrf.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* $Id$ */
  2. /*
  3. * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. *
  5. * This product is part of the Amsterdam Compiler Kit.
  6. *
  7. * Permission to use, sell, duplicate or disclose this software must be
  8. * obtained in writing. Requests for such permissions may be sent to
  9. *
  10. * Dr. Andrew S. Tanenbaum
  11. * Wiskundig Seminarium
  12. * Vrije Universiteit
  13. * Postbox 7161
  14. * 1007 MC Amsterdam
  15. * The Netherlands
  16. *
  17. */
  18. /* Author: J.W. Stevenson */
  19. #include <pc_err.h>
  20. #include <pc_file.h>
  21. extern _wstrin();
  22. extern char *_fcvt();
  23. #define assert(x) /* nothing */
  24. #if __STDC__
  25. #include <float.h>
  26. #define HUGE_DIG DBL_MAX_10_EXP /* log10(maxreal) */
  27. #else
  28. #define HUGE_DIG 400 /* log10(maxreal) */
  29. #endif
  30. #define PREC_DIG 80 /* the maximum digits returned by _fcvt() */
  31. #define FILL_CHAR '0' /* char printed if all of _fcvt() used */
  32. #define BUFSIZE HUGE_DIG + PREC_DIG + 3
  33. _wrf(n,w,r,f) int n,w; double r; struct file *f; {
  34. char *p,*b; int s,d; char buf[BUFSIZE];
  35. if ( n < 0 || w < 0) _trp(EWIDTH);
  36. p = buf;
  37. if (n > PREC_DIG)
  38. n = PREC_DIG;
  39. b = _fcvt(r,n,&d,&s);
  40. assert(abs(d) <= HUGE_DIG);
  41. if (s)
  42. *p++ = '-';
  43. if (d<=0)
  44. *p++ = '0';
  45. else
  46. do
  47. *p++ = (*b ? *b++ : FILL_CHAR);
  48. while (--d > 0);
  49. if (n > 0)
  50. *p++ = '.';
  51. while (++d <= 0) {
  52. if (--n < 0)
  53. break;
  54. *p++ = '0';
  55. }
  56. while (--n >= 0) {
  57. *p++ = (*b ? *b++ : FILL_CHAR);
  58. assert(p <= buf+BUFSIZE);
  59. }
  60. _wstrin(w,(int)(p-buf),buf,f);
  61. }