blob: d36f9310882808a50aee44116365f8c0c1f0009a [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
Martin v. Löwis737ea822004-06-08 18:52:54 +00006/**
7 * PyOS_ascii_strtod:
8 * @nptr: the string to convert to a numeric value.
9 * @endptr: if non-%NULL, it returns the character after
10 * the last character used in the conversion.
11 *
12 * Converts a string to a #gdouble value.
13 * This function behaves like the standard strtod() function
14 * does in the C locale. It does this without actually
15 * changing the current locale, since that would not be
16 * thread-safe.
17 *
18 * This function is typically used when reading configuration
19 * files or other non-user input that should be locale independent.
20 * To handle input from the user you should normally use the
21 * locale-sensitive system strtod() function.
22 *
23 * If the correct value would cause overflow, plus or minus %HUGE_VAL
24 * is returned (according to the sign of the value), and %ERANGE is
25 * stored in %errno. If the correct value would cause underflow,
26 * zero is returned and %ERANGE is stored in %errno.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000027 * If memory allocation fails, %ENOMEM is stored in %errno.
Martin v. Löwis737ea822004-06-08 18:52:54 +000028 *
29 * This function resets %errno before calling strtod() so that
30 * you can reliably detect overflow and underflow.
31 *
32 * Return value: the #gdouble value.
33 **/
Eric Smith0923d1d2009-04-16 20:16:10 +000034
35#ifndef PY_NO_SHORT_FLOAT_REPR
36
37double
Mark Dickinson725bfd82009-05-03 20:33:40 +000038_PyOS_ascii_strtod(const char *nptr, char **endptr)
Eric Smith0923d1d2009-04-16 20:16:10 +000039{
40 double result;
41 _Py_SET_53BIT_PRECISION_HEADER;
42
43 assert(nptr != NULL);
44 /* Set errno to zero, so that we can distinguish zero results
45 and underflows */
46 errno = 0;
47
48 _Py_SET_53BIT_PRECISION_START;
49 result = _Py_dg_strtod(nptr, endptr);
50 _Py_SET_53BIT_PRECISION_END;
51
52 return result;
53
54}
55
56#else
57
58/*
59 Use system strtod; since strtod is locale aware, we may
60 have to first fix the decimal separator.
61
62 Note that unlike _Py_dg_strtod, the system strtod may not always give
63 correctly rounded results.
64*/
65
Mark Dickinson129ab1d2009-05-03 22:36:01 +000066/* Case-insensitive string match used for nan and inf detection; t should be
67 lower-case. Returns 1 for a successful match, 0 otherwise. */
68
69static int
70case_insensitive_match(const char *s, const char *t)
71{
72 while(*t && Py_TOLOWER(*s) == *t) {
73 s++;
74 t++;
75 }
76 return *t ? 0 : 1;
77}
78
Martin v. Löwis737ea822004-06-08 18:52:54 +000079double
Mark Dickinson725bfd82009-05-03 20:33:40 +000080_PyOS_ascii_strtod(const char *nptr, char **endptr)
Martin v. Löwis737ea822004-06-08 18:52:54 +000081{
82 char *fail_pos;
Neal Norwitz0e7a0ed2005-12-18 05:37:36 +000083 double val = -1.0;
Martin v. Löwis737ea822004-06-08 18:52:54 +000084 struct lconv *locale_data;
85 const char *decimal_point;
Neal Norwitzd39d8612006-01-08 01:03:36 +000086 size_t decimal_point_len;
Martin v. Löwis737ea822004-06-08 18:52:54 +000087 const char *p, *decimal_point_pos;
88 const char *end = NULL; /* Silence gcc */
Christian Heimesfaf2f632008-01-06 16:59:19 +000089 const char *digits_pos = NULL;
90 int negate = 0;
Martin v. Löwis737ea822004-06-08 18:52:54 +000091
Martin v. Löwis737ea822004-06-08 18:52:54 +000092 assert(nptr != NULL);
93
94 fail_pos = NULL;
95
96 locale_data = localeconv();
97 decimal_point = locale_data->decimal_point;
98 decimal_point_len = strlen(decimal_point);
99
100 assert(decimal_point_len != 0);
101
102 decimal_point_pos = NULL;
Christian Heimesfaf2f632008-01-06 16:59:19 +0000103
Mark Dickinson6d65df12009-04-26 15:30:47 +0000104 /* Set errno to zero, so that we can distinguish zero results
105 and underflows */
106 errno = 0;
107
Mark Dickinson725bfd82009-05-03 20:33:40 +0000108 /* We process the optional sign manually, then pass the remainder to
109 the system strtod. This ensures that the result of an underflow
110 has the correct sign. (bug #1725) */
Christian Heimesfaf2f632008-01-06 16:59:19 +0000111 p = nptr;
Christian Heimesfaf2f632008-01-06 16:59:19 +0000112 /* Process leading sign, if present */
113 if (*p == '-') {
114 negate = 1;
115 p++;
Mark Dickinson6d65df12009-04-26 15:30:47 +0000116 }
117 else if (*p == '+') {
Christian Heimesfaf2f632008-01-06 16:59:19 +0000118 p++;
119 }
120
Mark Dickinson6d65df12009-04-26 15:30:47 +0000121 /* Parse infinities and nans */
122 if (*p == 'i' || *p == 'I') {
Mark Dickinson129ab1d2009-05-03 22:36:01 +0000123 if (case_insensitive_match(p+1, "nf")) {
Mark Dickinson6d65df12009-04-26 15:30:47 +0000124 val = Py_HUGE_VAL;
Mark Dickinson129ab1d2009-05-03 22:36:01 +0000125 if (case_insensitive_match(p+3, "inity"))
Mark Dickinson6d65df12009-04-26 15:30:47 +0000126 fail_pos = (char *)p+8;
127 else
128 fail_pos = (char *)p+3;
129 goto got_val;
130 }
131 else
132 goto invalid_string;
Christian Heimesfaf2f632008-01-06 16:59:19 +0000133 }
Mark Dickinson6d65df12009-04-26 15:30:47 +0000134#ifdef Py_NAN
135 if (*p == 'n' || *p == 'N') {
Mark Dickinson129ab1d2009-05-03 22:36:01 +0000136 if (case_insensitive_match(p+1, "an")) {
Mark Dickinson6d65df12009-04-26 15:30:47 +0000137 val = Py_NAN;
138 fail_pos = (char *)p+3;
139 goto got_val;
140 }
141 else
142 goto invalid_string;
143 }
144#endif
Christian Heimesfaf2f632008-01-06 16:59:19 +0000145
Mark Dickinson6d65df12009-04-26 15:30:47 +0000146 /* Some platform strtods accept hex floats; Python shouldn't (at the
147 moment), so we check explicitly for strings starting with '0x'. */
148 if (*p == '0' && (*(p+1) == 'x' || *(p+1) == 'X'))
149 goto invalid_string;
150
151 /* Check that what's left begins with a digit or decimal point */
Eric Smith6dc46f52009-04-27 20:39:49 +0000152 if (!Py_ISDIGIT(*p) && *p != '.')
Mark Dickinson6d65df12009-04-26 15:30:47 +0000153 goto invalid_string;
154
155 digits_pos = p;
156 if (decimal_point[0] != '.' ||
Martin v. Löwis737ea822004-06-08 18:52:54 +0000157 decimal_point[1] != 0)
158 {
Mark Dickinson6d65df12009-04-26 15:30:47 +0000159 /* Look for a '.' in the input; if present, it'll need to be
160 swapped for the current locale's decimal point before we
161 call strtod. On the other hand, if we find the current
162 locale's decimal point then the input is invalid. */
Eric Smith6dc46f52009-04-27 20:39:49 +0000163 while (Py_ISDIGIT(*p))
Neal Norwitze7214a12005-12-18 05:03:17 +0000164 p++;
165
166 if (*p == '.')
Martin v. Löwis737ea822004-06-08 18:52:54 +0000167 {
Neal Norwitze7214a12005-12-18 05:03:17 +0000168 decimal_point_pos = p++;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000169
Mark Dickinson6d65df12009-04-26 15:30:47 +0000170 /* locate end of number */
Eric Smith6dc46f52009-04-27 20:39:49 +0000171 while (Py_ISDIGIT(*p))
Martin v. Löwis737ea822004-06-08 18:52:54 +0000172 p++;
173
Neal Norwitze7214a12005-12-18 05:03:17 +0000174 if (*p == 'e' || *p == 'E')
175 p++;
176 if (*p == '+' || *p == '-')
177 p++;
Eric Smith6dc46f52009-04-27 20:39:49 +0000178 while (Py_ISDIGIT(*p))
Neal Norwitze7214a12005-12-18 05:03:17 +0000179 p++;
180 end = p;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000181 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000182 else if (strncmp(p, decimal_point, decimal_point_len) == 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000183 /* Python bug #1417699 */
Mark Dickinson6d65df12009-04-26 15:30:47 +0000184 goto invalid_string;
Christian Heimesb186d002008-03-18 15:15:01 +0000185 /* For the other cases, we need not convert the decimal
186 point */
Martin v. Löwis737ea822004-06-08 18:52:54 +0000187 }
188
Mark Dickinson6d65df12009-04-26 15:30:47 +0000189 if (decimal_point_pos) {
Martin v. Löwis737ea822004-06-08 18:52:54 +0000190 char *copy, *c;
Mark Dickinson6d65df12009-04-26 15:30:47 +0000191 /* Create a copy of the input, with the '.' converted to the
192 locale-specific decimal point */
Christian Heimesfaf2f632008-01-06 16:59:19 +0000193 copy = (char *)PyMem_MALLOC(end - digits_pos +
194 1 + decimal_point_len);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000195 if (copy == NULL) {
Mark Dickinson725bfd82009-05-03 20:33:40 +0000196 *endptr = (char *)nptr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000197 errno = ENOMEM;
198 return val;
199 }
Martin v. Löwis737ea822004-06-08 18:52:54 +0000200
201 c = copy;
Christian Heimesfaf2f632008-01-06 16:59:19 +0000202 memcpy(c, digits_pos, decimal_point_pos - digits_pos);
203 c += decimal_point_pos - digits_pos;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000204 memcpy(c, decimal_point, decimal_point_len);
205 c += decimal_point_len;
Christian Heimesb186d002008-03-18 15:15:01 +0000206 memcpy(c, decimal_point_pos + 1,
207 end - (decimal_point_pos + 1));
Martin v. Löwis737ea822004-06-08 18:52:54 +0000208 c += end - (decimal_point_pos + 1);
209 *c = 0;
210
211 val = strtod(copy, &fail_pos);
212
213 if (fail_pos)
214 {
215 if (fail_pos > decimal_point_pos)
Christian Heimesfaf2f632008-01-06 16:59:19 +0000216 fail_pos = (char *)digits_pos +
217 (fail_pos - copy) -
218 (decimal_point_len - 1);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000219 else
Christian Heimesfaf2f632008-01-06 16:59:19 +0000220 fail_pos = (char *)digits_pos +
221 (fail_pos - copy);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000222 }
223
Thomas Wouters477c8d52006-05-27 19:21:47 +0000224 PyMem_FREE(copy);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000225
226 }
Neal Norwitze7214a12005-12-18 05:03:17 +0000227 else {
Christian Heimesfaf2f632008-01-06 16:59:19 +0000228 val = strtod(digits_pos, &fail_pos);
Neal Norwitze7214a12005-12-18 05:03:17 +0000229 }
Martin v. Löwis737ea822004-06-08 18:52:54 +0000230
Christian Heimesfaf2f632008-01-06 16:59:19 +0000231 if (fail_pos == digits_pos)
Mark Dickinson6d65df12009-04-26 15:30:47 +0000232 goto invalid_string;
Christian Heimesfaf2f632008-01-06 16:59:19 +0000233
Mark Dickinson6d65df12009-04-26 15:30:47 +0000234 got_val:
Christian Heimesfaf2f632008-01-06 16:59:19 +0000235 if (negate && fail_pos != nptr)
236 val = -val;
Mark Dickinson725bfd82009-05-03 20:33:40 +0000237 *endptr = fail_pos;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000238
239 return val;
Mark Dickinson6d65df12009-04-26 15:30:47 +0000240
241 invalid_string:
Mark Dickinson725bfd82009-05-03 20:33:40 +0000242 *endptr = (char*)nptr;
Mark Dickinson6d65df12009-04-26 15:30:47 +0000243 errno = EINVAL;
244 return -1.0;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000245}
246
Eric Smith0923d1d2009-04-16 20:16:10 +0000247#endif
248
Mark Dickinson725bfd82009-05-03 20:33:40 +0000249/* PyOS_ascii_strtod is DEPRECATED in Python 3.1 */
250
251double
252PyOS_ascii_strtod(const char *nptr, char **endptr)
253{
254 char *fail_pos;
255 const char *p;
256 double x;
257
258 if (PyErr_WarnEx(PyExc_DeprecationWarning,
259 "PyOS_ascii_strtod and PyOS_ascii_atof are "
260 "deprecated. Use PyOS_string_to_double "
261 "instead.", 1) < 0)
262 return -1.0;
263
264 /* _PyOS_ascii_strtod already does everything that we want,
265 except that it doesn't parse leading whitespace */
266 p = nptr;
267 while (Py_ISSPACE(*p))
268 p++;
269 x = _PyOS_ascii_strtod(p, &fail_pos);
270 if (fail_pos == p)
271 fail_pos = (char *)nptr;
272 if (endptr)
273 *endptr = (char *)fail_pos;
274 return x;
275}
276
277/* PyOS_ascii_strtod is DEPRECATED in Python 3.1 */
278
Eric Smith0923d1d2009-04-16 20:16:10 +0000279double
280PyOS_ascii_atof(const char *nptr)
281{
282 return PyOS_ascii_strtod(nptr, NULL);
283}
284
Mark Dickinson725bfd82009-05-03 20:33:40 +0000285/* PyOS_string_to_double is the recommended replacement for the deprecated
286 PyOS_ascii_strtod and PyOS_ascii_atof functions. It converts a
287 null-terminated byte string s (interpreted as a string of ASCII characters)
288 to a float. The string should not have leading or trailing whitespace (in
289 contrast, PyOS_ascii_strtod allows leading whitespace but not trailing
290 whitespace). The conversion is independent of the current locale.
291
292 If endptr is NULL, try to convert the whole string. Raise ValueError and
293 return -1.0 if the string is not a valid representation of a floating-point
294 number.
295
296 If endptr is non-NULL, try to convert as much of the string as possible.
297 If no initial segment of the string is the valid representation of a
298 floating-point number then *endptr is set to point to the beginning of the
299 string, -1.0 is returned and again ValueError is raised.
300
301 On overflow (e.g., when trying to convert '1e500' on an IEEE 754 machine),
302 if overflow_exception is NULL then +-Py_HUGE_VAL is returned, and no Python
303 exception is raised. Otherwise, overflow_exception should point to a
304 a Python exception, this exception will be raised, -1.0 will be returned,
305 and *endptr will point just past the end of the converted value.
306
307 If any other failure occurs (for example lack of memory), -1.0 is returned
308 and the appropriate Python exception will have been set.
309*/
310
311double
312PyOS_string_to_double(const char *s,
313 char **endptr,
314 PyObject *overflow_exception)
315{
316 double x, result=-1.0;
317 char *fail_pos;
318
319 errno = 0;
320 PyFPE_START_PROTECT("PyOS_string_to_double", return -1.0)
321 x = _PyOS_ascii_strtod(s, &fail_pos);
322 PyFPE_END_PROTECT(x)
323
324 if (errno == ENOMEM) {
325 PyErr_NoMemory();
326 fail_pos = (char *)s;
327 }
328 else if (!endptr && (fail_pos == s || *fail_pos != '\0'))
329 PyErr_Format(PyExc_ValueError,
330 "could not convert string to float: "
331 "%.200s", s);
332 else if (fail_pos == s)
333 PyErr_Format(PyExc_ValueError,
334 "could not convert string to float: "
335 "%.200s", s);
336 else if (errno == ERANGE && fabs(x) >= 1.0 && overflow_exception)
337 PyErr_Format(overflow_exception,
338 "value too large to convert to float: "
339 "%.200s", s);
340 else
341 result = x;
342
343 if (endptr != NULL)
344 *endptr = fail_pos;
345 return result;
346}
Eric Smith0923d1d2009-04-16 20:16:10 +0000347
Eric Smithb2c7af82008-04-30 02:12:09 +0000348/* Given a string that may have a decimal point in the current
349 locale, change it back to a dot. Since the string cannot get
350 longer, no need for a maximum buffer size parameter. */
351Py_LOCAL_INLINE(void)
352change_decimal_from_locale_to_dot(char* buffer)
353{
354 struct lconv *locale_data = localeconv();
355 const char *decimal_point = locale_data->decimal_point;
356
357 if (decimal_point[0] != '.' || decimal_point[1] != 0) {
358 size_t decimal_point_len = strlen(decimal_point);
359
360 if (*buffer == '+' || *buffer == '-')
361 buffer++;
Eric Smith6dc46f52009-04-27 20:39:49 +0000362 while (Py_ISDIGIT(*buffer))
Eric Smithb2c7af82008-04-30 02:12:09 +0000363 buffer++;
364 if (strncmp(buffer, decimal_point, decimal_point_len) == 0) {
365 *buffer = '.';
366 buffer++;
367 if (decimal_point_len > 1) {
368 /* buffer needs to get smaller */
369 size_t rest_len = strlen(buffer +
370 (decimal_point_len - 1));
371 memmove(buffer,
372 buffer + (decimal_point_len - 1),
373 rest_len);
374 buffer[rest_len] = 0;
375 }
376 }
377 }
378}
379
Martin v. Löwis737ea822004-06-08 18:52:54 +0000380
Christian Heimesc3f30c42008-02-22 16:37:40 +0000381/* From the C99 standard, section 7.19.6:
382The exponent always contains at least two digits, and only as many more digits
383as necessary to represent the exponent.
384*/
385#define MIN_EXPONENT_DIGITS 2
386
Eric Smithb2c7af82008-04-30 02:12:09 +0000387/* Ensure that any exponent, if present, is at least MIN_EXPONENT_DIGITS
388 in length. */
389Py_LOCAL_INLINE(void)
Mark Dickinsonce95e562009-04-26 20:02:24 +0000390ensure_minimum_exponent_length(char* buffer, size_t buf_size)
Eric Smithb2c7af82008-04-30 02:12:09 +0000391{
392 char *p = strpbrk(buffer, "eE");
393 if (p && (*(p + 1) == '-' || *(p + 1) == '+')) {
394 char *start = p + 2;
395 int exponent_digit_cnt = 0;
396 int leading_zero_cnt = 0;
397 int in_leading_zeros = 1;
398 int significant_digit_cnt;
399
400 /* Skip over the exponent and the sign. */
401 p += 2;
402
403 /* Find the end of the exponent, keeping track of leading
404 zeros. */
Eric Smith6dc46f52009-04-27 20:39:49 +0000405 while (*p && Py_ISDIGIT(*p)) {
Eric Smithb2c7af82008-04-30 02:12:09 +0000406 if (in_leading_zeros && *p == '0')
407 ++leading_zero_cnt;
408 if (*p != '0')
409 in_leading_zeros = 0;
410 ++p;
411 ++exponent_digit_cnt;
412 }
413
414 significant_digit_cnt = exponent_digit_cnt - leading_zero_cnt;
415 if (exponent_digit_cnt == MIN_EXPONENT_DIGITS) {
416 /* If there are 2 exactly digits, we're done,
417 regardless of what they contain */
418 }
419 else if (exponent_digit_cnt > MIN_EXPONENT_DIGITS) {
420 int extra_zeros_cnt;
421
422 /* There are more than 2 digits in the exponent. See
423 if we can delete some of the leading zeros */
424 if (significant_digit_cnt < MIN_EXPONENT_DIGITS)
425 significant_digit_cnt = MIN_EXPONENT_DIGITS;
426 extra_zeros_cnt = exponent_digit_cnt -
427 significant_digit_cnt;
428
429 /* Delete extra_zeros_cnt worth of characters from the
430 front of the exponent */
431 assert(extra_zeros_cnt >= 0);
432
433 /* Add one to significant_digit_cnt to copy the
434 trailing 0 byte, thus setting the length */
435 memmove(start,
436 start + extra_zeros_cnt,
437 significant_digit_cnt + 1);
438 }
439 else {
440 /* If there are fewer than 2 digits, add zeros
441 until there are 2, if there's enough room */
442 int zeros = MIN_EXPONENT_DIGITS - exponent_digit_cnt;
443 if (start + zeros + exponent_digit_cnt + 1
444 < buffer + buf_size) {
445 memmove(start + zeros, start,
446 exponent_digit_cnt + 1);
447 memset(start, '0', zeros);
448 }
449 }
450 }
451}
452
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000453/* Remove trailing zeros after the decimal point from a numeric string; also
454 remove the decimal point if all digits following it are zero. The numeric
455 string must end in '\0', and should not have any leading or trailing
456 whitespace. Assumes that the decimal point is '.'. */
Eric Smithb2c7af82008-04-30 02:12:09 +0000457Py_LOCAL_INLINE(void)
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000458remove_trailing_zeros(char *buffer)
Eric Smithb2c7af82008-04-30 02:12:09 +0000459{
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000460 char *old_fraction_end, *new_fraction_end, *end, *p;
461
462 p = buffer;
463 if (*p == '-' || *p == '+')
464 /* Skip leading sign, if present */
465 ++p;
466 while (Py_ISDIGIT(*p))
467 ++p;
468
469 /* if there's no decimal point there's nothing to do */
470 if (*p++ != '.')
471 return;
472
473 /* scan any digits after the point */
474 while (Py_ISDIGIT(*p))
475 ++p;
476 old_fraction_end = p;
477
478 /* scan up to ending '\0' */
479 while (*p != '\0')
480 p++;
481 /* +1 to make sure that we move the null byte as well */
482 end = p+1;
483
484 /* scan back from fraction_end, looking for removable zeros */
485 p = old_fraction_end;
486 while (*(p-1) == '0')
487 --p;
488 /* and remove point if we've got that far */
489 if (*(p-1) == '.')
490 --p;
491 new_fraction_end = p;
492
493 memmove(new_fraction_end, old_fraction_end, end-old_fraction_end);
494}
495
496/* Ensure that buffer has a decimal point in it. The decimal point will not
497 be in the current locale, it will always be '.'. Don't add a decimal point
498 if an exponent is present. Also, convert to exponential notation where
499 adding a '.0' would produce too many significant digits (see issue 5864).
500
501 Returns a pointer to the fixed buffer, or NULL on failure.
502*/
503Py_LOCAL_INLINE(char *)
504ensure_decimal_point(char* buffer, size_t buf_size, int precision)
505{
506 int digit_count, insert_count = 0, convert_to_exp = 0;
507 char *chars_to_insert, *digits_start;
Eric Smithb2c7af82008-04-30 02:12:09 +0000508
509 /* search for the first non-digit character */
510 char *p = buffer;
Eric Smith2ad79e82008-07-19 00:33:23 +0000511 if (*p == '-' || *p == '+')
512 /* Skip leading sign, if present. I think this could only
513 ever be '-', but it can't hurt to check for both. */
514 ++p;
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000515 digits_start = p;
Eric Smith6dc46f52009-04-27 20:39:49 +0000516 while (*p && Py_ISDIGIT(*p))
Eric Smithb2c7af82008-04-30 02:12:09 +0000517 ++p;
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000518 digit_count = Py_SAFE_DOWNCAST(p - digits_start, Py_ssize_t, int);
Eric Smithb2c7af82008-04-30 02:12:09 +0000519
520 if (*p == '.') {
Eric Smith6dc46f52009-04-27 20:39:49 +0000521 if (Py_ISDIGIT(*(p+1))) {
Eric Smithb2c7af82008-04-30 02:12:09 +0000522 /* Nothing to do, we already have a decimal
523 point and a digit after it */
524 }
525 else {
526 /* We have a decimal point, but no following
527 digit. Insert a zero after the decimal. */
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000528 /* can't ever get here via PyOS_double_to_string */
529 assert(precision == -1);
Eric Smithb2c7af82008-04-30 02:12:09 +0000530 ++p;
531 chars_to_insert = "0";
532 insert_count = 1;
533 }
534 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000535 else if (!(*p == 'e' || *p == 'E')) {
536 /* Don't add ".0" if we have an exponent. */
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000537 if (digit_count == precision) {
538 /* issue 5864: don't add a trailing .0 in the case
539 where the '%g'-formatted result already has as many
540 significant digits as were requested. Switch to
541 exponential notation instead. */
542 convert_to_exp = 1;
543 /* no exponent, no point, and we shouldn't land here
544 for infs and nans, so we must be at the end of the
545 string. */
546 assert(*p == '\0');
547 }
548 else {
549 assert(precision == -1 || digit_count < precision);
550 chars_to_insert = ".0";
551 insert_count = 2;
552 }
Eric Smithb2c7af82008-04-30 02:12:09 +0000553 }
554 if (insert_count) {
555 size_t buf_len = strlen(buffer);
556 if (buf_len + insert_count + 1 >= buf_size) {
557 /* If there is not enough room in the buffer
558 for the additional text, just skip it. It's
559 not worth generating an error over. */
560 }
561 else {
562 memmove(p + insert_count, p,
563 buffer + strlen(buffer) - p + 1);
564 memcpy(p, chars_to_insert, insert_count);
565 }
566 }
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000567 if (convert_to_exp) {
568 int written;
569 size_t buf_avail;
570 p = digits_start;
571 /* insert decimal point */
572 assert(digit_count >= 1);
573 memmove(p+2, p+1, digit_count); /* safe, but overwrites nul */
574 p[1] = '.';
575 p += digit_count+1;
576 assert(p <= buf_size+buffer);
577 buf_avail = buf_size+buffer-p;
578 if (buf_avail == 0)
579 return NULL;
580 /* Add exponent. It's okay to use lower case 'e': we only
581 arrive here as a result of using the empty format code or
582 repr/str builtins and those never want an upper case 'E' */
583 written = PyOS_snprintf(p, buf_avail, "e%+.02d", digit_count-1);
584 if (!(0 <= written &&
585 written < Py_SAFE_DOWNCAST(buf_avail, size_t, int)))
586 /* output truncated, or something else bad happened */
587 return NULL;
588 remove_trailing_zeros(buffer);
589 }
590 return buffer;
Eric Smithb2c7af82008-04-30 02:12:09 +0000591}
592
Christian Heimesc3f30c42008-02-22 16:37:40 +0000593/* see FORMATBUFLEN in unicodeobject.c */
594#define FLOAT_FORMATBUFLEN 120
595
Martin v. Löwis737ea822004-06-08 18:52:54 +0000596/**
597 * PyOS_ascii_formatd:
598 * @buffer: A buffer to place the resulting string in
Christian Heimesb186d002008-03-18 15:15:01 +0000599 * @buf_size: The length of the buffer.
Martin v. Löwis737ea822004-06-08 18:52:54 +0000600 * @format: The printf()-style format to use for the
601 * code to use for converting.
602 * @d: The #gdouble to convert
603 *
604 * Converts a #gdouble to a string, using the '.' as
605 * decimal point. To format the number you pass in
606 * a printf()-style format string. Allowed conversion
Eric Smith0923d1d2009-04-16 20:16:10 +0000607 * specifiers are 'e', 'E', 'f', 'F', 'g', 'G', and 'Z'.
Martin v. Löwis737ea822004-06-08 18:52:54 +0000608 *
Christian Heimesb186d002008-03-18 15:15:01 +0000609 * 'Z' is the same as 'g', except it always has a decimal and
610 * at least one digit after the decimal.
Christian Heimesc3f30c42008-02-22 16:37:40 +0000611 *
Martin v. Löwis737ea822004-06-08 18:52:54 +0000612 * Return value: The pointer to the buffer with the converted string.
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000613 * On failure returns NULL but does not set any Python exception.
Martin v. Löwis737ea822004-06-08 18:52:54 +0000614 **/
615char *
Eric Smithcc32a112009-04-26 21:35:14 +0000616_PyOS_ascii_formatd(char *buffer,
Christian Heimesb186d002008-03-18 15:15:01 +0000617 size_t buf_size,
Martin v. Löwis737ea822004-06-08 18:52:54 +0000618 const char *format,
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000619 double d,
620 int precision)
Martin v. Löwis737ea822004-06-08 18:52:54 +0000621{
Martin v. Löwis737ea822004-06-08 18:52:54 +0000622 char format_char;
Christian Heimesc3f30c42008-02-22 16:37:40 +0000623 size_t format_len = strlen(format);
624
Christian Heimesb186d002008-03-18 15:15:01 +0000625 /* Issue 2264: code 'Z' requires copying the format. 'Z' is 'g', but
626 also with at least one character past the decimal. */
Christian Heimesc3f30c42008-02-22 16:37:40 +0000627 char tmp_format[FLOAT_FORMATBUFLEN];
Martin v. Löwis737ea822004-06-08 18:52:54 +0000628
Christian Heimesc3f30c42008-02-22 16:37:40 +0000629 /* The last character in the format string must be the format char */
630 format_char = format[format_len - 1];
Martin v. Löwis737ea822004-06-08 18:52:54 +0000631
Martin v. Löwis737ea822004-06-08 18:52:54 +0000632 if (format[0] != '%')
633 return NULL;
634
Christian Heimesc3f30c42008-02-22 16:37:40 +0000635 /* I'm not sure why this test is here. It's ensuring that the format
636 string after the first character doesn't have a single quote, a
637 lowercase l, or a percent. This is the reverse of the commented-out
638 test about 10 lines ago. */
Martin v. Löwis737ea822004-06-08 18:52:54 +0000639 if (strpbrk(format + 1, "'l%"))
640 return NULL;
641
Christian Heimesb186d002008-03-18 15:15:01 +0000642 /* Also curious about this function is that it accepts format strings
643 like "%xg", which are invalid for floats. In general, the
644 interface to this function is not very good, but changing it is
645 difficult because it's a public API. */
646
Martin v. Löwis737ea822004-06-08 18:52:54 +0000647 if (!(format_char == 'e' || format_char == 'E' ||
648 format_char == 'f' || format_char == 'F' ||
Christian Heimesc3f30c42008-02-22 16:37:40 +0000649 format_char == 'g' || format_char == 'G' ||
Eric Smith0923d1d2009-04-16 20:16:10 +0000650 format_char == 'Z'))
Martin v. Löwis737ea822004-06-08 18:52:54 +0000651 return NULL;
652
Eric Smith0923d1d2009-04-16 20:16:10 +0000653 /* Map 'Z' format_char to 'g', by copying the format string and
Christian Heimesb186d002008-03-18 15:15:01 +0000654 replacing the final char with a 'g' */
Eric Smith0923d1d2009-04-16 20:16:10 +0000655 if (format_char == 'Z') {
Christian Heimesc3f30c42008-02-22 16:37:40 +0000656 if (format_len + 1 >= sizeof(tmp_format)) {
657 /* The format won't fit in our copy. Error out. In
Christian Heimesb186d002008-03-18 15:15:01 +0000658 practice, this will never happen and will be
659 detected by returning NULL */
Christian Heimesc3f30c42008-02-22 16:37:40 +0000660 return NULL;
661 }
662 strcpy(tmp_format, format);
663 tmp_format[format_len - 1] = 'g';
664 format = tmp_format;
665 }
Martin v. Löwis737ea822004-06-08 18:52:54 +0000666
Christian Heimesb186d002008-03-18 15:15:01 +0000667
Christian Heimesc3f30c42008-02-22 16:37:40 +0000668 /* Have PyOS_snprintf do the hard work */
Christian Heimesb186d002008-03-18 15:15:01 +0000669 PyOS_snprintf(buffer, buf_size, format, d);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000670
Eric Smithb2c7af82008-04-30 02:12:09 +0000671 /* Do various fixups on the return string */
Martin v. Löwis737ea822004-06-08 18:52:54 +0000672
Eric Smithb2c7af82008-04-30 02:12:09 +0000673 /* Get the current locale, and find the decimal point string.
Eric Smith0923d1d2009-04-16 20:16:10 +0000674 Convert that string back to a dot. */
675 change_decimal_from_locale_to_dot(buffer);
Christian Heimesc3f30c42008-02-22 16:37:40 +0000676
677 /* If an exponent exists, ensure that the exponent is at least
678 MIN_EXPONENT_DIGITS digits, providing the buffer is large enough
679 for the extra zeros. Also, if there are more than
680 MIN_EXPONENT_DIGITS, remove as many zeros as possible until we get
681 back to MIN_EXPONENT_DIGITS */
Mark Dickinsonce95e562009-04-26 20:02:24 +0000682 ensure_minimum_exponent_length(buffer, buf_size);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000683
Christian Heimesb186d002008-03-18 15:15:01 +0000684 /* If format_char is 'Z', make sure we have at least one character
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000685 after the decimal point (and make sure we have a decimal point);
686 also switch to exponential notation in some edge cases where the
687 extra character would produce more significant digits that we
688 really want. */
Eric Smithb2c7af82008-04-30 02:12:09 +0000689 if (format_char == 'Z')
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000690 buffer = ensure_decimal_point(buffer, buf_size, precision);
Christian Heimesb186d002008-03-18 15:15:01 +0000691
Martin v. Löwis737ea822004-06-08 18:52:54 +0000692 return buffer;
693}
694
Eric Smithcc32a112009-04-26 21:35:14 +0000695char *
696PyOS_ascii_formatd(char *buffer,
697 size_t buf_size,
698 const char *format,
699 double d)
700{
701 if (PyErr_WarnEx(PyExc_DeprecationWarning,
702 "PyOS_ascii_formatd is deprecated, "
703 "use PyOS_double_to_string instead", 1) < 0)
704 return NULL;
705
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000706 return _PyOS_ascii_formatd(buffer, buf_size, format, d, -1);
Eric Smithcc32a112009-04-26 21:35:14 +0000707}
708
Eric Smith0923d1d2009-04-16 20:16:10 +0000709#ifdef PY_NO_SHORT_FLOAT_REPR
710
711/* The fallback code to use if _Py_dg_dtoa is not available. */
712
713PyAPI_FUNC(char *) PyOS_double_to_string(double val,
714 char format_code,
715 int precision,
716 int flags,
717 int *type)
Martin v. Löwis737ea822004-06-08 18:52:54 +0000718{
Eric Smith0923d1d2009-04-16 20:16:10 +0000719 char format[32];
Mark Dickinsonf489caf2009-05-01 11:42:00 +0000720 Py_ssize_t bufsize;
721 char *buf;
722 int t, exp;
Eric Smith0923d1d2009-04-16 20:16:10 +0000723 int upper = 0;
724
725 /* Validate format_code, and map upper and lower case */
726 switch (format_code) {
727 case 'e': /* exponent */
728 case 'f': /* fixed */
729 case 'g': /* general */
730 break;
731 case 'E':
732 upper = 1;
733 format_code = 'e';
734 break;
735 case 'F':
736 upper = 1;
737 format_code = 'f';
738 break;
739 case 'G':
740 upper = 1;
741 format_code = 'g';
742 break;
743 case 'r': /* repr format */
744 /* Supplied precision is unused, must be 0. */
745 if (precision != 0) {
746 PyErr_BadInternalCall();
747 return NULL;
748 }
Eric Smith63376222009-05-05 14:04:18 +0000749 /* The repr() precision (17 significant decimal digits) is the
750 minimal number that is guaranteed to have enough precision
751 so that if the number is read back in the exact same binary
752 value is recreated. This is true for IEEE floating point
753 by design, and also happens to work for all other modern
754 hardware. */
Eric Smith0923d1d2009-04-16 20:16:10 +0000755 precision = 17;
756 format_code = 'g';
757 break;
Eric Smith0923d1d2009-04-16 20:16:10 +0000758 default:
759 PyErr_BadInternalCall();
760 return NULL;
761 }
762
Mark Dickinsonf489caf2009-05-01 11:42:00 +0000763 /* Here's a quick-and-dirty calculation to figure out how big a buffer
764 we need. In general, for a finite float we need:
765
766 1 byte for each digit of the decimal significand, and
767
768 1 for a possible sign
769 1 for a possible decimal point
770 2 for a possible [eE][+-]
771 1 for each digit of the exponent; if we allow 19 digits
772 total then we're safe up to exponents of 2**63.
773 1 for the trailing nul byte
774
775 This gives a total of 24 + the number of digits in the significand,
776 and the number of digits in the significand is:
777
778 for 'g' format: at most precision, except possibly
779 when precision == 0, when it's 1.
780 for 'e' format: precision+1
781 for 'f' format: precision digits after the point, at least 1
782 before. To figure out how many digits appear before the point
783 we have to examine the size of the number. If fabs(val) < 1.0
784 then there will be only one digit before the point. If
785 fabs(val) >= 1.0, then there are at most
786
787 1+floor(log10(ceiling(fabs(val))))
788
789 digits before the point (where the 'ceiling' allows for the
790 possibility that the rounding rounds the integer part of val
791 up). A safe upper bound for the above quantity is
792 1+floor(exp/3), where exp is the unique integer such that 0.5
793 <= fabs(val)/2**exp < 1.0. This exp can be obtained from
794 frexp.
795
796 So we allow room for precision+1 digits for all formats, plus an
797 extra floor(exp/3) digits for 'f' format.
798
799 */
800
801 if (Py_IS_NAN(val) || Py_IS_INFINITY(val))
802 /* 3 for 'inf'/'nan', 1 for sign, 1 for '\0' */
803 bufsize = 5;
804 else {
805 bufsize = 25 + precision;
806 if (format_code == 'f' && fabs(val) >= 1.0) {
807 frexp(val, &exp);
808 bufsize += exp/3;
809 }
810 }
811
812 buf = PyMem_Malloc(bufsize);
813 if (buf == NULL) {
814 PyErr_NoMemory();
815 return NULL;
816 }
817
Eric Smith0923d1d2009-04-16 20:16:10 +0000818 /* Handle nan and inf. */
819 if (Py_IS_NAN(val)) {
820 strcpy(buf, "nan");
821 t = Py_DTST_NAN;
822 } else if (Py_IS_INFINITY(val)) {
823 if (copysign(1., val) == 1.)
824 strcpy(buf, "inf");
825 else
826 strcpy(buf, "-inf");
827 t = Py_DTST_INFINITE;
828 } else {
829 t = Py_DTST_FINITE;
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000830 if (flags & Py_DTSF_ADD_DOT_0)
Eric Smith0923d1d2009-04-16 20:16:10 +0000831 format_code = 'Z';
832
Eric Smithcc32a112009-04-26 21:35:14 +0000833 PyOS_snprintf(format, sizeof(format), "%%%s.%i%c",
834 (flags & Py_DTSF_ALT ? "#" : ""), precision,
835 format_code);
Mark Dickinsonf489caf2009-05-01 11:42:00 +0000836 _PyOS_ascii_formatd(buf, bufsize, format, val, precision);
Eric Smith0923d1d2009-04-16 20:16:10 +0000837 }
838
Mark Dickinsonad476da2009-04-23 19:14:16 +0000839 /* Add sign when requested. It's convenient (esp. when formatting
840 complex numbers) to include a sign even for inf and nan. */
Mark Dickinsonf489caf2009-05-01 11:42:00 +0000841 if (flags & Py_DTSF_SIGN && buf[0] != '-') {
842 size_t len = strlen(buf);
843 /* the bufsize calculations above should ensure that we've got
844 space to add a sign */
845 assert((size_t)bufsize >= len+2);
846 memmove(buf+1, buf, len+1);
847 buf[0] = '+';
848 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000849 if (upper) {
850 /* Convert to upper case. */
851 char *p1;
Mark Dickinsonf489caf2009-05-01 11:42:00 +0000852 for (p1 = buf; *p1; p1++)
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000853 *p1 = Py_TOUPPER(*p1);
Eric Smith0923d1d2009-04-16 20:16:10 +0000854 }
855
856 if (type)
857 *type = t;
Mark Dickinsonf489caf2009-05-01 11:42:00 +0000858 return buf;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000859}
Eric Smith0923d1d2009-04-16 20:16:10 +0000860
861#else
862
863/* _Py_dg_dtoa is available. */
864
865/* I'm using a lookup table here so that I don't have to invent a non-locale
866 specific way to convert to uppercase */
867#define OFS_INF 0
868#define OFS_NAN 1
869#define OFS_E 2
870
871/* The lengths of these are known to the code below, so don't change them */
872static char *lc_float_strings[] = {
873 "inf",
874 "nan",
875 "e",
876};
877static char *uc_float_strings[] = {
878 "INF",
879 "NAN",
880 "E",
881};
882
883
884/* Convert a double d to a string, and return a PyMem_Malloc'd block of
885 memory contain the resulting string.
886
887 Arguments:
888 d is the double to be converted
Eric Smith63376222009-05-05 14:04:18 +0000889 format_code is one of 'e', 'f', 'g', 'r'. 'e', 'f' and 'g'
890 correspond to '%e', '%f' and '%g'; 'r' corresponds to repr.
Eric Smith0923d1d2009-04-16 20:16:10 +0000891 mode is one of '0', '2' or '3', and is completely determined by
Eric Smith63376222009-05-05 14:04:18 +0000892 format_code: 'e' and 'g' use mode 2; 'f' mode 3, 'r' mode 0.
Eric Smith0923d1d2009-04-16 20:16:10 +0000893 precision is the desired precision
894 always_add_sign is nonzero if a '+' sign should be included for positive
895 numbers
896 add_dot_0_if_integer is nonzero if integers in non-exponential form
Eric Smith63376222009-05-05 14:04:18 +0000897 should have ".0" added. Only applies to format codes 'r' and 'g'.
Eric Smith0923d1d2009-04-16 20:16:10 +0000898 use_alt_formatting is nonzero if alternative formatting should be
Eric Smith63376222009-05-05 14:04:18 +0000899 used. Only applies to format codes 'e', 'f' and 'g'. For code 'g',
900 at most one of use_alt_formatting and add_dot_0_if_integer should
901 be nonzero.
Eric Smith0923d1d2009-04-16 20:16:10 +0000902 type, if non-NULL, will be set to one of these constants to identify
903 the type of the 'd' argument:
904 Py_DTST_FINITE
905 Py_DTST_INFINITE
906 Py_DTST_NAN
907
908 Returns a PyMem_Malloc'd block of memory containing the resulting string,
909 or NULL on error. If NULL is returned, the Python error has been set.
910 */
911
912static char *
913format_float_short(double d, char format_code,
914 int mode, Py_ssize_t precision,
915 int always_add_sign, int add_dot_0_if_integer,
916 int use_alt_formatting, char **float_strings, int *type)
917{
918 char *buf = NULL;
919 char *p = NULL;
920 Py_ssize_t bufsize = 0;
921 char *digits, *digits_end;
922 int decpt_as_int, sign, exp_len, exp = 0, use_exp = 0;
923 Py_ssize_t decpt, digits_len, vdigits_start, vdigits_end;
924 _Py_SET_53BIT_PRECISION_HEADER;
925
926 /* _Py_dg_dtoa returns a digit string (no decimal point or exponent).
927 Must be matched by a call to _Py_dg_freedtoa. */
928 _Py_SET_53BIT_PRECISION_START;
929 digits = _Py_dg_dtoa(d, mode, precision, &decpt_as_int, &sign,
930 &digits_end);
931 _Py_SET_53BIT_PRECISION_END;
932
933 decpt = (Py_ssize_t)decpt_as_int;
934 if (digits == NULL) {
935 /* The only failure mode is no memory. */
936 PyErr_NoMemory();
937 goto exit;
938 }
939 assert(digits_end != NULL && digits_end >= digits);
940 digits_len = digits_end - digits;
941
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000942 if (digits_len && !Py_ISDIGIT(digits[0])) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000943 /* Infinities and nans here; adapt Gay's output,
944 so convert Infinity to inf and NaN to nan, and
945 ignore sign of nan. Then return. */
946
Mark Dickinsonad476da2009-04-23 19:14:16 +0000947 /* ignore the actual sign of a nan */
948 if (digits[0] == 'n' || digits[0] == 'N')
949 sign = 0;
950
Eric Smith0923d1d2009-04-16 20:16:10 +0000951 /* We only need 5 bytes to hold the result "+inf\0" . */
952 bufsize = 5; /* Used later in an assert. */
953 buf = (char *)PyMem_Malloc(bufsize);
954 if (buf == NULL) {
955 PyErr_NoMemory();
956 goto exit;
957 }
958 p = buf;
959
Mark Dickinsonad476da2009-04-23 19:14:16 +0000960 if (sign == 1) {
961 *p++ = '-';
962 }
963 else if (always_add_sign) {
964 *p++ = '+';
965 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000966 if (digits[0] == 'i' || digits[0] == 'I') {
Eric Smith0923d1d2009-04-16 20:16:10 +0000967 strncpy(p, float_strings[OFS_INF], 3);
968 p += 3;
969
970 if (type)
971 *type = Py_DTST_INFINITE;
972 }
973 else if (digits[0] == 'n' || digits[0] == 'N') {
Eric Smith0923d1d2009-04-16 20:16:10 +0000974 strncpy(p, float_strings[OFS_NAN], 3);
975 p += 3;
976
977 if (type)
978 *type = Py_DTST_NAN;
979 }
980 else {
981 /* shouldn't get here: Gay's code should always return
982 something starting with a digit, an 'I', or 'N' */
983 strncpy(p, "ERR", 3);
984 p += 3;
985 assert(0);
986 }
987 goto exit;
988 }
989
990 /* The result must be finite (not inf or nan). */
991 if (type)
992 *type = Py_DTST_FINITE;
993
994
995 /* We got digits back, format them. We may need to pad 'digits'
996 either on the left or right (or both) with extra zeros, so in
997 general the resulting string has the form
998
999 [<sign>]<zeros><digits><zeros>[<exponent>]
1000
1001 where either of the <zeros> pieces could be empty, and there's a
1002 decimal point that could appear either in <digits> or in the
1003 leading or trailing <zeros>.
1004
1005 Imagine an infinite 'virtual' string vdigits, consisting of the
1006 string 'digits' (starting at index 0) padded on both the left and
1007 right with infinite strings of zeros. We want to output a slice
1008
1009 vdigits[vdigits_start : vdigits_end]
1010
1011 of this virtual string. Thus if vdigits_start < 0 then we'll end
1012 up producing some leading zeros; if vdigits_end > digits_len there
1013 will be trailing zeros in the output. The next section of code
1014 determines whether to use an exponent or not, figures out the
1015 position 'decpt' of the decimal point, and computes 'vdigits_start'
1016 and 'vdigits_end'. */
1017 vdigits_end = digits_len;
1018 switch (format_code) {
1019 case 'e':
1020 use_exp = 1;
1021 vdigits_end = precision;
1022 break;
1023 case 'f':
1024 vdigits_end = decpt + precision;
1025 break;
1026 case 'g':
Mark Dickinsond3ca5572009-04-29 18:47:07 +00001027 if (decpt <= -4 || decpt >
1028 (add_dot_0_if_integer ? precision-1 : precision))
Eric Smith0923d1d2009-04-16 20:16:10 +00001029 use_exp = 1;
1030 if (use_alt_formatting)
1031 vdigits_end = precision;
1032 break;
1033 case 'r':
1034 /* convert to exponential format at 1e16. We used to convert
1035 at 1e17, but that gives odd-looking results for some values
1036 when a 16-digit 'shortest' repr is padded with bogus zeros.
1037 For example, repr(2e16+8) would give 20000000000000010.0;
1038 the true value is 20000000000000008.0. */
1039 if (decpt <= -4 || decpt > 16)
1040 use_exp = 1;
1041 break;
Eric Smith0923d1d2009-04-16 20:16:10 +00001042 default:
1043 PyErr_BadInternalCall();
1044 goto exit;
1045 }
1046
1047 /* if using an exponent, reset decimal point position to 1 and adjust
1048 exponent accordingly.*/
1049 if (use_exp) {
1050 exp = decpt - 1;
1051 decpt = 1;
1052 }
1053 /* ensure vdigits_start < decpt <= vdigits_end, or vdigits_start <
1054 decpt < vdigits_end if add_dot_0_if_integer and no exponent */
1055 vdigits_start = decpt <= 0 ? decpt-1 : 0;
1056 if (!use_exp && add_dot_0_if_integer)
1057 vdigits_end = vdigits_end > decpt ? vdigits_end : decpt + 1;
1058 else
1059 vdigits_end = vdigits_end > decpt ? vdigits_end : decpt;
1060
1061 /* double check inequalities */
1062 assert(vdigits_start <= 0 &&
1063 0 <= digits_len &&
1064 digits_len <= vdigits_end);
1065 /* decimal point should be in (vdigits_start, vdigits_end] */
1066 assert(vdigits_start < decpt && decpt <= vdigits_end);
1067
1068 /* Compute an upper bound how much memory we need. This might be a few
1069 chars too long, but no big deal. */
1070 bufsize =
1071 /* sign, decimal point and trailing 0 byte */
1072 3 +
1073
1074 /* total digit count (including zero padding on both sides) */
1075 (vdigits_end - vdigits_start) +
1076
1077 /* exponent "e+100", max 3 numerical digits */
1078 (use_exp ? 5 : 0);
1079
1080 /* Now allocate the memory and initialize p to point to the start of
1081 it. */
1082 buf = (char *)PyMem_Malloc(bufsize);
1083 if (buf == NULL) {
1084 PyErr_NoMemory();
1085 goto exit;
1086 }
1087 p = buf;
1088
1089 /* Add a negative sign if negative, and a plus sign if non-negative
1090 and always_add_sign is true. */
1091 if (sign == 1)
1092 *p++ = '-';
1093 else if (always_add_sign)
1094 *p++ = '+';
1095
1096 /* note that exactly one of the three 'if' conditions is true,
1097 so we include exactly one decimal point */
1098 /* Zero padding on left of digit string */
1099 if (decpt <= 0) {
1100 memset(p, '0', decpt-vdigits_start);
1101 p += decpt - vdigits_start;
1102 *p++ = '.';
1103 memset(p, '0', 0-decpt);
1104 p += 0-decpt;
1105 }
1106 else {
1107 memset(p, '0', 0-vdigits_start);
1108 p += 0 - vdigits_start;
1109 }
1110
1111 /* Digits, with included decimal point */
1112 if (0 < decpt && decpt <= digits_len) {
1113 strncpy(p, digits, decpt-0);
1114 p += decpt-0;
1115 *p++ = '.';
1116 strncpy(p, digits+decpt, digits_len-decpt);
1117 p += digits_len-decpt;
1118 }
1119 else {
1120 strncpy(p, digits, digits_len);
1121 p += digits_len;
1122 }
1123
1124 /* And zeros on the right */
1125 if (digits_len < decpt) {
1126 memset(p, '0', decpt-digits_len);
1127 p += decpt-digits_len;
1128 *p++ = '.';
1129 memset(p, '0', vdigits_end-decpt);
1130 p += vdigits_end-decpt;
1131 }
1132 else {
1133 memset(p, '0', vdigits_end-digits_len);
1134 p += vdigits_end-digits_len;
1135 }
1136
1137 /* Delete a trailing decimal pt unless using alternative formatting. */
1138 if (p[-1] == '.' && !use_alt_formatting)
1139 p--;
1140
1141 /* Now that we've done zero padding, add an exponent if needed. */
1142 if (use_exp) {
1143 *p++ = float_strings[OFS_E][0];
1144 exp_len = sprintf(p, "%+.02d", exp);
1145 p += exp_len;
1146 }
1147 exit:
1148 if (buf) {
1149 *p = '\0';
1150 /* It's too late if this fails, as we've already stepped on
1151 memory that isn't ours. But it's an okay debugging test. */
1152 assert(p-buf < bufsize);
1153 }
1154 if (digits)
1155 _Py_dg_freedtoa(digits);
1156
1157 return buf;
1158}
1159
1160
1161PyAPI_FUNC(char *) PyOS_double_to_string(double val,
Eric Smith193125a2009-04-16 22:08:31 +00001162 char format_code,
1163 int precision,
1164 int flags,
Eric Smith0923d1d2009-04-16 20:16:10 +00001165 int *type)
1166{
Eric Smith193125a2009-04-16 22:08:31 +00001167 char **float_strings = lc_float_strings;
1168 int mode;
Eric Smith0923d1d2009-04-16 20:16:10 +00001169
Eric Smith193125a2009-04-16 22:08:31 +00001170 /* Validate format_code, and map upper and lower case. Compute the
1171 mode and make any adjustments as needed. */
Eric Smith0923d1d2009-04-16 20:16:10 +00001172 switch (format_code) {
Eric Smith193125a2009-04-16 22:08:31 +00001173 /* exponent */
Eric Smith0923d1d2009-04-16 20:16:10 +00001174 case 'E':
Eric Smith0923d1d2009-04-16 20:16:10 +00001175 float_strings = uc_float_strings;
Eric Smith193125a2009-04-16 22:08:31 +00001176 format_code = 'e';
1177 /* Fall through. */
Eric Smith0923d1d2009-04-16 20:16:10 +00001178 case 'e':
1179 mode = 2;
1180 precision++;
1181 break;
Eric Smith193125a2009-04-16 22:08:31 +00001182
1183 /* fixed */
1184 case 'F':
1185 float_strings = uc_float_strings;
1186 format_code = 'f';
1187 /* Fall through. */
Eric Smith0923d1d2009-04-16 20:16:10 +00001188 case 'f':
1189 mode = 3;
1190 break;
Eric Smith193125a2009-04-16 22:08:31 +00001191
1192 /* general */
1193 case 'G':
1194 float_strings = uc_float_strings;
1195 format_code = 'g';
1196 /* Fall through. */
Eric Smith0923d1d2009-04-16 20:16:10 +00001197 case 'g':
1198 mode = 2;
1199 /* precision 0 makes no sense for 'g' format; interpret as 1 */
1200 if (precision == 0)
1201 precision = 1;
1202 break;
Eric Smith193125a2009-04-16 22:08:31 +00001203
1204 /* repr format */
Eric Smith0923d1d2009-04-16 20:16:10 +00001205 case 'r':
Eric Smith0923d1d2009-04-16 20:16:10 +00001206 mode = 0;
1207 /* Supplied precision is unused, must be 0. */
1208 if (precision != 0) {
1209 PyErr_BadInternalCall();
1210 return NULL;
1211 }
1212 break;
Eric Smith193125a2009-04-16 22:08:31 +00001213
Eric Smith193125a2009-04-16 22:08:31 +00001214 default:
1215 PyErr_BadInternalCall();
1216 return NULL;
Eric Smith0923d1d2009-04-16 20:16:10 +00001217 }
1218
Eric Smith193125a2009-04-16 22:08:31 +00001219 return format_float_short(val, format_code, mode, precision,
Eric Smith0923d1d2009-04-16 20:16:10 +00001220 flags & Py_DTSF_SIGN,
1221 flags & Py_DTSF_ADD_DOT_0,
1222 flags & Py_DTSF_ALT,
1223 float_strings, type);
1224}
1225#endif /* ifdef PY_NO_SHORT_FLOAT_REPR */