setprecision problem

W

Wing

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

Nate Barney

Wing said:
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
 

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,698
Latest member
LydiaHalle

Latest Threads

Top