Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
whole number and fraction
#11
Using decimal.Decimal(float) or fractions.Fraction(float) is not right.
The float you see is wrong. The representation is binary and can't represent all decimal fractions.
Avoid using float as value for Decimal or Fraction.

Compare the results:
print(fractions.Fraction(3, 10)) #right
print(fractions.Fraction('0.3')) #right
print(fractions.Fraction(0.3)) # wrong
The last result is wrong, because the literal 0.3 is converted to a binary fraction, which is very close to 0.3, but it is not real 0.3.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
whole number and fraction - by Skaperen - Mar-15-2019, 06:59 PM
RE: whole number and fraction - by ichabod801 - Mar-15-2019, 07:10 PM
RE: whole number and fraction - by Skaperen - Mar-15-2019, 10:51 PM
RE: whole number and fraction - by ichabod801 - Mar-16-2019, 02:27 AM
RE: whole number and fraction - by samsonite - Mar-16-2019, 08:48 AM
RE: whole number and fraction - by farhan275 - Mar-16-2019, 12:00 AM
RE: whole number and fraction - by ichabod801 - Mar-16-2019, 03:14 PM
RE: whole number and fraction - by samsonite - Mar-16-2019, 04:59 PM
RE: whole number and fraction - by casevh - Mar-16-2019, 07:03 PM
RE: whole number and fraction - by Skaperen - Mar-16-2019, 07:08 PM
RE: whole number and fraction - by DeaD_EyE - Mar-17-2019, 06:40 AM
RE: whole number and fraction - by Skaperen - Mar-17-2019, 11:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  method to remove zero digits from fraction in decimal Skaperen 17 2,828 Oct-23-2022, 04:02 AM
Last Post: Skaperen
  Fraction Calculation with Limitations TINMAN01 13 5,464 Dec-22-2020, 04:45 AM
Last Post: bowlofred
  fraction module: can you stop the reducing? qmfoam 1 2,437 Oct-10-2020, 06:10 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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