float/double/long double range?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shweta khare
    New Member
    • Aug 2008
    • 13

    float/double/long double range?

    the following program does not give any output supposedly becoz a double const is being assigned to a float variable....i dont quiet understand by what is meant by this...the program goes like this....
    void main( )
    {
    float x=1.1;
    while(x==1.1)
    {
    printf("\n %f",x);
    x=x-.1;
    }
    getch( );
    }

    why isnt the program giving any output?
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    You cant compare float variable like this.
    You can check this way
    [code=cpp]
    if(x-1.11 < 0.01)
    {
    //matched
    }
    [/code]

    I think this is told by one of the experts here(Banfa/Josah)
    Serach in the forum for this

    Raghu

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Are you sure that the the reason that there is no output isn't in fact because you invoked undefined behaviour by declaring main as returning void?

      main returns int, always, without exception. To do otherwise invokes undefined behaviour.

      Undefined behaviour is exactly what it says, once invoked in anyway the behaviour of the program is completely undefined. The program may do anything including formatting your hard disk, although results are normally less drastic but often still a disaster for the program. You should always avoid invoking undefined behaviour.


      OK onto your observed behaviour, the statement
      the following program does not give any output supposedly becoz a double const is being assigned to a float variable
      is wrong. This is not the reason that the program does not give any output.

      You are getting warnings about assigning a double constant to a float variable because that is what you are doing on lines 3 and 7 of your program.

      C has 2 floating point types float and double these have different precisions, often 7 and 15 significant digits, and are held in a different number of bytes, often 4 and 8 bytes respectively.

      That means that when you assign a double (variable or constant) to a float variable the compiler has to truncate the value, that is convert it to the new format and you loose precision.
      1.1
      .1
      1e-3
      are all examples of double constants, if you want a float constant you have to
      tack an f on the end
      1.1f
      .1f
      1e-3f

      If you do this for all double constants in your program then it will apparently fix the behaviour and you will get output (probably).

      However there is another problem. You are using a float variable (x) as a control variable, that is you are using it in an if, while or for condition.

      This is very poor practice because as i mentioned earlier a float holds 7 significant digits (and a double 15). But they can hold a vast range of values from very large to very small. The reason they can do this is that they actually only hold approximations to the number in question.

      Because the value held is an approximation trying to test it against a specific value is almost bound to fail at sometime.

      You should absolutely never use the comparisons == and != with float and double variables and you should only use <, <=, > and >= with very great care and in the knowledge that it is possible to get cases that when worked out on paper produce one result but that the computer will come up with the inverse.

      Comment

      • shweta khare
        New Member
        • Aug 2008
        • 13

        #4
        Thank u for clarifying my doubts.....

        Comment

        • shweta khare
          New Member
          • Aug 2008
          • 13

          #5
          I had the misconception that all decimal point numbers are floating point numbers...but thereafter i came across double constants....
          by d ways i looked up the range of both ie
          float- 3.4e-38 to 3.4e38
          double- 1.7e-308 to 1.7e308
          altough these are in the exponential form....
          can u please cite some examples of floating point numbers in the fractional form and bring abt the pts of diff btw floats and double in the fractional form........... ..what will be the range of both in the fractional form?why is 1.1,3.14 double constants and not floats?
          and yes if i use 1.1f then i do get an output....

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            1.1 and 3.14 are double constants and not floatconstants because that is what the standard says.

            Floating point literals are of type double unless you specifically give them type float by appending an f e.g. 1.1f, 3.14f

            Like I said float holds 7 significant digits and double 15 so

            double dv = 1.0000000001;

            would work as expected but

            float fv = 1.0000000001f;

            would not, you would most likely get a compiler warning or error but if you didn't then fv would end up with a value of 1.

            Comment

            • shweta khare
              New Member
              • Aug 2008
              • 13

              #7
              are all decimal point numbers by default stored as double constants unless u append f wid dem?...can u cite some examples of floats.....
              am sorry am jus a beginner in c programming language,would u please suggest a site where i can read more into this....ie float-double interconversion s?

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Also read "What every computer scientist should know about floating-point arithmetic".

                kind regards,

                Jos

                Comment

                • newb16
                  Contributor
                  • Jul 2008
                  • 687

                  #9
                  Originally posted by shweta khare
                  are all decimal point numbers by default stored as double constants unless u append f wid dem?...can u cite some examples of floats.....
                  You can't append u to float number, compiler will not allow unsigned float. I'd suggest reading ieee 754.

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    As I already said floating point literals are of type double unless you append an f in which case they are of type float.

                    Comment

                    Working...