Sign in to follow this  

Problem dealing with float numbers

Recommended Posts

EvilNando    96
Okay so Im making a physics engine for circles

and Im doing the following code to check whenever an object is resting


if ( (Velocity.x - 0.000001f <= 0.01f && Velocity.x - 0.000001f >= -0.002f)
&& (Velocity.y - 0.000001f <= 0.01f && Velocity.y - 0.000001f >= -0.002f) )
Velocity = Vector2(0.0f);

the thing is not all of my objects go through that if even when in the simulation they clearly seem to be resting..

a quick debug shows this...

x = -1.3607700e-008
y = 0.33765292

at first I thought I was dealing with some kind NaN value but a quick _isnan(Velocity.x) showed me that this is not the case

so Im out of ideas of what that value mean and what to do

Share this post

Link to post
Share on other sites
EvilNando    96
Oh yeah youre right I guess I was too focused on the [color=#1C2837][size=2]-1.3607700e-008 issue[/size][/color]
[color=#1C2837][size=2]any ideas what does this mean?[/size][/color]

Share this post

Link to post
Share on other sites
ApochPiQ    23003
Yes. It's in my first reply to this thread.

If you really want "true 0" then you can do this:

[code]float clamp(float invalue)
static const float epsilon_ = 0.0001f; // Ideally share this globally and use it instead of magic numbers

if(abs(invalue) < epsilon_)
return 0.0f;

return invalue;

But that is NOT smart. You should never do equality checks on floating point numbers, so using an epsilon comparison is the right thing to do anyways... and I already posted the code for that above.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this