Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / [BUG] Subtracting 0.1 from a float value until it is zero causes it to fail in a comparison with zero

Author
Message
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 19th Mar 2019 00:07

Attachments

Login to view attachments
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 19th Mar 2019 00:20
Interesting y is -0.000000 and v is 0.000000 so the if statement is working but for some reason y is negative zero
fubar
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 19th Mar 2019 00:30
Yeah. I think that might be the issue
I have a work around for two decimal places (Which is all a care about right now)

y = Round(y * 10.0) / 10.0

Seems to flush out the negative value
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 19th Mar 2019 00:37
I did find converting to string for the comparison helped but they had to be ABS values

fubar
Bengismo
6
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 19th Mar 2019 09:10 Edited at: 19th Mar 2019 09:13
Sigh....this again?? (Any why is everything a bug - maybe go do a search first? )

Its not a bug but a limitation with floating point numbers. Most fractions and real numbers cannot actually be accurately represented by a float value. They are approximated and so error creeps in.
https://floating-point-gui.de/errors/rounding/
https://www.theregister.co.uk/2006/08/12/floating_point_approximation/

This code below shows the ACTUAL value being stored and you might be suprised to know that most of those values are not actually what you think they are



The best way to correctly account for this is to use integers where possible or when comparing floats use <= or >= when you want to detect crossing thresholds.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 19th Mar 2019 09:19 Edited at: 19th Mar 2019 10:22
Very very sorry mr gizmo.
I did search the board but it's hard to know what to search for.
I promise i will never ever post anything as a bug again.

Thank you very much for taking the time i really do appreciate it
Santman
12
Years of Service
User Offline
Joined: 15th Sep 2011
Location: Inverness
Posted: 19th Mar 2019 18:33
Frosty.......

Login to post a reply

Server time is: 2024-04-18 13:42:11
Your offset time is: 2024-04-18 13:42:11