What is the maximum length of str(float(any_possible_value))
in Python?

- 14,854
- 11
- 100
- 103
-
Does this answer your question? [What is the maximum float in Python?](https://stackoverflow.com/questions/3477283/what-is-the-maximum-float-in-python) – azro Oct 06 '20 at 17:25
-
2If you are trying to format a table, you almost certainly want to pick a column width, and round the values to some fixed, but *smaller*, number of digits. – chepner Oct 06 '20 at 17:26
-
3@azro. That's definitely not a duplicate – Mad Physicist Oct 06 '20 at 17:57
-
With exponential decimal notation, -d.ddddddddddddddd-eyyy (1+1+1+16+1+1+3 = 24) is sufficient to distinguish all values. -1.797...e+308 needs 310 characters otherwise. Small values near 0.0 need a few more. – chux - Reinstate Monica Oct 06 '20 at 22:51
-
1I do not follow the Python specification closely, but, last I knew, it did not impose specific formats on floating-point, and I am not sure it imposes specific requirements on the results of `str` as applied to floating-point. E.g., one Python implementation might choose to show only as many digits are needed to uniquely distinguish a number from its neighboring floating-point values while another Python implementation might choose to show the exact value (requiring possibly hundreds of digits). If so, there is no maximum length determined by the Python specification. – Eric Postpischil Oct 06 '20 at 23:59
3 Answers
There is no maximum length since you can print as many digits as you choose. For example:
f'{0.1:.25g}'
It also depends on the format:
f'{1e-6:.6f}' # 0.000001
vs
f'{1e-6:.6g}' # 1e-06
Then of course there's locale, and probably other factors.
But you can still get a reasonable upper bound on what str(float(x))
can reasonably return:
- There are 53 bits of mantissa precision in an IEEE 754 binary double precision float. That's about 15-16 decimal digits. 17 with the leading zero or one (e.g.,
sys.float_info.max
shows that many) - One digit for the decimal point.
- Scientific notation will have
'e'
, a sign, and up to three decimal digits of exponent (bounded by the radix precision), for a total of five. - There can be a sign character.
24 characters seems like a reasonable upper bound for what str(float(x))
might return in the default locale under normal circumstances.

- 107,652
- 25
- 181
- 264
-
You forgot the negative sign, the answer is 24. And also format strings have nothing to do with `str(float())` but I appreciate that you’re one of the first people to actually read the question – Boris Verkhovskiy Oct 06 '20 at 19:45
-
@Boris. I mentioned the formatting because internally, `str` has to choose some representation. I could look it up or make some educated hand wavy guesses. I chose the latter since it's less work :) – Mad Physicist Oct 06 '20 at 20:04
-
@Boris. I updated to 24. I missed a potential digit. Thanks for the catch. – Mad Physicist Oct 06 '20 at 20:09
24 characters:
sign digit point fraction e sign exponent
- 1 . 7976931348623157 e + 308
1 1 1 16 1 1 3
In CPython, str(-1.7976931348623157e+308)
ultimately calls this code (simplified):
snprintf(buf, buf_size, "%.17g", val)
followed by (also simplified)
change_decimal_from_locale_to_dot(buffer);
ensure_minimum_exponent_length(buffer, buf_size);
ensure_decimal_point(buffer, buf_size, 17); // can switch to exponent notation
In my understanding none of those 3 functions should affect the maximum length, so this becomes this question: What's the longest string that can be printed with "%1.17g" format for any double precision float? but note that the answers there add 1 character for the trailing null.

- 14,854
- 11
- 100
- 103
You can get the max lenght of a float by running:
sys.float_info.max
It returns: 1.7976931348623157e+308
. A string is only limited to pythons ram, so thats the biggest number.

- 656
- 5
- 18
-
1
-
I would say that positive/negative is stored in one bit extra, so it would be `-(1.7976931348623157e+308)`, but i couldn't find info on that. – Lukas Neumann Oct 06 '20 at 17:32
-
3