printing doubles and robustness of setw()

U

utab

Dear all,

On a formatted printing operation that I am trying to accomplish for
doubles, I have a question. I would like to print a double, d, and its
square, d*d, but I would like to make the argument to be computed upon
the length of the numbers(d and d*d) so that it lines up neatly in the
columns. For this operation, I need the number of digits in a double,
however, due to floating point representation, the double values are
not represented exactly. So is there a way to find the number of
digits of a double number in a robust way for print out?

P.S. For integers I coded a similar version like the one I explained
above but for doubles the above problem made me think on that a bit.

Rgds,
 
K

Kai-Uwe Bux

utab said:
Dear all,

On a formatted printing operation that I am trying to accomplish for
doubles, I have a question. I would like to print a double, d, and its
square, d*d, but I would like to make the argument to be computed upon
the length of the numbers(d and d*d) so that it lines up neatly in the
columns. For this operation, I need the number of digits in a double,
however, due to floating point representation, the double values are
not represented exactly.

Nit: each double (apart from special values such as nan) represents a real
number and does so exactly. It just might happen that you are not
interested in that number but in a number nearby. But that is not the fault
of the double.
So is there a way to find the number of
digits of a double number in a robust way for print out?

I would print the doubles into strings and then search for the decimal
point.
P.S. For integers I coded a similar version like the one I explained
above but for doubles the above problem made me think on that a bit.


Best

Kai-Uwe Bux
 
U

utab

Nit: each double (apart from special values such as nan) represents a real
number and does so exactly. It just might happen that you are not
interested in that number but in a number nearby. But that is not the fault
of the double.

Thx for the reply. Ok, but it is not exact anyway in mathematical
sense ;)
exact in the *closest* sense.
I would print the doubles into strings and then search for the decimal
point.

I forgot to tell that I wanted to constraint myself to find a solution
without stringstreams(by printing to strings, you mean to use a
stringstream I suppose). Do not find this as a stupid idea, I wanted
to develop a solutions list for the "Accelerated C++ book" and this
question is related to Exercise 4.4, till then on no stringstreams are
mentioned, this is the reason of the constraint ;)

Best,
 
K

Kai-Uwe Bux

utab said:
Thx for the reply. Ok, but it is not exact anyway in mathematical
sense ;)
exact in the *closest* sense.

I think you misunderstood: floating points numbers are exact in the
mathematical sense. They can be _used_ as approximations to
non-representable real numbers. But whether you do that is up to you.

Since floating point numbers are exact, their decimal representations are
well-defined. You have to find some way to extract that representation.

I forgot to tell that I wanted to constraint myself to find a solution
without stringstreams(by printing to strings, you mean to use a
stringstream I suppose). Do not find this as a stupid idea, I wanted
to develop a solutions list for the "Accelerated C++ book" and this
question is related to Exercise 4.4, till then on no stringstreams are
mentioned, this is the reason of the constraint ;)

You can use the printf familiy instead of stringstreams.

Other than that, you would have to extract the decimal representation of the
double by hand. Making sure that all digits are correct requires some
carefull analysis. Also, it will be hard to beat the printf-family or
stringstreams performance-wise. For inspiration, you could have a look at
the implementation of printf in some free library implementation.


Best

Kai-Uwe Bux
 
J

James Kanze

I think you misunderstood: floating points numbers are exact
in the mathematical sense. They can be _used_ as
approximations to non-representable real numbers. But whether
you do that is up to you.
Since floating point numbers are exact, their decimal
representations are well-defined. You have to find some way to
extract that representation.

Not necessarily. He needs some way of determining how many
decimal digits where will be to the right of the decimal point
in that representation. (I'm supposing that he's outputting in
a fixed format. Otherwise, I don't think the question makes
much sense.)
You can use the printf familiy instead of stringstreams.
Other than that, you would have to extract the decimal
representation of the double by hand. Making sure that all
digits are correct requires some carefull analysis. Also, it
will be hard to beat the printf-family or stringstreams
performance-wise. For inspiration, you could have a look at
the implementation of printf in some free library
implementation.

One solution I've used in the past has been a lookup table.
Something like:

double decimals[] = { 1E0, 1E1, 1E2 ..., 1E306 } ;

then std::lower_bound. (Obviously, I wrote a small program in
AWK to generate the table; I didn't type in 300 some numbers, or
600 some in my case, since I also needed those with negative
exponents, by hand.)

Of course, this will return the number of decimal digits in the
actual number; if he's only outputting say only two digits after
the decimal, or whatever, rounding might add an extra digit in
some cases.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top