Python Forum
Why is 2/3 not just .666 repeating?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is 2/3 not just .666 repeating?
#2
Python floats are normally represented as 64 bits binary numbers, which means that they cannot represent faithfully more than 16 decimal digits. To see the capabilities of your Python print
sys.float_info
Also see the following example shows that python cannot tell the difference between 2/3 and 2/3 truncated to 16 digits
>>> x = 2/3
>>> f"{x:.16}"
'0.6666666666666666'
>>> 
>>> x == float(f"{x:.15}")
False
>>> x == float(f"{x:.16}")
True
>>> 
Note: this is not a limitation of Python alone, most usual programming languages use the same floating numbers. Specialized libraries such as gmpy2 handle multiprecision floating numbers with any number of digits.
RockBlok and DocFro like this post
Reply


Messages In This Thread
Why is 2/3 not just .666 repeating? - by DocFro - Nov-25-2023, 10:09 PM
RE: Why is 2/3 not just .666 repeating? - by Gribouillis - Nov-25-2023, 10:41 PM
RE: Why is 2/3 not just .666 repeating? - by DocFro - Nov-26-2023, 12:59 AM
RE: Why is 2/3 not just .666 repeating? - by loben - Dec-12-2023, 08:48 AM
RE: Why is 2/3 not just .666 repeating? - by buran - Dec-12-2023, 09:09 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  repeating a user_input astral_travel 17 2,467 Oct-26-2022, 04:15 PM
Last Post: astral_travel
  if else repeating Frankduc 12 2,711 Jul-14-2022, 12:40 PM
Last Post: Frankduc
  factorial, repeating Aldiyar 4 2,890 Sep-01-2020, 05:22 PM
Last Post: DPaul
  Repeating equations Tbot100 2 3,335 May-29-2019, 02:38 AM
Last Post: heiner55
  repeating for loop Kaldesyvon 5 3,957 Dec-06-2018, 08:00 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020