Wierd precision for std::cout

Discussion in 'C++' started by none, May 17, 2010.

  1. none

    none Guest

    When I do:

    #include <vector>
    #include <iostream>

    ....

    std::vector<float> vec;
    vec.push_back(2.2);
    vec.push_back(2.3);
    std::cout << vec[0] << std::endl;
    std::cout << vec[1] << std::endl;

    I get:

    2.2000000476837158203125
    2.2999999523162841796875

    what goes on with the precision? I have not specified std::setprecision anywhere. I compile and run
    the code on Ubuntu 9.04.
     
    none, May 17, 2010
    #1
    1. Advertising

  2. On 5/17/2010 4:28 PM, none wrote:
    > When I do:
    >
    > #include <vector>
    > #include <iostream>
    >
    > ...
    >
    > std::vector<float> vec;
    > vec.push_back(2.2);
    > vec.push_back(2.3);
    > std::cout << vec[0] << std::endl;
    > std::cout << vec[1] << std::endl;
    >
    > I get:
    >
    > 2.2000000476837158203125
    > 2.2999999523162841796875
    >
    > what goes on with the precision? I have not specified std::setprecision
    > anywhere. I compile and run the code on Ubuntu 9.04.


    Could it be that your library implementation somehow has 22 as the
    default precision?... You can always interrogate what the current
    precision is by outputting 'cout.precision()'...

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 17, 2010
    #2
    1. Advertising

  3. Am 17.05.2010 22:28, schrieb none:

    > std::vector<float> vec;
    > vec.push_back(2.2);
    > vec.push_back(2.3);
    > std::cout << vec[0] << std::endl;
    > std::cout << vec[1] << std::endl;
    >
    > I get:
    >
    > 2.2000000476837158203125
    > 2.2999999523162841796875
    >
    > what goes on with the precision? I have not specified std::setprecision
    > anywhere. I compile and run the code on Ubuntu 9.04.


    http://www.parashift.com/c -faq-lite/newbie.html#faq-29.16

    Regards,
    Johannes

    --
    >> Wo hattest Du das Beben nochmal GENAU vorhergesagt?

    > Zumindest nicht öffentlich!

    Ah, der neueste und bis heute genialste Streich unsere großen
    Kosmologen: Die Geheim-Vorhersage.
    - Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$>
     
    Johannes Bauer, May 17, 2010
    #3
  4. On 5/17/2010 4:59 PM, Johannes Bauer wrote:
    > Am 17.05.2010 22:28, schrieb none:
    >
    >> std::vector<float> vec;
    >> vec.push_back(2.2);
    >> vec.push_back(2.3);
    >> std::cout<< vec[0]<< std::endl;
    >> std::cout<< vec[1]<< std::endl;
    >>
    >> I get:
    >>
    >> 2.2000000476837158203125
    >> 2.2999999523162841796875
    >>
    >> what goes on with the precision? I have not specified std::setprecision
    >> anywhere. I compile and run the code on Ubuntu 9.04.

    >
    > http://www.parashift.com/c -faq-lite/newbie.html#faq-29.16
    >


    The OP was talking about the number of digits in the output, which the
    Standard specifies as 6 *by default*. IOW, the output should be

    2.200000
    2.300000

    (because we expect rounding to the sixth digit after the decimal delimeter).

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 17, 2010
    #4
    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. Pmb

    std::cout vs cout

    Pmb, Jun 2, 2004, in forum: C++
    Replies:
    2
    Views:
    4,449
    Leor Zolman
    Jun 2, 2004
  2. Stefan Ram

    Re: cout vs std::cout

    Stefan Ram, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    513
  3. Hendrik Schober

    Re: cout vs std::cout

    Hendrik Schober, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    464
    Jerry Coffin
    Oct 7, 2008
  4. saurabh29789

    Sending std::cout to std::cout !!

    saurabh29789, Jun 11, 2009, in forum: C++
    Replies:
    1
    Views:
    313
    Harald Finster
    Jun 11, 2009
  5. saurabh29789

    Sending std::cout to std::cout

    saurabh29789, Jun 11, 2009, in forum: C++
    Replies:
    2
    Views:
    359
    Rolf Magnus
    Jun 12, 2009
Loading...

Share This Page