Jump to content
  • Advertisement

Archived

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

GPX

A BUG ! with floats in visual c++ 6, or i'm just dumb :|

This topic is 5361 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, please take a minute and compile this code : #include <iostream.h> int main() { float x=0.09f; x-=0.01f; if (x==0.08f) { cout << "good !"; } else { cout << "bad !"; } float y=0.1f; y-=0.01f; if (y==0.09f) { cout << "good !"; } else { cout << "bad !"; } return 0; } I get "bad ! good !" Which is really weird ... 0.09-0.01 != 0.08, wtf ?? I decided to investigate a little more, and these are my results : 0.1 , 0.09 good 0.09 , 0.08 bad 0.08 , 0.07 bad 0.07 , 0.06 bad 0.06 , 0.05 bad 0.05 , 0.04 good 0.04 , 0.03 good 0.03 , 0.02 good 0.02 , 0.01 good 0.01 , 0.0 good I have no idea why is it acting like that, and that''s a really weird problem that i need to solve ....... wtf..........

Share this post


Link to post
Share on other sites
Advertisement
You can''t compare floats for equality like that. Floating point numbers aren''t exact. You need to compare a float within a certain range, e.g. if(abs(f-e) < EPSILON) (where EPSILON is a very small number like 0.00000001). Search or google for "floating point errors" and you''ll get thousands of results.

Share this post


Link to post
Share on other sites
I can''t understand the reason behind it ..
Your solution sounds good, but the value inside EPSILON is really important, what should it be ?

Share this post


Link to post
Share on other sites
Search for the book What Every Computer Scientist Should Know About Floating-Point Arithmetic, which is available for free in PDF form.

Share this post


Link to post
Share on other sites
I found an answer here :
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q42/9/80.ASP&NoWebContent=1

I still need a good "EPSILON" number, i''m working with numbers like i showed in the example (2 places after the point max)

Share this post


Link to post
Share on other sites
quote:
Original post by Alpha_ProgDes
can''t you just overload the operator so it''ll do that automagically?


automagically..you crack me up

Share this post


Link to post
Share on other sites
quote:
Original post by NatasDM
quote:
Original post by Alpha_ProgDes
can''t you just overload the operator so it''ll do that automagically?


automagically..you crack me up


so i take it that means no....

Share this post


Link to post
Share on other sites
I'd strongly recommend that you redesign the code so that you perform < or > otherwise you will fall into trouble. The EPSILON technique certainly works, but could be slow.

[edited by - superdeveloper on March 5, 2004 12:26:40 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Alpha_ProgDes
quote:
Original post by NatasDM
quote:
Original post by Alpha_ProgDes
can''t you just overload the operator so it''ll do that automagically?


automagically..you crack me up


so i take it that means no....




No man, I just like that word...automagically

Share this post


Link to post
Share on other sites

  • 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!