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?
#1
Hey I'm learning python now and was just learning formatting in my online class. When I ran the following code I got the following output. I thought 2/3 was always .666 repeating. Is this not the case? or can someone explain the output to me.

Code:
print("{0:1.54}".format(2/3))

Output:
0.66666666666666662965923251249478198587894439697265625
Reply
#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.
DocFro and RockBlok like this post
Reply
#3
Ah that explains it. Thank you!
Reply
#4
To achieve the expected decimal representation of 2/3 with a precision of 54 decimal places, you can use the decimal module in Python, which provides decimal arithmetic for higher precision. Here's an example:

from decimal import Decimal

result = Decimal(2) / Decimal(3)
print("{0:1.54}".format(result))
This will give you the desired output:
Output:
0.66666666666666666666666666666666666666666666666666667
By using the Decimal type and the decimal module, you can perform calculations with higher precision and avoid some of the limitations of floating-point arithmetic. However, keep in mind that working with decimal numbers at very high precision can significantly impact performance and memory usage, so it's important to use it judiciously when necessary.
buran write Dec-12-2023, 09:06 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#5
Mandatory readings:
Floating Point Arithmetic: Issues and Limitations
Is floating point math broken?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  repeating a user_input astral_travel 17 2,312 Oct-26-2022, 04:15 PM
Last Post: astral_travel
  if else repeating Frankduc 12 2,521 Jul-14-2022, 12:40 PM
Last Post: Frankduc
  matching a repeating string Skaperen 2 1,257 Jun-23-2022, 10:34 PM
Last Post: Skaperen
  Random Number Repeating Tzenesh 5 4,052 Jan-13-2021, 10:00 PM
Last Post: deanhystad
  factorial, repeating Aldiyar 4 2,815 Sep-01-2020, 05:22 PM
Last Post: DPaul
  number repeating twice in loop JonnyEnglish 3 3,321 Nov-24-2019, 09:23 AM
Last Post: ThomasL
  Repeating equations Tbot100 2 3,282 May-29-2019, 02:38 AM
Last Post: heiner55
  First non-repeating char and some errors. blackknite 1 2,283 Jan-06-2019, 02:19 PM
Last Post: stullis
  repeating for loop Kaldesyvon 5 3,865 Dec-06-2018, 08:00 PM
Last Post: ichabod801
  Repeating same number in sequence Rudinirudini 6 4,250 Oct-28-2018, 07:44 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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