Results 1 to 3 of 3

Thread: Program output

  1. #1
    Junior Member
    Join Date
    Dec 2007
    Answers
    9

    Program output

    What do you think would be the output of the following program and why? (If you are about to say "f is 1.0", I would say check it out again)

    #include

    int main()
    {
    float f=0.0f;
    int i;

    for(i=0;i<10;i++)
    f = f + 0.1f;

    if(f == 1.0f)
    printf("f is 1.0 n");
    else
    printf("f is NOT 1.0n");

    return 0;
    }


  2. #2
    Junior Member
    Join Date
    Feb 2008
    Answers
    3

    Re: Program output

    Ouput is :

    f is NOT 1.0n

    This is because floating point numbers doesn't have accuracy in certain cases (unlike 'double'). The interpretation inside the machine will be different than what we see.Thus, even though we see the value as 1.0f, due to different storage interpretation, it will fail to compare with actual 1.0f.
    The full article and details can be found in wikipedia related to floating point arithmatics.


  3. #3
    Junior Member
    Join Date
    Feb 2008
    Answers
    4

    Re: Program output

    yet the result is coming out to be 1.0f but coz of the interpretiion of floating point numbers value gets diferent frm the value tat it ought to be....................
    tat's y here result won't be 1.0f but somewat different frm tat.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact