Hello!

I was writing a code and got an unexpected result, I broke it down in order to analyse it and in the end I have 8 rows of code. The problem I have is that I print x and it gives me the value 0.9 then if I say: If x < 0.9: print "Yes" it will do.

But since 0.9 isn’t < then 0.9 my program fails.
The code:

x= 0
for i in range(9):
    x += 0.1
    if x < 0.9:
        print 'Yes'
    else:
        print "No"

This will print 'yes' nine times instead of yes eight times and 'No' one time.
i have phyton 2.6.5.

edit*
Worth noticing is that this code just going 99 steps work properly and write 98 'Yes' and 1 'No':

x= 0
for i in range(99):
    x += 0.01
    if x < 0.99:
        print 'Yes'
    else:
        print "No"

Here some debugging tip: When in doubt, use print!

x= 0
for i in range(9):
    x += 0.1
    print x # print the damn thing.
    if x < 0.9:
        print 'Yes'
    else:
        print "No"

And please, use the code tag next time. This way to makes it so much easier to read?

Here some debugging tip: When in doubt, use print!

x= 0
for i in range(9):
    x += 0.1
    print x # print the damn thing.
    if x < 0.9:
        print 'Yes'
    else:
        print "No"

And please, use the code tag next time. This way to makes it so much easier to read?

Oh, didn’t know about that, I will do that next time.
Yea I have been trying print x, but just say x = 0.9 and then print 'Yes' anyhow, but thx for the quick replay

This should tell you the reason

>>> x= 0
>>> for i in range(9):
    x += 0.1
    print x-0.9 # print the damn thing.
    if x < 0.9:
        print 'Yes'
    else:
        print "No"

        
-0.8
Yes
-0.7
Yes
-0.6
Yes
-0.5
Yes
-0.4
Yes
-0.3
Yes
-0.2
Yes
-0.1
Yes
-1.11022302463e-16
Yes
>>>

Oh ty very much!

Even though I still cant understand how adding 0.1 nine times in a row isn’t exactly 0.9 I can at lest solve my problem.

You should realize in floating point numbers,
.1 equals .100000000000000000001 or so.

So if you want exact, you can use Decimal

Or instead of = you must allways do
Find one reasonably small number for possible error amount. Call it epsilon, then use :

if abs(my_number-compared_to)<epsilon:

The way a binary system like a computer represents a decimal or floating point number can create a tiny difference that makes it hard to compare floats directly. All computer languages have this problem, and beginners just love to stumble over it.

To illustrate this ...

def fuzzy_compare(a, b, delta=0.000000001):
    """
    used for == comparison of floating point numbers a and b
    returns True if the difference between a and b does not
    exceed delta otherwise returns False
    """
    return abs(a-b) < delta

# testing ...
a = 6.4
b = 6.3 + 0.1
# a list truly shows the reality
# the difference is a very tiny 0.0000000000000009
print( [a, b] )  # [6.4000000000000004, 6.3999999999999995]

print( a == b )  # False

print( fuzzy_compare(a, b) )  # True

Yes that I meant. one possible delta value can be found like this. However if you do many calculations with inexact numbers the errors add up. This constant would work with calculations with small numbers.

>>> delta=1.0
>>> while 1+delta/2.0>1.0:
	delta/=2.0

>>> print delta
2.22044604925e-16
>>> print 1.0+0.1==1.1
True
>>> print 1.0-0.1==0.9
True
>>> print 1.0-9*0.1==0.1
False
>>> print abs(1234-1001*0.1)
1133.9
>>> print abs(1234-1001*0.1-1133.9)<delta
True
>>> print abs(1.0-9*0.1-0.1)<delta
True
>>> print abs(1.0-9*0.1-0.1)
2.77555756156e-17
>>> print abs(1234-1001*0.1-1133.9)
0.0
>>> x=1234
>>> for i in range(1001): x-=0.1

>>> print abs(x-1133.9)
9.09494701773e-11
>>> print abs(x-1133.9)<delta
False
>>> print abs(x-1133.9)/delta
409600.0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.