Thread: Differences between float and double variables

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    3

    Differences between float and double variables

    Hello!

    Thantos told me the difference between:

    //first command
    float n = 2.59f;

    and

    //second command
    float n = 2.59;

    The first command create a float variable and the second create a double variable.

    What the difference between the first one and the second one?

    The precision and the lenght only?

    Thank you very much!

    Bye!!!

    Silas Justiniano - Brazil

  2. #2
    Registered User
    Join Date
    Aug 2004
    Posts
    17
    A double is bigger and more precise. It is 8 bytes while a float is 4 bytes.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    what's mean "f" after 2.59, how does it work?
    float n = 2.59f;

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    In programming you often have to deal with different number systems. For example, if you're going to specify something in hexadecimal you must precede the number with "0x" (zero x), octals require just the zero, and in this case, if you want to specify it as a float you put an f at the end.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    IEEE standard floating point numbers have about eight or nine decimal digits of precision. Double precision numbers have more.

    Now, in C, (therefore, by extension C++), all internal calculations are carried out in double precision. Assignment statements evaluate the right hand side, then convert to whatever is required by the left hand side.

    Therefore, the following declarations store the same value in f:

    Code:
      float f = 0.1;
      float f = 0.1f;
    Why would you want to use 0.1f rather than 0.1? In this case, at least, it's a matter of style, rather than substance. I'm sure that there are sometimes reasons that people want to do this, but it doesn't affect the results.

    Try the following with your favorite C++ compiler:

    Code:
    #include <iostream>
    
    int main()
    {
      float f = 0.1;
      double d = 0.1;
      float ff = 0.1f;
      double df = 0.1f;
    
      std::cout.precision(20);
      std::cout << "f  = " << f << std::endl;
      std::cout << "d  = " << d << std::endl;
      std::cout << "ff = " << ff << std::endl;
      std::cout << "df = " << df << std::endl;
    
      return 0;
    }
    Now, I have told the program to print out 20 digits after the decimal point. This is more than the precision of float or double variables (at least for IEEE standard numbers, which are used in the compilers that I have at hand).

    The number 0.1 cannot be represented exactly by a binary (or hex) floating point number. Double precision still cannot store the number exactly either.

    How many correct significant digits do you see?

    Is there any difference between 0.1 and 0.1f?

    Regards,

    Dave

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Is there any difference between 0.1 and 0.1f?
    Code:
            float   a = 0.1f;
            double  b = 0.1;
            if ( a == b ) printf("boo\n");
    Overly picky compilers and code checking tools will warn you about loss of precision if you do
    float a = 0.1;
    since 0.1 is a double floating point constant and therefore information is lost when its assigned to a float.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    a float will leave 23 bits for the fraction value, a double will leave 52
    http://research.microsoft.com/~holla...ieeefloat.html
    you can find other information about it if you google for ieee 754, which is the doc that defines how they want floating point values to be defined. Kinda scary how I know that document off hand.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Of course the ANSI standard doesn't specify that a computer has to use the IEEE standard

  9. #9
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    ANSI wants at least 6 and 10 for float and double respectively if my memory serves me well, can you verify that for me thantos?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  2. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. writing double quote & comma delimited files
    By emt in forum C++ Programming
    Replies: 5
    Last Post: 06-30-2003, 12:28 AM
  5. Configurations give different results
    By Hubas in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2003, 11:43 AM