Windows NT 4.0 source code leak
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.1 KiB

4 years ago
  1. /***
  2. *_fptostr.c - workhorse routine for converting floating point to string
  3. *
  4. * Copyright (c) 1985-1991, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Workhorse routine for fcvt, ecvt.
  8. *
  9. *Revision History:
  10. * 09-17-84 DFW created
  11. * 03-05-90 GJF Fixed calling type, added #include <cruntime.h>,
  12. * removed #include <register.h>, fixed copyright. Also,
  13. * cleaned up the formatting a bit.
  14. * 07-20-90 SBM Compiles cleanly with -W3 (added #include <string.h>)
  15. * 08-01-90 SBM Renamed <struct.h> to <fltintrn.h>
  16. * 09-27-90 GJF New-style function declarator.
  17. * 06-11-92 GDP Bug fix: Shorten string if leadig (overflow) digit is 1
  18. * 10-09-92 GDP Backed out last fix for ecvt (printf regressed)
  19. *
  20. *******************************************************************************/
  21. #include <cruntime.h>
  22. #include <string.h>
  23. #include <fltintrn.h>
  24. /***
  25. *void _fptostr(buf, digits, pflt) - workhorse floating point conversion
  26. *
  27. *Purpose:
  28. * This is the workhorse routine for fcvt, ecvt. Here is where
  29. * all the digits are put into a buffer and the rounding is
  30. * performed and indicators of the decimal point position are set. Note,
  31. * this must not change the mantissa field of pflt since routines which
  32. * use this routine rely on this being unchanged.
  33. *
  34. *Entry:
  35. * char *buf - the buffer in which the digits are to be put
  36. * int digits - the number of digits which are to go into the buffer
  37. * STRFLT pflt - a pointer to a structure containing information on the
  38. * floating point value, including a string containing the
  39. * non-zero significant digits of the mantissa.
  40. *
  41. *Exit:
  42. * Changes the contents of the buffer and also may increment the decpt
  43. * field of the structure pointer to by the 'pflt' parameter if overflow
  44. * occurs during rounding (e.g. 9.999999... gets rounded to 10.000...).
  45. *
  46. *Exceptions:
  47. *
  48. *******************************************************************************/
  49. void _CALLTYPE1 _fptostr (
  50. char *buf,
  51. REG4 int digits,
  52. REG3 STRFLT pflt
  53. )
  54. {
  55. REG1 char *pbuf = buf;
  56. REG2 char *mantissa = pflt->mantissa;
  57. /* initialize the first digit in the buffer to '0' (NOTE - NOT '\0')
  58. * and set the pointer to the second digit of the buffer. The first
  59. * digit is used to handle overflow on rounding (e.g. 9.9999...
  60. * becomes 10.000...) which requires a carry into the first digit.
  61. */
  62. *pbuf++ = '0';
  63. /* Copy the digits of the value into the buffer (with 0 padding)
  64. * and insert the terminating null character.
  65. */
  66. while (digits > 0) {
  67. *pbuf++ = (*mantissa) ? *mantissa++ : (char)'0';
  68. digits--;
  69. }
  70. *pbuf = '\0';
  71. /* do any rounding which may be needed. Note - if digits < 0 don't
  72. * do any rounding since in this case, the rounding occurs in a digit
  73. * which will not be output beause of the precision requested
  74. */
  75. if (digits >= 0 && *mantissa >= '5') {
  76. pbuf--;
  77. while (*pbuf == '9')
  78. *pbuf-- = '0';
  79. *pbuf += 1;
  80. }
  81. if (*buf == '1') {
  82. /* the rounding caused overflow into the leading digit (e.g.
  83. * 9.999.. went to 10.000...), so increment the decpt position
  84. * by 1
  85. */
  86. pflt->decpt++;
  87. }
  88. else {
  89. /* move the entire string to the left one digit to remove the
  90. * unused overflow digit.
  91. */
  92. strcpy(buf, buf+1);
  93. }
  94. }