472,121 Members | 1,475 Online

# printing doubles and robustness of setw()

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,
Apr 1 '08 #1
2 1720 utab wrote:
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
Apr 1 '08 #2
On Apr 2, 12:14 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:
utab wrote:
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.
Thx for the reply. Ok, but it is not exact anyway in mathematical
sense ;)
exact in the *closest* sense.
>
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.
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,
>
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
Apr 1 '08 #3

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

 1 post views Thread by Matthew David Hills | last post: by 14 posts views Thread by Steven T. Hatton | last post: by 2 posts views Thread by tvn007 | last post: by 68 posts views Thread by Martin Joergensen | last post: by 4 posts views Thread by alternativa | last post: by 1 post views Thread by Gary Wessle | last post: by 10 posts views Thread by edd | last post: by 3 posts views Thread by Jess | last post: by 16 posts views Thread by Simonius | last post: by 3 posts views Thread by isladogs | last post: by reply views Thread by beacampos | last post: by 1 post views Thread by beacampos | last post: by reply views Thread by BenitoJuares | last post: by reply views Thread by bakertaylor28 | last post: by reply views Thread by leo001 | last post: by reply views Thread by antdb | last post: by 7 posts views Thread by bounthong | last post: by 5 posts views Thread by Bright1Light | last post: by