blob: a1d7ff09fcbd45d72535a6516f0353ee288615e6 [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{
12 while(*t && Py_TOLOWER(*s) == *t) {
13 s++;
14 t++;
15 }
16 return *t ? 0 : 1;
17}
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{
28 double retval;
29 const char *s;
30 int negate = 0;
31
32 s = p;
33 if (*s == '-') {
34 negate = 1;
35 s++;
36 }
37 else if (*s == '+') {
38 s++;
39 }
40 if (case_insensitive_match(s, "inf")) {
41 s += 3;
42 if (case_insensitive_match(s, "inity"))
43 s += 5;
44 retval = negate ? -Py_HUGE_VAL : Py_HUGE_VAL;
45 }
46#ifdef Py_NAN
47 else if (case_insensitive_match(s, "nan")) {
48 s += 3;
49 retval = negate ? -Py_NAN : Py_NAN;
50 }
51#endif
52 else {
53 s = p;
54 retval = -1.0;
55 }
56 *endptr = (char *)s;
57 return retval;
58}
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.
65 *
66 * Converts a string to a #gdouble value.
67 * This function behaves like the standard strtod() function
68 * does in the C locale. It does this without actually
69 * changing the current locale, since that would not be
70 * thread-safe.
71 *
72 * This function is typically used when reading configuration
73 * files or other non-user input that should be locale independent.
74 * To handle input from the user you should normally use the
75 * locale-sensitive system strtod() function.
76 *
77 * If the correct value would cause overflow, plus or minus %HUGE_VAL
78 * is returned (according to the sign of the value), and %ERANGE is
79 * stored in %errno. If the correct value would cause underflow,
80 * zero is returned and %ERANGE is stored in %errno.
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000081 * If memory allocation fails, %ENOMEM is stored in %errno.
Martin v. Löwis737ea822004-06-08 18:52:54 +000082 *
83 * This function resets %errno before calling strtod() so that
84 * you can reliably detect overflow and underflow.
85 *
86 * Return value: the #gdouble value.
87 **/
Eric 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{
94 double result;
95 _Py_SET_53BIT_PRECISION_HEADER;
96
97 assert(nptr != NULL);
98 /* Set errno to zero, so that we can distinguish zero results
99 and underflows */
100 errno = 0;
101
102 _Py_SET_53BIT_PRECISION_START;
103 result = _Py_dg_strtod(nptr, endptr);
104 _Py_SET_53BIT_PRECISION_END;
105
Mark Dickinsonbd16edd2009-05-20 22:05:25 +0000106 if (*endptr == nptr)
Mark Dickinsonf41d29a2010-01-24 10:16:29 +0000107 /* string might represent an inf or nan */
Mark Dickinsonbd16edd2009-05-20 22:05:25 +0000108 result = _Py_parse_inf_or_nan(nptr, endptr);
109
Eric Smith0923d1d2009-04-16 20:16:10 +0000110 return result;
111
112}
113
114#else
115
116/*
117 Use system strtod; since strtod is locale aware, we may
118 have to first fix the decimal separator.
119
120 Note that unlike _Py_dg_strtod, the system strtod may not always give
121 correctly rounded results.
122*/
123
Eric 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{
127 char *fail_pos;
Neal Norwitz0e7a0ed2005-12-18 05:37:36 +0000128 double val = -1.0;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000129 struct lconv *locale_data;
130 const char *decimal_point;
Neal Norwitzd39d8612006-01-08 01:03:36 +0000131 size_t decimal_point_len;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000132 const char *p, *decimal_point_pos;
133 const char *end = NULL; /* Silence gcc */
Christian Heimesfaf2f632008-01-06 16:59:19 +0000134 const char *digits_pos = NULL;
135 int negate = 0;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000136
Martin v. Löwis737ea822004-06-08 18:52:54 +0000137 assert(nptr != NULL);
138
139 fail_pos = NULL;
140
141 locale_data = localeconv();
142 decimal_point = locale_data->decimal_point;
143 decimal_point_len = strlen(decimal_point);
144
145 assert(decimal_point_len != 0);
146
147 decimal_point_pos = NULL;
Christian Heimesfaf2f632008-01-06 16:59:19 +0000148
Mark Dickinsonbd16edd2009-05-20 22:05:25 +0000149 /* Parse infinities and nans */
150 val = _Py_parse_inf_or_nan(nptr, endptr);
151 if (*endptr != nptr)
152 return val;
153
Mark Dickinson6d65df12009-04-26 15:30:47 +0000154 /* Set errno to zero, so that we can distinguish zero results
155 and underflows */
156 errno = 0;
157
Mark Dickinson725bfd82009-05-03 20:33:40 +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) */
Christian Heimesfaf2f632008-01-06 16:59:19 +0000161 p = nptr;
Christian Heimesfaf2f632008-01-06 16:59:19 +0000162 /* Process leading sign, if present */
163 if (*p == '-') {
164 negate = 1;
165 p++;
Mark Dickinson6d65df12009-04-26 15:30:47 +0000166 }
167 else if (*p == '+') {
Christian Heimesfaf2f632008-01-06 16:59:19 +0000168 p++;
169 }
170
Mark Dickinson6d65df12009-04-26 15:30:47 +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;
175
176 /* Check that what's left begins with a digit or decimal point */
Eric Smith6dc46f52009-04-27 20:39:49 +0000177 if (!Py_ISDIGIT(*p) && *p != '.')
Mark Dickinson6d65df12009-04-26 15:30:47 +0000178 goto invalid_string;
179
180 digits_pos = p;
181 if (decimal_point[0] != '.' ||
Martin v. Löwis737ea822004-06-08 18:52:54 +0000182 decimal_point[1] != 0)
183 {
Mark Dickinson6d65df12009-04-26 15:30:47 +0000184 /* 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. */
Eric Smith6dc46f52009-04-27 20:39:49 +0000188 while (Py_ISDIGIT(*p))
Neal Norwitze7214a12005-12-18 05:03:17 +0000189 p++;
190
191 if (*p == '.')
Martin v. Löwis737ea822004-06-08 18:52:54 +0000192 {
Neal Norwitze7214a12005-12-18 05:03:17 +0000193 decimal_point_pos = p++;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000194
Mark Dickinson6d65df12009-04-26 15:30:47 +0000195 /* locate end of number */
Eric Smith6dc46f52009-04-27 20:39:49 +0000196 while (Py_ISDIGIT(*p))
Martin v. Löwis737ea822004-06-08 18:52:54 +0000197 p++;
198
Neal Norwitze7214a12005-12-18 05:03:17 +0000199 if (*p == 'e' || *p == 'E')
200 p++;
201 if (*p == '+' || *p == '-')
202 p++;
Eric Smith6dc46f52009-04-27 20:39:49 +0000203 while (Py_ISDIGIT(*p))
Neal Norwitze7214a12005-12-18 05:03:17 +0000204 p++;
205 end = p;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000206 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000207 else if (strncmp(p, decimal_point, decimal_point_len) == 0)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000208 /* Python bug #1417699 */
Mark Dickinson6d65df12009-04-26 15:30:47 +0000209 goto invalid_string;
Christian Heimesb186d002008-03-18 15:15:01 +0000210 /* For the other cases, we need not convert the decimal
211 point */
Martin v. Löwis737ea822004-06-08 18:52:54 +0000212 }
213
Mark Dickinson6d65df12009-04-26 15:30:47 +0000214 if (decimal_point_pos) {
Martin v. Löwis737ea822004-06-08 18:52:54 +0000215 char *copy, *c;
Mark Dickinson6d65df12009-04-26 15:30:47 +0000216 /* Create a copy of the input, with the '.' converted to the
217 locale-specific decimal point */
Christian Heimesfaf2f632008-01-06 16:59:19 +0000218 copy = (char *)PyMem_MALLOC(end - digits_pos +
219 1 + decimal_point_len);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000220 if (copy == NULL) {
Mark Dickinson725bfd82009-05-03 20:33:40 +0000221 *endptr = (char *)nptr;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000222 errno = ENOMEM;
223 return val;
224 }
Martin v. Löwis737ea822004-06-08 18:52:54 +0000225
226 c = copy;
Christian Heimesfaf2f632008-01-06 16:59:19 +0000227 memcpy(c, digits_pos, decimal_point_pos - digits_pos);
228 c += decimal_point_pos - digits_pos;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000229 memcpy(c, decimal_point, decimal_point_len);
230 c += decimal_point_len;
Christian Heimesb186d002008-03-18 15:15:01 +0000231 memcpy(c, decimal_point_pos + 1,
232 end - (decimal_point_pos + 1));
Martin v. Löwis737ea822004-06-08 18:52:54 +0000233 c += end - (decimal_point_pos + 1);
234 *c = 0;
235
236 val = strtod(copy, &fail_pos);
237
238 if (fail_pos)
239 {
240 if (fail_pos > decimal_point_pos)
Christian Heimesfaf2f632008-01-06 16:59:19 +0000241 fail_pos = (char *)digits_pos +
242 (fail_pos - copy) -
243 (decimal_point_len - 1);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000244 else
Christian Heimesfaf2f632008-01-06 16:59:19 +0000245 fail_pos = (char *)digits_pos +
246 (fail_pos - copy);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000247 }
248
Thomas Wouters477c8d52006-05-27 19:21:47 +0000249 PyMem_FREE(copy);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000250
251 }
Neal Norwitze7214a12005-12-18 05:03:17 +0000252 else {
Christian Heimesfaf2f632008-01-06 16:59:19 +0000253 val = strtod(digits_pos, &fail_pos);
Neal Norwitze7214a12005-12-18 05:03:17 +0000254 }
Martin v. Löwis737ea822004-06-08 18:52:54 +0000255
Christian Heimesfaf2f632008-01-06 16:59:19 +0000256 if (fail_pos == digits_pos)
Mark Dickinson6d65df12009-04-26 15:30:47 +0000257 goto invalid_string;
Christian Heimesfaf2f632008-01-06 16:59:19 +0000258
259 if (negate && fail_pos != nptr)
260 val = -val;
Mark Dickinson725bfd82009-05-03 20:33:40 +0000261 *endptr = fail_pos;
Martin v. Löwis737ea822004-06-08 18:52:54 +0000262
263 return val;
Mark Dickinson6d65df12009-04-26 15:30:47 +0000264
265 invalid_string:
Mark Dickinson725bfd82009-05-03 20:33:40 +0000266 *endptr = (char*)nptr;
Mark Dickinson6d65df12009-04-26 15:30:47 +0000267 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,
299 char **endptr,
300 PyObject *overflow_exception)
301{
302 double x, result=-1.0;
303 char *fail_pos;
304
305 errno = 0;
306 PyFPE_START_PROTECT("PyOS_string_to_double", return -1.0)
307 x = _PyOS_ascii_strtod(s, &fail_pos);
308 PyFPE_END_PROTECT(x)
309
310 if (errno == ENOMEM) {
311 PyErr_NoMemory();
312 fail_pos = (char *)s;
313 }
314 else if (!endptr && (fail_pos == s || *fail_pos != '\0'))
315 PyErr_Format(PyExc_ValueError,
316 "could not convert string to float: "
317 "%.200s", s);
318 else if (fail_pos == s)
319 PyErr_Format(PyExc_ValueError,
320 "could not convert string to float: "
321 "%.200s", s);
322 else if (errno == ERANGE && fabs(x) >= 1.0 && overflow_exception)
323 PyErr_Format(overflow_exception,
324 "value too large to convert to float: "
325 "%.200s", s);
326 else
327 result = x;
328
329 if (endptr != NULL)
330 *endptr = fail_pos;
331 return result;
332}
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{
342 struct lconv *locale_data = localeconv();
343 const char *decimal_point = locale_data->decimal_point;
344
345 if (decimal_point[0] != '.' || decimal_point[1] != 0) {
346 size_t decimal_point_len = strlen(decimal_point);
347
348 if (*buffer == '+' || *buffer == '-')
349 buffer++;
Eric Smith6dc46f52009-04-27 20:39:49 +0000350 while (Py_ISDIGIT(*buffer))
Eric Smithb2c7af82008-04-30 02:12:09 +0000351 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 }
366}
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{
380 char *p = strpbrk(buffer, "eE");
381 if (p && (*(p + 1) == '-' || *(p + 1) == '+')) {
382 char *start = p + 2;
383 int exponent_digit_cnt = 0;
384 int leading_zero_cnt = 0;
385 int in_leading_zeros = 1;
386 int significant_digit_cnt;
387
388 /* Skip over the exponent and the sign. */
389 p += 2;
390
391 /* Find the end of the exponent, keeping track of leading
392 zeros. */
Eric Smith6dc46f52009-04-27 20:39:49 +0000393 while (*p && Py_ISDIGIT(*p)) {
Eric Smithb2c7af82008-04-30 02:12:09 +0000394 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 }
401
402 significant_digit_cnt = exponent_digit_cnt - leading_zero_cnt;
403 if (exponent_digit_cnt == MIN_EXPONENT_DIGITS) {
404 /* If there are 2 exactly digits, we're done,
405 regardless of what they contain */
406 }
407 else if (exponent_digit_cnt > MIN_EXPONENT_DIGITS) {
408 int extra_zeros_cnt;
409
410 /* There are more than 2 digits in the exponent. See
411 if we can delete some of the leading zeros */
412 if (significant_digit_cnt < MIN_EXPONENT_DIGITS)
413 significant_digit_cnt = MIN_EXPONENT_DIGITS;
414 extra_zeros_cnt = exponent_digit_cnt -
415 significant_digit_cnt;
416
417 /* Delete extra_zeros_cnt worth of characters from the
418 front of the exponent */
419 assert(extra_zeros_cnt >= 0);
420
421 /* Add one to significant_digit_cnt to copy the
422 trailing 0 byte, thus setting the length */
423 memmove(start,
424 start + extra_zeros_cnt,
425 significant_digit_cnt + 1);
426 }
427 else {
428 /* If there are fewer than 2 digits, add zeros
429 until there are 2, if there's enough room */
430 int zeros = MIN_EXPONENT_DIGITS - exponent_digit_cnt;
431 if (start + zeros + exponent_digit_cnt + 1
432 < buffer + buf_size) {
433 memmove(start + zeros, start,
434 exponent_digit_cnt + 1);
435 memset(start, '0', zeros);
436 }
437 }
438 }
439}
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{
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000448 char *old_fraction_end, *new_fraction_end, *end, *p;
449
450 p = buffer;
451 if (*p == '-' || *p == '+')
452 /* Skip leading sign, if present */
453 ++p;
454 while (Py_ISDIGIT(*p))
455 ++p;
456
457 /* if there's no decimal point there's nothing to do */
458 if (*p++ != '.')
459 return;
460
461 /* scan any digits after the point */
462 while (Py_ISDIGIT(*p))
463 ++p;
464 old_fraction_end = p;
465
466 /* scan up to ending '\0' */
467 while (*p != '\0')
468 p++;
469 /* +1 to make sure that we move the null byte as well */
470 end = p+1;
471
472 /* scan back from fraction_end, looking for removable zeros */
473 p = old_fraction_end;
474 while (*(p-1) == '0')
475 --p;
476 /* and remove point if we've got that far */
477 if (*(p-1) == '.')
478 --p;
479 new_fraction_end = p;
480
481 memmove(new_fraction_end, old_fraction_end, end-old_fraction_end);
482}
483
484/* Ensure that buffer has a decimal point in it. The decimal point will not
485 be in the current locale, it will always be '.'. Don't add a decimal point
486 if an exponent is present. Also, convert to exponential notation where
487 adding a '.0' would produce too many significant digits (see issue 5864).
488
489 Returns a pointer to the fixed buffer, or NULL on failure.
490*/
491Py_LOCAL_INLINE(char *)
492ensure_decimal_point(char* buffer, size_t buf_size, int precision)
493{
494 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
497 /* search for the first non-digit character */
498 char *p = buffer;
Eric Smith2ad79e82008-07-19 00:33:23 +0000499 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;
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000503 digits_start = p;
Eric Smith6dc46f52009-04-27 20:39:49 +0000504 while (*p && Py_ISDIGIT(*p))
Eric Smithb2c7af82008-04-30 02:12:09 +0000505 ++p;
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000506 digit_count = Py_SAFE_DOWNCAST(p - digits_start, Py_ssize_t, int);
Eric Smithb2c7af82008-04-30 02:12:09 +0000507
508 if (*p == '.') {
Eric Smith6dc46f52009-04-27 20:39:49 +0000509 if (Py_ISDIGIT(*(p+1))) {
Eric Smithb2c7af82008-04-30 02:12:09 +0000510 /* 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. */
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000516 /* can't ever get here via PyOS_double_to_string */
517 assert(precision == -1);
Eric Smithb2c7af82008-04-30 02:12:09 +0000518 ++p;
519 chars_to_insert = "0";
520 insert_count = 1;
521 }
522 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000523 else if (!(*p == 'e' || *p == 'E')) {
524 /* Don't add ".0" if we have an exponent. */
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000525 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 }
Eric Smithb2c7af82008-04-30 02:12:09 +0000541 }
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 }
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000555 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
589 * code to use for converting.
590 * @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'.
Martin v. Löwis737ea822004-06-08 18:52:54 +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 *
Eric Smithcc32a112009-04-26 21:35:14 +0000605_PyOS_ascii_formatd(char *buffer,
Christian Heimesb186d002008-03-18 15:15:01 +0000606 size_t buf_size,
Martin v. Löwis737ea822004-06-08 18:52:54 +0000607 const char *format,
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000608 double d,
609 int precision)
Martin v. Löwis737ea822004-06-08 18:52:54 +0000610{
Martin v. Löwis737ea822004-06-08 18:52:54 +0000611 char format_char;
Christian Heimesc3f30c42008-02-22 16:37:40 +0000612 size_t format_len = strlen(format);
613
Christian Heimesb186d002008-03-18 15:15:01 +0000614 /* Issue 2264: code 'Z' requires copying the format. 'Z' is 'g', but
615 also with at least one character past the decimal. */
Christian Heimesc3f30c42008-02-22 16:37:40 +0000616 char tmp_format[FLOAT_FORMATBUFLEN];
Martin v. Löwis737ea822004-06-08 18:52:54 +0000617
Christian Heimesc3f30c42008-02-22 16:37:40 +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
Martin v. Löwis737ea822004-06-08 18:52:54 +0000621 if (format[0] != '%')
622 return NULL;
623
Christian Heimesc3f30c42008-02-22 16:37:40 +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. */
Martin v. Löwis737ea822004-06-08 18:52:54 +0000628 if (strpbrk(format + 1, "'l%"))
629 return NULL;
630
Christian Heimesb186d002008-03-18 15:15:01 +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. */
635
Martin v. Löwis737ea822004-06-08 18:52:54 +0000636 if (!(format_char == 'e' || format_char == 'E' ||
637 format_char == 'f' || format_char == 'F' ||
Christian Heimesc3f30c42008-02-22 16:37:40 +0000638 format_char == 'g' || format_char == 'G' ||
Eric Smith0923d1d2009-04-16 20:16:10 +0000639 format_char == 'Z'))
Martin v. Löwis737ea822004-06-08 18:52:54 +0000640 return NULL;
641
Eric Smith0923d1d2009-04-16 20:16:10 +0000642 /* Map 'Z' format_char to 'g', by copying the format string and
Christian Heimesb186d002008-03-18 15:15:01 +0000643 replacing the final char with a 'g' */
Eric Smith0923d1d2009-04-16 20:16:10 +0000644 if (format_char == 'Z') {
Christian Heimesc3f30c42008-02-22 16:37:40 +0000645 if (format_len + 1 >= sizeof(tmp_format)) {
646 /* The format won't fit in our copy. Error out. In
Christian Heimesb186d002008-03-18 15:15:01 +0000647 practice, this will never happen and will be
648 detected by returning NULL */
Christian Heimesc3f30c42008-02-22 16:37:40 +0000649 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
Christian Heimesc3f30c42008-02-22 16:37:40 +0000657 /* Have PyOS_snprintf do the hard work */
Christian Heimesb186d002008-03-18 15:15:01 +0000658 PyOS_snprintf(buffer, buf_size, format, d);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000659
Eric Smithb2c7af82008-04-30 02:12:09 +0000660 /* Do various fixups on the return string */
Martin v. Löwis737ea822004-06-08 18:52:54 +0000661
Eric Smithb2c7af82008-04-30 02:12:09 +0000662 /* Get the current locale, and find the decimal point string.
Eric Smith0923d1d2009-04-16 20:16:10 +0000663 Convert that string back to a dot. */
664 change_decimal_from_locale_to_dot(buffer);
Christian Heimesc3f30c42008-02-22 16:37:40 +0000665
666 /* If an exponent exists, ensure that the exponent is at least
667 MIN_EXPONENT_DIGITS digits, providing the buffer is large enough
668 for the extra zeros. Also, if there are more than
669 MIN_EXPONENT_DIGITS, remove as many zeros as possible until we get
670 back to MIN_EXPONENT_DIGITS */
Mark Dickinsonce95e562009-04-26 20:02:24 +0000671 ensure_minimum_exponent_length(buffer, buf_size);
Martin v. Löwis737ea822004-06-08 18:52:54 +0000672
Christian Heimesb186d002008-03-18 15:15:01 +0000673 /* If format_char is 'Z', make sure we have at least one character
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000674 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. */
Eric Smithb2c7af82008-04-30 02:12:09 +0000678 if (format_char == 'Z')
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000679 buffer = ensure_decimal_point(buffer, buf_size, precision);
Christian Heimesb186d002008-03-18 15:15:01 +0000680
Martin v. Löwis737ea822004-06-08 18:52:54 +0000681 return buffer;
682}
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{
Eric Smith0923d1d2009-04-16 20:16:10 +0000692 char format[32];
Mark Dickinsonf489caf2009-05-01 11:42:00 +0000693 Py_ssize_t bufsize;
694 char *buf;
695 int t, exp;
Eric Smith0923d1d2009-04-16 20:16:10 +0000696 int upper = 0;
697
698 /* Validate format_code, and map upper and lower case */
699 switch (format_code) {
700 case 'e': /* exponent */
701 case 'f': /* fixed */
702 case 'g': /* general */
703 break;
704 case 'E':
705 upper = 1;
706 format_code = 'e';
707 break;
708 case 'F':
709 upper = 1;
710 format_code = 'f';
711 break;
712 case 'G':
713 upper = 1;
714 format_code = 'g';
715 break;
716 case 'r': /* repr format */
717 /* Supplied precision is unused, must be 0. */
718 if (precision != 0) {
719 PyErr_BadInternalCall();
720 return NULL;
721 }
Eric Smith63376222009-05-05 14:04:18 +0000722 /* 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. */
Eric Smith0923d1d2009-04-16 20:16:10 +0000728 precision = 17;
729 format_code = 'g';
730 break;
Eric Smith0923d1d2009-04-16 20:16:10 +0000731 default:
732 PyErr_BadInternalCall();
733 return NULL;
734 }
735
Mark Dickinsonf489caf2009-05-01 11:42:00 +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:
738
739 1 byte for each digit of the decimal significand, and
740
741 1 for a possible sign
742 1 for a possible decimal point
743 2 for a possible [eE][+-]
744 1 for each digit of the exponent; if we allow 19 digits
745 total then we're safe up to exponents of 2**63.
746 1 for the trailing nul byte
747
748 This gives a total of 24 + the number of digits in the significand,
749 and the number of digits in the significand is:
750
751 for 'g' format: at most precision, except possibly
752 when precision == 0, when it's 1.
753 for 'e' format: precision+1
754 for 'f' format: precision digits after the point, at least 1
755 before. To figure out how many digits appear before the point
756 we have to examine the size of the number. If fabs(val) < 1.0
757 then there will be only one digit before the point. If
758 fabs(val) >= 1.0, then there are at most
759
760 1+floor(log10(ceiling(fabs(val))))
761
762 digits before the point (where the 'ceiling' allows for the
763 possibility that the rounding rounds the integer part of val
764 up). A safe upper bound for the above quantity is
765 1+floor(exp/3), where exp is the unique integer such that 0.5
766 <= fabs(val)/2**exp < 1.0. This exp can be obtained from
767 frexp.
768
769 So we allow room for precision+1 digits for all formats, plus an
770 extra floor(exp/3) digits for 'f' format.
771
772 */
773
774 if (Py_IS_NAN(val) || Py_IS_INFINITY(val))
775 /* 3 for 'inf'/'nan', 1 for sign, 1 for '\0' */
776 bufsize = 5;
777 else {
778 bufsize = 25 + precision;
779 if (format_code == 'f' && fabs(val) >= 1.0) {
780 frexp(val, &exp);
781 bufsize += exp/3;
782 }
783 }
784
785 buf = PyMem_Malloc(bufsize);
786 if (buf == NULL) {
787 PyErr_NoMemory();
788 return NULL;
789 }
790
Eric Smith0923d1d2009-04-16 20:16:10 +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;
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000803 if (flags & Py_DTSF_ADD_DOT_0)
Eric Smith0923d1d2009-04-16 20:16:10 +0000804 format_code = 'Z';
805
Eric Smithcc32a112009-04-26 21:35:14 +0000806 PyOS_snprintf(format, sizeof(format), "%%%s.%i%c",
807 (flags & Py_DTSF_ALT ? "#" : ""), precision,
808 format_code);
Mark Dickinsonf489caf2009-05-01 11:42:00 +0000809 _PyOS_ascii_formatd(buf, bufsize, format, val, precision);
Eric Smith0923d1d2009-04-16 20:16:10 +0000810 }
811
Mark Dickinsonad476da2009-04-23 19:14:16 +0000812 /* Add sign when requested. It's convenient (esp. when formatting
813 complex numbers) to include a sign even for inf and nan. */
Mark Dickinsonf489caf2009-05-01 11:42:00 +0000814 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 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000822 if (upper) {
823 /* Convert to upper case. */
824 char *p1;
Mark Dickinsonf489caf2009-05-01 11:42:00 +0000825 for (p1 = buf; *p1; p1++)
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000826 *p1 = Py_TOUPPER(*p1);
Eric Smith0923d1d2009-04-16 20:16:10 +0000827 }
828
829 if (type)
830 *type = t;
Mark Dickinsonf489caf2009-05-01 11:42:00 +0000831 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[] = {
846 "inf",
847 "nan",
848 "e",
849};
850static char *uc_float_strings[] = {
851 "INF",
852 "NAN",
853 "E",
854};
855
856
857/* Convert a double d to a string, and return a PyMem_Malloc'd block of
858 memory contain the resulting string.
859
860 Arguments:
861 d is the double to be converted
Eric 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:
877 Py_DTST_FINITE
878 Py_DTST_INFINITE
879 Py_DTST_NAN
880
881 Returns a PyMem_Malloc'd block of memory containing the resulting string,
882 or NULL on error. If NULL is returned, the Python error has been set.
883 */
884
885static char *
886format_float_short(double d, char format_code,
887 int mode, Py_ssize_t precision,
888 int always_add_sign, int add_dot_0_if_integer,
889 int use_alt_formatting, char **float_strings, int *type)
890{
891 char *buf = NULL;
892 char *p = NULL;
893 Py_ssize_t bufsize = 0;
894 char *digits, *digits_end;
895 int decpt_as_int, sign, exp_len, exp = 0, use_exp = 0;
896 Py_ssize_t decpt, digits_len, vdigits_start, vdigits_end;
897 _Py_SET_53BIT_PRECISION_HEADER;
898
899 /* _Py_dg_dtoa returns a digit string (no decimal point or exponent).
900 Must be matched by a call to _Py_dg_freedtoa. */
901 _Py_SET_53BIT_PRECISION_START;
902 digits = _Py_dg_dtoa(d, mode, precision, &decpt_as_int, &sign,
903 &digits_end);
904 _Py_SET_53BIT_PRECISION_END;
905
906 decpt = (Py_ssize_t)decpt_as_int;
907 if (digits == NULL) {
908 /* The only failure mode is no memory. */
909 PyErr_NoMemory();
910 goto exit;
911 }
912 assert(digits_end != NULL && digits_end >= digits);
913 digits_len = digits_end - digits;
914
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000915 if (digits_len && !Py_ISDIGIT(digits[0])) {
Eric Smith0923d1d2009-04-16 20:16:10 +0000916 /* 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. */
919
Mark Dickinsonad476da2009-04-23 19:14:16 +0000920 /* ignore the actual sign of a nan */
921 if (digits[0] == 'n' || digits[0] == 'N')
922 sign = 0;
923
Eric Smith0923d1d2009-04-16 20:16:10 +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;
932
Mark Dickinsonad476da2009-04-23 19:14:16 +0000933 if (sign == 1) {
934 *p++ = '-';
935 }
936 else if (always_add_sign) {
937 *p++ = '+';
938 }
Eric Smith0923d1d2009-04-16 20:16:10 +0000939 if (digits[0] == 'i' || digits[0] == 'I') {
Eric Smith0923d1d2009-04-16 20:16:10 +0000940 strncpy(p, float_strings[OFS_INF], 3);
941 p += 3;
942
943 if (type)
944 *type = Py_DTST_INFINITE;
945 }
946 else if (digits[0] == 'n' || digits[0] == 'N') {
Eric Smith0923d1d2009-04-16 20:16:10 +0000947 strncpy(p, float_strings[OFS_NAN], 3);
948 p += 3;
949
950 if (type)
951 *type = Py_DTST_NAN;
952 }
953 else {
954 /* shouldn't get here: Gay's code should always return
955 something starting with a digit, an 'I', or 'N' */
956 strncpy(p, "ERR", 3);
957 p += 3;
958 assert(0);
959 }
960 goto exit;
961 }
962
963 /* The result must be finite (not inf or nan). */
964 if (type)
965 *type = Py_DTST_FINITE;
966
967
968 /* We got digits back, format them. We may need to pad 'digits'
969 either on the left or right (or both) with extra zeros, so in
970 general the resulting string has the form
971
972 [<sign>]<zeros><digits><zeros>[<exponent>]
973
974 where either of the <zeros> pieces could be empty, and there's a
975 decimal point that could appear either in <digits> or in the
976 leading or trailing <zeros>.
977
978 Imagine an infinite 'virtual' string vdigits, consisting of the
979 string 'digits' (starting at index 0) padded on both the left and
980 right with infinite strings of zeros. We want to output a slice
981
982 vdigits[vdigits_start : vdigits_end]
983
984 of this virtual string. Thus if vdigits_start < 0 then we'll end
985 up producing some leading zeros; if vdigits_end > digits_len there
986 will be trailing zeros in the output. The next section of code
987 determines whether to use an exponent or not, figures out the
988 position 'decpt' of the decimal point, and computes 'vdigits_start'
989 and 'vdigits_end'. */
990 vdigits_end = digits_len;
991 switch (format_code) {
992 case 'e':
993 use_exp = 1;
994 vdigits_end = precision;
995 break;
996 case 'f':
997 vdigits_end = decpt + precision;
998 break;
999 case 'g':
Mark Dickinsond3ca5572009-04-29 18:47:07 +00001000 if (decpt <= -4 || decpt >
1001 (add_dot_0_if_integer ? precision-1 : precision))
Eric Smith0923d1d2009-04-16 20:16:10 +00001002 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;
Eric Smith0923d1d2009-04-16 20:16:10 +00001015 default:
1016 PyErr_BadInternalCall();
1017 goto exit;
1018 }
1019
1020 /* if using an exponent, reset decimal point position to 1 and adjust
1021 exponent accordingly.*/
1022 if (use_exp) {
1023 exp = decpt - 1;
1024 decpt = 1;
1025 }
1026 /* ensure vdigits_start < decpt <= vdigits_end, or vdigits_start <
1027 decpt < vdigits_end if add_dot_0_if_integer and no exponent */
1028 vdigits_start = decpt <= 0 ? decpt-1 : 0;
1029 if (!use_exp && add_dot_0_if_integer)
1030 vdigits_end = vdigits_end > decpt ? vdigits_end : decpt + 1;
1031 else
1032 vdigits_end = vdigits_end > decpt ? vdigits_end : decpt;
1033
1034 /* double check inequalities */
1035 assert(vdigits_start <= 0 &&
1036 0 <= digits_len &&
1037 digits_len <= vdigits_end);
1038 /* decimal point should be in (vdigits_start, vdigits_end] */
1039 assert(vdigits_start < decpt && decpt <= vdigits_end);
1040
1041 /* Compute an upper bound how much memory we need. This might be a few
1042 chars too long, but no big deal. */
1043 bufsize =
1044 /* sign, decimal point and trailing 0 byte */
1045 3 +
1046
1047 /* total digit count (including zero padding on both sides) */
1048 (vdigits_end - vdigits_start) +
1049
1050 /* exponent "e+100", max 3 numerical digits */
1051 (use_exp ? 5 : 0);
1052
1053 /* Now allocate the memory and initialize p to point to the start of
1054 it. */
1055 buf = (char *)PyMem_Malloc(bufsize);
1056 if (buf == NULL) {
1057 PyErr_NoMemory();
1058 goto exit;
1059 }
1060 p = buf;
1061
1062 /* Add a negative sign if negative, and a plus sign if non-negative
1063 and always_add_sign is true. */
1064 if (sign == 1)
1065 *p++ = '-';
1066 else if (always_add_sign)
1067 *p++ = '+';
1068
1069 /* note that exactly one of the three 'if' conditions is true,
1070 so we include exactly one decimal point */
1071 /* Zero padding on left of digit string */
1072 if (decpt <= 0) {
1073 memset(p, '0', decpt-vdigits_start);
1074 p += decpt - vdigits_start;
1075 *p++ = '.';
1076 memset(p, '0', 0-decpt);
1077 p += 0-decpt;
1078 }
1079 else {
1080 memset(p, '0', 0-vdigits_start);
1081 p += 0 - vdigits_start;
1082 }
1083
1084 /* Digits, with included decimal point */
1085 if (0 < decpt && decpt <= digits_len) {
1086 strncpy(p, digits, decpt-0);
1087 p += decpt-0;
1088 *p++ = '.';
1089 strncpy(p, digits+decpt, digits_len-decpt);
1090 p += digits_len-decpt;
1091 }
1092 else {
1093 strncpy(p, digits, digits_len);
1094 p += digits_len;
1095 }
1096
1097 /* And zeros on the right */
1098 if (digits_len < decpt) {
1099 memset(p, '0', decpt-digits_len);
1100 p += decpt-digits_len;
1101 *p++ = '.';
1102 memset(p, '0', vdigits_end-decpt);
1103 p += vdigits_end-decpt;
1104 }
1105 else {
1106 memset(p, '0', vdigits_end-digits_len);
1107 p += vdigits_end-digits_len;
1108 }
1109
1110 /* Delete a trailing decimal pt unless using alternative formatting. */
1111 if (p[-1] == '.' && !use_alt_formatting)
1112 p--;
1113
1114 /* Now that we've done zero padding, add an exponent if needed. */
1115 if (use_exp) {
1116 *p++ = float_strings[OFS_E][0];
1117 exp_len = sprintf(p, "%+.02d", exp);
1118 p += exp_len;
1119 }
1120 exit:
1121 if (buf) {
1122 *p = '\0';
1123 /* It's too late if this fails, as we've already stepped on
1124 memory that isn't ours. But it's an okay debugging test. */
1125 assert(p-buf < bufsize);
1126 }
1127 if (digits)
1128 _Py_dg_freedtoa(digits);
1129
1130 return buf;
1131}
1132
1133
1134PyAPI_FUNC(char *) PyOS_double_to_string(double val,
Eric Smith193125a2009-04-16 22:08:31 +00001135 char format_code,
1136 int precision,
1137 int flags,
Eric Smith0923d1d2009-04-16 20:16:10 +00001138 int *type)
1139{
Eric Smith193125a2009-04-16 22:08:31 +00001140 char **float_strings = lc_float_strings;
1141 int mode;
Eric Smith0923d1d2009-04-16 20:16:10 +00001142
Eric Smith193125a2009-04-16 22:08:31 +00001143 /* Validate format_code, and map upper and lower case. Compute the
1144 mode and make any adjustments as needed. */
Eric Smith0923d1d2009-04-16 20:16:10 +00001145 switch (format_code) {
Eric Smith193125a2009-04-16 22:08:31 +00001146 /* exponent */
Eric Smith0923d1d2009-04-16 20:16:10 +00001147 case 'E':
Eric Smith0923d1d2009-04-16 20:16:10 +00001148 float_strings = uc_float_strings;
Eric Smith193125a2009-04-16 22:08:31 +00001149 format_code = 'e';
1150 /* Fall through. */
Eric Smith0923d1d2009-04-16 20:16:10 +00001151 case 'e':
1152 mode = 2;
1153 precision++;
1154 break;
Eric Smith193125a2009-04-16 22:08:31 +00001155
1156 /* fixed */
1157 case 'F':
1158 float_strings = uc_float_strings;
1159 format_code = 'f';
1160 /* Fall through. */
Eric Smith0923d1d2009-04-16 20:16:10 +00001161 case 'f':
1162 mode = 3;
1163 break;
Eric Smith193125a2009-04-16 22:08:31 +00001164
1165 /* general */
1166 case 'G':
1167 float_strings = uc_float_strings;
1168 format_code = 'g';
1169 /* Fall through. */
Eric Smith0923d1d2009-04-16 20:16:10 +00001170 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
1177 /* repr format */
Eric Smith0923d1d2009-04-16 20:16:10 +00001178 case 'r':
Eric Smith0923d1d2009-04-16 20:16:10 +00001179 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
Eric Smith193125a2009-04-16 22:08:31 +00001187 default:
1188 PyErr_BadInternalCall();
1189 return NULL;
Eric Smith0923d1d2009-04-16 20:16:10 +00001190 }
1191
Eric Smith193125a2009-04-16 22:08:31 +00001192 return format_float_short(val, format_code, mode, precision,
Eric Smith0923d1d2009-04-16 20:16:10 +00001193 flags & Py_DTSF_SIGN,
1194 flags & Py_DTSF_ADD_DOT_0,
1195 flags & Py_DTSF_ALT,
1196 float_strings, type);
1197}
1198#endif /* ifdef PY_NO_SHORT_FLOAT_REPR */