Wierd precision for std::cout

N

none

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.
 
V

Victor Bazarov

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
 
J

Johannes Bauer

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

--
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 <[email protected]>
 
V

Victor Bazarov

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top