5

Possible Duplicate:
Floating point equality in python

I have a small "issue" about my Python code (I use version 2.5 right now, in ika game engine). I currently script objects for my game, and i would like to know, if its safe to compare two floating point number This way:

I will make a short example of what I do currently.

Every objects has a speed, from 0-9, represented in floating points. For example

speed = 4.83 cord_x = 10.0 cord_y = 10.0 

They have an AddMovement method. This sets an X,Y value, and represents the goal coordinates for the object.

target_x = 25.0 target_x = 26.75 movement = True # This represents if the object is moveing or not 

Every frame, the Maximum speed equals to this value:

maximum_x_speed = abs(cord_x-target_x) maximum_y_speed = abs(cord_y-target_y) 

And the real speed will be:

# Get the real X speed in this frame if maximum_x_speed < speed: real_x_speed = maximum_x_speed else: real_x_speed = speed # Get the real Y speed in this frame if maximum_y_speed < speed: real_y_speed = maximum_y_speed else: real_y_speed = speed 

Now, based on this real_x_speed value, and real_y_speed value, i substract this value from the coordinates.

if target_x < cord_x: cord_x -= real_x_speed elif target_x > cord_x: cord_x += real_x_speed if target_y < cord_y: cord_y -= real_y_speed elif target_y > cord_y: cord_y += real_y_speed 

And at the end, i check for equality

if cord_x == target_x and cord_y == target_y: # Halt movement, reached goal movement = False 

I had floating point errors in the past, with the 0.1 issue, and like... I fear, that this will cause some kind of error. Or this is logically impossible?

If i had grammar mistakes, sorry for them. english is not my native language... I would really appreciate some tips, if i should change this logic, or not.

2
  • What is the "0.1" issue? Are you floating point errors, exceptions or it's saying two thinks are not equal when you think it should? Commented Oct 11, 2012 at 12:09
  • Two things are not equal. I had a case a while ago, when i added 0.1 together 10 times, and made a check if its equal to 1.0 or not. Commented Oct 11, 2012 at 12:12

3 Answers 3

6

No, this type of code is never a good idea.

There will be floating point errors that accumulate, making it very hard to track. You really should use an "epsilon" approach, i.e. consider the two values equal if the difference is smaller than some set constant, which is often called epsilon:

def floats_equal(x, y, epsilon = 1e-4): return abs(x - y) < epsilon 

Of course you must tweak the epsilon value as required for your application, but a game where the values represent pixel-based locations shouldn't need too much precision.

Note that, as also pointed out in other answers, there is a smallest epsilon that will work on the machine, but often you are better off using an application-level epsilon, such as the one above. When interpolating things in 2D pixel space for presentational purposes, there's seldom any point in precision in on the order of 2.22044604925 * 10-16, which happens to be the value of sys.float_info.epsilon on my local system.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Actually epsilon = 0.01 would work too in my case (I tested this now). The object will use integer value for rendering, so the difference is basically unseen.
4

It is usually better to do this this way:

EPSILON = 0.00001 # or whatever if abs(cord_x - target_x) < EPSILON and abs(cord_y - target_y) < EPSILON: # Halt movement, reached goal movement = False 

Some people choose EPSILON to be:

import sys sys.float_info.epsilon 

Which is the difference between 1 and the least value greater than 1 that is representable as a float - here

Comments

0

The short answer is what you are doing is bad. The long answer is that the right way depends a bit on what you're doing. Take a look at a few different ways to compare floats:

http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

If you work with floating point numbers, it's worth reading through the whole thing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.