Velocity Reviews > C++ > setprecision problem

# setprecision problem

Wing
Guest
Posts: n/a

 10-11-2006
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.

Nate Barney
Guest
Posts: n/a

 10-12-2006
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-lit...html#faq-29.16

Nate

Spoon
Guest
Posts: n/a

 10-12-2006
Wing wrote:

> ///////////////////////////////////////
> 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?

http://en.wikipedia.org/wiki/Floatin...uracy_problems

http://docs.sun.com/source/806-3568/ncg_goldberg.html