ftp.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/04/24/07:30:14

X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f
From: Alan Didey <a_didey AT hotmail DOT com>
Newsgroups: comp.lang.c++,comp.os.msdos.djgpp
Subject: Re: setw & notation
Date: 24 Apr 2002 12:19:46 +0100
Organization: Oxford University, England
Lines: 113
Sender: alex AT dhc38 DOT chch DOT ox DOT ac DOT uk
Message-ID: <m31yd5z6lp.fsf@dhc38.chch.ox.ac.uk>
References: <aa37pj$79p7q$1 AT ID-79865 DOT news DOT dfncis DOT de> <3CC55D84 DOT 5FA93D1 AT earthlink DOT net> <aa3nkb$7luo2$1 AT ID-79865 DOT news DOT dfncis DOT de> <3CC59DBB DOT 1000201 AT hotmail DOT com> <aa5dsg$80s87$1 AT ID-79865 DOT news DOT dfncis DOT de>
NNTP-Posting-Host: dhc38.chch.ox.ac.uk
Mime-Version: 1.0
X-Trace: news.ox.ac.uk 1019647185 7466 163.1.237.38 (24 Apr 2002 11:19:45 GMT)
X-Complaints-To: newsmaster AT ox DOT ac DOT uk
NNTP-Posting-Date: Wed, 24 Apr 2002 11:19:45 +0000 (UTC)
User-Agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

"Alex Vinokur" <alexvn AT bigfoot DOT com> writes:

> "Alan Didey" <a_didey AT hotmail DOT com> 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 <iostream>
> | #include <limits>
> | #include <iomanip>
> | #include <cmath>
> | 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<streamsize>(
> |     -log10(d) + 1 - numeric_limits<double>::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
> ............

Did you really mean:

        0.1
      0.01
    0.001
  0.0001
0.00001

or did you mean:

    0.1
   0.01
  0.001
 0.0001
0.00001

(monospaced typeface needed to tell the difference)

If you meant the latter, then just put a setw(7) manipulator on the
stream before outputting the number.

If you really meant the former, then you can calculate how many spaces
you want from the figure you pass to the precision, and output a
temporary std::string(the_right_number_of_spaces, ' ').

I had kind of hoped you might figure this last bit out for yourself
from the information in some of the replies from other people.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright 2019   by DJ Delorie     Updated Jul 2019