467,207 Members | 1,344 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
• viewed: 1468
Share:
2 Replies
 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 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.