Thread: Unexpected output!!!!!!!!!

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    75

    Unexpected output!!!!!!!!!

    i know this is simple program but why output is B?
    Code:
    void main(){
    	float a;
    	a=6.7;
    	if(a==6.7)
    	printf("A");
    	else
    	printf("B");
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    Did you try to print the contents of your variable?

    Code:
    #include <stdio.h>
    
    int main(void)
    {
       float a = 6.7;
    
       printf("%10f\n", a);
    
       return 0;
    }
    Jim

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    75
    yes output is 6.7000000

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Try this:

    Code:
    printf("%.10f\n", a);

  5. #5
    Registered User
    Join Date
    Aug 2015
    Posts
    75
    output is 6.6999998093 but in my case it prints B tell me about that

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Very good. Now look at the original program and see if you can understand why you get the output you're getting.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    So does that answer your question? a is not equal to 6.7.

    Jim

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    a is a float, 6.7 is a double. When you assign the double to the float you lose some precision. Then, you compare the float with a double which again have different levels of precision and you get a weird result. Try this and see if you get your expected result:
    Code:
    float a;
    a=6.7f;
    if(a==6.7f)
        printf("A");
    else
        printf("B");
    Bottom line is that some base-10 floating point values cannot be accurately represented in binary floating-point form and moreover aren't represented the same in their float/double variants.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Aug 2015
    Posts
    75
    u mean value of a is not 6.7 even i declare it as 6.7 then does value of a is 6.6999998093 ? so a!=6.7 so it prints B ?

  10. #10
    Registered User
    Join Date
    Aug 2015
    Posts
    75
    Ohhh!! YES now i understand it... so i have to declare it as 6.7f to comare it with float ....thx for ur replies

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by san12345 View Post
    u mean value of a is not 6.7 even i declare it as 6.7 then does value of a is 6.6999998093 ? so a!=6.7 so it prints B ?
    Yes.

    Quote Originally Posted by san12345 View Post
    Ohhh!! YES now i understand it... so i have to declare it as 6.7f to comare it with float ....thx for ur replies
    Well, not exactly. Floating point values are often approximations, since real numbers are continuous on the number line and the computer cannot represent infinite values. Thus, there are often inaccuracies when working with floating point values. Hence, using direct comparison (i.e. == ) on floats is not a good idea. I assume this was the lesson you were supposed to learn from the original code example.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,539
    so i have to declare it as 6.7f to comare it with float
    But that won't always help. You should never compare floating point numbers with the operator== or operator!=. Floating point numbers are approximations and even using a long double will not always compare equal.

    I would give you a link to read to help explain some of these problems but I doubt you would take the time to actually read the link so I won't bother.

    Jim

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Quote Originally Posted by san12345 View Post
    Ohhh!! YES now i understand it... so i have to declare it as 6.7f to comare it with float ....thx for ur replies
    Well that all depends on the binary number itself. If 6.7 was actually expressed (closely) as 6.69... as a double, putting it in an even smaller memory space is not going to improve things.

    To understand floating point you really need to understand a couple of things:
    1. Floating point is scientific notation in binary base. Just as you can write a very large or small number, like say Avagadro's constant in this notation, (6.022 * 1023) floating point types use the same tactic to represent very large or small numbers in a finite, uniform set of bits.
    2. Often what determines the chance of ever getting a true equality comparison depends on the nature of that notation. Did the number actually terminate, or is it approximate? Just like in decimal, there are repeating rational numbers that don't terminate in binary, or you might have simply run out of bits.

    This ignores rounding errors too...

    But for all of the above, comparing floating point for equality requires cleverness. Most people are not prepared for the work when they first encounter it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unexpected output
    By alien1 in forum C Programming
    Replies: 2
    Last Post: 11-07-2014, 02:24 PM
  2. Unexpected output
    By juice in forum Windows Programming
    Replies: 6
    Last Post: 03-10-2012, 11:13 AM
  3. Unexpected output
    By juice in forum C Programming
    Replies: 24
    Last Post: 11-18-2011, 11:18 PM
  4. Unexpected output
    By GolDRoger in forum C Programming
    Replies: 9
    Last Post: 11-18-2011, 02:49 PM
  5. unexpected output
    By crash88 in forum C Programming
    Replies: 2
    Last Post: 05-16-2006, 09:03 PM