Archived

This topic is now archived and is closed to further replies.

Eleventy

Variables with Undefined Values

Recommended Posts

Is there any way to check if a variable has an undefined value in C++ before using it. For some reason, I sometimes get the main player's position's x, y, or z values ending up being undefined after doing collision detection. I want to know if I can implement a check to correct this so the player doesn't end up being in infinite space . Thanks. EDIT: Fixed anything that was unclear.
My Project Homepage (Infinity) | E-Mail Me | Message Me on AIM [edited by - JoshT172 on May 30, 2003 12:09:58 AM]

Share this post


Link to post
Share on other sites
undefined variables are compile-time errors. Maybe you mean uninitalized?

If that is the case, default values should be provided in constructors and they should never really be magicly reset. You must be doing something else wrong.


-SniperBoB-

Share this post


Link to post
Share on other sites
Sorry, i was a bit unclear, i meant that during the game, sometimes the variable will become undefined (divided by zero) - I can tell this since I have a debug feature that lists all the variables and their values - one of the x, y, or z values of the ship will have -1.#IND as it's value, and the screen will go black (since DirectX can't render an object with an undefined location).


My Project Homepage (Infinity) | E-Mail Me | Message Me on AIM

[edited by - JoshT172 on May 29, 2003 3:33:16 PM]

Share this post


Link to post
Share on other sites
What you have is a bug. Instead of implementing code to check for it and deal with it at runtime, simply fix the bug - make it so your x,y,z values can never be undefined (divided by zero). Something is hammering your variables: get rid of them hammer, don''t put on extra armour.

Share this post


Link to post
Share on other sites
Yeh, i tried looking over the code - but my collision detection algorithm is so huge that it would take nearly a day! (It's polygon-level collision detection, if anyone wants to know). I'll see what I can do - I was just wondering if I could find an easy way out.


My Project Homepage (Infinity) | E-Mail Me | Message Me on AIM

[edited by - JoshT172 on May 29, 2003 3:54:29 PM]

Share this post


Link to post
Share on other sites
Put a *temporary* check in for the invalid values, and set a breakpoint in it. Then run your app in the debugger, and you''ll hit your breakpoint when your variable gets nuked. Then trace backwards and figure out why. It shouldn''t take long, and it will be worth it, trust me.

Share this post


Link to post
Share on other sites
You probably would have thought of this, but did you make sure you are assigning values to your variables before using them?

"Skepticism.... that great rot of the intellect." - V.H.
Bah, what does HE know?


Albekerky Software

Share this post


Link to post
Share on other sites
quote:
Original post by JoshT172
Is there any way to check if a variable has an undefined value in C++ before using it. For some reason, I sometimes get the main player''s position''s x, y, or z values ending up being undefined after doing collision detection. I want to know if I can implement a check to correct this so the player doesn''t end up being in infinite space . Thanks.



There are functions in the C++ library that return NaN or infinity -- std::numeric_limits::quiet_NaN() and std::numeric_limits::infinity(). You can compare your value to the appropriate one of those.

Share this post


Link to post
Share on other sites
I don't think there's any easy way around just fixing the bug. Go through the entire code searching for the '/' character. Each time you find a division, decide whether the denominator could be zero and make the appropriate changes. I don't want to sound like i'm telling you what to do, but every time you write a division into your program, it's a good idea to check if the denominator could be zero.

[edited by - kdogg on May 30, 2003 3:56:36 AM]

Share this post


Link to post
Share on other sites
By the way, a floating exception can be caused by things other than dividing by zero. If you overflow an int, for example, you might have a floating exception. Using a number smaller than the precision of the machine can result in a floating exception, as could using an uninitialized variable. Sometimes, windows can be a difficult OS to be working in when it comes to diagnosing a floating exception. Case and point, you don't even know for sure a FPE is the culprit in this case. I'd guess (mind you without even looking at your code) that your algorithm divides a very small number by a very big number somewhere and then tries to assign the result in some variable. While dividing by zero is certainly the most common cause of a FPE in general programming, mathematical routines can create nagging errors if you're not careful about your variable types. You might consider using a math library that will take care of some of that stuff for you. For example, i've used the Cephes C library in the past for high precision math routines. It provides some error checking and will catch some exceptions that might cause fatal runtime errors otherwise if you aren't careful.

[edited by - kdogg on May 30, 2003 4:34:58 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by kdogg
By the way, a floating exception can be caused by things other than dividing by zero. If you overflow an int, for example, you might have a floating exception. Using a number smaller than the precision of the machine can result in a floating exception, as could using an uninitialized variable. Sometimes, windows can be a difficult OS to be working in when it comes to diagnosing a floating exception. Case and point, you don''t even know for sure a FPE is the culprit in this case. I''d guess (mind you without even looking at your code) that your algorithm divides a very small number by a very big number somewhere and then tries to assign the result in some variable. While dividing by zero is certainly the most common cause of a FPE in general programming, mathematical routines can create nagging errors if you''re not careful about your variable types. You might consider using a math library that will take care of some of that stuff for you. For example, i''ve used the Cephes C library in the past for high precision math routines. It provides some error checking and will catch some exceptions that might cause fatal runtime errors otherwise if you aren''t careful.


Are those math libraries any slower than the standard C includes? That''s exactly what I need - more precision. The main reason I''m having trouble with collision detection is the lack of precision the current C libraries have. Btw, thanks for the link.


My Project Homepage (Infinity) | E-Mail Me | Message Me on AIM

Share this post


Link to post
Share on other sites
Also, a variable could get nuked if you try something weird like calculating a cosinus or sinus of a value which is out of range for that function. (As I accidently tried once until i noticed, that i needed tangens in that case ;-)

Just my 2-Cents


LightBrain website relaunched, BomberFUN only USD 9.99
visit http://www.lightbrain.de

Share this post


Link to post
Share on other sites
quote:
Original post by JoshT172
Thanks - but, I''ve never used the debugger...



This is the perfect time to learn. Stepping through code and watching the values of variables change is *easy* with the right IDE. Once you start, you''ll wonder how you ever developed code without it. Which compiler are you using?

Share this post


Link to post
Share on other sites
BriTeg: I'm using MSVC++ .NET Enterprise Architect (entire studio collection). However, I believe that I've found my problem. Might as well go ahead and tell me how to use the debugger, that way I can find out which acos(), asin(), etc funtion is causing the problem without having to test each one.

TroneX: Hah - I believe you have just found my problem! Never even considered the fact that acos(), asin() and atan() can return undefined values... Doh!! Thanks for your reply - that really helps!


P.S. Sorry for taking so much time to reply, my stupid IE brower locked up my computer for no apparent reason... All I did was simply open another window and suddenly it locks up (Typical M$ problem) - making me have to reboot my computer.


My Project Homepage (Infinity) | E-Mail Me | Message Me on AIM

[edited by - JoshT172 on May 30, 2003 12:37:24 PM]

Share this post


Link to post
Share on other sites
On a side note, if you''re going to download software off Kazaa or IRC, try not to be too obvious about it. JoshT172, I''m not trying to pick on you or make fun of you; I''ve seen this in many other threads. If you have a pirated copy of software, don''t bother saying exactly what it is. In this case, just say "I have Visual C++ .Net". Warez kiddies always offer the most expensive version of any software package, so it''s clear to anyone ''in the know'' that it''s not legal. Pirating is looked down upon in these forums, and you don''t want to have a reputation of stealing stuff. Just a bit of advice.

P.S. If you actually did buy the Enterprise Architect version of VS.Net, then you wasted a whole lot of money.

--Buzzy

Share this post


Link to post
Share on other sites