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.

DarkBASIC Professional Discussion / Floating point accuracy in dbpro

Author
Message
luskos
19
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 11th Apr 2010 23:51
Did someone know how accurate is dbpro with floating points, where could be expected rounding of the floats.

I`m asking because working on my own level editor and testing level save and load functions i see that when i make some simple level save it and then load it again, sometimes if i look closely i can see some objects are not quit the same place.I can say that because when i load the level my camera did not reset it`s position.

I`m using Matrix1 plugin for saving and loading if this can be the reason.

Where there is a will, there is a way.
I often edit my posts, that`s who i am
BMacZero
20
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 12th Apr 2010 01:00
Make sure you aren't saving the data as integers unintentionally. I don't know exactly what the accuracy is but unless you're dealing with coordinates in the tens of thousands it should not be noticable at all.

Sepnon
16
Years of Service
User Offline
Joined: 7th Feb 2010
Location: Brazil
Posted: 12th Apr 2010 01:49
Float and doubles are not accurate



run it, wait some time, you'll see a lot of 9s
BMacZero
20
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 12th Apr 2010 05:43
I'd call that highly accurate. I didn't see lasting irregularities until well into the 400th iteration. This is level loading we're talking about. One simple read operation is not going to produce anywhere near the error you get from doing floating point operations 400 times in a row (which is 1/1000 of a digit, by the way).

luskos
19
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 12th Apr 2010 14:02
It was my obligation to ask for this because i make my sample levels really small, and i imagine that if there is some inaccuracy with floats it may be progresive and in the end i get some mess with the real levels.But difference of 1/1000 of a digit in positioning can be seen only in my case and i think this will not affect the real game much.It`s a relief to know that.

Where there is a will, there is a way.
I often edit my posts, that`s who i am
veltro
User Banned
Posted: 12th Apr 2010 16:34
I don't remember what is the internal representetion for float in DBPro. According to IEEE standard float are stored in 4 bytes (single precision) or 8 bytes (double precision).

Anyway it doesn't matter. When you save you write X bytes and when you load you read the same X bytes.

This is true if you don't convert float before saving (e.g to string)
Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 12th Apr 2010 19:38
Perhaps it's the scale of your game world - what size is it?

There is one alternative that I found quite useful, sometimes I use integers, nice safe integers - which gives a lot of accuracy - you just have to pick a size that affords enough control. For instance, using terrain tiles of 100x100 units. So if your game is that big, maybe just using integers would keep it right.


Health, Ammo, and bacon and eggs!
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 12th Apr 2010 19:53
But yet again, if you make a 100x100 game world, floats will be more accurate than integers...

TheComet

Sepnon
16
Years of Service
User Offline
Joined: 7th Feb 2010
Location: Brazil
Posted: 12th Apr 2010 20:16
but wait..

Quote: "I can say that because when i load the level my camera did not reset it`s position."


It's definitely not a precision problem, there's something wrong with your load/save functions
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 12th Apr 2010 22:42 Edited at: 12th Apr 2010 22:43
Quote: "But yet again, if you make a 100x100 game world, floats will be more accurate than integers"


No they won't. If you use integers you'll always be 100% accurate. With floats you are inherently inaccurate, for example you can't represent 0.1 no matter how many decimal places you use.

I know where you're coming from - you have more positional range, but it's not more accurate.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 12th Apr 2010 23:00
Quote: "If you use integers you'll always be 100% accurate. "


What makes you think that?

More predictable perhaps.
dark coder
23
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 13th Apr 2010 03:15
Use double floats then, they can represent every number on the 32bit integer number line exactly, as well as a lot of values in between if needed. Single precision ones can also represent a large number of them, comparing an integer to a float's inability to represent 0.1 isn't exactly valid because an integer can't represent it either. As it's already been mentioned, if you want to minimize loss then never convert the floats to ASCII and back again, that's just asking for data loss.

Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 13th Apr 2010 09:32
By using integers, I was meaning that the 'scale' of a DBPro unit would be more like 1 unit = 1 inch, then positioning would snap into this 1 inch grid because the data would be stored as integers.

If your world was 100x100 DBPro units then integers wouldn't give a lot of scope, you'd be better off using floats, and at those ranges accuracy should not be a problem.

I never store positional data as floats, always integers, but I prefer big ranges, typically 100x100 DBPro units per terrain tile, so if a terrain was using a heightmap of 256x256, the game world would be 25600x25600. In that sort of scale, integers are fine and more robust.


There's another thing - rotation angles, why not take your angles and wrap them and divide by 1.5 - then you have a range of 0 - 240. 240 is a very very divisible number, and can be stored as a byte, and gives you 15 extra control values that you just don't get with other methods without an extra variable. Maybe an angle of 241 is a slow spin, 255 is a random angle, 250 to 252 could be the hands on a clock that show the system time!.
I'm using this for a 2D collision system that has to be super fast, so the angle and distance of a point can be stored as a stream of bytes - the distance is 0-255, and is a multiplier depending on the size of the sprite. I'm using those 15 control values to specify points, box, circle, line etc.
It's a good way to show your data who's boss! - 360 degrees is too much freedom anyway, I mean we are talking about loosing half a degree of resolution accuracy, but gaining the option of storing it as a byte, meaning 1/4 the memory usage.


Health, Ammo, and bacon and eggs!
aonyn
16
Years of Service
User Offline
Joined: 16th Oct 2009
Location:
Posted: 13th Apr 2010 10:13
Nice Tip on rotation angle Van B. Thanks.

Regarding integers vs floats, what I often do is a hybrid method. I often do all my math routines as integer, then when I use the result for updating objects in my game world, I will use a function which will return the integer as a float, multiplied by something like 0.001 number of decimal places depends on the circumstance of the world I am working in, but often I do work to thousands as shown.

By doing this, I keep the integrity of my data, no loss due to floating point error, to the extend of the multiplier I am using, so accuracy is adjustable to the situation at hand. At the same time, I get the benefit of more positional range in smaller scales with this method. Of course there is the drawback to this method of extra math in every cycle, so use it as necessary, and as recommended above, use pure integer placement in larger scales for better performance if necessary.

regards,
Dave
Hawkblood
16
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 14th Apr 2010 16:05
Quote: "Use double floats then"

I think it has been discovered that floats and double floats have the same accuracy in DBP for some reason....

There are a lot of long-winded posts (not that I haven't had my share), but I think that if you make all your static objects to whole number diminsions and place them at integer locations, you won't have the problem you described in your original post.... If you do, then try using another plugin to load/save (or make your own).

The fastest code is the code never written.
Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 14th Apr 2010 16:51
Hey, there's nothing wrong with long winded when it comes to DBPro discussion .

Really, I think the more we explain things in these threads, the more useful they are in the long run. It's good to go the extra mile, and consider what the poster might have issue with next, or what other people might have an issue with alongside the original problem. If it saves people from starting their own threads, then it's all good in my book.


Health, Ammo, and bacon and eggs!
Hawkblood
16
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 14th Apr 2010 17:02
Oh yea... I really get the whole "answer the question before it comes up" idea. What I don't like is seeing how "we" answer the poster's question (or at least we think we do) and get no report on whether it was the answer he/she was looking for.... I like to keep things in a wrapper of Question-Answer-Thanks. It makes it more definitive; otherwise we keep beating a dead horse without knowing if the horse even exists lol

The fastest code is the code never written.
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 15th Apr 2010 00:01
Quote: " comparing an integer to a float's inability to represent 0.1 isn't exactly valid because an integer can't represent it either."


Comparing abilities is valid, the discussion refers to "if i look closely i can see some objects are not quit the same place". Any integer you use will remain the integer you intended it to be - 1 will always be 1. If you specify a float of 0.1, you won't get 0.1.

luskos
19
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 15th Apr 2010 08:35
I`m using floats because of the metod i use to place objects.I have 1 object i use for gadget, wherever it`s position there is new object placed when i need.My gadget is WASD controled and the only integer position i can have is on Y axis because i inc or dec it whole 1 everytime i`m moving the gadget up, and down.And my editor is terrainless, for now.My goal was other so i left behind the terrains, if the projects expand much then i`ll think about supporting them.

Where there is a will, there is a way.
I often edit my posts, that`s who i am
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 15th Apr 2010 15:09
Quote: "the discussion refers to "if i look closely i can see some objects are not quit the same place". Any integer you use will remain the integer you intended it to be - 1 will always be 1. If you specify a float of 0.1, you won't get 0.1."


You've still missed the point.

Object positions are stored as floats. So no matter how careful you are with integers in your arithmetic the final positions of the objects will be subject to minor inaccuracies if you go beyond the 7 digit range supported by floats.

For example, if you tried to do the following then you'd find the object isn't placed quite where you might have thought:

luskos
19
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 15th Apr 2010 17:57
So basicly objects with higher positions will have much minor inacuracy in position because looking at my gadget positions the values are allways 12 digit floats.When there is increase with 1 digit in left, there is decrease with 1 digit in right, but allways 12 digits.And anything with 7 or less digits in the right side will be more acurate placed.Is this true?

Where there is a will, there is a way.
I often edit my posts, that`s who i am
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 15th Apr 2010 19:13 Edited at: 15th Apr 2010 19:14
The important thing is the number of significant digits - ordinary DBPro floats are single precision and are only accurate to about the first 7 significant figures. The last 5 of your 12 significant figures will be meaningless.

I find it hard to see why you need 12 significant figures. Perhaps you need to organize things rather differently in your application.

Another thing to remember when dealing with object positions is that positions (and vertices) can only be sensibly distinguished if their positions differ in the first few significant figures. Try the following with different values for offset.



If you use fractional values such as 0.01 you'll have the added complication that BatVink mentioned, i.e. that most can't be accurately represented using 32 bits - but they will still be accurate to about 7 significant figures in decimal.
Hawkblood
16
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 15th Apr 2010 20:32
A work-around is to make your world move relative to the camera and keep the camera at 0,0,0. There is only so much of the world that can be seen at one time anyway and the further things are from the camera, the less accurate the measurments have to be. Just a thought.....

The fastest code is the code never written.
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 16th Apr 2010 00:24
That should work.

The key issue is to ask why such excessive precision seems to be necessary in the first place.
luskos
19
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 16th Apr 2010 11:22
Actually there is no need of this kind of precision.Just the method i use for placing objects is that i get position of my placment gadget which are 12 digit floats.I write the coordinates the same way i get them, without touching them, but loading them back gives incorections, i`ve just wanted to know why.I`ve learned something useful about floats and integers in the process.I`m not sure if it nesecery to write some function to deal with this and skip everything beyond 7th digit, because there is no significant inaccuracy in placment of objects.

@Hawkblood
What you suggest means that i need to rewrite everything in my code, because my camera is folowing the placement gadget and i need to know the coordinates where i`m placing objects.If i move everything else but camera and the gadget i need to follow them not, but track world offset to know where i`m positioning objects.

Where there is a will, there is a way.
I often edit my posts, that`s who i am
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 16th Apr 2010 15:05
Quote: "I`m not sure if it nesecery to write some function to deal with this and skip everything beyond 7th digit, because there is no significant inaccuracy in placment of objects."


Sighs of relief all round I guess.
Hawkblood
16
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 16th Apr 2010 15:38
Quote: "What you suggest means that i need to rewrite everything in my code"

I know how you feel! Sometimes that's what it takes to get the result you want. If your world is so huge that you would go beyond the floating point's ability to accurately place objects, I know of no other way than what I suggested.

The fastest code is the code never written.
luskos
19
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 17th Apr 2010 06:00
Hawkblood i`m not that far, and levels i make are small.If you read all posts carefully you`ll see that any float gives inacuracy, but as we cleared, it`s not significant.

Where there is a will, there is a way.
I often edit my posts, that`s who i am

Login to post a reply

Server time is: 2026-07-26 07:36:25
Your offset time is: 2026-07-26 07:36:25