ftoastr.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* floating point to accurate string
  2. Copyright (C) 2010-2011 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* Written by Paul Eggert. */
  14. /* This code can misbehave on some buggy or older platforms, when
  15. operating on arguments on floating types other than 'double', or
  16. when given unusual combinations of options. Gnulib's
  17. snprintf-posix module works around many of these problems.
  18. This code relies on sprintf, strtod, etc. operating accurately;
  19. otherwise, the resulting strings could be inaccurate or too long. */
  20. #include <config.h>
  21. #include "ftoastr.h"
  22. #include "intprops.h"
  23. #include <float.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #if LENGTH == 3
  27. # define FLOAT long double
  28. # define FLOAT_DIG LDBL_DIG
  29. # define FLOAT_MIN LDBL_MIN
  30. # define FLOAT_PREC_BOUND _GL_LDBL_PREC_BOUND
  31. # define FTOASTR ldtoastr
  32. # if HAVE_C99_STRTOLD
  33. # define STRTOF strtold
  34. # endif
  35. #elif LENGTH == 2
  36. # define FLOAT double
  37. # define FLOAT_DIG DBL_DIG
  38. # define FLOAT_MIN DBL_MIN
  39. # define FLOAT_PREC_BOUND _GL_DBL_PREC_BOUND
  40. # define FTOASTR dtoastr
  41. #else
  42. # define LENGTH 1
  43. # define FLOAT float
  44. # define FLOAT_DIG FLT_DIG
  45. # define FLOAT_MIN FLT_MIN
  46. # define FLOAT_PREC_BOUND _GL_FLT_PREC_BOUND
  47. # define FTOASTR ftoastr
  48. # if HAVE_STRTOF
  49. # define STRTOF strtof
  50. # endif
  51. #endif
  52. /* On pre-C99 hosts, approximate strtof and strtold with strtod. This
  53. may generate one or two extra digits, but that's better than not
  54. working at all. */
  55. #ifndef STRTOF
  56. # define STRTOF strtod
  57. #endif
  58. /* On hosts where it's not known that snprintf works, use sprintf to
  59. implement the subset needed here. Typically BUFSIZE is big enough
  60. and there's little or no performance hit. */
  61. #if ! GNULIB_SNPRINTF
  62. # undef snprintf
  63. # define snprintf ftoastr_snprintf
  64. static int
  65. ftoastr_snprintf (char *buf, size_t bufsize, char const *format,
  66. int width, int prec, FLOAT x)
  67. {
  68. char width_0_buffer[LENGTH == 1 ? FLT_BUFSIZE_BOUND
  69. : LENGTH == 2 ? DBL_BUFSIZE_BOUND
  70. : LDBL_BUFSIZE_BOUND];
  71. int n = width;
  72. if (bufsize < sizeof width_0_buffer)
  73. {
  74. n = sprintf (width_0_buffer, format, 0, prec, x);
  75. if (n < 0)
  76. return n;
  77. if (n < width)
  78. n = width;
  79. }
  80. if (n < bufsize)
  81. n = sprintf (buf, format, width, prec, x);
  82. return n;
  83. }
  84. #endif
  85. int
  86. FTOASTR (char *buf, size_t bufsize, int flags, int width, FLOAT x)
  87. {
  88. /* The following method is simple but slow.
  89. For ideas about speeding things up, please see:
  90. Florian Loitsch, Printing floating-point numbers quickly and accurately
  91. with integers. ACM SIGPLAN notices 46, 6 (June 2010), 233-243
  92. <http://dx.doi.org/10.1145/1809028.1806623>; also see the
  93. 2010-03-21 draft <http://florian.loitsch.com/tmp/article.pdf>. */
  94. char format[sizeof "%-+ 0*.*Lg"];
  95. FLOAT abs_x = x < 0 ? -x : x;
  96. int prec;
  97. char *p = format;
  98. *p++ = '%';
  99. /* Support flags that generate output parsable by strtof. */
  100. *p = '-'; p += (flags & FTOASTR_LEFT_JUSTIFY ) != 0;
  101. *p = '+'; p += (flags & FTOASTR_ALWAYS_SIGNED ) != 0;
  102. *p = ' '; p += (flags & FTOASTR_SPACE_POSITIVE) != 0;
  103. *p = '0'; p += (flags & FTOASTR_ZERO_PAD ) != 0;
  104. *p++ = '*';
  105. *p++ = '.';
  106. *p++ = '*';
  107. *p = 'L'; p += 2 < LENGTH;
  108. *p++ = flags & FTOASTR_UPPER_E ? 'G' : 'g';
  109. *p = '\0';
  110. for (prec = abs_x < FLOAT_MIN ? 1 : FLOAT_DIG; ; prec++)
  111. {
  112. int n = snprintf (buf, bufsize, format, width, prec, x);
  113. if (n < 0
  114. || FLOAT_PREC_BOUND <= prec
  115. || (n < bufsize && STRTOF (buf, NULL) == x))
  116. return n;
  117. }
  118. }