Jump to content
  • Advertisement
Sign in to follow this  
Slug

A bug with float in VC6.0???

This topic is 4902 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone, now listen, that is strange... I'm trying to do semothing like that: //Declaration float g_Alpha; //Init. g_Alpha = 0.3f //error //g_Alpha = 0.1f //no error now, suppose i want to inc., or dec that value by 0.1 when i press a key ... case 107: if(g_Alpha < 1.0f) g_Alpha += 0.1f; break; case 109: if(g_Alpha > 0.0f) g_Alpha -= 0.1f; break; ... so, if i init the float g_alpha with 0.1f, all will work ok, but if i init it with 0.3f or 0.5f, alpha will have a wrong value when sub. 0.1f of g_Alpha when he is equal to 0.1f. g_Alpha will have the value 1.49012e-008 instead of 0. Did I missed something???

Share this post


Link to post
Share on other sites
Advertisement
How can i correct that?

if i write

if(g_Alpha > 0.0f)
g_Alpha -= 0.1f;


then g_Alpha will be set to -0.1 when this situation occur...

Share this post


Link to post
Share on other sites
Quote:
Original post by Slug
How can i correct that?

if i write

if(g_Alpha > 0.0f)
g_Alpha -= 0.1f;


then g_Alpha will be set to -0.1 when this situation occur...

if(g_Alpha < 0.0f)
g_Alpha = 0.0f;

Share this post


Link to post
Share on other sites
read the link posted, floating point numbers have roundoff /trunctation errors, especially when trying to represent decimal numbers ...

you would be surprised to know that 0.1 CANNOT be respresented correctly in a floating point number. So you are really adding / subtracting a number slightly bigger or smaller than you think you are.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Slug
How can i correct that?

if i write

if(g_Alpha > 0.0f)
g_Alpha -= 0.1f;


then g_Alpha will be set to -0.1 when this situation occur...


you could use an epsilon value



static const float fEpsilon = 0.001f; // change this depending on precision
// desired...

if( g_Alpha < EPSILON)
g_Alpha = 0.0f;

// or use it to see if two floating point values are close enough to each
// other
if( fabs( g_Alpha - someFloat) < EPSILON) )
{
// handle the situation however you want....
}




hth

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!