Thread: flaot -double comparison

  1. #1
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Question flaot -double comparison

    I m getting equal as output...

    My doubt is float compared with double in if statement,
    then it should b unequal right??

    Is it something like type conversion?? I want just to clarify this point..




    Code:
    int main()
    {
        float a=7.0; //6.9999999
        a=a+0.0000001;// 7.0f
        if(a==7.0)
             printf("equal");
        else
             printf("unequal");
             
             getchar();
             
             }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Floating point values (with rare exceptions) can not be compared for equality with constants (or values derived from constants other than those making up the original number). In general, comparing floating point for "is this equal to that" should really be done as "delta" comparisons, e.g.
    Code:
    if (fabs(a-b) < 0.000001) 
       printf("equal");
    else
       printf("not equal");

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by matsp View Post
    should really be done as "delta" comparisons,
    I think the constant you compare to is called epsilon.

    QuantumPete
    Last edited by QuantumPete; 02-24-2009 at 03:37 PM.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    A float cannot distinguish 7.0000001 from 7 because it does not have enough significant bits to do so. The range of magnitudes of those numbers is too great.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM