Python Forum
When is it safe to compare (==) two floats?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
When is it safe to compare (==) two floats?
#3
(Nov-11-2023, 07:51 PM)buran Wrote: have a look at decimal module from standard library. you are working with money, so always 2 decimal places

I just tried it out and it seems to be doing the exact thing I was afraid float would do.
previousOwing = decimal.Decimal(234.55)
print(previousOwing)
Output:
234.55000000000001136868377216160297393798828125
which is ironic, because float seems to work perfectly:
previousOwing = float(234.55)
print(previousOwing)
Output:
234.55
Reply


Messages In This Thread
RE: When is it safe to compare (==) two floats? - by Radical - Nov-11-2023, 10:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  floats 2 decimals rwahdan 3 1,711 Dec-19-2021, 10:30 PM
Last Post: snippsat
  rounding and floats Than999 2 3,217 Oct-26-2020, 09:36 PM
Last Post: deanhystad
  int, floats, eval menator01 2 2,521 Jun-26-2020, 09:03 PM
Last Post: menator01
  Stuck comparing two floats Tizzle 7 3,192 Jun-26-2020, 08:23 AM
Last Post: Tizzle
  rounding floats to a number of bits Skaperen 2 2,383 Sep-13-2019, 04:37 AM
Last Post: Skaperen
  is try/except safe inside of another except? Skaperen 5 2,704 Aug-04-2019, 07:14 PM
Last Post: buran
  is it safe to close stdin Skaperen 1 2,752 Apr-04-2019, 06:57 AM
Last Post: Gribouillis
  comparing fractional parts of floats Skaperen 4 3,469 Mar-19-2019, 03:19 AM
Last Post: casevh
  Integer vs Floats Tazbo 2 2,951 Jan-09-2019, 12:06 PM
Last Post: Gribouillis
  Formatting floats Irhcsa 6 4,304 Oct-04-2018, 04:23 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

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