CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2008
    Posts
    3

    Error while comparing Float variable

    Dear friends,
    Please take a look at the following code. Its storing .7 to b and comparing b with .7 . But getting just the opposite of expected result!!

    Please explain!

    (Three is expected o/p but we get two when run)

    Code:
    #include<stdio.h>
    int main(int argc,char **argv)
    {
     float a=0.5,b=0.7;
     if(b<0.7)
     {
    	 
    	if(a<0.5)
    	 {
       printf("\nONe");
    	}
         else
    	 {
       printf("\nTwo");
    	 }
     }
     else
     {
      printf("\nThree");
     }
     return 0;
    }
    Last edited by cijothomas; November 30th, 2008 at 10:05 AM. Reason: to <code>

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Error while comparing Float variable

    This is probably due to floating point inaccuracy, so you might check if (b < 0.7 + epsilon) instead, where epsilon is some small value.

    By the way, please post your properly indented code in [code][/code] bbcode tags.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Nov 2008
    Posts
    3

    Re: Error while comparing Float variable

    Quote Originally Posted by laserlight View Post
    This is probably due to floating point inaccuracy, so you might check if (b < 0.7 + epsilon) instead, where epsilon is some small value.

    By the way, please post your properly indented code in [code][/code] bbcode tags.
    Sorry about not useing <code> tags.

    One hint : If we use .7f in all places, this works fine! Anyone explain?

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Error while comparing Float variable

    Quote Originally Posted by cijothomas
    If we use .7f in all places, this works fine! Anyone explain?
    As far as I can tell, 0.7 cannot be precisely represented in binary, so floating point inaccuracy would still apply. My guess is that once you correctly switch to using float literals, the inaccuracy is the same in both cases, so your answer is consistent. Nonetheless, using an epsilon would still be good idea, methinks.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    Re: Error while comparing Float variable

    laserlight explain it...
    so you might check if (b < 0.7 + epsilon) instead

    using f you set the value 0.7 as float, in the same range of b

  6. #6
    Join Date
    Nov 2003
    Posts
    1,902

  7. #7
    Join Date
    Nov 2008
    Posts
    3

    Smile Re: Error while comparing Float variable

    Thanks for replying

    Now i understand the things better

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured