converted long to double lost significant digits...

Discussion in 'C++' started by Jonny, Oct 10, 2005.

  1. Jonny

    Jonny Guest

    If you run the following code , you will get 1.12346e07 as the output. What
    do I need to do if you want all the significant digits output, like
    1.123456789e07?

    #include<iostream>

    using namespace std;

    int main () {

    long a = 1123456789;
    float p = 100;
    double d = (double) (a / p) ;

    cout << "double = " << d << endl;

    }

    Thanks in advance..

    -Jonny
     
    Jonny, Oct 10, 2005
    #1
    1. Advertising

  2. On Mon, 10 Oct 2005 01:17:34 -0700, Jonny wrote:

    > If you run the following code , you will get 1.12346e07 as the output. What
    > do I need to do if you want all the significant digits output, like
    > 1.123456789e07?


    Modified lines below.

    > #include<iostream>

    #include <iomanip>
    >
    > using namespace std;
    >
    > int main () {
    >
    > long a = 1123456789;
    >// float p = 100;

    double p = 100;
    >// double d = (double) (a / p) ;

    double d = a / p;

    >// cout << "double = " << d << endl;

    cout << "double = " << setprecision (10) << d << endl;

    > }


    Useful info:

    <http://cplusplus.com/ref/#libs>
    <http://www.parashift.com/c++-faq-lite/>
     
    Owen Jacobson, Oct 10, 2005
    #2
    1. Advertising

  3. On Mon, 10 Oct 2005 01:17:34 -0700, "Jonny" <> wrote:

    >If you run the following code , you will get 1.12346e07 as the output. What
    >do I need to do if you want all the significant digits output, like
    >1.123456789e07?
    >
    >#include<iostream>
    >
    >using namespace std;
    >
    >int main () {
    >
    > long a = 1123456789;
    > float p = 100;
    > double d = (double) (a / p) ;
    >
    > cout << "double = " << d << endl;
    >
    >}
    >
    >Thanks in advance..
    >
    >-Jonny
    >


    Look for ios_base::precision, width, etc. in the STL documentation.

    --
    Bob Hairgrove
     
    Bob Hairgrove, Oct 10, 2005
    #3
  4. Jonny

    Kai-Uwe Bux Guest

    Jonny wrote:

    > If you run the following code , you will get 1.12346e07 as the output.
    > What do I need to do if you want all the significant digits output, like
    > 1.123456789e07?
    >
    > #include<iostream>


    add:

    #include <iomanip>

    >
    > using namespace std;
    >
    > int main () {
    >
    > long a = 1123456789;
    > float p = 100;
    > double d = (double) (a / p) ;
    >
    > cout << "double = " << d << endl;


    make that:

    cout << "double = " << setprecision(10) << d << endl;

    > }


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Oct 10, 2005
    #4
  5. On Mon, 10 Oct 2005 10:36:33 +0200, Bob Hairgrove wrote:

    > On Mon, 10 Oct 2005 01:17:34 -0700, "Jonny" <> wrote:
    >
    >>If you run the following code , you will get 1.12346e07 as the output. What
    >>do I need to do if you want all the significant digits output, like
    >>1.123456789e07?


    (Snip)

    >> long a = 1123456789;
    >> float p = 100;
    >> double d = (double) (a / p) ;


    (Snip)

    > Look for ios_base::precision, width, etc. in the STL documentation.


    The other problem is the intermediate converstion to float during the
    evaluation of (a / p), prior to conversion to double... I'm not
    comfortable enough with FP math to say for certain that that'll lose
    digits in this case, but it may well.

    -O
     
    Owen Jacobson, Oct 10, 2005
    #5
  6. On Mon, 10 Oct 2005 08:46:34 GMT, Owen Jacobson
    <> wrote:

    >On Mon, 10 Oct 2005 10:36:33 +0200, Bob Hairgrove wrote:
    >
    >> On Mon, 10 Oct 2005 01:17:34 -0700, "Jonny" <> wrote:
    >>
    >>>If you run the following code , you will get 1.12346e07 as the output. What
    >>>do I need to do if you want all the significant digits output, like
    >>>1.123456789e07?

    >
    >(Snip)
    >
    >>> long a = 1123456789;
    >>> float p = 100;
    >>> double d = (double) (a / p) ;

    >
    >(Snip)
    >
    >> Look for ios_base::precision, width, etc. in the STL documentation.

    >
    >The other problem is the intermediate converstion to float during the
    >evaluation of (a / p), prior to conversion to double... I'm not
    >comfortable enough with FP math to say for certain that that'll lose
    >digits in this case, but it may well.
    >
    >-O


    Good catch ... I overlooked the fact that p is float, not double.

    --
    Bob Hairgrove
     
    Bob Hairgrove, Oct 10, 2005
    #6
  7. "Owen Jacobson" <> wrote in
    message
    news:p...
    > >> long a = 1123456789;
    > >> float p = 100;
    > >> double d = (double) (a / p) ;

    > The other problem is the intermediate converstion to float during the
    > evaluation of (a / p), prior to conversion to double... I'm not
    > comfortable enough with FP math to say for certain that that'll lose
    > digits in this case, but it may well.


    A 32 bit float only has 24 bits for the mantissa, and a 32 bit long won't
    fit without losing a few. However, an implementation is allowed to evaluate
    (a/p) as a double internally, or even long double, so it may work anyway.

    -Walter Bright
    www.digitalmars.com C, C++, D programming language compilers
     
    Walter Bright, Oct 10, 2005
    #7
    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. pw

    Significant digits

    pw, Jun 26, 2003, in forum: C++
    Replies:
    2
    Views:
    3,052
    John H. Guillory
    Jun 29, 2003
  2. David Corby

    Round float to X significant digits

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

    How to detect a double's significant digits

    mrstephengross, May 5, 2005, in forum: Python
    Replies:
    29
    Views:
    831
    AnswerGuy
    May 23, 2005
  4. a01lida
    Replies:
    2
    Views:
    712
    a01lida
    Nov 16, 2008
  5. SMH
    Replies:
    0
    Views:
    232
Loading...

Share This Page