Why the control is not going into the loop?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mohanphani2003
    New Member
    • Apr 2009
    • 8

    #1

    Why the control is not going into the loop?

    Hi all,

    I executed the following program in compiler,the control didn't go into the loop What is the reason?Your help is high li appreciable.

    #include <stdio.h>

    void main()
    {
    float x=1.1;
    while(x == 1.1)
    {
    printf("%f\n",x );
    x=x-0.1;
    }
    }

    Pls help me.....
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Originally posted by mohanphani2003
    Hi all,

    I executed the following program in compiler,the control didn't go into the loop What is the reason?Your help is high li appreciable.
    Code:
    #include <stdio.h>
    
    void main()
    {
       float x=1.1;
       while(x == 1.1)
       {
          printf("%f\n",x);
          x=x-0.1;
       }
    }
    Pls help me.....
    The first rule is NEVER compare floating point numbers (float or double) for equality (==) or inequality (!=). Floating point representations are approximations; you cannot predict which cases will compare precisely equal and which won't.

    Even so, you might expect the equality comparison to work in your case. Notice that floating point literals are of type double unless you provide the "f" type suffix: for instance, "1.1f". Therefore, the comparison within the while statement is between float variable x and double constant 1.1; causing x to be promoted to a double. The promoted value of x has less precision that the constant, so the values compare not equal. If you are determined to proceed with this experiment, I suggest you either declare x as a double or add an "f" suffix to all of your floating pointer literal constants.

    There's a link to a floating point tutorial that is often provided in cases like this. I can't find the site. Hopefully one of the other experts will give it to us.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      This is the Insights bulletin board. You should't post questions here. Questions should go in the answers bulletin board.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by mohanphani2003
        Hi all,

        I executed the following program in compiler,the control didn't go into the loop What is the reason?Your help is high li appreciable.
        Check out the very first article in this section; it contains a link to a canonical article that explains all of the gory details of floating point operations in great detail.

        kind regards,

        Jos

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Originally posted by donbock
          There's a link to a floating point tutorial that is often provided in cases like this. I can't find the site. Hopefully one of the other experts will give it to us.
          Originally posted by JosAH
          Check out the very first article in this section; it contains a link to a canonical article that explains all of the gory details of floating point operations in great detail.
          I guess you can tell that I didn't really look that hard. I suggest you read What Every Computer Scientist Should Know About Floating-Point Arithmetic.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by donbock
            I guess you can tell that I didn't really look that hard. I suggest you read What Every Computer Scientist Should Know About Floating-Point Arithmetic.
            I must admit that I only read your remark about this thread not belonging in the 'insights' section, but yes I've read that FP article; I agree that it is a must read if one ever wants to do something sensible with floating point numbers.

            kind regards,

            Jos

            Comment

            Working...