Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Python comparing float
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Ricardo Franco  
View profile  
 More options Aug 22 2012, 3:07 pm
From: Ricardo Franco <ricardo.kr...@gmail.com>
Date: Wed, 22 Aug 2012 16:07:40 -0300
Local: Wed, Aug 22 2012 3:07 pm
Subject: [pygame] Python comparing float

Hi, this is more a question about python than pygame. Considering this code:

#####################################################
rate = 1
desired_rate = 2

while True:
    if rate != desired_rate:
        print rate, desired_rate, (rate != desired_rate)
        rate += 0.1 * (1 if desired_rate > rate else -1)
    # if
# while
#####################################################

It should smoothly move rate from 1 until 2.
But the output is this:

1 2 True
1.1 2 True
1.2 2 True
1.3 2 True
1.4 2 True
1.5 2 True
1.6 2 True
1.7 2 True
1.8 2 True
1.9 2 True
2.0 2 True
1.9 2 True
2.0 2 True
1.9 2 True
2.0 2 True
1.9 2 True
2.0 2 True
1.9 2 True
2.0 2 True
...

Is python missing in comparison?

--
Ricardo Franco Andrade             @ricardokrieg

( Game | Desktop | Web ) Developer

email: ricardo.kr...@gmail.com
contact: +55 (86) 9958 9725
             +55 (86) 9436 0830
twitter: twitter.com/ricardokrieg
facebooK: https://www.facebook.com/krieg.ricardo
github: https://github.com/ricardokrieg


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Radomir Dopieralski  
View profile  
 More options Aug 22 2012, 3:20 pm
From: Radomir Dopieralski <pyg...@sheep.art.pl>
Date: Wed, 22 Aug 2012 21:20:33 +0200
Local: Wed, Aug 22 2012 3:20 pm
Subject: Re: [pygame] Python comparing float

One thing you need to understand about floating point numbers is that
they are not exact -- they are the nearest values that can be
represented in binary in the limited number of bytes (double
precision, in case of python, AFAIR).

Another thing to remember is that the decimal value 0.1 is a periodic
fraction in binary -- it doesn't have a finite exact representation,
just like 1/3 doesn't have one in decimal. That means that whenever
you write 0.1, the computer really stores a value that is a little bit
larger than that. You can try it in the python console:

>>> '%f' % 0.1
'0.100000'
>>> '%.40f' % 0.1

'0.1000000000000000055511151231257827021182'


Now, consider this:

>>> '%.40f' % (1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1)

'2.0000000000000008881784197001252323389053'

You can clearly see, that this is not equal 2.0.

That's why when you are comparing floating point numbers, you always
have to check if they are within some small error value from each
other. How large that value is depends on your particular use case.

If you really need to have *exact* results, at the expense of slower
computation, use the Decimal type.

I hope that helps,

--
Radomir Dopieralski


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ricardo Franco  
View profile  
 More options Aug 22 2012, 3:27 pm
From: Ricardo Franco <ricardo.kr...@gmail.com>
Date: Wed, 22 Aug 2012 16:27:25 -0300
Local: Wed, Aug 22 2012 3:27 pm
Subject: Re: [pygame] Python comparing float

exactly
I changed my comparison to:

if abs(rate-desired_rate) > 0.01:

And now it works :D

thanks

2012/8/22 Radomir Dopieralski <pyg...@sheep.art.pl>

--
Ricardo Franco Andrade             @ricardokrieg

( Game | Desktop | Web ) Developer

email: ricardo.kr...@gmail.com
contact: +55 (86) 9958 9725
             +55 (86) 9436 0830
twitter: twitter.com/ricardokrieg
facebooK: https://www.facebook.com/krieg.ricardo
github: https://github.com/ricardokrieg


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Greg Ewing  
View profile  
 More options Aug 22 2012, 10:27 pm
From: Greg Ewing <greg.ew...@canterbury.ac.nz>
Date: Thu, 23 Aug 2012 14:27:03 +1200
Local: Wed, Aug 22 2012 10:27 pm
Subject: Re: [pygame] Python comparing float

Ricardo Franco wrote:
>     That's why when you are comparing floating point numbers, you always
>     have to check if they are within some small error value from each
>     other. How large that value is depends on your particular use case.

It's even better if you can avoid comparing floats for
equality at all. You can do that in this case by using an
integer loop counter and computing the float value from it:

 >>> for i in xrange(11):
...    rate = 1.0 + i * 0.1
...    print rate
...
1.0
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
2.0

--
Greg


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ricardo Franco  
View profile  
 More options Aug 23 2012, 9:34 am
From: Ricardo Franco <ricardo.kr...@gmail.com>
Date: Thu, 23 Aug 2012 10:34:30 -0300
Local: Thurs, Aug 23 2012 9:34 am
Subject: Re: [pygame] Python comparing float

Well, this is a good idea, I go do this way. tks

2012/8/22 Greg Ewing <greg.ew...@canterbury.ac.nz>

--
Ricardo Franco Andrade             @ricardokrieg

( Game | Desktop | Web ) Developer

email: ricardo.kr...@gmail.com
contact: +55 (86) 9958 9725
             +55 (86) 9436 0830
twitter: twitter.com/ricardokrieg
facebooK: https://www.facebook.com/krieg.ricardo
github: https://github.com/ricardokrieg


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Russell Jones  
View profile  
 More options Aug 28 2012, 3:26 pm
From: Russell Jones <russell.jo...@gmail.com>
Date: Tue, 28 Aug 2012 20:25:58 +0100
Local: Tues, Aug 28 2012 3:25 pm
Subject: Re: [pygame] Python comparing float

Alternatively, in the numpy module (part of the Scientific Tools for Python
http://www.scipy.org/ ) there is the linspace function, which you could use
like this
for x in numpy.linspace(0,2,21):
    print(x)

Russell

On 23 August 2012 14:34, Ricardo Franco <ricardo.kr...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »