1 /* -*- Mode: C; c-file-style: "python" -*- */
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.
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
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.
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.
27 * If memory allocation fails, %ENOMEM is stored in %errno.
29 * This function resets %errno before calling strtod() so that
30 * you can reliably detect overflow and underflow.
32 * Return value: the #gdouble value.
36 Use system strtod; since strtod is locale aware, we may
37 have to first fix the decimal separator.
39 Note that unlike _Py_dg_strtod, the system strtod may not always give
40 correctly rounded results.
44 PyOS_ascii_strtod(const char *nptr
, char **endptr
)
48 struct lconv
*locale_data
;
49 const char *decimal_point
;
50 size_t decimal_point_len
;
51 const char *p
, *decimal_point_pos
;
52 const char *end
= NULL
; /* Silence gcc */
53 const char *digits_pos
= NULL
;
60 locale_data
= localeconv();
61 decimal_point
= locale_data
->decimal_point
;
62 decimal_point_len
= strlen(decimal_point
);
64 assert(decimal_point_len
!= 0);
66 decimal_point_pos
= NULL
;
68 /* Set errno to zero, so that we can distinguish zero results
72 /* We process any leading whitespace and the optional sign manually,
73 then pass the remainder to the system strtod. This ensures that
74 the result of an underflow has the correct sign. (bug #1725) */
77 /* Skip leading space */
78 while (Py_ISSPACE(*p
))
81 /* Process leading sign, if present */
90 /* Parse infinities and nans */
91 if (*p
== 'i' || *p
== 'I') {
92 if (PyOS_strnicmp(p
, "inf", 3) == 0) {
94 if (PyOS_strnicmp(p
+3, "inity", 5) == 0)
95 fail_pos
= (char *)p
+8;
97 fail_pos
= (char *)p
+3;
104 if (*p
== 'n' || *p
== 'N') {
105 if (PyOS_strnicmp(p
, "nan", 3) == 0) {
107 fail_pos
= (char *)p
+3;
115 /* Some platform strtods accept hex floats; Python shouldn't (at the
116 moment), so we check explicitly for strings starting with '0x'. */
117 if (*p
== '0' && (*(p
+1) == 'x' || *(p
+1) == 'X'))
120 /* Check that what's left begins with a digit or decimal point */
121 if (!Py_ISDIGIT(*p
) && *p
!= '.')
125 if (decimal_point
[0] != '.' ||
126 decimal_point
[1] != 0)
128 /* Look for a '.' in the input; if present, it'll need to be
129 swapped for the current locale's decimal point before we
130 call strtod. On the other hand, if we find the current
131 locale's decimal point then the input is invalid. */
132 while (Py_ISDIGIT(*p
))
137 decimal_point_pos
= p
++;
139 /* locate end of number */
140 while (Py_ISDIGIT(*p
))
143 if (*p
== 'e' || *p
== 'E')
145 if (*p
== '+' || *p
== '-')
147 while (Py_ISDIGIT(*p
))
151 else if (strncmp(p
, decimal_point
, decimal_point_len
) == 0)
152 /* Python bug #1417699 */
154 /* For the other cases, we need not convert the decimal
158 if (decimal_point_pos
) {
160 /* Create a copy of the input, with the '.' converted to the
161 locale-specific decimal point */
162 copy
= (char *)PyMem_MALLOC(end
- digits_pos
+
163 1 + decimal_point_len
);
166 *endptr
= (char *)nptr
;
172 memcpy(c
, digits_pos
, decimal_point_pos
- digits_pos
);
173 c
+= decimal_point_pos
- digits_pos
;
174 memcpy(c
, decimal_point
, decimal_point_len
);
175 c
+= decimal_point_len
;
176 memcpy(c
, decimal_point_pos
+ 1,
177 end
- (decimal_point_pos
+ 1));
178 c
+= end
- (decimal_point_pos
+ 1);
181 val
= strtod(copy
, &fail_pos
);
185 if (fail_pos
> decimal_point_pos
)
186 fail_pos
= (char *)digits_pos
+
188 (decimal_point_len
- 1);
190 fail_pos
= (char *)digits_pos
+
198 val
= strtod(digits_pos
, &fail_pos
);
201 if (fail_pos
== digits_pos
)
205 if (negate
&& fail_pos
!= nptr
)
215 *endptr
= (char*)nptr
;
221 PyOS_ascii_atof(const char *nptr
)
223 return PyOS_ascii_strtod(nptr
, NULL
);
227 /* Given a string that may have a decimal point in the current
228 locale, change it back to a dot. Since the string cannot get
229 longer, no need for a maximum buffer size parameter. */
230 Py_LOCAL_INLINE(void)
231 change_decimal_from_locale_to_dot(char* buffer
)
233 struct lconv
*locale_data
= localeconv();
234 const char *decimal_point
= locale_data
->decimal_point
;
236 if (decimal_point
[0] != '.' || decimal_point
[1] != 0) {
237 size_t decimal_point_len
= strlen(decimal_point
);
239 if (*buffer
== '+' || *buffer
== '-')
241 while (Py_ISDIGIT(*buffer
))
243 if (strncmp(buffer
, decimal_point
, decimal_point_len
) == 0) {
246 if (decimal_point_len
> 1) {
247 /* buffer needs to get smaller */
248 size_t rest_len
= strlen(buffer
+
249 (decimal_point_len
- 1));
251 buffer
+ (decimal_point_len
- 1),
253 buffer
[rest_len
] = 0;
260 Py_LOCAL_INLINE(void)
261 ensure_sign(char* buffer
, size_t buf_size
)
265 if (buffer
[0] == '-')
266 /* Already have a sign. */
269 /* Include the trailing 0 byte. */
270 len
= strlen(buffer
)+1;
271 if (len
>= buf_size
+1)
272 /* No room for the sign, don't do anything. */
275 memmove(buffer
+1, buffer
, len
);
279 /* From the C99 standard, section 7.19.6:
280 The exponent always contains at least two digits, and only as many more digits
281 as necessary to represent the exponent.
283 #define MIN_EXPONENT_DIGITS 2
285 /* Ensure that any exponent, if present, is at least MIN_EXPONENT_DIGITS
287 Py_LOCAL_INLINE(void)
288 ensure_minimum_exponent_length(char* buffer
, size_t buf_size
)
290 char *p
= strpbrk(buffer
, "eE");
291 if (p
&& (*(p
+ 1) == '-' || *(p
+ 1) == '+')) {
293 int exponent_digit_cnt
= 0;
294 int leading_zero_cnt
= 0;
295 int in_leading_zeros
= 1;
296 int significant_digit_cnt
;
298 /* Skip over the exponent and the sign. */
301 /* Find the end of the exponent, keeping track of leading
303 while (*p
&& Py_ISDIGIT(*p
)) {
304 if (in_leading_zeros
&& *p
== '0')
307 in_leading_zeros
= 0;
309 ++exponent_digit_cnt
;
312 significant_digit_cnt
= exponent_digit_cnt
- leading_zero_cnt
;
313 if (exponent_digit_cnt
== MIN_EXPONENT_DIGITS
) {
314 /* If there are 2 exactly digits, we're done,
315 regardless of what they contain */
317 else if (exponent_digit_cnt
> MIN_EXPONENT_DIGITS
) {
320 /* There are more than 2 digits in the exponent. See
321 if we can delete some of the leading zeros */
322 if (significant_digit_cnt
< MIN_EXPONENT_DIGITS
)
323 significant_digit_cnt
= MIN_EXPONENT_DIGITS
;
324 extra_zeros_cnt
= exponent_digit_cnt
-
325 significant_digit_cnt
;
327 /* Delete extra_zeros_cnt worth of characters from the
328 front of the exponent */
329 assert(extra_zeros_cnt
>= 0);
331 /* Add one to significant_digit_cnt to copy the
332 trailing 0 byte, thus setting the length */
334 start
+ extra_zeros_cnt
,
335 significant_digit_cnt
+ 1);
338 /* If there are fewer than 2 digits, add zeros
339 until there are 2, if there's enough room */
340 int zeros
= MIN_EXPONENT_DIGITS
- exponent_digit_cnt
;
341 if (start
+ zeros
+ exponent_digit_cnt
+ 1
342 < buffer
+ buf_size
) {
343 memmove(start
+ zeros
, start
,
344 exponent_digit_cnt
+ 1);
345 memset(start
, '0', zeros
);
351 /* Remove trailing zeros after the decimal point from a numeric string; also
352 remove the decimal point if all digits following it are zero. The numeric
353 string must end in '\0', and should not have any leading or trailing
354 whitespace. Assumes that the decimal point is '.'. */
355 Py_LOCAL_INLINE(void)
356 remove_trailing_zeros(char *buffer
)
358 char *old_fraction_end
, *new_fraction_end
, *end
, *p
;
361 if (*p
== '-' || *p
== '+')
362 /* Skip leading sign, if present */
364 while (Py_ISDIGIT(*p
))
367 /* if there's no decimal point there's nothing to do */
371 /* scan any digits after the point */
372 while (Py_ISDIGIT(*p
))
374 old_fraction_end
= p
;
376 /* scan up to ending '\0' */
379 /* +1 to make sure that we move the null byte as well */
382 /* scan back from fraction_end, looking for removable zeros */
383 p
= old_fraction_end
;
384 while (*(p
-1) == '0')
386 /* and remove point if we've got that far */
389 new_fraction_end
= p
;
391 memmove(new_fraction_end
, old_fraction_end
, end
-old_fraction_end
);
394 /* Ensure that buffer has a decimal point in it. The decimal point will not
395 be in the current locale, it will always be '.'. Don't add a decimal point
396 if an exponent is present. Also, convert to exponential notation where
397 adding a '.0' would produce too many significant digits (see issue 5864).
399 Returns a pointer to the fixed buffer, or NULL on failure.
401 Py_LOCAL_INLINE(char *)
402 ensure_decimal_point(char* buffer
, size_t buf_size
, int precision
)
404 int digit_count
, insert_count
= 0, convert_to_exp
= 0;
405 char* chars_to_insert
, *digits_start
;
407 /* search for the first non-digit character */
409 if (*p
== '-' || *p
== '+')
410 /* Skip leading sign, if present. I think this could only
411 ever be '-', but it can't hurt to check for both. */
414 while (*p
&& Py_ISDIGIT(*p
))
416 digit_count
= Py_SAFE_DOWNCAST(p
- digits_start
, Py_ssize_t
, int);
419 if (Py_ISDIGIT(*(p
+1))) {
420 /* Nothing to do, we already have a decimal
421 point and a digit after it */
424 /* We have a decimal point, but no following
425 digit. Insert a zero after the decimal. */
426 /* can't ever get here via PyOS_double_to_string */
427 assert(precision
== -1);
429 chars_to_insert
= "0";
433 else if (!(*p
== 'e' || *p
== 'E')) {
434 /* Don't add ".0" if we have an exponent. */
435 if (digit_count
== precision
) {
436 /* issue 5864: don't add a trailing .0 in the case
437 where the '%g'-formatted result already has as many
438 significant digits as were requested. Switch to
439 exponential notation instead. */
441 /* no exponent, no point, and we shouldn't land here
442 for infs and nans, so we must be at the end of the
447 assert(precision
== -1 || digit_count
< precision
);
448 chars_to_insert
= ".0";
453 size_t buf_len
= strlen(buffer
);
454 if (buf_len
+ insert_count
+ 1 >= buf_size
) {
455 /* If there is not enough room in the buffer
456 for the additional text, just skip it. It's
457 not worth generating an error over. */
460 memmove(p
+ insert_count
, p
,
461 buffer
+ strlen(buffer
) - p
+ 1);
462 memcpy(p
, chars_to_insert
, insert_count
);
465 if (convert_to_exp
) {
469 /* insert decimal point */
470 assert(digit_count
>= 1);
471 memmove(p
+2, p
+1, digit_count
); /* safe, but overwrites nul */
474 assert(p
<= buf_size
+buffer
);
475 buf_avail
= buf_size
+buffer
-p
;
478 /* Add exponent. It's okay to use lower case 'e': we only
479 arrive here as a result of using the empty format code or
480 repr/str builtins and those never want an upper case 'E' */
481 written
= PyOS_snprintf(p
, buf_avail
, "e%+.02d", digit_count
-1);
482 if (!(0 <= written
&&
483 written
< Py_SAFE_DOWNCAST(buf_avail
, size_t, int)))
484 /* output truncated, or something else bad happened */
486 remove_trailing_zeros(buffer
);
491 /* see FORMATBUFLEN in unicodeobject.c */
492 #define FLOAT_FORMATBUFLEN 120
495 * _PyOS_ascii_formatd:
496 * @buffer: A buffer to place the resulting string in
497 * @buf_size: The length of the buffer.
498 * @format: The printf()-style format to use for the
499 * code to use for converting.
500 * @d: The #gdouble to convert
502 * Converts a #gdouble to a string, using the '.' as
503 * decimal point. To format the number you pass in
504 * a printf()-style format string. Allowed conversion
505 * specifiers are 'e', 'E', 'f', 'F', 'g', 'G', and 'Z'.
507 * 'Z' is the same as 'g', except it always has a decimal and
508 * at least one digit after the decimal.
510 * Return value: The pointer to the buffer with the converted string.
511 * On failure returns NULL but does not set any Python exception.
513 /* DEPRECATED, will be deleted in 2.8 and 3.2 */
515 PyOS_ascii_formatd(char *buffer
,
521 size_t format_len
= strlen(format
);
523 /* Issue 2264: code 'Z' requires copying the format. 'Z' is 'g', but
524 also with at least one character past the decimal. */
525 char tmp_format
[FLOAT_FORMATBUFLEN
];
527 if (PyErr_WarnEx(PyExc_DeprecationWarning
,
528 "PyOS_ascii_formatd is deprecated, "
529 "use PyOS_double_to_string instead", 1) < 0)
532 /* The last character in the format string must be the format char */
533 format_char
= format
[format_len
- 1];
535 if (format
[0] != '%')
538 /* I'm not sure why this test is here. It's ensuring that the format
539 string after the first character doesn't have a single quote, a
540 lowercase l, or a percent. This is the reverse of the commented-out
541 test about 10 lines ago. */
542 if (strpbrk(format
+ 1, "'l%"))
545 /* Also curious about this function is that it accepts format strings
546 like "%xg", which are invalid for floats. In general, the
547 interface to this function is not very good, but changing it is
548 difficult because it's a public API. */
550 if (!(format_char
== 'e' || format_char
== 'E' ||
551 format_char
== 'f' || format_char
== 'F' ||
552 format_char
== 'g' || format_char
== 'G' ||
556 /* Map 'Z' format_char to 'g', by copying the format string and
557 replacing the final char with a 'g' */
558 if (format_char
== 'Z') {
559 if (format_len
+ 1 >= sizeof(tmp_format
)) {
560 /* The format won't fit in our copy. Error out. In
561 practice, this will never happen and will be
562 detected by returning NULL */
565 strcpy(tmp_format
, format
);
566 tmp_format
[format_len
- 1] = 'g';
571 /* Have PyOS_snprintf do the hard work */
572 PyOS_snprintf(buffer
, buf_size
, format
, d
);
574 /* Do various fixups on the return string */
576 /* Get the current locale, and find the decimal point string.
577 Convert that string back to a dot. */
578 change_decimal_from_locale_to_dot(buffer
);
580 /* If an exponent exists, ensure that the exponent is at least
581 MIN_EXPONENT_DIGITS digits, providing the buffer is large enough
582 for the extra zeros. Also, if there are more than
583 MIN_EXPONENT_DIGITS, remove as many zeros as possible until we get
584 back to MIN_EXPONENT_DIGITS */
585 ensure_minimum_exponent_length(buffer
, buf_size
);
587 /* If format_char is 'Z', make sure we have at least one character
588 after the decimal point (and make sure we have a decimal point);
589 also switch to exponential notation in some edge cases where the
590 extra character would produce more significant digits that we
592 if (format_char
== 'Z')
593 buffer
= ensure_decimal_point(buffer
, buf_size
, -1);
599 _PyOS_double_to_string(char *buf
, size_t buf_len
, double val
,
600 char format_code
, int precision
,
601 int flags
, int *ptype
)
609 /* There's no way to signal this error. Just return. */
614 /* Validate format_code, and map upper and lower case */
615 switch (format_code
) {
616 case 'e': /* exponent */
617 case 'f': /* fixed */
618 case 'g': /* general */
632 case 'r': /* repr format */
633 /* Supplied precision is unused, must be 0. */
639 case 's': /* str format */
640 /* Supplied precision is unused, must be 0. */
651 /* Check for buf too small to fit "-inf". Other buffer too small
652 conditions are dealt with when converting or formatting finite
659 /* Handle nan and inf. */
660 if (Py_IS_NAN(val
)) {
663 } else if (Py_IS_INFINITY(val
)) {
664 if (copysign(1., val
) == 1.)
668 t
= Py_DTST_INFINITE
;
672 /* Build the format string. */
673 PyOS_snprintf(format
, sizeof(format
), "%%%s.%i%c",
674 (flags
& Py_DTSF_ALT
? "#" : ""), precision
,
677 /* Have PyOS_snprintf do the hard work. */
678 PyOS_snprintf(buf
, buf_len
, format
, val
);
680 /* Do various fixups on the return string */
682 /* Get the current locale, and find the decimal point string.
683 Convert that string back to a dot. */
684 change_decimal_from_locale_to_dot(buf
);
686 /* If an exponent exists, ensure that the exponent is at least
687 MIN_EXPONENT_DIGITS digits, providing the buffer is large
688 enough for the extra zeros. Also, if there are more than
689 MIN_EXPONENT_DIGITS, remove as many zeros as possible until
690 we get back to MIN_EXPONENT_DIGITS */
691 ensure_minimum_exponent_length(buf
, buf_len
);
693 /* Possibly make sure we have at least one character after the
694 decimal point (and make sure we have a decimal point). */
695 if (flags
& Py_DTSF_ADD_DOT_0
)
696 buf
= ensure_decimal_point(buf
, buf_len
, precision
);
699 /* Add the sign if asked and the result isn't negative. */
700 if (flags
& Py_DTSF_SIGN
&& buf
[0] != '-')
701 ensure_sign(buf
, buf_len
);
704 /* Convert to upper case. */
706 for (p
= buf
; *p
; p
++)
715 PyAPI_FUNC(char *) PyOS_double_to_string(double val
,
725 _PyOS_double_to_string(buf
, sizeof(buf
), val
, format_code
, precision
,
729 PyErr_BadInternalCall();
733 /* Add 1 for the trailing 0 byte. */
734 result
= PyMem_Malloc(len
+ 1);
735 if (result
== NULL
) {