Results 1 to 3 of 3
 08152012, 08:55 AM #1Member
 Join Date
 Aug 2012
 Posts
 9
 Rep Power
 0
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.
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; 08152012 at 01:50 PM. Reason: Solved
 08152012, 09:22 AM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,188
 Blog Entries
 7
 Rep Power
 24
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 08152012, 09:56 AM #3Member
 Join Date
 Aug 2012
 Posts
 9
 Rep Power
 0
Re: Addition not being performed correctly?
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; 08152012 at 09:59 AM.
Similar Threads

Help with Action Performed method
By program junkie in forum New To JavaReplies: 8Last Post: 12102012, 03:24 AM 
action performed method for
By thousif291989 in forum AWT / SwingReplies: 3Last Post: 04212011, 05:08 PM 
actionListener/Performed different classes (CS106A NameSurfer)
By eXistenZ in forum New To JavaReplies: 2Last Post: 03302011, 11:10 PM 
JXTaskPane Action performed
By Rama Koti Reddy in forum AWT / SwingReplies: 0Last Post: 12102007, 02:27 PM
Bookmarks