unexpected output value of floating type value

Discussion in 'C' started by shyam_oec, Feb 18, 2008.

  1. shyam_oec

    shyam_oec New Member

    Joined:
    Nov 26, 2007
    Messages:
    89
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Developer, .NET Framework
    Location:
    Jamshedpur
    #include<stdio.h>
    main()
    {
    float i=65.8;
    printf("i= %f",i);
    }

    with reference to the above code,
    output of this prgogram is i=65.800003

    as i expected to be i=65.800000
    can u explain why?
     
  2. technosavvy

    technosavvy New Member

    Joined:
    Jan 2, 2008
    Messages:
    52
    Likes Received:
    0
    Trophy Points:
    0
    it is not an unexpected output...
    remember everything is stored at the end of the day as binary number only...

    now try representing it 65.8 in binary...u can only get close to 65.8 and never equal to 65.8...

    make it
    Code:
    double i = 65.8 
    and ur problem will be resolved as the resolution of the number increases and hence u see the truncated output which is 65.8 !!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice