Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8658865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:52:10+00:00 2026-06-12T15:52:10+00:00

Possible Duplicate: Floating point equality in python I have a small issue about my

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:52:11+00:00Added an answer on June 12, 2026 at 3:52 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Floating point comparison I have a problem about the accuracy of float
Possible Duplicate: Floating point comparison I have some simple C code and the output
Possible Duplicate: Floating point issue in C #include<stdio.h> main() { int a,b; float f;
Possible Duplicate: Floating point inaccuracy examples PHP rounding issue - Is this a bug?
Possible Duplicate: Floating point comparison When I run the code below I get the
Possible Duplicate: Floating Point Limitations Using Python 2.7 here. Can someone explain why this
Possible Duplicate: Java floating point arithmetic What is special about this double math in
Possible Duplicate: How to convert floats to human-readable fractions? I have a floating point
Possible Duplicate: In R, what is the difference between these two? floating point issue
Possible Duplicate: Why is floating point arithmetic in C# imprecise? I have been dealing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.