blob: 75e3032fb74d3bb7a246e0982668a75d21b820cd [file] [log] [blame]
Martin v. Löwis737ea822004-06-08 18:52:54 +00001/* -*- Mode: C; c-file-style: "python" -*- */
2
3#include <Python.h>
4#include <locale.h>
5
Mark Dickinson3b38df22009-10-26 14:36:29 +00006/* 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 Dickinsonbd16edd2009-05-20 22:05:25 +00008
9static int
10case_insensitive_match(const char *s, const char *t)
11{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000012 while(*t && Py_TOLOWER(*s) == *t) {
13 s++;
14 t++;
15 }
16 return *t ? 0 : 1;
Mark Dickinsonbd16edd2009-05-20 22:05:25 +000017}
18
Mark Dickinson3b38df22009-10-26 14:36:29 +000019/* _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 Dickinsonbd16edd2009-05-20 22:05:25 +000025double
26_Py_parse_inf_or_nan(const char *p, char **endptr)
27{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000028 double retval;
29 const char *s;
30 int negate = 0;
Mark Dickinsonbd16edd2009-05-20 22:05:25 +000031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 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 Dickinsonbd16edd2009-05-20 22:05:25 +000046#ifdef Py_NAN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 else if (case_insensitive_match(s, "nan")) {
48 s += 3;
49 retval = negate ? -Py_NAN : Py_NAN;
50 }
Mark Dickinsonbd16edd2009-05-20 22:05:25 +000051#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 else {
53 s = p;
54 retval = -1.0;
55 }
56 *endptr = (char *)s;
57 return retval;
Mark Dickinsonbd16edd2009-05-20 22:05:25 +000058}
59
Martin v. Löwis737ea822004-06-08 18:52:54 +000060/**
Eric Smith68af50b2010-02-22 14:58:30 +000061 * _PyOS_ascii_strtod:
Martin v. Löwis737ea822004-06-08 18:52:54 +000062 * @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 Pitrouf95a1b32010-05-09 15:52:27 +000065 *
Martin v. Löwis737ea822004-06-08 18:52:54 +000066 * 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 Wouters4d70c3d2006-06-08 14:42:34 +000081 * If memory allocation fails, %ENOMEM is stored in %errno.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 *
Martin v. Löwis737ea822004-06-08 18:52:54 +000083 * 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 Smith0923d1d2009-04-16 20:16:10 +000088
89#ifndef PY_NO_SHORT_FLOAT_REPR
90
Eric Smith68af50b2010-02-22 14:58:30 +000091static double
Mark Dickinson725bfd82009-05-03 20:33:40 +000092_PyOS_ascii_strtod(const char *nptr, char **endptr)
Eric Smith0923d1d2009-04-16 20:16:10 +000093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 double result;
95 _Py_SET_53BIT_PRECISION_HEADER;
Eric Smith0923d1d2009-04-16 20:16:10 +000096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 assert(nptr != NULL);
98 /* Set errno to zero, so that we can distinguish zero results
99 and underflows */
100 errno = 0;
Eric Smith0923d1d2009-04-16 20:16:10 +0000101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 _Py_SET_53BIT_PRECISION_START;
103 result = _Py_dg_strtod(nptr, endptr);
104 _Py_SET_53BIT_PRECISION_END;
Eric Smith0923d1d2009-04-16 20:16:10 +0000105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 if (*endptr == nptr)
107 /* string might represent an inf or nan */
108 result = _Py_parse_inf_or_nan(nptr, endptr);
Mark Dickinsonbd16edd2009-05-20 22:05:25 +0000109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 return result;
Eric Smith0923d1d2009-04-16 20:16:10 +0000111
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 Smith68af50b2010-02-22 14:58:30 +0000124static double
Mark Dickinson725bfd82009-05-03 20:33:40 +0000125_PyOS_ascii_strtod(const char *nptr, char **endptr)
Martin v. Löwis737ea822004-06-08 18:52:54 +0000126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 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öwis737ea822004-06-08 18:52:54 +0000136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 assert(nptr != NULL);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 fail_pos = NULL;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 locale_data = localeconv();
142 decimal_point = locale_data->decimal_point;
143 decimal_point_len = strlen(decimal_point);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 assert(decimal_point_len != 0);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 decimal_point_pos = NULL;
Christian Heimesfaf2f632008-01-06 16:59:19 +0000148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 /* Parse infinities and nans */
150 val = _Py_parse_inf_or_nan(nptr, endptr);
151 if (*endptr != nptr)
152 return val;
Mark Dickinsonbd16edd2009-05-20 22:05:25 +0000153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 /* Set errno to zero, so that we can distinguish zero results
155 and underflows */
156 errno = 0;
Mark Dickinson6d65df12009-04-26 15:30:47 +0000157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 /* 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 Heimesfaf2f632008-01-06 16:59:19 +0000170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 /* 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 Dickinson6d65df12009-04-26 15:30:47 +0000175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 /* Check that what's left begins with a digit or decimal point */
177 if (!Py_ISDIGIT(*p) && *p != '.')
178 goto invalid_string;
Mark Dickinson6d65df12009-04-26 15:30:47 +0000179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 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 Norwitze7214a12005-12-18 05:03:17 +0000190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 if (*p == '.')
192 {
193 decimal_point_pos = p++;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 /* locate end of number */
196 while (Py_ISDIGIT(*p))
197 p++;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 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öwis737ea822004-06-08 18:52:54 +0000213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 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öwis737ea822004-06-08 18:52:54 +0000225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 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öwis737ea822004-06-08 18:52:54 +0000235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 val = strtod(copy, &fail_pos);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 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öwis737ea822004-06-08 18:52:54 +0000248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 PyMem_FREE(copy);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 }
252 else {
253 val = strtod(digits_pos, &fail_pos);
254 }
Martin v. Löwis737ea822004-06-08 18:52:54 +0000255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 if (fail_pos == digits_pos)
257 goto invalid_string;
Christian Heimesfaf2f632008-01-06 16:59:19 +0000258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 if (negate && fail_pos != nptr)
260 val = -val;
261 *endptr = fail_pos;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 return val;
Mark Dickinson6d65df12009-04-26 15:30:47 +0000264
265 invalid_string:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 *endptr = (char*)nptr;
267 errno = EINVAL;
268 return -1.0;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000269}
270
Eric Smith0923d1d2009-04-16 20:16:10 +0000271#endif
272
Eric Smith68af50b2010-02-22 14:58:30 +0000273/* 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 Dickinson725bfd82009-05-03 20:33:40 +0000277
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
297double
298PyOS_string_to_double(const char *s,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 char **endptr,
300 PyObject *overflow_exception)
Mark Dickinson725bfd82009-05-03 20:33:40 +0000301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 double x, result=-1.0;
303 char *fail_pos;
Mark Dickinson725bfd82009-05-03 20:33:40 +0000304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 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 Dickinson725bfd82009-05-03 20:33:40 +0000309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 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 Dickinson725bfd82009-05-03 20:33:40 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 if (endptr != NULL)
330 *endptr = fail_pos;
331 return result;
Mark Dickinson725bfd82009-05-03 20:33:40 +0000332}
Eric Smith0923d1d2009-04-16 20:16:10 +0000333
Eric Smith68af50b2010-02-22 14:58:30 +0000334#ifdef PY_NO_SHORT_FLOAT_REPR
335
Eric Smithb2c7af82008-04-30 02:12:09 +0000336/* 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. */
339Py_LOCAL_INLINE(void)
340change_decimal_from_locale_to_dot(char* buffer)
341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 struct lconv *locale_data = localeconv();
343 const char *decimal_point = locale_data->decimal_point;
Eric Smithb2c7af82008-04-30 02:12:09 +0000344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 if (decimal_point[0] != '.' || decimal_point[1] != 0) {
346 size_t decimal_point_len = strlen(decimal_point);
Eric Smithb2c7af82008-04-30 02:12:09 +0000347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 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 Smithb2c7af82008-04-30 02:12:09 +0000366}
367
Martin v. Löwis737ea822004-06-08 18:52:54 +0000368
Christian Heimesc3f30c42008-02-22 16:37:40 +0000369/* From the C99 standard, section 7.19.6:
370The exponent always contains at least two digits, and only as many more digits
371as necessary to represent the exponent.
372*/
373#define MIN_EXPONENT_DIGITS 2
374
Eric Smithb2c7af82008-04-30 02:12:09 +0000375/* Ensure that any exponent, if present, is at least MIN_EXPONENT_DIGITS
376 in length. */
377Py_LOCAL_INLINE(void)
Mark Dickinsonce95e562009-04-26 20:02:24 +0000378ensure_minimum_exponent_length(char* buffer, size_t buf_size)
Eric Smithb2c7af82008-04-30 02:12:09 +0000379{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 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 Smithb2c7af82008-04-30 02:12:09 +0000387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 /* Skip over the exponent and the sign. */
389 p += 2;
Eric Smithb2c7af82008-04-30 02:12:09 +0000390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 /* 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 Smithb2c7af82008-04-30 02:12:09 +0000401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 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 Smithb2c7af82008-04-30 02:12:09 +0000409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 /* 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 Smithb2c7af82008-04-30 02:12:09 +0000416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 /* Delete extra_zeros_cnt worth of characters from the
418 front of the exponent */
419 assert(extra_zeros_cnt >= 0);
Eric Smithb2c7af82008-04-30 02:12:09 +0000420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 /* 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 Smithb2c7af82008-04-30 02:12:09 +0000439}
440
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000441/* 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 Smithb2c7af82008-04-30 02:12:09 +0000445Py_LOCAL_INLINE(void)
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000446remove_trailing_zeros(char *buffer)
Eric Smithb2c7af82008-04-30 02:12:09 +0000447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 char *old_fraction_end, *new_fraction_end, *end, *p;
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 p = buffer;
451 if (*p == '-' || *p == '+')
452 /* Skip leading sign, if present */
453 ++p;
454 while (Py_ISDIGIT(*p))
455 ++p;
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 /* if there's no decimal point there's nothing to do */
458 if (*p++ != '.')
459 return;
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 /* scan any digits after the point */
462 while (Py_ISDIGIT(*p))
463 ++p;
464 old_fraction_end = p;
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 /* 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 Dickinsond3ca5572009-04-29 18:47:07 +0000471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 /* 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 Dickinsond3ca5572009-04-29 18:47:07 +0000480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 memmove(new_fraction_end, old_fraction_end, end-old_fraction_end);
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000482}
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*/
491Py_LOCAL_INLINE(char *)
492ensure_decimal_point(char* buffer, size_t buf_size, int precision)
493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 int digit_count, insert_count = 0, convert_to_exp = 0;
495 char *chars_to_insert, *digits_start;
Eric Smithb2c7af82008-04-30 02:12:09 +0000496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 /* 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 Smithb2c7af82008-04-30 02:12:09 +0000507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 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 Smithb2c7af82008-04-30 02:12:09 +0000579}
580
Christian Heimesc3f30c42008-02-22 16:37:40 +0000581/* see FORMATBUFLEN in unicodeobject.c */
582#define FLOAT_FORMATBUFLEN 120
583
Martin v. Löwis737ea822004-06-08 18:52:54 +0000584/**
Eric Smith68af50b2010-02-22 14:58:30 +0000585 * _PyOS_ascii_formatd:
Martin v. Löwis737ea822004-06-08 18:52:54 +0000586 * @buffer: A buffer to place the resulting string in
Christian Heimesb186d002008-03-18 15:15:01 +0000587 * @buf_size: The length of the buffer.
Martin v. Löwis737ea822004-06-08 18:52:54 +0000588 * @format: The printf()-style format to use for the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 * code to use for converting.
Martin v. Löwis737ea822004-06-08 18:52:54 +0000590 * @d: The #gdouble to convert
Eric Smith68af50b2010-02-22 14:58:30 +0000591 * @precision: The precision to use when formatting.
Martin v. Löwis737ea822004-06-08 18:52:54 +0000592 *
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 Smith0923d1d2009-04-16 20:16:10 +0000596 * specifiers are 'e', 'E', 'f', 'F', 'g', 'G', and 'Z'.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 *
Christian Heimesb186d002008-03-18 15:15:01 +0000598 * 'Z' is the same as 'g', except it always has a decimal and
599 * at least one digit after the decimal.
Christian Heimesc3f30c42008-02-22 16:37:40 +0000600 *
Martin v. Löwis737ea822004-06-08 18:52:54 +0000601 * Return value: The pointer to the buffer with the converted string.
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000602 * On failure returns NULL but does not set any Python exception.
Martin v. Löwis737ea822004-06-08 18:52:54 +0000603 **/
Eric Smith68af50b2010-02-22 14:58:30 +0000604static char *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605_PyOS_ascii_formatd(char *buffer,
606 size_t buf_size,
607 const char *format,
608 double d,
609 int precision)
Martin v. Löwis737ea822004-06-08 18:52:54 +0000610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 char format_char;
612 size_t format_len = strlen(format);
Christian Heimesc3f30c42008-02-22 16:37:40 +0000613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 /* 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öwis737ea822004-06-08 18:52:54 +0000617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 /* The last character in the format string must be the format char */
619 format_char = format[format_len - 1];
Martin v. Löwis737ea822004-06-08 18:52:54 +0000620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 if (format[0] != '%')
622 return NULL;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 /* 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öwis737ea822004-06-08 18:52:54 +0000630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 /* 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 Heimesb186d002008-03-18 15:15:01 +0000635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 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öwis737ea822004-06-08 18:52:54 +0000641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 /* 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öwis737ea822004-06-08 18:52:54 +0000655
Christian Heimesb186d002008-03-18 15:15:01 +0000656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 /* Have PyOS_snprintf do the hard work */
658 PyOS_snprintf(buffer, buf_size, format, d);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 /* Do various fixups on the return string */
Martin v. Löwis737ea822004-06-08 18:52:54 +0000661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 /* 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 Heimesc3f30c42008-02-22 16:37:40 +0000665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 /* 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öwis737ea822004-06-08 18:52:54 +0000672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 /* 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 Heimesb186d002008-03-18 15:15:01 +0000680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 return buffer;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000682}
683
Eric Smith0923d1d2009-04-16 20:16:10 +0000684/* The fallback code to use if _Py_dg_dtoa is not available. */
685
686PyAPI_FUNC(char *) PyOS_double_to_string(double val,
687 char format_code,
688 int precision,
689 int flags,
690 int *type)
Martin v. Löwis737ea822004-06-08 18:52:54 +0000691{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 char format[32];
693 Py_ssize_t bufsize;
694 char *buf;
695 int t, exp;
696 int upper = 0;
Eric Smith0923d1d2009-04-16 20:16:10 +0000697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 /* 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 Smith0923d1d2009-04-16 20:16:10 +0000735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 /* 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 Dickinsonf489caf2009-05-01 11:42:00 +0000738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 1 byte for each digit of the decimal significand, and
Mark Dickinsonf489caf2009-05-01 11:42:00 +0000740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 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 Dickinsonf489caf2009-05-01 11:42:00 +0000747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 This gives a total of 24 + the number of digits in the significand,
749 and the number of digits in the significand is:
Mark Dickinsonf489caf2009-05-01 11:42:00 +0000750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 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 Dickinsonf489caf2009-05-01 11:42:00 +0000759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 1+floor(log10(ceiling(fabs(val))))
Mark Dickinsonf489caf2009-05-01 11:42:00 +0000761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 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 Dickinsonf489caf2009-05-01 11:42:00 +0000768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 So we allow room for precision+1 digits for all formats, plus an
770 extra floor(exp/3) digits for 'f' format.
Mark Dickinsonf489caf2009-05-01 11:42:00 +0000771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 */
Mark Dickinsonf489caf2009-05-01 11:42:00 +0000773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 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 Dickinsonf489caf2009-05-01 11:42:00 +0000784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 buf = PyMem_Malloc(bufsize);
786 if (buf == NULL) {
787 PyErr_NoMemory();
788 return NULL;
789 }
Mark Dickinsonf489caf2009-05-01 11:42:00 +0000790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 /* 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 Smith0923d1d2009-04-16 20:16:10 +0000805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 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 Smith0923d1d2009-04-16 20:16:10 +0000811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 /* 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 Smith0923d1d2009-04-16 20:16:10 +0000828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 if (type)
830 *type = t;
831 return buf;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000832}
Eric Smith0923d1d2009-04-16 20:16:10 +0000833
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 */
845static char *lc_float_strings[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 "inf",
847 "nan",
848 "e",
Eric Smith0923d1d2009-04-16 20:16:10 +0000849};
850static char *uc_float_strings[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 "INF",
852 "NAN",
853 "E",
Eric Smith0923d1d2009-04-16 20:16:10 +0000854};
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 Smith63376222009-05-05 14:04:18 +0000862 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 Smith0923d1d2009-04-16 20:16:10 +0000864 mode is one of '0', '2' or '3', and is completely determined by
Eric Smith63376222009-05-05 14:04:18 +0000865 format_code: 'e' and 'g' use mode 2; 'f' mode 3, 'r' mode 0.
Eric Smith0923d1d2009-04-16 20:16:10 +0000866 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 Smith63376222009-05-05 14:04:18 +0000870 should have ".0" added. Only applies to format codes 'r' and 'g'.
Eric Smith0923d1d2009-04-16 20:16:10 +0000871 use_alt_formatting is nonzero if alternative formatting should be
Eric Smith63376222009-05-05 14:04:18 +0000872 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 Smith0923d1d2009-04-16 20:16:10 +0000875 type, if non-NULL, will be set to one of these constants to identify
876 the type of the 'd' argument:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 Py_DTST_FINITE
878 Py_DTST_INFINITE
879 Py_DTST_NAN
Eric Smith0923d1d2009-04-16 20:16:10 +0000880
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
885static char *
886format_float_short(double d, char format_code,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 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 Smith0923d1d2009-04-16 20:16:10 +0000890{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 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 Smith0923d1d2009-04-16 20:16:10 +0000898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 /* _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 Smith0923d1d2009-04-16 20:16:10 +0000905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 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 Smith0923d1d2009-04-16 20:16:10 +0000914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 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 Smith0923d1d2009-04-16 20:16:10 +0000919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 /* ignore the actual sign of a nan */
921 if (digits[0] == 'n' || digits[0] == 'N')
922 sign = 0;
Mark Dickinsonad476da2009-04-23 19:14:16 +0000923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 /* 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 Smith0923d1d2009-04-16 20:16:10 +0000932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 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 Smith0923d1d2009-04-16 20:16:10 +0000942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 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 Smith0923d1d2009-04-16 20:16:10 +0000949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 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 Smith0923d1d2009-04-16 20:16:10 +0000962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 /* The result must be finite (not inf or nan). */
964 if (type)
965 *type = Py_DTST_FINITE;
Eric Smith0923d1d2009-04-16 20:16:10 +0000966
967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 /* 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 Smith0923d1d2009-04-16 20:16:10 +0000971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 [<sign>]<zeros><digits><zeros>[<exponent>]
Eric Smith0923d1d2009-04-16 20:16:10 +0000973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 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 Smith0923d1d2009-04-16 20:16:10 +0000977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 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 Smith0923d1d2009-04-16 20:16:10 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 vdigits[vdigits_start : vdigits_end]
Eric Smith0923d1d2009-04-16 20:16:10 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 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 Smith0923d1d2009-04-16 20:16:10 +00001019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 /* 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 Smith0923d1d2009-04-16 20:16:10 +00001033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 /* 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 Smith0923d1d2009-04-16 20:16:10 +00001040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 /* 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 Smith0923d1d2009-04-16 20:16:10 +00001046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 /* total digit count (including zero padding on both sides) */
1048 (vdigits_end - vdigits_start) +
Eric Smith0923d1d2009-04-16 20:16:10 +00001049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 /* exponent "e+100", max 3 numerical digits */
1051 (use_exp ? 5 : 0);
Eric Smith0923d1d2009-04-16 20:16:10 +00001052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 /* 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 Smith0923d1d2009-04-16 20:16:10 +00001061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 /* 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 Smith0923d1d2009-04-16 20:16:10 +00001068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 /* 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 Smith0923d1d2009-04-16 20:16:10 +00001083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 /* 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 Smith0923d1d2009-04-16 20:16:10 +00001096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 /* 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 Smith0923d1d2009-04-16 20:16:10 +00001109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 /* Delete a trailing decimal pt unless using alternative formatting. */
1111 if (p[-1] == '.' && !use_alt_formatting)
1112 p--;
Eric Smith0923d1d2009-04-16 20:16:10 +00001113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 /* 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 Smith0923d1d2009-04-16 20:16:10 +00001120 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 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 Smith0923d1d2009-04-16 20:16:10 +00001129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 return buf;
Eric Smith0923d1d2009-04-16 20:16:10 +00001131}
1132
1133
1134PyAPI_FUNC(char *) PyOS_double_to_string(double val,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 char format_code,
1136 int precision,
1137 int flags,
1138 int *type)
Eric Smith0923d1d2009-04-16 20:16:10 +00001139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 char **float_strings = lc_float_strings;
1141 int mode;
Eric Smith0923d1d2009-04-16 20:16:10 +00001142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 /* 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 Smith193125a2009-04-16 22:08:31 +00001155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 /* 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 Smith193125a2009-04-16 22:08:31 +00001164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 /* 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 Smith193125a2009-04-16 22:08:31 +00001176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 /* 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 Smith193125a2009-04-16 22:08:31 +00001186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 default:
1188 PyErr_BadInternalCall();
1189 return NULL;
1190 }
Eric Smith0923d1d2009-04-16 20:16:10 +00001191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 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 Smith0923d1d2009-04-16 20:16:10 +00001197}
1198#endif /* ifdef PY_NO_SHORT_FLOAT_REPR */