2

Hello I have a quick question before I go and do a complicated loop full of type conversions and stuff.

While comparing two values, will this result in True?

0.1 == 0.10 (in floating point)

I'm really comparing members of a list and they might come out like this and I just wanted to make sure equal values will result in true for my if statements

Will this result in true or would I need to change the decimal point precision for one of them?

maazza
  • 7,016
  • 15
  • 63
  • 96
  • possible duplicate of [What is the best way to compare floats for almost-equality in Python?](http://stackoverflow.com/questions/5595425/what-is-the-best-way-to-compare-floats-for-almost-equality-in-python) – C.B. Apr 09 '15 at 14:57

2 Answers2

2

If you run 0.1 == 0.10 in IDLE and it will show that it evaluates to true.

Same goes for 0.1 == 0.10000, this will evaluate to true.

Danny Bentley
  • 60
  • 2
  • 9
  • Yeah I'm really comparing members of a list and they might come out like this and I just wanted to make sure equal values will result in true for my if statements. – spacedancechimp Apr 09 '15 at 15:04
  • If the value in the list is 0.1 or 0.100 or something to that effect it will be true when compared with 0.1 – Danny Bentley Apr 09 '15 at 15:08
1

If you are doing decimal arithmetic that needs to be exact, unlike float, use the Decimal type.

Robert Jacobs
  • 3,266
  • 1
  • 20
  • 30