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 / Float precision issue?

Author
Message
Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 20th Jun 2013 17:42
AGK 10813
I was trying to figure out why my tiles weren't being placed properly and after some debugging I've found that floats are not what I expect them to be.
If you set a float = 2.2 then what is actually stored is 2.1999998...

Not sure if this is a bug or not, but it feels like it is to me. If you put 2.2 into a float variable I would expect 2.2000000 out.

Here's my testing code

I tried a calculation of the float * 1000 to check to see if it was something odd with Str(), but the float value is definitely quite off.

Here's the log of results:




Funnell7
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 20th Jun 2013 17:56 Edited at: 20th Jun 2013 17:56
I had a similar issue way back when, and as I understand it, float's in their very nature arn't very precise. I don't know much about how it works, but take a look at this post;

http://forum.thegamecreators.com/?m=forum_view&t=199263&b=41
Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 20th Jun 2013 18:09
Yeah, they only have 7-digit precision, but I'm not clear as to why a 2-digit float would lose any precision. It seems like 3.2 would simply be 3.200000 not 3.1999998

I came across this because I had a calculation that is placing tiles end up being off by a pixel after only 16 out of 32 rows. SetSPriteSnap wasn't helping either so I dug and found that there was some weird rounding. Then I dug further and simply put in 3.2 as the tile's width I still get the precision error because AppGameKit is seeing 3.2 as 3.1999998...

I just want 2 digits! lol

Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 20th Jun 2013 18:40
maybe you add this values.
did you try
3.2*1.0
3.2*2.0
3.2*3.0
3.2*4.0
...
3.2*16.0
for positions?
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 20th Jun 2013 19:12
This is not an AppGameKit issue, but a PC Architecture issue. Not something solvable in AGK.

This is my current project, check it out! [href]forum.thegamecreators.com/?m=forum_view&t=204576&b=8[/href]
This is my website, check it out! [href]http:\\www.TeamDefiant.co.uk[/href]
Naphier
13
Years of Service
User Offline
Joined: 2nd Oct 2010
Location: St Petersburg, Florida
Posted: 20th Jun 2013 19:16
I was doing it with an integer multiple in each iteration.

Something like this:
3.2 * 1
3.2 * 2
3.2 * 3
...

I tried using both floats for both values and the result is the same.

I've worked around this just find it weird that the precision falls off so readily with a 2 digit float.

THE ISSUE IS MINE!
I was using this
tileW# = GetImageWidth(bgtile)
screenH# = GetDeviceHeight() * 1.0
tileHpct# = 100 * tileW# / screenH#

Which is ever so slightly wrong even though the image is 32x32 pixels.
My fault!

I created this to set precision



Something similar could be created with Trunc()

I guess I need to go back to school and learn how to handle floats !

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 20th Jun 2013 22:24
Quote: "This is not an AppGameKit issue, but a PC Architecture issue. Not something solvable in AGK"

+1

If you used DB in the past, you'll notice it had the same issue as well. And as Mobiius mentioned, it's all down to the cpu architecture.

Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 20th Jun 2013 22:47
Integer values get stored in straight binary, so are very 'precise'.

Floats and Doubles get stored as strange things using powers of two and positive/negative exponents and stuff. For a very complete explanation of how they are handled, try this Wikipedia entry.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
JimHawkins
14
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 21st Jun 2013 00:06
As AL wisely says - floating point numbers are a little bit strange, and best avoided, if possible. They are imprecise, and NEVER used for money transactions.

AGK uses 32-bit floats probably because OpenGL uses 32-bit (single precision) floats.

You should not rely on print or format routines to tell you the value. They may be slightly wrong.

This is NOT a PC issue - it's the nature of floating point representation on all processors and platforms.

-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL

Login to post a reply

Server time is: 2024-04-28 18:03:23
Your offset time is: 2024-04-28 18:03:23