Results 1 to 3 of 3
  1. #1
    Zwander is offline Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Default Addition not being performed correctly? <Solved- Floating point calculation errors>

    First of all, hello, this is my first post on these forums. So uh, yea. Hi!

    The below image demonstrates my problem. The green highlights show how the variables are set to the same values. The purple/blue shows the difference in resulting values, and the red shows the problem.

    Addition not being performed correctly?-bug-fixing-1.png

    The number in red is the distance between the two objects. This should ALWAYS be "-1<dy>1" with dy being the distance.

    Pretty much a[1] and b[1] are the objects y values. a[3] and b[3] is the dy values. ((y1+y1)-y2) (oops, its supposed to be +h1 not +y1. This is just visual though, the code is correct) is equal to the distance between the two objects.

    a[1] and b[1] are added to a[3] and b[3] so that they move. The resulting difference in distance should be equal to -1. Especially seeing as I set a[1] and b[1] exactly 21 px apart, and subtracted the same numbers from them (see in green). If your wondering, both the green lines were run (there's a console entry a bit further up that proves it).

    I'm sure this is due to something I have missed or miss spelt in my code, but I cant seem to find it.

    If this is too confusing just say and I'll try to rephrase.

    Thanks :D

    Edit: Fixed some spelling.
    Last edited by DarrylBurke; 08-15-2012 at 01:50 PM. Reason: Solved

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Addition not being performed correctly?

    Read this article; it thoroughly explains the inaccuracies you get with floating point calculations; you have to live with it or come up with something smarter that reduces those numerical errors.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Zwander is offline Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Default Re: Addition not being performed correctly?

    Quote Originally Posted by JosAH View Post
    Read this article; it thoroughly explains the inaccuracies you get with floating point calculations; you have to live with it or come up with something smarter that reduces those numerical errors.

    kind regards,

    Jos
    Ah, I see. I'm glad to see its not an error on my part.

    Thing that intrigues me is that even when I rounded both sets of numbers the error still persisted. Not only that, but the error always occurs in the exacts same place despite the use off different numbers.

    I guess I will resort to adding an approximation of the difference to the lacking number in order to fix this problem.

    Thanks for your help :D

    Edit: Wow, I dont think I have the patience to read all that :P I'll give it a go though.
    Last edited by Zwander; 08-15-2012 at 09:59 AM.

Similar Threads

  1. Help with Action Performed method
    By program junkie in forum New To Java
    Replies: 8
    Last Post: 12-10-2012, 02:24 AM
  2. action performed method for
    By thousif291989 in forum AWT / Swing
    Replies: 3
    Last Post: 04-21-2011, 05:08 PM
  3. Replies: 2
    Last Post: 03-30-2011, 11:10 PM
  4. JXTaskPane Action performed
    By Rama Koti Reddy in forum AWT / Swing
    Replies: 0
    Last Post: 12-10-2007, 01:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •