Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualC0lumbo

Posted 06 February 2013 - 10:16 AM

If you do any calculations using floats, you should generally expect precision errors to creep in, and you should (almost) never compare results of float calculations with ==, but use an epsilon to compare if results are approximately equal.

 

I strongly recommend (to everyone) reading Bruce Dawson's articles about floating point numbers on AltDevBlogADay, they're excellent (http://www.altdevblogaday.com/author/bruce-dawson/)

 

Your point about floating point numbers looking identical in your printf is covered, they can even wrongly look identical in the Visual Studio debugger. According to Bruce Dawson in this article (http://randomascii.wordpress.com/2012/02/11/they-sure-look-equal/) you need 9 digits to reliably disambiguate floating point numbers, so using "%1.8e" instead of "%f" would show it up.


#1C0lumbo

Posted 06 February 2013 - 10:15 AM

If you do any calculations using floats, you should generally expect precision errors to creep in, and you should (almost) never compare results of float calculations with ==, but use an epsilon to compare if results are approximately equal.

 

I strongly recommend (to everyone) reading Bruce Dawsons articles about floating point numbers on AltDevBlogADay, they're excellent (http://www.altdevblogaday.com/author/bruce-dawson/)

 

Your point about floating point numbers looking identical in your printf is covered, they can even wrongly look identical in the Visual Studio debugger. According to Bruce Dawson in this article you need 9 digits to reliably disambiguate floating point numbers, so using "%1.8e" instead of "%f" would show it up.


PARTNERS