Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > setprecision problem

Reply
Thread Tools

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.

 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
behaviour of setprecision(0) jacek.dziedzic@gmail.com C++ 7 06-13-2007 08:18 PM
Unexpected setprecision behavior Anjo Gasa C++ 3 02-14-2007 03:29 PM
setprecision in output Gary Wessle C++ 1 08-08-2006 06:42 AM
std::setprecision and scientific format Woodster C++ 2 11-14-2003 09:03 AM
std::setprecision tarmat C++ 5 11-09-2003 08:24 AM



Advertisments