X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f From: "Alex Vinokur" Newsgroups: comp.lang.c++,comp.os.msdos.djgpp Subject: Re: setw & notation Date: Wed, 24 Apr 2002 07:57:12 +0200 Organization: Scopus Lines: 100 Message-ID: References: <3CC55D84 DOT 5FA93D1 AT earthlink DOT net> <3CC59DBB DOT 1000201 AT hotmail DOT com> NNTP-Posting-Host: gateway.scopus.net (62.90.123.5) X-Trace: fu-berlin.de 1019624145 8417543 62.90.123.5 (16 [79865]) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com "Alan Didey" wrote in message news:3CC59DBB DOT 1000201 AT hotmail DOT com... [snip] | | 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: [snip] | | 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 | | [snip] Thanks. It is almost OK. The only thing I want to change is to see output as following : 0.1 0.01 0.001 0.0001 0.00001 ............ Thanks in advance, ==================== Alex Vinokur http://up.to/alexvn http://go.to/alexv_math mailto:alexvn AT bigfoot DOT com mailto:alexvn AT go DOT to ====================