-1

I know that python's integer is unbounded, but how many significant digits can a floating point number contain

  • 1
    You can find answer here - https://stackoverflow.com/questions/3477283/what-is-the-maximum-float-in-python – Paweł Kowalski Jan 24 '20 at 17:29
  • integer doesn't have decimal place. – furas Jan 24 '20 at 17:29
  • I am asking about floating point numbers –  Jan 24 '20 at 17:30
  • if you ask floating point numbers then why you mentioned integers? – furas Jan 24 '20 at 17:30
  • I knew the maximum int was unbounded, but I am asking about the maximum float –  Jan 24 '20 at 17:31
  • @Pawl Kowalski the question is for the maximum number i am asking for the maximum decimal place –  Jan 24 '20 at 17:33
  • Does this answer your question? [How to get largest possible precision? (Python - Decimal)](https://stackoverflow.com/questions/53660036/how-to-get-largest-possible-precision-python-decimal) – G. Anderson Jan 24 '20 at 17:37
  • It does not, i want to know how many decimal places python can have –  Jan 24 '20 at 17:38
  • 1
    Your question makes no sense given how floating point is represented internally. It would be more reasonable to ask how many significant digits can a floating point number contain. – martineau Jan 24 '20 at 17:40
  • Ok. How many significant digits can a floating point number contain –  Jan 24 '20 at 17:41
  • @martineau `sys.float_info.min` has 715 significant digits: `len(('%.2000f' % sys.float_info.min).strip('0.'))`. – Kelly Bundy Jan 24 '20 at 17:52
  • A 64-bit double has about 16 digits of precision (and Python uses those to represent floating point numbers). You can get arbitrary-precision floating-point arithmetic by using the third-party [`mpmath`](http://mpmath.org) module. – martineau Jan 24 '20 at 17:52
  • @HeapOverflow: That's not what that info means. – martineau Jan 24 '20 at 17:53
  • @martineau What do you mean? – Kelly Bundy Jan 24 '20 at 17:53
  • @HeapOverflow: It's the magnitude of the largest number that can represented — which is limited by max size of the biggest possible exponent. – martineau Jan 24 '20 at 17:55
  • @martineau Not sure why you're talking about magnitudes and exponents. You yourself turned this into "**significant digits**". I'm merely showing some number with 715 significant digits. It's too long to post here, but you can see it [here](https://repl.it/repls/QuixoticTenseOs). That number is exactly representable, and has 715 significant digits. I'm not saying it's the number with the most significant digits, I'm just saying there's one with 715. – Kelly Bundy Jan 24 '20 at 18:00
  • @HeapOverflow: I mentioned magnitudes and exponents to answer _your question_. – martineau Jan 24 '20 at 18:03
  • @martineau Hmm, ok, but it doesn't really answer my question. What are the "That" and the "what that info means" that you referred to? – Kelly Bundy Jan 24 '20 at 18:11
  • @HeapOverflow: `sys.float_info` – martineau Jan 24 '20 at 18:16
  • @martineau Which one is that, and what's the other one? – Kelly Bundy Jan 24 '20 at 18:19
  • @martineau And I didn't even say anything about `sys.float_info` other than that its `.min` has 715 significant digits. Which is correct. – Kelly Bundy Jan 24 '20 at 18:30
  • Does this answer your question? [What is the maximum float in Python?](https://stackoverflow.com/questions/3477283/what-is-the-maximum-float-in-python) – funnydman Jan 25 '20 at 06:17

2 Answers2

2

I think is what you want.

import sys
sys.float_info.max

I get 1.7976931348623157e+308

The docs: https://docs.python.org/3/library/sys.html#sys.float_info

Part 2:

The answer is tricky as several factors may get in the way and the practical use of float in an application is also a limiting factor (what do you want to do?). Rounding and OS may change this, but that is a whole different discussion.

Here is a pedestrian means to get a simple and imperfect answer:

>>> import math
>>> format(math.pi, '.48g')
'3.14159265358979311599796346854418516159057617188'
>>> format(math.pi, '.49g')
'3.141592653589793115997963468544185161590576171875'
>>> format(math.pi, '.50g')
'3.141592653589793115997963468544185161590576171875'
>>> format(math.pi, '.51g')
'3.141592653589793115997963468544185161590576171875'
>>> len(str(141592653589793115997963468544185161590576171875))
48

Some info to look at:

Use of float in various systems: https://press.rebus.community/programmingfundamentals/chapter/floating-point-data-type/

Intro to the standard: https://stackoverflow.com/a/36720298/860715

The standard: https://en.wikipedia.org/wiki/IEEE_754

There are several ways I could see attacking question, but any result would be easy for a comp-sci or serious math person to bust. In PHP I have been burned by OS and float many times. Python is much better, but it still extends a lot of the underlying operating system's operations and that is something that can get unpredictable when pushing the limits.

I hope this helps as any answer provided is sure to be riddled with debate. In the long run you probably need to get something running and want to know what you can use reliably. I hope my proof gets you closer to that conclusion.

Marc
  • 1,895
  • 18
  • 25
1

python [...] how many significant digits can a floating point number contain

767 is the most (when it uses double-precision, which I think it does for pretty much everyone).

One such number is (253 - 1) / 21074.

Demo:

>>> len(('%.2000f' % ((2**53 - 1) / 2**1074)).strip('0.'))
767

Got it from this article which goes into more detail.

The number in its full beauty (broken into lines for readability:

>>> print('\n'.join(textwrap.wrap(('%.2000f' % ((2**53 - 1) / 2**1074)).rstrip('0'))))
0.00000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000044501477170144022721148195934182639518696
3909270329129604685221944964444404215389103305904781627017582829831782
6079242213740172877389189291055314414815641243486759976282126534658507
1045737627442980259622449029037796981144446145705102663115100318287949
5279596682360399864792509657803421416370138126133331198987655154514403
1526125381326665295130600018491776632866075559583739224098994780755659
4098101021612198814605258742579179000071675999344145086087205681577915
4359230189103349648694206140521828924314457976051636509036065141403772
1744226256159024466852576737244643007551333245007965068671949137768847
8005309963967709758965844137894433796621993967316936280457084866613206
7970177289160800206986794085513437288676754097207572324554347709124613
17493580281734466552734375
Kelly Bundy
  • 23,480
  • 7
  • 29
  • 65