Thread: doubt on comparision operator

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    3

    doubt on comparision operator

    Hi
    the followiing code is giving 1 as answer but answer should be 0 ...becoz 0.7 is not grater than 0.7

    Code:
    int main()
    {
    float a=0.7;
    printf("%d",(0.7>a));
    }
    please tell me the reason

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    It's due to the way the numbers are stored. 0.7 cannot be accurately stored, only approximated, with a float. However, when you compare it to the other 0.7, you're likely comparing a float (a) to a double (0.7). The double has more accuracy, and can store a value that is more accurate (and apperanly larger) than a.
    If you change "float" to "double", your comparison will probably fail.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    3
    thanks for your information

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Quote Originally Posted by Cactus_Hugger View Post
    It's due to the way the numbers are stored. 0.7 cannot be accurately stored, only approximated, with a float. However, when you compare it to the other 0.7, you're likely comparing a float (a) to a double (0.7). The double has more accuracy, and can store a value that is more accurate (and apperanly larger) than a.
    If you change "float" to "double", your comparison will probably fail.
    bad advice though to give to somebody who doesnt understand this yet. He now may think that using doubles prevents those bugs

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Cactus_Hugger View Post
    If you change "float" to "double", your comparison will probably fail.
    How about "most assuredly" instead of "probably?" Finite precision or not, the literal "0.7" will translate to the same double precision value in either context. Doubles are not infinitely precise; that doesn't mean they have some kind of quantum uncertainty

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubt relating to delete operator :(
    By chottachatri in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2008, 06:47 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM