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
#2
have a look at decimal module from standard library. you are working with money, so always 2 decimal places
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
#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
#4
Read before using. You cannot create a decimal object using a float.

https://docs.python.org/3/library/decimal.html

import decimal

a = decimal.Decimal("234.55")
b = decimal.Decimal(234.55)
print(a, b)
Output:
234.55 234.55000000000001136868377216160297393798828125
Well, you CAN create a decimal object using a float, but it defeats the whole purpose of using decimal. The float number is already inaccurate, even if that doesn't show up when you print it. Using the inaccurate float to initialize the Decimal object doesn't magically create more accuracy.
Radical and ibreeden like this post
Reply
#5
For CAD stuff, I use a tolerance, Python has a math.isclose
https://docs.python.org/3/whatsnew/3.5.h...e-equality
RockBlok likes this post
Reply


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