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 / Float value changing itself/not being accurate?

Author
Message
Omega gamer 89
17
Years of Service
User Offline
Joined: 10th Sep 2007
Location: Pittsburgh, PA
Posted: 8th Dec 2024 20:45 Edited at: 8th Dec 2024 21:03
Hi! So to make a long story short, I have a global variable that I declared as a float, called "Game_state" and in my main loop, I have a series of if/else statements for what to do depending on what the game_state is set to., and in various other places, the Game_state variable is set to specific values depending on what the user does.



I was having some trouble with certain code not getting executed inside one of these if statements, and I couldnt figure out why. I add a PRINT command to have the program print the current value of Game_state in the top corner so I could see what it was at all times, and to my surprise, when game_state SHOULD be set to 0.41, it was instead displaying as "0.409999996424"

This is very odd, as Game_state is only ever set directly with a "game_state = " and never changed via, like, any math or variables, so the only way it should be able to equal 0.409999996424, is if I explicitly typed in "if CONDITION then Game_state = 0.409999996424" and obviously I know for a fact Ive never done that. I know for a fact that, at the time Im encountering this issue, Ive just done "Game_state = 0.41" so it should just... BE that value. It should be "0.41" and not anything else.

Ive never had this issue before despite having other sections using Game state as 0, 0.1, 0.2, 0.3, 0.21, 0.22, 0.23, 2, 4, 6, 6.4, etc. I cant figure out why 0.41 is an issue and somehow changes itself to be ever so slightly less than that, but 0.21 and 0.22 and such stay their correct values just fine.

Any ideas? Is this just some inherent weirdness of Floats or am I missing something obvious?
LBFN
18
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 9th Dec 2024 04:06
I get the same results you did, only all of the floats I used did the same thing as the 0.409999...... number. It seems like I remember someone explaining it years ago that it was due to the way floats are calculated. I would suggest using integers for that you are doing, or perhaps even the SELECT / CASE commands. Floats are typically used when you need precision, such as in 3D angles, 3D position, etc.



So many games to code.....so little time.
Mage
Valued Member
18
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 9th Dec 2024 10:36
1. You can try and declare the value as a DOUBLE FLOAT instead. Precision is 15 digits according to the first principles documentation.

2. This is normal for floats. Your CPU will chat the math and processing of floats (especially for digits after 5 or 6 decimal places). So you can never trust floats completely.
Your typical desktop calculator app is specially written to avoid these kinds of rounding errors.

3. I don't want to be a jerk here, but if you take a step back you are probably using floats wrong. Your game state variable probably should be an Integer, or a DWORD (positive integers only). Getting really specific, possibly a BYTE (0 to 255) or a WORD (0 to 65535).

I suspect you could represent your logic (at least at the level where you are comparing very specific numbers) with integers instead.

Mage's Modular Code - Get awesome UI controls and powerful Bitmap Fonts!
Screen Device Recovery - Stop your apps from crashing when minimized/tabbed/screen locked.
CPU Friendly Frame Limiter - Make your app generate less computer heat + noise, and use less battery life!
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 10th Dec 2024 12:20 Edited at: 11th Dec 2024 03:19
Single-precision floating-point format
https://en.wikipedia.org/wiki/Single-precision_floating-point_format

So Just Use Integers. Rather than 1.02 use 102

You can use STRINGS for your states. Than write a routine parse (tokenize) out combinations of states that your game might need.

PlayBASIC To HTML5/WEB - Convert PlayBASIC To Machine Code
Omega gamer 89
17
Years of Service
User Offline
Joined: 10th Sep 2007
Location: Pittsburgh, PA
Posted: 13th Dec 2024 02:35
Thank you everyone for the info! I had originally planned on using integers but for various reasons ended up needing "sub states" that were basically just more specific instances of certain "parent" states and didnt feel like going and changing EVERY instance of the game_state variable in my code, so I figured just add decimals. Had I known floats would cause this issue, I wouldve just put in the leg work back then and changed all the game state values. Ill have to just bite the bullet and do that now.
Derek Darkly
13
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 14th Dec 2024 20:17

You could put your game states into strings instead of actual numbers.

Another easy option could be to put it into an array such as DIM GameState#(n,n,n)
Then you could define each state like GameState#(0,4,1)=0.41 but use a loop to easily fill them all in.
Send your parents to noisy sprite demo hell... enter the D-Zone

Login to post a reply

Server time is: 2025-06-01 20:57:37
Your offset time is: 2025-06-01 20:57:37