X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f From: Alan Didey Newsgroups: comp.lang.c++,comp.os.msdos.djgpp Subject: Re: setw & notation Date: Tue, 23 Apr 2002 18:45:31 +0100 Organization: Oxford University, England Lines: 141 Message-ID: <3CC59DBB.1000201@hotmail.com> References: <3CC55D84 DOT 5FA93D1 AT earthlink DOT net> NNTP-Posting-Host: dhc38.chch.ox.ac.uk Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.ox.ac.uk 1019583929 12853 163.1.237.38 (23 Apr 2002 17:45:29 GMT) X-Complaints-To: newsmaster AT ox DOT ac DOT uk NNTP-Posting-Date: Tue, 23 Apr 2002 17:45:29 +0000 (UTC) User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.2.1) Gecko/20010901 X-Accept-Language: en-us To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Alex Vinokur wrote: > "Martin Ambuhl" wrote in message news:3CC55D84 DOT 5FA93D1 AT earthlink DOT net... > | Alex Vinokur wrote: > | > | > 5e-05 || MY COMMENT : Why not 0.00005 ? > | > 6e-06 || MY COMMENT : Why not 0.000006 ? > | > | If you want fixed point, say so. Change > | cout << setw(show_size_i) << value_i << endl; > | to > | cout << fixed << setw(show_size_i) << value_i << endl; > | > [snip] > > I don't want fixed point. > I want the output to be as following : > 0.1 > 0.02 > 0.003 > 0.0004 > 0.00005 > 0.000006 You certainly do want fixed format. That's what the fixed manipulator (or the ios_base::fixed flag) gives you. This program prints numbers without their fractional part (though rounded not truncated), or if they are smaller than one, prints only the first non-zero decimal place. You have to calculate the precision required to do this yourself. Note the use of epsilon() - this is important. #include #include #include #include using namespace std; int main() { // remember the original format of the stream ios_base::fmtflags flags = cout.flags(); streamsize prec = cout.precision(); // we never want scientific notation cout.setf(ios_base::fixed, ios_base::floatfield); // my implementation won't ever print more than 18 decimal places for (double d=100000000000000.0; d>=1e-18; d/=10.0) { if(d < 1) cout.precision( // enough precision for one decimal place static_cast( -log10(d) + 1 - numeric_limits::epsilon() ) ); else // don't print fractional part of numbers greater than 1 cout.precision(0); cout << d << '\n'; } // force the output on to the screen cout.flush(); // restore the stream to its former glory cout.precision(prec); cout.setf(ios_base::fmtflags(0), ios_base::floatfield); } Its output: 100000000000000 10000000000000 1000000000000 100000000000 10000000000 1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1 0.1 0.01 0.001 0.0001 0.00001 0.000001 0.0000001 0.00000001 0.000000001 0.0000000001 0.00000000001 0.000000000001 0.0000000000001 0.00000000000001 0.000000000000001 0.0000000000000001 0.00000000000000001 0.000000000000000001 Change the starting number to 348903497856.0 and get: 348903497856 34890349786 3489034979 348903498 34890350 3489035 348903 34890 3489 349 35 3 0.3 0.03 0.003 0.0003 0.00003 0.000003 0.0000003 0.00000003 0.000000003 0.0000000003 0.00000000003 0.000000000003 0.0000000000003 0.00000000000003 0.000000000000003 0.0000000000000003 0.00000000000000003 0.000000000000000003 Hope this helps.