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?
#1
I am writing a program that will automatically check & pay off the amount I owe on my credit card. Since it takes a few days to process payment, I am saving the value I owed to a .txt file on my PC, then when the script goes off the following day, it will check the text file and see if the amount I currently owe is the same as the amount I owed yesterday. If it is the same it will not pay again.

Since I am not performing mathematical operations on the floats, it may be fine to compare. But I am just concerned because everyone says comparing floats is unreliable.

The exact steps I'm performing:
- Copy the amount owing from the text on the website (ex: 237.55)
- Read the contents of my text file, and assign it to a variable (ex: 237.55)
- Compare the two floats (==) and if they are equal, I exit the program.

Could this potentially cause issues?
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  floats 2 decimals rwahdan 3 1,732 Dec-19-2021, 10:30 PM
Last Post: snippsat
  rounding and floats Than999 2 3,266 Oct-26-2020, 09:36 PM
Last Post: deanhystad
  int, floats, eval menator01 2 2,533 Jun-26-2020, 09:03 PM
Last Post: menator01
  Stuck comparing two floats Tizzle 7 3,247 Jun-26-2020, 08:23 AM
Last Post: Tizzle
  rounding floats to a number of bits Skaperen 2 2,415 Sep-13-2019, 04:37 AM
Last Post: Skaperen
  is try/except safe inside of another except? Skaperen 5 2,725 Aug-04-2019, 07:14 PM
Last Post: buran
  is it safe to close stdin Skaperen 1 2,765 Apr-04-2019, 06:57 AM
Last Post: Gribouillis
  comparing fractional parts of floats Skaperen 4 3,498 Mar-19-2019, 03:19 AM
Last Post: casevh
  Integer vs Floats Tazbo 2 2,965 Jan-09-2019, 12:06 PM
Last Post: Gribouillis
  Formatting floats Irhcsa 6 4,321 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