ftoastr.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* floating point to accurate string
  2. Copyright (C) 2010-2017 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. #ifndef _GL_FTOASTR_H
  15. #include "intprops.h"
  16. #include <float.h>
  17. #include <stddef.h>
  18. /* Store into BUF (of size BUFSIZE) an accurate minimal-precision
  19. string representation of a floating point number. FLAGS affect the
  20. formatting of the number. Pad the output string with spaces as
  21. necessary to width WIDTH bytes, in the style of printf. WIDTH must
  22. be nonnegative. X is the floating-point number to be converted.
  23. Return the number of bytes stored into BUF, not counting the
  24. terminating null. However, do not overrun BUF: if BUF is too
  25. small, return a fairly tight (but not necessarily exact) upper
  26. bound on the value that would have been returned if BUF had been
  27. big enough. If SIZE is zero, BUF may be a null pointer. On error
  28. (e.g., returned value would exceed INT_MAX), return -1 and set
  29. errno.
  30. Example:
  31. char buf[DBL_BUFSIZE_BOUND];
  32. int r = dtoastr (buf, sizeof buf, 0, 0, 0.1);
  33. In the C locale, this sets R to 3 and stores "0.1" into BUF. */
  34. int ftoastr (char *buf, size_t bufsize, int flags, int width, float x);
  35. int dtoastr (char *buf, size_t bufsize, int flags, int width, double x);
  36. int ldtoastr (char *buf, size_t bufsize, int flags, int width, long double x);
  37. /* Flag values for ftoastr etc. These can be ORed together. */
  38. enum
  39. {
  40. /* Left justify within the width; the default is right justification. */
  41. FTOASTR_LEFT_JUSTIFY = 1,
  42. /* Output "+" before positive numbers; the default outputs nothing. */
  43. FTOASTR_ALWAYS_SIGNED = 2,
  44. /* Output " " before positive numbers; ignored if
  45. FTOASTR_ALWAYS_SIGNED is also given. */
  46. FTOASTR_SPACE_POSITIVE = 4,
  47. /* Pad with zeros instead of spaces; ignored if FTOASTR_LEFT_JUSTIFY
  48. is also given. */
  49. FTOASTR_ZERO_PAD = 8,
  50. /* Use 'E' instead of 'e' before the exponent. */
  51. FTOASTR_UPPER_E = 16
  52. };
  53. /* _GL_FLT_PREC_BOUND is an upper bound on the precision needed to
  54. represent a float value without losing information. Likewise for
  55. _GL_DBL_PREC_BOUND and double, and _GL_LDBL_PREC_BOUND and long double.
  56. These are macros, not enums, to work around a bug in IBM xlc 12.1. */
  57. #if FLT_RADIX == 10 /* decimal floating point */
  58. # define _GL_FLT_PREC_BOUND FLT_MANT_DIG
  59. # define _GL_DBL_PREC_BOUND DBL_MANT_DIG
  60. # define _GL_LDBL_PREC_BOUND LDBL_MANT_DIG
  61. #else
  62. /* An upper bound on the number of bits needed to represent a single
  63. digit in a floating-point fraction. */
  64. # if FLT_RADIX == 2 /* IEEE 754 floating point, VAX floating point, etc. */
  65. # define _GL_FLOAT_DIG_BITS_BOUND 1
  66. # elif FLT_RADIX <= 16 /* IBM hex floating point has FLT_RADIX == 16. */
  67. # define _GL_FLOAT_DIG_BITS_BOUND 4
  68. # else /* no machine is this bad, but let's be complete */
  69. # define _GL_FLOAT_DIG_BITS_BOUND ((int) TYPE_WIDTH (int) - 1)
  70. # endif
  71. /* An upper bound on the number of decimal digits needed to represent
  72. a floating point number accurately, assuming a fraction contains
  73. DIG digits. For why the "+ 1" is needed, see "Binary to Decimal
  74. Conversion" in David Goldberg's paper "What Every Computer
  75. Scientist Should Know About Floating-Point Arithmetic"
  76. <http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html>. */
  77. # define _GL_FLOAT_PREC_BOUND(dig) \
  78. (INT_BITS_STRLEN_BOUND ((dig) * _GL_FLOAT_DIG_BITS_BOUND) + 1)
  79. # define _GL_FLT_PREC_BOUND _GL_FLOAT_PREC_BOUND ( FLT_MANT_DIG)
  80. # define _GL_DBL_PREC_BOUND _GL_FLOAT_PREC_BOUND ( DBL_MANT_DIG)
  81. # define _GL_LDBL_PREC_BOUND _GL_FLOAT_PREC_BOUND (LDBL_MANT_DIG)
  82. #endif
  83. /* Bound on the number of bytes printed for an exponent in the range
  84. MIN..MAX, where MIN < 0 < MAX; printf always prints a sign and at
  85. least 2 digits. Although the maximum known exponent is 4932 for
  86. IEEE 754 binary128, support tight bounds for exponents up to a
  87. million, just in case. */
  88. #define _GL_FLOAT_EXPONENT_STRLEN_BOUND(min, max) \
  89. ( -100 < (min) && (max) < 100 ? 3 \
  90. : -1000 < (min) && (max) < 1000 ? 4 \
  91. : -10000 < (min) && (max) < 10000 ? 5 \
  92. : -100000 < (min) && (max) < 100000 ? 6 \
  93. : -1000000 < (min) && (max) < 1000000 ? 7 \
  94. : INT_STRLEN_BOUND (int) /* not a tight bound */)
  95. /* A reasonably tight bound on the length of a type-T floating value
  96. formatted with ftoastr etc. Room is needed for sign, fraction
  97. digits, decimal point, "e", and exponent. POINTLEN should be a
  98. reasonably tight bound on the string length of the decimal
  99. point. */
  100. #define _GL_FLOAT_STRLEN_BOUND_L(t, pointlen) \
  101. (1 + _GL_##t##_PREC_BOUND + pointlen + 1 \
  102. + _GL_FLOAT_EXPONENT_STRLEN_BOUND (t##_MIN_10_EXP, t##_MAX_10_EXP))
  103. #define FLT_STRLEN_BOUND_L(pointlen) _GL_FLOAT_STRLEN_BOUND_L ( FLT, pointlen)
  104. #define DBL_STRLEN_BOUND_L(pointlen) _GL_FLOAT_STRLEN_BOUND_L ( DBL, pointlen)
  105. #define LDBL_STRLEN_BOUND_L(pointlen) _GL_FLOAT_STRLEN_BOUND_L (LDBL, pointlen)
  106. /* Looser bounds that are locale-independent and are integral constant
  107. expressions. */
  108. #define FLT_STRLEN_BOUND FLT_STRLEN_BOUND_L (MB_LEN_MAX)
  109. #define DBL_STRLEN_BOUND DBL_STRLEN_BOUND_L (MB_LEN_MAX)
  110. #define LDBL_STRLEN_BOUND LDBL_STRLEN_BOUND_L (MB_LEN_MAX)
  111. /* Looser, locale-independent bounds that include the trailing null byte. */
  112. #define FLT_BUFSIZE_BOUND ( FLT_STRLEN_BOUND + 1)
  113. #define DBL_BUFSIZE_BOUND ( DBL_STRLEN_BOUND + 1)
  114. #define LDBL_BUFSIZE_BOUND (LDBL_STRLEN_BOUND + 1)
  115. #endif /* _GL_FTOASTR_H */