Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 1 | /* -*- Mode: C; c-file-style: "python" -*- */ |
| 2 | |
| 3 | #include <Python.h> |
| 4 | #include <locale.h> |
| 5 | |
Mark Dickinson | 3b38df2 | 2009-10-26 14:36:29 +0000 | [diff] [blame] | 6 | /* Case-insensitive string match used for nan and inf detection; t should be |
| 7 | lower-case. Returns 1 for a successful match, 0 otherwise. */ |
Mark Dickinson | bd16edd | 2009-05-20 22:05:25 +0000 | [diff] [blame] | 8 | |
| 9 | static int |
| 10 | case_insensitive_match(const char *s, const char *t) |
| 11 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 12 | while(*t && Py_TOLOWER(*s) == *t) { |
| 13 | s++; |
| 14 | t++; |
| 15 | } |
| 16 | return *t ? 0 : 1; |
Mark Dickinson | bd16edd | 2009-05-20 22:05:25 +0000 | [diff] [blame] | 17 | } |
| 18 | |
Mark Dickinson | 3b38df2 | 2009-10-26 14:36:29 +0000 | [diff] [blame] | 19 | /* _Py_parse_inf_or_nan: Attempt to parse a string of the form "nan", "inf" or |
| 20 | "infinity", with an optional leading sign of "+" or "-". On success, |
| 21 | return the NaN or Infinity as a double and set *endptr to point just beyond |
| 22 | the successfully parsed portion of the string. On failure, return -1.0 and |
| 23 | set *endptr to point to the start of the string. */ |
| 24 | |
Mark Dickinson | bd16edd | 2009-05-20 22:05:25 +0000 | [diff] [blame] | 25 | double |
| 26 | _Py_parse_inf_or_nan(const char *p, char **endptr) |
| 27 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 28 | double retval; |
| 29 | const char *s; |
| 30 | int negate = 0; |
Mark Dickinson | bd16edd | 2009-05-20 22:05:25 +0000 | [diff] [blame] | 31 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 32 | s = p; |
| 33 | if (*s == '-') { |
| 34 | negate = 1; |
| 35 | s++; |
| 36 | } |
| 37 | else if (*s == '+') { |
| 38 | s++; |
| 39 | } |
| 40 | if (case_insensitive_match(s, "inf")) { |
| 41 | s += 3; |
| 42 | if (case_insensitive_match(s, "inity")) |
| 43 | s += 5; |
| 44 | retval = negate ? -Py_HUGE_VAL : Py_HUGE_VAL; |
| 45 | } |
Mark Dickinson | bd16edd | 2009-05-20 22:05:25 +0000 | [diff] [blame] | 46 | #ifdef Py_NAN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 | else if (case_insensitive_match(s, "nan")) { |
| 48 | s += 3; |
| 49 | retval = negate ? -Py_NAN : Py_NAN; |
| 50 | } |
Mark Dickinson | bd16edd | 2009-05-20 22:05:25 +0000 | [diff] [blame] | 51 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | else { |
| 53 | s = p; |
| 54 | retval = -1.0; |
| 55 | } |
| 56 | *endptr = (char *)s; |
| 57 | return retval; |
Mark Dickinson | bd16edd | 2009-05-20 22:05:25 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 60 | /** |
Eric Smith | 68af50b | 2010-02-22 14:58:30 +0000 | [diff] [blame] | 61 | * _PyOS_ascii_strtod: |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 62 | * @nptr: the string to convert to a numeric value. |
| 63 | * @endptr: if non-%NULL, it returns the character after |
| 64 | * the last character used in the conversion. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 65 | * |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 66 | * Converts a string to a #gdouble value. |
| 67 | * This function behaves like the standard strtod() function |
| 68 | * does in the C locale. It does this without actually |
| 69 | * changing the current locale, since that would not be |
| 70 | * thread-safe. |
| 71 | * |
| 72 | * This function is typically used when reading configuration |
| 73 | * files or other non-user input that should be locale independent. |
| 74 | * To handle input from the user you should normally use the |
| 75 | * locale-sensitive system strtod() function. |
| 76 | * |
| 77 | * If the correct value would cause overflow, plus or minus %HUGE_VAL |
| 78 | * is returned (according to the sign of the value), and %ERANGE is |
| 79 | * stored in %errno. If the correct value would cause underflow, |
| 80 | * zero is returned and %ERANGE is stored in %errno. |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 81 | * If memory allocation fails, %ENOMEM is stored in %errno. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 82 | * |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 83 | * This function resets %errno before calling strtod() so that |
| 84 | * you can reliably detect overflow and underflow. |
| 85 | * |
| 86 | * Return value: the #gdouble value. |
| 87 | **/ |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 88 | |
| 89 | #ifndef PY_NO_SHORT_FLOAT_REPR |
| 90 | |
Eric Smith | 68af50b | 2010-02-22 14:58:30 +0000 | [diff] [blame] | 91 | static double |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 92 | _PyOS_ascii_strtod(const char *nptr, char **endptr) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 93 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | double result; |
| 95 | _Py_SET_53BIT_PRECISION_HEADER; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 96 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | assert(nptr != NULL); |
| 98 | /* Set errno to zero, so that we can distinguish zero results |
| 99 | and underflows */ |
| 100 | errno = 0; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 101 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 102 | _Py_SET_53BIT_PRECISION_START; |
| 103 | result = _Py_dg_strtod(nptr, endptr); |
| 104 | _Py_SET_53BIT_PRECISION_END; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 106 | if (*endptr == nptr) |
| 107 | /* string might represent an inf or nan */ |
| 108 | result = _Py_parse_inf_or_nan(nptr, endptr); |
Mark Dickinson | bd16edd | 2009-05-20 22:05:25 +0000 | [diff] [blame] | 109 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 110 | return result; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 111 | |
| 112 | } |
| 113 | |
| 114 | #else |
| 115 | |
| 116 | /* |
| 117 | Use system strtod; since strtod is locale aware, we may |
| 118 | have to first fix the decimal separator. |
| 119 | |
| 120 | Note that unlike _Py_dg_strtod, the system strtod may not always give |
| 121 | correctly rounded results. |
| 122 | */ |
| 123 | |
Eric Smith | 68af50b | 2010-02-22 14:58:30 +0000 | [diff] [blame] | 124 | static double |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 125 | _PyOS_ascii_strtod(const char *nptr, char **endptr) |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 126 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | char *fail_pos; |
| 128 | double val = -1.0; |
| 129 | struct lconv *locale_data; |
| 130 | const char *decimal_point; |
| 131 | size_t decimal_point_len; |
| 132 | const char *p, *decimal_point_pos; |
| 133 | const char *end = NULL; /* Silence gcc */ |
| 134 | const char *digits_pos = NULL; |
| 135 | int negate = 0; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | assert(nptr != NULL); |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | fail_pos = NULL; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | locale_data = localeconv(); |
| 142 | decimal_point = locale_data->decimal_point; |
| 143 | decimal_point_len = strlen(decimal_point); |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | assert(decimal_point_len != 0); |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | decimal_point_pos = NULL; |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | /* Parse infinities and nans */ |
| 150 | val = _Py_parse_inf_or_nan(nptr, endptr); |
| 151 | if (*endptr != nptr) |
| 152 | return val; |
Mark Dickinson | bd16edd | 2009-05-20 22:05:25 +0000 | [diff] [blame] | 153 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 154 | /* Set errno to zero, so that we can distinguish zero results |
| 155 | and underflows */ |
| 156 | errno = 0; |
Mark Dickinson | 6d65df1 | 2009-04-26 15:30:47 +0000 | [diff] [blame] | 157 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | /* We process the optional sign manually, then pass the remainder to |
| 159 | the system strtod. This ensures that the result of an underflow |
| 160 | has the correct sign. (bug #1725) */ |
| 161 | p = nptr; |
| 162 | /* Process leading sign, if present */ |
| 163 | if (*p == '-') { |
| 164 | negate = 1; |
| 165 | p++; |
| 166 | } |
| 167 | else if (*p == '+') { |
| 168 | p++; |
| 169 | } |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 171 | /* Some platform strtods accept hex floats; Python shouldn't (at the |
| 172 | moment), so we check explicitly for strings starting with '0x'. */ |
| 173 | if (*p == '0' && (*(p+1) == 'x' || *(p+1) == 'X')) |
| 174 | goto invalid_string; |
Mark Dickinson | 6d65df1 | 2009-04-26 15:30:47 +0000 | [diff] [blame] | 175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | /* Check that what's left begins with a digit or decimal point */ |
| 177 | if (!Py_ISDIGIT(*p) && *p != '.') |
| 178 | goto invalid_string; |
Mark Dickinson | 6d65df1 | 2009-04-26 15:30:47 +0000 | [diff] [blame] | 179 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 180 | digits_pos = p; |
| 181 | if (decimal_point[0] != '.' || |
| 182 | decimal_point[1] != 0) |
| 183 | { |
| 184 | /* Look for a '.' in the input; if present, it'll need to be |
| 185 | swapped for the current locale's decimal point before we |
| 186 | call strtod. On the other hand, if we find the current |
| 187 | locale's decimal point then the input is invalid. */ |
| 188 | while (Py_ISDIGIT(*p)) |
| 189 | p++; |
Neal Norwitz | e7214a1 | 2005-12-18 05:03:17 +0000 | [diff] [blame] | 190 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | if (*p == '.') |
| 192 | { |
| 193 | decimal_point_pos = p++; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 194 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 195 | /* locate end of number */ |
| 196 | while (Py_ISDIGIT(*p)) |
| 197 | p++; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | if (*p == 'e' || *p == 'E') |
| 200 | p++; |
| 201 | if (*p == '+' || *p == '-') |
| 202 | p++; |
| 203 | while (Py_ISDIGIT(*p)) |
| 204 | p++; |
| 205 | end = p; |
| 206 | } |
| 207 | else if (strncmp(p, decimal_point, decimal_point_len) == 0) |
| 208 | /* Python bug #1417699 */ |
| 209 | goto invalid_string; |
| 210 | /* For the other cases, we need not convert the decimal |
| 211 | point */ |
| 212 | } |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | if (decimal_point_pos) { |
| 215 | char *copy, *c; |
| 216 | /* Create a copy of the input, with the '.' converted to the |
| 217 | locale-specific decimal point */ |
| 218 | copy = (char *)PyMem_MALLOC(end - digits_pos + |
| 219 | 1 + decimal_point_len); |
| 220 | if (copy == NULL) { |
| 221 | *endptr = (char *)nptr; |
| 222 | errno = ENOMEM; |
| 223 | return val; |
| 224 | } |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | c = copy; |
| 227 | memcpy(c, digits_pos, decimal_point_pos - digits_pos); |
| 228 | c += decimal_point_pos - digits_pos; |
| 229 | memcpy(c, decimal_point, decimal_point_len); |
| 230 | c += decimal_point_len; |
| 231 | memcpy(c, decimal_point_pos + 1, |
| 232 | end - (decimal_point_pos + 1)); |
| 233 | c += end - (decimal_point_pos + 1); |
| 234 | *c = 0; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | val = strtod(copy, &fail_pos); |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 238 | if (fail_pos) |
| 239 | { |
| 240 | if (fail_pos > decimal_point_pos) |
| 241 | fail_pos = (char *)digits_pos + |
| 242 | (fail_pos - copy) - |
| 243 | (decimal_point_len - 1); |
| 244 | else |
| 245 | fail_pos = (char *)digits_pos + |
| 246 | (fail_pos - copy); |
| 247 | } |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | PyMem_FREE(copy); |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 251 | } |
| 252 | else { |
| 253 | val = strtod(digits_pos, &fail_pos); |
| 254 | } |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 255 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 256 | if (fail_pos == digits_pos) |
| 257 | goto invalid_string; |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | if (negate && fail_pos != nptr) |
| 260 | val = -val; |
| 261 | *endptr = fail_pos; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | return val; |
Mark Dickinson | 6d65df1 | 2009-04-26 15:30:47 +0000 | [diff] [blame] | 264 | |
| 265 | invalid_string: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 266 | *endptr = (char*)nptr; |
| 267 | errno = EINVAL; |
| 268 | return -1.0; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 271 | #endif |
| 272 | |
Eric Smith | 68af50b | 2010-02-22 14:58:30 +0000 | [diff] [blame] | 273 | /* PyOS_string_to_double converts a null-terminated byte string s (interpreted |
| 274 | as a string of ASCII characters) to a float. The string should not have |
| 275 | leading or trailing whitespace. The conversion is independent of the |
| 276 | current locale. |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 277 | |
| 278 | If endptr is NULL, try to convert the whole string. Raise ValueError and |
| 279 | return -1.0 if the string is not a valid representation of a floating-point |
| 280 | number. |
| 281 | |
| 282 | If endptr is non-NULL, try to convert as much of the string as possible. |
| 283 | If no initial segment of the string is the valid representation of a |
| 284 | floating-point number then *endptr is set to point to the beginning of the |
| 285 | string, -1.0 is returned and again ValueError is raised. |
| 286 | |
| 287 | On overflow (e.g., when trying to convert '1e500' on an IEEE 754 machine), |
| 288 | if overflow_exception is NULL then +-Py_HUGE_VAL is returned, and no Python |
| 289 | exception is raised. Otherwise, overflow_exception should point to a |
| 290 | a Python exception, this exception will be raised, -1.0 will be returned, |
| 291 | and *endptr will point just past the end of the converted value. |
| 292 | |
| 293 | If any other failure occurs (for example lack of memory), -1.0 is returned |
| 294 | and the appropriate Python exception will have been set. |
| 295 | */ |
| 296 | |
| 297 | double |
| 298 | PyOS_string_to_double(const char *s, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | char **endptr, |
| 300 | PyObject *overflow_exception) |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 301 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | double x, result=-1.0; |
| 303 | char *fail_pos; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 305 | errno = 0; |
| 306 | PyFPE_START_PROTECT("PyOS_string_to_double", return -1.0) |
| 307 | x = _PyOS_ascii_strtod(s, &fail_pos); |
| 308 | PyFPE_END_PROTECT(x) |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 | if (errno == ENOMEM) { |
| 311 | PyErr_NoMemory(); |
| 312 | fail_pos = (char *)s; |
| 313 | } |
| 314 | else if (!endptr && (fail_pos == s || *fail_pos != '\0')) |
| 315 | PyErr_Format(PyExc_ValueError, |
| 316 | "could not convert string to float: " |
| 317 | "%.200s", s); |
| 318 | else if (fail_pos == s) |
| 319 | PyErr_Format(PyExc_ValueError, |
| 320 | "could not convert string to float: " |
| 321 | "%.200s", s); |
| 322 | else if (errno == ERANGE && fabs(x) >= 1.0 && overflow_exception) |
| 323 | PyErr_Format(overflow_exception, |
| 324 | "value too large to convert to float: " |
| 325 | "%.200s", s); |
| 326 | else |
| 327 | result = x; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | if (endptr != NULL) |
| 330 | *endptr = fail_pos; |
| 331 | return result; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 332 | } |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 333 | |
Eric Smith | 68af50b | 2010-02-22 14:58:30 +0000 | [diff] [blame] | 334 | #ifdef PY_NO_SHORT_FLOAT_REPR |
| 335 | |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 336 | /* Given a string that may have a decimal point in the current |
| 337 | locale, change it back to a dot. Since the string cannot get |
| 338 | longer, no need for a maximum buffer size parameter. */ |
| 339 | Py_LOCAL_INLINE(void) |
| 340 | change_decimal_from_locale_to_dot(char* buffer) |
| 341 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 342 | struct lconv *locale_data = localeconv(); |
| 343 | const char *decimal_point = locale_data->decimal_point; |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 345 | if (decimal_point[0] != '.' || decimal_point[1] != 0) { |
| 346 | size_t decimal_point_len = strlen(decimal_point); |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 348 | if (*buffer == '+' || *buffer == '-') |
| 349 | buffer++; |
| 350 | while (Py_ISDIGIT(*buffer)) |
| 351 | buffer++; |
| 352 | if (strncmp(buffer, decimal_point, decimal_point_len) == 0) { |
| 353 | *buffer = '.'; |
| 354 | buffer++; |
| 355 | if (decimal_point_len > 1) { |
| 356 | /* buffer needs to get smaller */ |
| 357 | size_t rest_len = strlen(buffer + |
| 358 | (decimal_point_len - 1)); |
| 359 | memmove(buffer, |
| 360 | buffer + (decimal_point_len - 1), |
| 361 | rest_len); |
| 362 | buffer[rest_len] = 0; |
| 363 | } |
| 364 | } |
| 365 | } |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 368 | |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 369 | /* From the C99 standard, section 7.19.6: |
| 370 | The exponent always contains at least two digits, and only as many more digits |
| 371 | as necessary to represent the exponent. |
| 372 | */ |
| 373 | #define MIN_EXPONENT_DIGITS 2 |
| 374 | |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 375 | /* Ensure that any exponent, if present, is at least MIN_EXPONENT_DIGITS |
| 376 | in length. */ |
| 377 | Py_LOCAL_INLINE(void) |
Mark Dickinson | ce95e56 | 2009-04-26 20:02:24 +0000 | [diff] [blame] | 378 | ensure_minimum_exponent_length(char* buffer, size_t buf_size) |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 379 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | char *p = strpbrk(buffer, "eE"); |
| 381 | if (p && (*(p + 1) == '-' || *(p + 1) == '+')) { |
| 382 | char *start = p + 2; |
| 383 | int exponent_digit_cnt = 0; |
| 384 | int leading_zero_cnt = 0; |
| 385 | int in_leading_zeros = 1; |
| 386 | int significant_digit_cnt; |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 388 | /* Skip over the exponent and the sign. */ |
| 389 | p += 2; |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 390 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 391 | /* Find the end of the exponent, keeping track of leading |
| 392 | zeros. */ |
| 393 | while (*p && Py_ISDIGIT(*p)) { |
| 394 | if (in_leading_zeros && *p == '0') |
| 395 | ++leading_zero_cnt; |
| 396 | if (*p != '0') |
| 397 | in_leading_zeros = 0; |
| 398 | ++p; |
| 399 | ++exponent_digit_cnt; |
| 400 | } |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | significant_digit_cnt = exponent_digit_cnt - leading_zero_cnt; |
| 403 | if (exponent_digit_cnt == MIN_EXPONENT_DIGITS) { |
| 404 | /* If there are 2 exactly digits, we're done, |
| 405 | regardless of what they contain */ |
| 406 | } |
| 407 | else if (exponent_digit_cnt > MIN_EXPONENT_DIGITS) { |
| 408 | int extra_zeros_cnt; |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 410 | /* There are more than 2 digits in the exponent. See |
| 411 | if we can delete some of the leading zeros */ |
| 412 | if (significant_digit_cnt < MIN_EXPONENT_DIGITS) |
| 413 | significant_digit_cnt = MIN_EXPONENT_DIGITS; |
| 414 | extra_zeros_cnt = exponent_digit_cnt - |
| 415 | significant_digit_cnt; |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 416 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 417 | /* Delete extra_zeros_cnt worth of characters from the |
| 418 | front of the exponent */ |
| 419 | assert(extra_zeros_cnt >= 0); |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 420 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 421 | /* Add one to significant_digit_cnt to copy the |
| 422 | trailing 0 byte, thus setting the length */ |
| 423 | memmove(start, |
| 424 | start + extra_zeros_cnt, |
| 425 | significant_digit_cnt + 1); |
| 426 | } |
| 427 | else { |
| 428 | /* If there are fewer than 2 digits, add zeros |
| 429 | until there are 2, if there's enough room */ |
| 430 | int zeros = MIN_EXPONENT_DIGITS - exponent_digit_cnt; |
| 431 | if (start + zeros + exponent_digit_cnt + 1 |
| 432 | < buffer + buf_size) { |
| 433 | memmove(start + zeros, start, |
| 434 | exponent_digit_cnt + 1); |
| 435 | memset(start, '0', zeros); |
| 436 | } |
| 437 | } |
| 438 | } |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Mark Dickinson | d3ca557 | 2009-04-29 18:47:07 +0000 | [diff] [blame] | 441 | /* Remove trailing zeros after the decimal point from a numeric string; also |
| 442 | remove the decimal point if all digits following it are zero. The numeric |
| 443 | string must end in '\0', and should not have any leading or trailing |
| 444 | whitespace. Assumes that the decimal point is '.'. */ |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 445 | Py_LOCAL_INLINE(void) |
Mark Dickinson | d3ca557 | 2009-04-29 18:47:07 +0000 | [diff] [blame] | 446 | remove_trailing_zeros(char *buffer) |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 447 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 448 | char *old_fraction_end, *new_fraction_end, *end, *p; |
Mark Dickinson | d3ca557 | 2009-04-29 18:47:07 +0000 | [diff] [blame] | 449 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 450 | p = buffer; |
| 451 | if (*p == '-' || *p == '+') |
| 452 | /* Skip leading sign, if present */ |
| 453 | ++p; |
| 454 | while (Py_ISDIGIT(*p)) |
| 455 | ++p; |
Mark Dickinson | d3ca557 | 2009-04-29 18:47:07 +0000 | [diff] [blame] | 456 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 457 | /* if there's no decimal point there's nothing to do */ |
| 458 | if (*p++ != '.') |
| 459 | return; |
Mark Dickinson | d3ca557 | 2009-04-29 18:47:07 +0000 | [diff] [blame] | 460 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 461 | /* scan any digits after the point */ |
| 462 | while (Py_ISDIGIT(*p)) |
| 463 | ++p; |
| 464 | old_fraction_end = p; |
Mark Dickinson | d3ca557 | 2009-04-29 18:47:07 +0000 | [diff] [blame] | 465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 466 | /* scan up to ending '\0' */ |
| 467 | while (*p != '\0') |
| 468 | p++; |
| 469 | /* +1 to make sure that we move the null byte as well */ |
| 470 | end = p+1; |
Mark Dickinson | d3ca557 | 2009-04-29 18:47:07 +0000 | [diff] [blame] | 471 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 472 | /* scan back from fraction_end, looking for removable zeros */ |
| 473 | p = old_fraction_end; |
| 474 | while (*(p-1) == '0') |
| 475 | --p; |
| 476 | /* and remove point if we've got that far */ |
| 477 | if (*(p-1) == '.') |
| 478 | --p; |
| 479 | new_fraction_end = p; |
Mark Dickinson | d3ca557 | 2009-04-29 18:47:07 +0000 | [diff] [blame] | 480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 481 | memmove(new_fraction_end, old_fraction_end, end-old_fraction_end); |
Mark Dickinson | d3ca557 | 2009-04-29 18:47:07 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | /* Ensure that buffer has a decimal point in it. The decimal point will not |
| 485 | be in the current locale, it will always be '.'. Don't add a decimal point |
| 486 | if an exponent is present. Also, convert to exponential notation where |
| 487 | adding a '.0' would produce too many significant digits (see issue 5864). |
| 488 | |
| 489 | Returns a pointer to the fixed buffer, or NULL on failure. |
| 490 | */ |
| 491 | Py_LOCAL_INLINE(char *) |
| 492 | ensure_decimal_point(char* buffer, size_t buf_size, int precision) |
| 493 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | int digit_count, insert_count = 0, convert_to_exp = 0; |
| 495 | char *chars_to_insert, *digits_start; |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 | /* search for the first non-digit character */ |
| 498 | char *p = buffer; |
| 499 | if (*p == '-' || *p == '+') |
| 500 | /* Skip leading sign, if present. I think this could only |
| 501 | ever be '-', but it can't hurt to check for both. */ |
| 502 | ++p; |
| 503 | digits_start = p; |
| 504 | while (*p && Py_ISDIGIT(*p)) |
| 505 | ++p; |
| 506 | digit_count = Py_SAFE_DOWNCAST(p - digits_start, Py_ssize_t, int); |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | if (*p == '.') { |
| 509 | if (Py_ISDIGIT(*(p+1))) { |
| 510 | /* Nothing to do, we already have a decimal |
| 511 | point and a digit after it */ |
| 512 | } |
| 513 | else { |
| 514 | /* We have a decimal point, but no following |
| 515 | digit. Insert a zero after the decimal. */ |
| 516 | /* can't ever get here via PyOS_double_to_string */ |
| 517 | assert(precision == -1); |
| 518 | ++p; |
| 519 | chars_to_insert = "0"; |
| 520 | insert_count = 1; |
| 521 | } |
| 522 | } |
| 523 | else if (!(*p == 'e' || *p == 'E')) { |
| 524 | /* Don't add ".0" if we have an exponent. */ |
| 525 | if (digit_count == precision) { |
| 526 | /* issue 5864: don't add a trailing .0 in the case |
| 527 | where the '%g'-formatted result already has as many |
| 528 | significant digits as were requested. Switch to |
| 529 | exponential notation instead. */ |
| 530 | convert_to_exp = 1; |
| 531 | /* no exponent, no point, and we shouldn't land here |
| 532 | for infs and nans, so we must be at the end of the |
| 533 | string. */ |
| 534 | assert(*p == '\0'); |
| 535 | } |
| 536 | else { |
| 537 | assert(precision == -1 || digit_count < precision); |
| 538 | chars_to_insert = ".0"; |
| 539 | insert_count = 2; |
| 540 | } |
| 541 | } |
| 542 | if (insert_count) { |
| 543 | size_t buf_len = strlen(buffer); |
| 544 | if (buf_len + insert_count + 1 >= buf_size) { |
| 545 | /* If there is not enough room in the buffer |
| 546 | for the additional text, just skip it. It's |
| 547 | not worth generating an error over. */ |
| 548 | } |
| 549 | else { |
| 550 | memmove(p + insert_count, p, |
| 551 | buffer + strlen(buffer) - p + 1); |
| 552 | memcpy(p, chars_to_insert, insert_count); |
| 553 | } |
| 554 | } |
| 555 | if (convert_to_exp) { |
| 556 | int written; |
| 557 | size_t buf_avail; |
| 558 | p = digits_start; |
| 559 | /* insert decimal point */ |
| 560 | assert(digit_count >= 1); |
| 561 | memmove(p+2, p+1, digit_count); /* safe, but overwrites nul */ |
| 562 | p[1] = '.'; |
| 563 | p += digit_count+1; |
| 564 | assert(p <= buf_size+buffer); |
| 565 | buf_avail = buf_size+buffer-p; |
| 566 | if (buf_avail == 0) |
| 567 | return NULL; |
| 568 | /* Add exponent. It's okay to use lower case 'e': we only |
| 569 | arrive here as a result of using the empty format code or |
| 570 | repr/str builtins and those never want an upper case 'E' */ |
| 571 | written = PyOS_snprintf(p, buf_avail, "e%+.02d", digit_count-1); |
| 572 | if (!(0 <= written && |
| 573 | written < Py_SAFE_DOWNCAST(buf_avail, size_t, int))) |
| 574 | /* output truncated, or something else bad happened */ |
| 575 | return NULL; |
| 576 | remove_trailing_zeros(buffer); |
| 577 | } |
| 578 | return buffer; |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 581 | /* see FORMATBUFLEN in unicodeobject.c */ |
| 582 | #define FLOAT_FORMATBUFLEN 120 |
| 583 | |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 584 | /** |
Eric Smith | 68af50b | 2010-02-22 14:58:30 +0000 | [diff] [blame] | 585 | * _PyOS_ascii_formatd: |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 586 | * @buffer: A buffer to place the resulting string in |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 587 | * @buf_size: The length of the buffer. |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 588 | * @format: The printf()-style format to use for the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | * code to use for converting. |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 590 | * @d: The #gdouble to convert |
Eric Smith | 68af50b | 2010-02-22 14:58:30 +0000 | [diff] [blame] | 591 | * @precision: The precision to use when formatting. |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 592 | * |
| 593 | * Converts a #gdouble to a string, using the '.' as |
| 594 | * decimal point. To format the number you pass in |
| 595 | * a printf()-style format string. Allowed conversion |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 596 | * specifiers are 'e', 'E', 'f', 'F', 'g', 'G', and 'Z'. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 597 | * |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 598 | * 'Z' is the same as 'g', except it always has a decimal and |
| 599 | * at least one digit after the decimal. |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 600 | * |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 601 | * Return value: The pointer to the buffer with the converted string. |
Mark Dickinson | d3ca557 | 2009-04-29 18:47:07 +0000 | [diff] [blame] | 602 | * On failure returns NULL but does not set any Python exception. |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 603 | **/ |
Eric Smith | 68af50b | 2010-02-22 14:58:30 +0000 | [diff] [blame] | 604 | static char * |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 605 | _PyOS_ascii_formatd(char *buffer, |
| 606 | size_t buf_size, |
| 607 | const char *format, |
| 608 | double d, |
| 609 | int precision) |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 610 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 611 | char format_char; |
| 612 | size_t format_len = strlen(format); |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 614 | /* Issue 2264: code 'Z' requires copying the format. 'Z' is 'g', but |
| 615 | also with at least one character past the decimal. */ |
| 616 | char tmp_format[FLOAT_FORMATBUFLEN]; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 617 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 618 | /* The last character in the format string must be the format char */ |
| 619 | format_char = format[format_len - 1]; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 621 | if (format[0] != '%') |
| 622 | return NULL; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 623 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 624 | /* I'm not sure why this test is here. It's ensuring that the format |
| 625 | string after the first character doesn't have a single quote, a |
| 626 | lowercase l, or a percent. This is the reverse of the commented-out |
| 627 | test about 10 lines ago. */ |
| 628 | if (strpbrk(format + 1, "'l%")) |
| 629 | return NULL; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 630 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | /* Also curious about this function is that it accepts format strings |
| 632 | like "%xg", which are invalid for floats. In general, the |
| 633 | interface to this function is not very good, but changing it is |
| 634 | difficult because it's a public API. */ |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | if (!(format_char == 'e' || format_char == 'E' || |
| 637 | format_char == 'f' || format_char == 'F' || |
| 638 | format_char == 'g' || format_char == 'G' || |
| 639 | format_char == 'Z')) |
| 640 | return NULL; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 642 | /* Map 'Z' format_char to 'g', by copying the format string and |
| 643 | replacing the final char with a 'g' */ |
| 644 | if (format_char == 'Z') { |
| 645 | if (format_len + 1 >= sizeof(tmp_format)) { |
| 646 | /* The format won't fit in our copy. Error out. In |
| 647 | practice, this will never happen and will be |
| 648 | detected by returning NULL */ |
| 649 | return NULL; |
| 650 | } |
| 651 | strcpy(tmp_format, format); |
| 652 | tmp_format[format_len - 1] = 'g'; |
| 653 | format = tmp_format; |
| 654 | } |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 655 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 656 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 657 | /* Have PyOS_snprintf do the hard work */ |
| 658 | PyOS_snprintf(buffer, buf_size, format, d); |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 659 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 660 | /* Do various fixups on the return string */ |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 661 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 662 | /* Get the current locale, and find the decimal point string. |
| 663 | Convert that string back to a dot. */ |
| 664 | change_decimal_from_locale_to_dot(buffer); |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 665 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 666 | /* If an exponent exists, ensure that the exponent is at least |
| 667 | MIN_EXPONENT_DIGITS digits, providing the buffer is large enough |
| 668 | for the extra zeros. Also, if there are more than |
| 669 | MIN_EXPONENT_DIGITS, remove as many zeros as possible until we get |
| 670 | back to MIN_EXPONENT_DIGITS */ |
| 671 | ensure_minimum_exponent_length(buffer, buf_size); |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | /* If format_char is 'Z', make sure we have at least one character |
| 674 | after the decimal point (and make sure we have a decimal point); |
| 675 | also switch to exponential notation in some edge cases where the |
| 676 | extra character would produce more significant digits that we |
| 677 | really want. */ |
| 678 | if (format_char == 'Z') |
| 679 | buffer = ensure_decimal_point(buffer, buf_size, precision); |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 681 | return buffer; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 684 | /* The fallback code to use if _Py_dg_dtoa is not available. */ |
| 685 | |
| 686 | PyAPI_FUNC(char *) PyOS_double_to_string(double val, |
| 687 | char format_code, |
| 688 | int precision, |
| 689 | int flags, |
| 690 | int *type) |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 691 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 692 | char format[32]; |
| 693 | Py_ssize_t bufsize; |
| 694 | char *buf; |
| 695 | int t, exp; |
| 696 | int upper = 0; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 697 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 698 | /* Validate format_code, and map upper and lower case */ |
| 699 | switch (format_code) { |
| 700 | case 'e': /* exponent */ |
| 701 | case 'f': /* fixed */ |
| 702 | case 'g': /* general */ |
| 703 | break; |
| 704 | case 'E': |
| 705 | upper = 1; |
| 706 | format_code = 'e'; |
| 707 | break; |
| 708 | case 'F': |
| 709 | upper = 1; |
| 710 | format_code = 'f'; |
| 711 | break; |
| 712 | case 'G': |
| 713 | upper = 1; |
| 714 | format_code = 'g'; |
| 715 | break; |
| 716 | case 'r': /* repr format */ |
| 717 | /* Supplied precision is unused, must be 0. */ |
| 718 | if (precision != 0) { |
| 719 | PyErr_BadInternalCall(); |
| 720 | return NULL; |
| 721 | } |
| 722 | /* The repr() precision (17 significant decimal digits) is the |
| 723 | minimal number that is guaranteed to have enough precision |
| 724 | so that if the number is read back in the exact same binary |
| 725 | value is recreated. This is true for IEEE floating point |
| 726 | by design, and also happens to work for all other modern |
| 727 | hardware. */ |
| 728 | precision = 17; |
| 729 | format_code = 'g'; |
| 730 | break; |
| 731 | default: |
| 732 | PyErr_BadInternalCall(); |
| 733 | return NULL; |
| 734 | } |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 735 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 736 | /* Here's a quick-and-dirty calculation to figure out how big a buffer |
| 737 | we need. In general, for a finite float we need: |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 739 | 1 byte for each digit of the decimal significand, and |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 741 | 1 for a possible sign |
| 742 | 1 for a possible decimal point |
| 743 | 2 for a possible [eE][+-] |
| 744 | 1 for each digit of the exponent; if we allow 19 digits |
| 745 | total then we're safe up to exponents of 2**63. |
| 746 | 1 for the trailing nul byte |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 747 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 748 | This gives a total of 24 + the number of digits in the significand, |
| 749 | and the number of digits in the significand is: |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 751 | for 'g' format: at most precision, except possibly |
| 752 | when precision == 0, when it's 1. |
| 753 | for 'e' format: precision+1 |
| 754 | for 'f' format: precision digits after the point, at least 1 |
| 755 | before. To figure out how many digits appear before the point |
| 756 | we have to examine the size of the number. If fabs(val) < 1.0 |
| 757 | then there will be only one digit before the point. If |
| 758 | fabs(val) >= 1.0, then there are at most |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 759 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 760 | 1+floor(log10(ceiling(fabs(val)))) |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 761 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 762 | digits before the point (where the 'ceiling' allows for the |
| 763 | possibility that the rounding rounds the integer part of val |
| 764 | up). A safe upper bound for the above quantity is |
| 765 | 1+floor(exp/3), where exp is the unique integer such that 0.5 |
| 766 | <= fabs(val)/2**exp < 1.0. This exp can be obtained from |
| 767 | frexp. |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 768 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 769 | So we allow room for precision+1 digits for all formats, plus an |
| 770 | extra floor(exp/3) digits for 'f' format. |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 772 | */ |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 773 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 774 | if (Py_IS_NAN(val) || Py_IS_INFINITY(val)) |
| 775 | /* 3 for 'inf'/'nan', 1 for sign, 1 for '\0' */ |
| 776 | bufsize = 5; |
| 777 | else { |
| 778 | bufsize = 25 + precision; |
| 779 | if (format_code == 'f' && fabs(val) >= 1.0) { |
| 780 | frexp(val, &exp); |
| 781 | bufsize += exp/3; |
| 782 | } |
| 783 | } |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 784 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 785 | buf = PyMem_Malloc(bufsize); |
| 786 | if (buf == NULL) { |
| 787 | PyErr_NoMemory(); |
| 788 | return NULL; |
| 789 | } |
Mark Dickinson | f489caf | 2009-05-01 11:42:00 +0000 | [diff] [blame] | 790 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 791 | /* Handle nan and inf. */ |
| 792 | if (Py_IS_NAN(val)) { |
| 793 | strcpy(buf, "nan"); |
| 794 | t = Py_DTST_NAN; |
| 795 | } else if (Py_IS_INFINITY(val)) { |
| 796 | if (copysign(1., val) == 1.) |
| 797 | strcpy(buf, "inf"); |
| 798 | else |
| 799 | strcpy(buf, "-inf"); |
| 800 | t = Py_DTST_INFINITE; |
| 801 | } else { |
| 802 | t = Py_DTST_FINITE; |
| 803 | if (flags & Py_DTSF_ADD_DOT_0) |
| 804 | format_code = 'Z'; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 805 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 806 | PyOS_snprintf(format, sizeof(format), "%%%s.%i%c", |
| 807 | (flags & Py_DTSF_ALT ? "#" : ""), precision, |
| 808 | format_code); |
| 809 | _PyOS_ascii_formatd(buf, bufsize, format, val, precision); |
| 810 | } |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 811 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 812 | /* Add sign when requested. It's convenient (esp. when formatting |
| 813 | complex numbers) to include a sign even for inf and nan. */ |
| 814 | if (flags & Py_DTSF_SIGN && buf[0] != '-') { |
| 815 | size_t len = strlen(buf); |
| 816 | /* the bufsize calculations above should ensure that we've got |
| 817 | space to add a sign */ |
| 818 | assert((size_t)bufsize >= len+2); |
| 819 | memmove(buf+1, buf, len+1); |
| 820 | buf[0] = '+'; |
| 821 | } |
| 822 | if (upper) { |
| 823 | /* Convert to upper case. */ |
| 824 | char *p1; |
| 825 | for (p1 = buf; *p1; p1++) |
| 826 | *p1 = Py_TOUPPER(*p1); |
| 827 | } |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 828 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 829 | if (type) |
| 830 | *type = t; |
| 831 | return buf; |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 832 | } |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 833 | |
| 834 | #else |
| 835 | |
| 836 | /* _Py_dg_dtoa is available. */ |
| 837 | |
| 838 | /* I'm using a lookup table here so that I don't have to invent a non-locale |
| 839 | specific way to convert to uppercase */ |
| 840 | #define OFS_INF 0 |
| 841 | #define OFS_NAN 1 |
| 842 | #define OFS_E 2 |
| 843 | |
| 844 | /* The lengths of these are known to the code below, so don't change them */ |
| 845 | static char *lc_float_strings[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 846 | "inf", |
| 847 | "nan", |
| 848 | "e", |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 849 | }; |
| 850 | static char *uc_float_strings[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 851 | "INF", |
| 852 | "NAN", |
| 853 | "E", |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 854 | }; |
| 855 | |
| 856 | |
| 857 | /* Convert a double d to a string, and return a PyMem_Malloc'd block of |
| 858 | memory contain the resulting string. |
| 859 | |
| 860 | Arguments: |
| 861 | d is the double to be converted |
Eric Smith | 6337622 | 2009-05-05 14:04:18 +0000 | [diff] [blame] | 862 | format_code is one of 'e', 'f', 'g', 'r'. 'e', 'f' and 'g' |
| 863 | correspond to '%e', '%f' and '%g'; 'r' corresponds to repr. |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 864 | mode is one of '0', '2' or '3', and is completely determined by |
Eric Smith | 6337622 | 2009-05-05 14:04:18 +0000 | [diff] [blame] | 865 | format_code: 'e' and 'g' use mode 2; 'f' mode 3, 'r' mode 0. |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 866 | precision is the desired precision |
| 867 | always_add_sign is nonzero if a '+' sign should be included for positive |
| 868 | numbers |
| 869 | add_dot_0_if_integer is nonzero if integers in non-exponential form |
Eric Smith | 6337622 | 2009-05-05 14:04:18 +0000 | [diff] [blame] | 870 | should have ".0" added. Only applies to format codes 'r' and 'g'. |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 871 | use_alt_formatting is nonzero if alternative formatting should be |
Eric Smith | 6337622 | 2009-05-05 14:04:18 +0000 | [diff] [blame] | 872 | used. Only applies to format codes 'e', 'f' and 'g'. For code 'g', |
| 873 | at most one of use_alt_formatting and add_dot_0_if_integer should |
| 874 | be nonzero. |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 875 | type, if non-NULL, will be set to one of these constants to identify |
| 876 | the type of the 'd' argument: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 877 | Py_DTST_FINITE |
| 878 | Py_DTST_INFINITE |
| 879 | Py_DTST_NAN |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 880 | |
| 881 | Returns a PyMem_Malloc'd block of memory containing the resulting string, |
| 882 | or NULL on error. If NULL is returned, the Python error has been set. |
| 883 | */ |
| 884 | |
| 885 | static char * |
| 886 | format_float_short(double d, char format_code, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 887 | int mode, Py_ssize_t precision, |
| 888 | int always_add_sign, int add_dot_0_if_integer, |
| 889 | int use_alt_formatting, char **float_strings, int *type) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 890 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 | char *buf = NULL; |
| 892 | char *p = NULL; |
| 893 | Py_ssize_t bufsize = 0; |
| 894 | char *digits, *digits_end; |
| 895 | int decpt_as_int, sign, exp_len, exp = 0, use_exp = 0; |
| 896 | Py_ssize_t decpt, digits_len, vdigits_start, vdigits_end; |
| 897 | _Py_SET_53BIT_PRECISION_HEADER; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 899 | /* _Py_dg_dtoa returns a digit string (no decimal point or exponent). |
| 900 | Must be matched by a call to _Py_dg_freedtoa. */ |
| 901 | _Py_SET_53BIT_PRECISION_START; |
| 902 | digits = _Py_dg_dtoa(d, mode, precision, &decpt_as_int, &sign, |
| 903 | &digits_end); |
| 904 | _Py_SET_53BIT_PRECISION_END; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | decpt = (Py_ssize_t)decpt_as_int; |
| 907 | if (digits == NULL) { |
| 908 | /* The only failure mode is no memory. */ |
| 909 | PyErr_NoMemory(); |
| 910 | goto exit; |
| 911 | } |
| 912 | assert(digits_end != NULL && digits_end >= digits); |
| 913 | digits_len = digits_end - digits; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 915 | if (digits_len && !Py_ISDIGIT(digits[0])) { |
| 916 | /* Infinities and nans here; adapt Gay's output, |
| 917 | so convert Infinity to inf and NaN to nan, and |
| 918 | ignore sign of nan. Then return. */ |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 919 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 920 | /* ignore the actual sign of a nan */ |
| 921 | if (digits[0] == 'n' || digits[0] == 'N') |
| 922 | sign = 0; |
Mark Dickinson | ad476da | 2009-04-23 19:14:16 +0000 | [diff] [blame] | 923 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | /* We only need 5 bytes to hold the result "+inf\0" . */ |
| 925 | bufsize = 5; /* Used later in an assert. */ |
| 926 | buf = (char *)PyMem_Malloc(bufsize); |
| 927 | if (buf == NULL) { |
| 928 | PyErr_NoMemory(); |
| 929 | goto exit; |
| 930 | } |
| 931 | p = buf; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 933 | if (sign == 1) { |
| 934 | *p++ = '-'; |
| 935 | } |
| 936 | else if (always_add_sign) { |
| 937 | *p++ = '+'; |
| 938 | } |
| 939 | if (digits[0] == 'i' || digits[0] == 'I') { |
| 940 | strncpy(p, float_strings[OFS_INF], 3); |
| 941 | p += 3; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 942 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 943 | if (type) |
| 944 | *type = Py_DTST_INFINITE; |
| 945 | } |
| 946 | else if (digits[0] == 'n' || digits[0] == 'N') { |
| 947 | strncpy(p, float_strings[OFS_NAN], 3); |
| 948 | p += 3; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 949 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 950 | if (type) |
| 951 | *type = Py_DTST_NAN; |
| 952 | } |
| 953 | else { |
| 954 | /* shouldn't get here: Gay's code should always return |
| 955 | something starting with a digit, an 'I', or 'N' */ |
| 956 | strncpy(p, "ERR", 3); |
| 957 | p += 3; |
| 958 | assert(0); |
| 959 | } |
| 960 | goto exit; |
| 961 | } |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | /* The result must be finite (not inf or nan). */ |
| 964 | if (type) |
| 965 | *type = Py_DTST_FINITE; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 966 | |
| 967 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 968 | /* We got digits back, format them. We may need to pad 'digits' |
| 969 | either on the left or right (or both) with extra zeros, so in |
| 970 | general the resulting string has the form |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 971 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 972 | [<sign>]<zeros><digits><zeros>[<exponent>] |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 973 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 974 | where either of the <zeros> pieces could be empty, and there's a |
| 975 | decimal point that could appear either in <digits> or in the |
| 976 | leading or trailing <zeros>. |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 977 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 978 | Imagine an infinite 'virtual' string vdigits, consisting of the |
| 979 | string 'digits' (starting at index 0) padded on both the left and |
| 980 | right with infinite strings of zeros. We want to output a slice |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 981 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 982 | vdigits[vdigits_start : vdigits_end] |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 983 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 984 | of this virtual string. Thus if vdigits_start < 0 then we'll end |
| 985 | up producing some leading zeros; if vdigits_end > digits_len there |
| 986 | will be trailing zeros in the output. The next section of code |
| 987 | determines whether to use an exponent or not, figures out the |
| 988 | position 'decpt' of the decimal point, and computes 'vdigits_start' |
| 989 | and 'vdigits_end'. */ |
| 990 | vdigits_end = digits_len; |
| 991 | switch (format_code) { |
| 992 | case 'e': |
| 993 | use_exp = 1; |
| 994 | vdigits_end = precision; |
| 995 | break; |
| 996 | case 'f': |
| 997 | vdigits_end = decpt + precision; |
| 998 | break; |
| 999 | case 'g': |
| 1000 | if (decpt <= -4 || decpt > |
| 1001 | (add_dot_0_if_integer ? precision-1 : precision)) |
| 1002 | use_exp = 1; |
| 1003 | if (use_alt_formatting) |
| 1004 | vdigits_end = precision; |
| 1005 | break; |
| 1006 | case 'r': |
| 1007 | /* convert to exponential format at 1e16. We used to convert |
| 1008 | at 1e17, but that gives odd-looking results for some values |
| 1009 | when a 16-digit 'shortest' repr is padded with bogus zeros. |
| 1010 | For example, repr(2e16+8) would give 20000000000000010.0; |
| 1011 | the true value is 20000000000000008.0. */ |
| 1012 | if (decpt <= -4 || decpt > 16) |
| 1013 | use_exp = 1; |
| 1014 | break; |
| 1015 | default: |
| 1016 | PyErr_BadInternalCall(); |
| 1017 | goto exit; |
| 1018 | } |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1019 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | /* if using an exponent, reset decimal point position to 1 and adjust |
| 1021 | exponent accordingly.*/ |
| 1022 | if (use_exp) { |
| 1023 | exp = decpt - 1; |
| 1024 | decpt = 1; |
| 1025 | } |
| 1026 | /* ensure vdigits_start < decpt <= vdigits_end, or vdigits_start < |
| 1027 | decpt < vdigits_end if add_dot_0_if_integer and no exponent */ |
| 1028 | vdigits_start = decpt <= 0 ? decpt-1 : 0; |
| 1029 | if (!use_exp && add_dot_0_if_integer) |
| 1030 | vdigits_end = vdigits_end > decpt ? vdigits_end : decpt + 1; |
| 1031 | else |
| 1032 | vdigits_end = vdigits_end > decpt ? vdigits_end : decpt; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1033 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1034 | /* double check inequalities */ |
| 1035 | assert(vdigits_start <= 0 && |
| 1036 | 0 <= digits_len && |
| 1037 | digits_len <= vdigits_end); |
| 1038 | /* decimal point should be in (vdigits_start, vdigits_end] */ |
| 1039 | assert(vdigits_start < decpt && decpt <= vdigits_end); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1040 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1041 | /* Compute an upper bound how much memory we need. This might be a few |
| 1042 | chars too long, but no big deal. */ |
| 1043 | bufsize = |
| 1044 | /* sign, decimal point and trailing 0 byte */ |
| 1045 | 3 + |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1047 | /* total digit count (including zero padding on both sides) */ |
| 1048 | (vdigits_end - vdigits_start) + |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1049 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1050 | /* exponent "e+100", max 3 numerical digits */ |
| 1051 | (use_exp ? 5 : 0); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1052 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | /* Now allocate the memory and initialize p to point to the start of |
| 1054 | it. */ |
| 1055 | buf = (char *)PyMem_Malloc(bufsize); |
| 1056 | if (buf == NULL) { |
| 1057 | PyErr_NoMemory(); |
| 1058 | goto exit; |
| 1059 | } |
| 1060 | p = buf; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1061 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1062 | /* Add a negative sign if negative, and a plus sign if non-negative |
| 1063 | and always_add_sign is true. */ |
| 1064 | if (sign == 1) |
| 1065 | *p++ = '-'; |
| 1066 | else if (always_add_sign) |
| 1067 | *p++ = '+'; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1068 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1069 | /* note that exactly one of the three 'if' conditions is true, |
| 1070 | so we include exactly one decimal point */ |
| 1071 | /* Zero padding on left of digit string */ |
| 1072 | if (decpt <= 0) { |
| 1073 | memset(p, '0', decpt-vdigits_start); |
| 1074 | p += decpt - vdigits_start; |
| 1075 | *p++ = '.'; |
| 1076 | memset(p, '0', 0-decpt); |
| 1077 | p += 0-decpt; |
| 1078 | } |
| 1079 | else { |
| 1080 | memset(p, '0', 0-vdigits_start); |
| 1081 | p += 0 - vdigits_start; |
| 1082 | } |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1083 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1084 | /* Digits, with included decimal point */ |
| 1085 | if (0 < decpt && decpt <= digits_len) { |
| 1086 | strncpy(p, digits, decpt-0); |
| 1087 | p += decpt-0; |
| 1088 | *p++ = '.'; |
| 1089 | strncpy(p, digits+decpt, digits_len-decpt); |
| 1090 | p += digits_len-decpt; |
| 1091 | } |
| 1092 | else { |
| 1093 | strncpy(p, digits, digits_len); |
| 1094 | p += digits_len; |
| 1095 | } |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1096 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | /* And zeros on the right */ |
| 1098 | if (digits_len < decpt) { |
| 1099 | memset(p, '0', decpt-digits_len); |
| 1100 | p += decpt-digits_len; |
| 1101 | *p++ = '.'; |
| 1102 | memset(p, '0', vdigits_end-decpt); |
| 1103 | p += vdigits_end-decpt; |
| 1104 | } |
| 1105 | else { |
| 1106 | memset(p, '0', vdigits_end-digits_len); |
| 1107 | p += vdigits_end-digits_len; |
| 1108 | } |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1109 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1110 | /* Delete a trailing decimal pt unless using alternative formatting. */ |
| 1111 | if (p[-1] == '.' && !use_alt_formatting) |
| 1112 | p--; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1114 | /* Now that we've done zero padding, add an exponent if needed. */ |
| 1115 | if (use_exp) { |
| 1116 | *p++ = float_strings[OFS_E][0]; |
| 1117 | exp_len = sprintf(p, "%+.02d", exp); |
| 1118 | p += exp_len; |
| 1119 | } |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1120 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1121 | if (buf) { |
| 1122 | *p = '\0'; |
| 1123 | /* It's too late if this fails, as we've already stepped on |
| 1124 | memory that isn't ours. But it's an okay debugging test. */ |
| 1125 | assert(p-buf < bufsize); |
| 1126 | } |
| 1127 | if (digits) |
| 1128 | _Py_dg_freedtoa(digits); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1129 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1130 | return buf; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | |
| 1134 | PyAPI_FUNC(char *) PyOS_double_to_string(double val, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1135 | char format_code, |
| 1136 | int precision, |
| 1137 | int flags, |
| 1138 | int *type) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1139 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1140 | char **float_strings = lc_float_strings; |
| 1141 | int mode; |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1142 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1143 | /* Validate format_code, and map upper and lower case. Compute the |
| 1144 | mode and make any adjustments as needed. */ |
| 1145 | switch (format_code) { |
| 1146 | /* exponent */ |
| 1147 | case 'E': |
| 1148 | float_strings = uc_float_strings; |
| 1149 | format_code = 'e'; |
| 1150 | /* Fall through. */ |
| 1151 | case 'e': |
| 1152 | mode = 2; |
| 1153 | precision++; |
| 1154 | break; |
Eric Smith | 193125a | 2009-04-16 22:08:31 +0000 | [diff] [blame] | 1155 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1156 | /* fixed */ |
| 1157 | case 'F': |
| 1158 | float_strings = uc_float_strings; |
| 1159 | format_code = 'f'; |
| 1160 | /* Fall through. */ |
| 1161 | case 'f': |
| 1162 | mode = 3; |
| 1163 | break; |
Eric Smith | 193125a | 2009-04-16 22:08:31 +0000 | [diff] [blame] | 1164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1165 | /* general */ |
| 1166 | case 'G': |
| 1167 | float_strings = uc_float_strings; |
| 1168 | format_code = 'g'; |
| 1169 | /* Fall through. */ |
| 1170 | case 'g': |
| 1171 | mode = 2; |
| 1172 | /* precision 0 makes no sense for 'g' format; interpret as 1 */ |
| 1173 | if (precision == 0) |
| 1174 | precision = 1; |
| 1175 | break; |
Eric Smith | 193125a | 2009-04-16 22:08:31 +0000 | [diff] [blame] | 1176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1177 | /* repr format */ |
| 1178 | case 'r': |
| 1179 | mode = 0; |
| 1180 | /* Supplied precision is unused, must be 0. */ |
| 1181 | if (precision != 0) { |
| 1182 | PyErr_BadInternalCall(); |
| 1183 | return NULL; |
| 1184 | } |
| 1185 | break; |
Eric Smith | 193125a | 2009-04-16 22:08:31 +0000 | [diff] [blame] | 1186 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1187 | default: |
| 1188 | PyErr_BadInternalCall(); |
| 1189 | return NULL; |
| 1190 | } |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1192 | return format_float_short(val, format_code, mode, precision, |
| 1193 | flags & Py_DTSF_SIGN, |
| 1194 | flags & Py_DTSF_ADD_DOT_0, |
| 1195 | flags & Py_DTSF_ALT, |
| 1196 | float_strings, type); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1197 | } |
| 1198 | #endif /* ifdef PY_NO_SHORT_FLOAT_REPR */ |