Thread: need some advice

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    21

    need some advice

    Hey all

    Can anyone suggest me some material (Advanced level) which contains many Multiple choice Q's on outputs or concepts (in C) or even MCQ's on Data structures using C.

    Also

    O/p

    float a=0.7;
    if(a>0.7) print hell
    else print no

    Ans is compiler dependent But y is it So ?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    floats cannot be perfectly stored in a 32 bit value, there is always some amount of precision that is lost. An easy way to see this in action is to do:
    Code:
    printf("%.10f\n", a);
    On my box, 0.7 is printed as 0.6999999881
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    21
    r u having a 16 bit compiler?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    0.7 (decimal) cannot be represented exactly as a binary (base 2) fraction. Floating point formats are a binary representation.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by bithub View Post
    floats cannot be perfectly stored in a 32 bit value, there is always some amount of precision that is lost. An easy way to see this in action is to do:
    Code:
    printf("%.10f\n", a);
    On my box, 0.7 is printed as 0.6999999881
    That is why you shouldn't compare floating point numbers with the C relational operators. You should take rounding errors into account like this:
    Code:
    #include <math.h>
    
    inline int Greater(float a, float b)
    {
        return (b-a) <= -TOLERANCE;
    }
    Or something like that.
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Dec 2010
    Location
    Austria
    Posts
    10
    The right way to program it is
    Code:
    float a = 0.7f;
    if (a > 0.7f) {
       /* you will not reach this hell */
    } else {
       /* this is the place we go */
    }
    The reason ist that 0.7 is a double value and therefor rounded when assigned to a float. Adding the f after the literal marks it explicitly as float.
    Well of corse 0.7f is in reality somewhere between 0.6999.. and 0.70...01... but at least there are no rounding surprises in the example code, as both values are rounded the same way.

    When working with float and double you shall always be aware that there is some fraction of uncertainty due to rounding issues. Use integer calculations if you have to be sure which way the rounding goes.
    Last edited by notan; 12-08-2010 at 06:06 AM.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    21
    Thanx a lot guys
    And can anyone guide me to some good MCQ book or online resource.
    Q's related to 'Whats the output'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice on C Programming with MSVC++ 2008
    By IT_Guy in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2009, 04:23 AM
  2. Advice on multithreading
    By Calef13 in forum C++ Programming
    Replies: 3
    Last Post: 08-24-2007, 03:28 PM
  3. Resume advice
    By Zughiaq in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-15-2005, 02:16 PM
  4. girl friend advice (prob. the wrong place)
    By B0bDole in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2004, 06:38 PM
  5. looking for advice on selling golf balls
    By lambs4 in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 05-30-2004, 04:03 PM