What is the maximum length of str(float(any_possible_value)) in Python?
Asked
Active
Viewed 164 times
1
Boris Verkhovskiy
- 10,733
- 7
- 77
- 79
-
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
-
2@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
-
I 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
2 Answers
1
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.maxshows 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.
Mad Physicist
- 95,415
- 23
- 151
- 231
-
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
-1
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.
Lukas Neumann
- 629
- 5
- 16
-
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
-
2