ftoastr.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* floating point to accurate string
  2. Copyright (C) 2010-2015 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 <float.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #if LENGTH == 3
  26. # define FLOAT long double
  27. # define FLOAT_DIG LDBL_DIG
  28. # define FLOAT_MIN LDBL_MIN
  29. # define FLOAT_PREC_BOUND _GL_LDBL_PREC_BOUND
  30. # define FTOASTR ldtoastr
  31. # if HAVE_C99_STRTOLD
  32. # define STRTOF strtold
  33. # endif
  34. #elif LENGTH == 2
  35. # define FLOAT double
  36. # define FLOAT_DIG DBL_DIG
  37. # define FLOAT_MIN DBL_MIN
  38. # define FLOAT_PREC_BOUND _GL_DBL_PREC_BOUND
  39. # define FTOASTR dtoastr
  40. #else
  41. # define LENGTH 1
  42. # define FLOAT float
  43. # define FLOAT_DIG FLT_DIG
  44. # define FLOAT_MIN FLT_MIN
  45. # define FLOAT_PREC_BOUND _GL_FLT_PREC_BOUND
  46. # define FTOASTR ftoastr
  47. # if HAVE_STRTOF
  48. # define STRTOF strtof
  49. # endif
  50. #endif
  51. /* On pre-C99 hosts, approximate strtof and strtold with strtod. This
  52. may generate one or two extra digits, but that's better than not
  53. working at all. */
  54. #ifndef STRTOF
  55. # define STRTOF strtod
  56. #endif
  57. /* On hosts where it's not known that snprintf works, use sprintf to
  58. implement the subset needed here. Typically BUFSIZE is big enough
  59. and there's little or no performance hit. */
  60. #if ! GNULIB_SNPRINTF
  61. # undef snprintf
  62. # define snprintf ftoastr_snprintf
  63. static int
  64. ftoastr_snprintf (char *buf, size_t bufsize, char const *format,
  65. int width, int prec, FLOAT x)
  66. {
  67. char width_0_buffer[LENGTH == 1 ? FLT_BUFSIZE_BOUND
  68. : LENGTH == 2 ? DBL_BUFSIZE_BOUND
  69. : LDBL_BUFSIZE_BOUND];
  70. int n = width;
  71. if (bufsize < sizeof width_0_buffer)
  72. {
  73. n = sprintf (width_0_buffer, format, 0, prec, x);
  74. if (n < 0)
  75. return n;
  76. if (n < width)
  77. n = width;
  78. }
  79. if (n < bufsize)
  80. n = sprintf (buf, format, width, prec, x);
  81. return n;
  82. }
  83. #endif
  84. int
  85. FTOASTR (char *buf, size_t bufsize, int flags, int width, FLOAT x)
  86. {
  87. /* The following method is simple but slow.
  88. For ideas about speeding things up, please see:
  89. Florian Loitsch, Printing floating-point numbers quickly and accurately
  90. with integers. ACM SIGPLAN notices 46, 6 (June 2010), 233-243
  91. <http://dx.doi.org/10.1145/1809028.1806623>; also see the
  92. 2010-03-21 draft <http://florian.loitsch.com/tmp/article.pdf>. */
  93. char format[sizeof "%-+ 0*.*Lg"];
  94. FLOAT abs_x = x < 0 ? -x : x;
  95. int prec;
  96. char *p = format;
  97. *p++ = '%';
  98. /* Support flags that generate output parsable by strtof. */
  99. *p = '-'; p += (flags & FTOASTR_LEFT_JUSTIFY ) != 0;
  100. *p = '+'; p += (flags & FTOASTR_ALWAYS_SIGNED ) != 0;
  101. *p = ' '; p += (flags & FTOASTR_SPACE_POSITIVE) != 0;
  102. *p = '0'; p += (flags & FTOASTR_ZERO_PAD ) != 0;
  103. *p++ = '*';
  104. *p++ = '.';
  105. *p++ = '*';
  106. *p = 'L'; p += 2 < LENGTH;
  107. *p++ = flags & FTOASTR_UPPER_E ? 'G' : 'g';
  108. *p = '\0';
  109. for (prec = abs_x < FLOAT_MIN ? 1 : FLOAT_DIG; ; prec++)
  110. {
  111. int n = snprintf (buf, bufsize, format, width, prec, x);
  112. if (n < 0
  113. || FLOAT_PREC_BOUND <= prec
  114. || (n < bufsize && STRTOF (buf, NULL) == x))
  115. return n;
  116. }
  117. }