none
Floating Point Understanding Question

    Question

  • Hi I'm new and trying to learn C++ from a book. I am currently trying to understand floating point and am a little confused. If I have the code:

     

    float value = 1.2345249996;

    std::cout << value;

     

    does this mean that after execution you are suppose to see a printed value such as:

     

    1.23452 six significant digits only?

     

    so if this is true then if I had the code:

     

    double value = 1.2345249996;

    std::cout << value;

     

    then after execution you are suppose to see a printed value such as:

     

    1.234524999 ten significant digits only?

     

    or when people say that float is six significant digits, double is ten significant digits they actually mean that when adding two numbers such in the following code:

     

    double value1 = 1.2345244996, value2 = 1.2345266000;

    value1 += value2;

    std::cout << value1;

     

    then after execution the printed value will be the addition of 10 significant digits not 6 significant digits as in a float but the compiler only displays a total of 6 digits after exection so you should see:

     

    2.46906

     

    I'm a little confused by the exact meaning of the difference between float  and double can anyone pls help, I'm using Visual Studio 2005 Express Edition.

     

     

    Tuesday, June 12, 2007 3:26 AM

Answers

  • When you want to display a number on the screen you should think about its format. The default format is sometimes not good enough.

     

    The following code shows you how to use one type of formating

    Code Snippet

     

    #include <iomanip>

     

    std::cout << std::setprecision(12) << value << std::endl;

     

    This is what you will get=> 1.234524965286e+000

    Tuesday, June 12, 2007 9:14 AM

All replies

  • When you want to display a number on the screen you should think about its format. The default format is sometimes not good enough.

     

    The following code shows you how to use one type of formating

    Code Snippet

     

    #include <iomanip>

     

    std::cout << std::setprecision(12) << value << std::endl;

     

    This is what you will get=> 1.234524965286e+000

    Tuesday, June 12, 2007 9:14 AM
  • Hmm thanks for your help but I was actually wondering what is the difference between these two lines of code

    float value = 1.2345249996F; and double value = 1.2345249996; is there any difference? My book says they differ in precision but I cannot see any difference in precision when I compile these code lines.

    Tuesday, June 19, 2007 5:11 AM
  • Yes there is a difference.

    A float uses 4 bytes and its range is 3.4E +/- 38 (7 digits).

    A double uses 8 bytes and its range is 1.7E +/- 308 (15 digits).

     

    So if you try the following code you will see the difference

     

    Code Snippet

    double value = 1.2345249996;

    float value2 = 1.2345249996F;

    std::cout << std::setprecision(12) << value << std::endl;

    std::cout << std::setprecision(12) << value2 << std::endl;

     

    Output:

    1.2345249996
      1.23452496529

    Richard

    Tuesday, June 19, 2007 7:49 AM
  • Thanks for your help I can now see there is a difference and I now understand why when I first tried to find the difference, it was not visible. I was having difficulty before finding difference because I didnt use setprecision and the output of my test program only showed 6 digits. So having the type float or double takes an entered decimal number converts it to binary to be used in any binary operations within the program and then converts the answer back into decimal. Having the type float or double determines how precise your answers will be, and will cause differences because of the dropping or rounding of digits after any binary operations and before being converted back to decimal. At least this is what I think it is, am I right? Thanks for your help mate.
    Sunday, June 24, 2007 7:07 AM