Significant digits

Discussion in 'C++' started by pw, Jun 26, 2003.

  1. pw

    pw Guest

    Running this on MSWin2000 PC, I show 6 only digits for float and double.
    i.e. l_float and l_double show 33333.1 (6 only digits).

    Is it possible to increase/alter this?

    Code:
    // Test ints, floats and doubles
    #include <iostream>
    using namespace std;
    
    int main()
    {
    int l_int;
    float l_float;
    double l_double;
    
    l_int = 33333.1415922662266;
    l_float = l_int;
    l_double = l_int;
    
    cout << "Define int: " << l_int << endl;
    cout << "l_int: " << l_int << endl;
    cout << "l_float: " << l_float << endl;
    cout << "l_double: " << l_double << endl;
    cout << endl;
    
    l_float = 33333.1415922662266;
    l_int = l_float;
    l_double = l_float;
    
    cout << "Define float: " << l_float << endl;
    cout << "l_int: " << l_int << endl;
    cout << "l_float: " << l_float << endl;
    cout << "l_double: " << l_double << endl;
    cout << endl;
    
    l_double = 33333.1415922662266;
    l_int = l_double;
    l_float = l_double;
    
    cout << "Define double: " << l_double << endl;
    cout << "l_int: " << l_int << endl;
    cout << "l_float: " << l_float << endl;
    cout << "l_double: " << l_double << endl;
    cout << endl;
    
    l_double = 333333333.1415922662266;
    l_int = l_double;
    l_float = l_double;
    
    cout << "Define double: " << l_double << endl;
    cout << "l_int: " << l_int << endl;
    cout << "l_float: " << l_float << endl;
    cout << "l_double: " << l_double << endl;
    cout << endl;
    
    return 0;
    }
     
    pw, Jun 26, 2003
    #1
    1. Advertising

  2. "pw" <> wrote in message
    news:cSuKa.323$...
    > Running this on MSWin2000 PC, I show 6 only digits for float and double.
    > i.e. l_float and l_double show 33333.1 (6 only digits).
    >
    > Is it possible to increase/alter this?
    >

    Sure. std::setprecision () is what you are looking for.
     
    Josephine Schafer, Jun 26, 2003
    #2
    1. Advertising

  3. On Thu, 26 Jun 2003 04:43:52 GMT, "pw" <>
    wrote:

    >Running this on MSWin2000 PC, I show 6 only digits for float and double.
    >i.e. l_float and l_double show 33333.1 (6 only digits).
    >
    >Is it possible to increase/alter this?
    >
    >
    Code:
    >// Test ints, floats and doubles
    >#include <iostream>
    >using namespace std;
    >
    >int main()
    >{
    > int l_int;
    > float l_float;
    > double l_double;
    >
    > l_int = 33333.1415922662266;
    > l_float = l_int;
    > l_double = l_int;
    >
    > cout << "Define int: " << l_int << endl;
    > cout << "l_int: " << l_int << endl;
    > cout << "l_float: " << l_float << endl;
    > cout << "l_double: " << l_double << endl;
    > cout << endl;[/color]
    
    
    
    If you even get the .1, your compiler must be whacked out.... Consider
    what you are doing here.... Storing a decimal number into an int
    (I_int), results in I_int equal to 33333.  I_float then gets that
    value stored into it.  (33333).  I_double, then gets the 33333 stored
    into it.... Consider changing it to:
    
    I_double = 33333.1415922662266;
    I_float = I_double;
    I_int = I_float;
    
    
    etc, then printing it and you should then notice better accuracy....
    
    [color=blue]
    >
    > l_float = 33333.1415922662266;
    > l_int = l_float;
    > l_double = l_float;
    >
    > cout << "Define float: " << l_float << endl;
    > cout << "l_int: " << l_int << endl;
    > cout << "l_float: " << l_float << endl;
    > cout << "l_double: " << l_double << endl;
    > cout << endl;
    >
    > l_double = 33333.1415922662266;
    > l_int = l_double;
    > l_float = l_double;
    >
    > cout << "Define double: " << l_double << endl;
    > cout << "l_int: " << l_int << endl;
    > cout << "l_float: " << l_float << endl;
    > cout << "l_double: " << l_double << endl;
    > cout << endl;
    >
    > l_double = 333333333.1415922662266;
    > l_int = l_double;
    > l_float = l_double;
    >
    > cout << "Define double: " << l_double << endl;
    > cout << "l_int: " << l_int << endl;
    > cout << "l_float: " << l_float << endl;
    > cout << "l_double: " << l_double << endl;
    > cout << endl;
    >
    > return 0;
    >}
    >[/color]
     
    John H. Guillory, Jun 29, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Corby

    Round float to X significant digits

    David Corby, May 1, 2004, in forum: C++
    Replies:
    8
    Views:
    30,004
    Siemel Naran
    May 2, 2004
  2. mrstephengross

    How to detect a double's significant digits

    mrstephengross, May 5, 2005, in forum: Python
    Replies:
    29
    Views:
    832
    AnswerGuy
    May 23, 2005
  3. Jonny
    Replies:
    6
    Views:
    336
    Walter Bright
    Oct 10, 2005
  4. a01lida
    Replies:
    2
    Views:
    712
    a01lida
    Nov 16, 2008
  5. SMH
    Replies:
    0
    Views:
    234
Loading...

Share This Page