setprecision problem

Discussion in 'C++' started by Wing, Oct 12, 2006.

  1. Wing

    Wing Guest

    Hello everyone,

    I have the following code:

    ///////////////////////////////////////
    double num;
    num=1234567890;
    num+=(1.0/3.0);

    cout.setf(ios::fixed);
    cout<<setprecision(10);


    cout<<num<<endl;
    ///////////////////////////////////////

    The output is 1234567890.3333332539. I am wondering why the floating
    numbers are not all "3". Why "2539" appears in the output?

    Can anyone help me to solve this problem? Thank for so much.
    Wing, Oct 12, 2006
    #1
    1. Advertising

  2. Wing

    Nate Barney Guest

    Wing wrote:
    > Hello everyone,
    >
    > I have the following code:
    >
    > ///////////////////////////////////////
    > double num;
    > num=1234567890;
    > num+=(1.0/3.0);
    >
    > cout.setf(ios::fixed);
    > cout<<setprecision(10);
    >
    >
    > cout<<num<<endl;
    > ///////////////////////////////////////
    >
    > The output is 1234567890.3333332539. I am wondering why the floating
    > numbers are not all "3". Why "2539" appears in the output?
    >
    > Can anyone help me to solve this problem? Thank for so much.
    >


    This is a FAQ. See
    http://www.parashift.com/c -faq-lite/newbie.html#faq-29.16

    Nate
    Nate Barney, Oct 12, 2006
    #2
    1. Advertising

  3. Wing

    Spoon Guest

    Spoon, Oct 12, 2006
    #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. tarmat

    std::setprecision

    tarmat, Nov 5, 2003, in forum: C++
    Replies:
    5
    Views:
    12,204
    Jerry Coffin
    Nov 9, 2003
  2. Woodster
    Replies:
    2
    Views:
    2,782
    Senthilvel Samatharman
    Nov 14, 2003
  3. Gary Wessle

    setprecision in output

    Gary Wessle, Aug 8, 2006, in forum: C++
    Replies:
    1
    Views:
    383
    Gary Wessle
    Aug 8, 2006
  4. Anjo Gasa

    Unexpected setprecision behavior

    Anjo Gasa, Feb 1, 2007, in forum: C++
    Replies:
    3
    Views:
    380
  5. Replies:
    7
    Views:
    505
    Jacek Dziedzic
    Jun 13, 2007
Loading...

Share This Page