473,320 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 1818
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matthew David Hills | last post by:
If setw() is being used to limit how many characters are pulled from a stream, should the # of characters taken from the stream depend on whether the stream is being sent to a std::string or a...
14
by: Steven T. Hatton | last post by:
I'm trying to write a program like hexel. I guess I could fish out the source for hexel and look at that, but for now I'm trying to figure out how I can do with with std::stringstream and...
2
by: tvn007 | last post by:
Below is my code: #include <string> #include <iostream> #include <iomanip> int main (void){ using namespace std; string name1 = "JOHN"; int id1 = 1234;
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
4
by: alternativa | last post by:
Hi, I'd like to obtain an output looking as follows: name number phone address Caroline 233 34234 White St. 12 Anna 929043 093284 Brown St. 325...
1
by: Gary Wessle | last post by:
Hi I am expecting to get "0" (that is zero) printed but getting "-2.7311e-14" almost zero. not nice for printing. double a = \*...code calculations ...*\ some times a becomes a real, integer...
10
by: edd | last post by:
Hello all, I'm trying to put together some code that reads 3 adjacent pairs of hex digits from an istream. Each hex digit pair represents a non-negative integer. For example I would like to...
3
by: Jess | last post by:
Hello, I'm trying to use setw and width functions to set the padding spaces between numbers. The aim is to create two white spaces between two double e.g. "1.1 1.2". I did: streamsize s =...
16
by: Simonius | last post by:
Hello, I'm teaching myself C++ with Accelerated C++ by Andrew Koenig. I'm having a small problem with an exercise at page 73. Now I was wondering how I should do that with setw, easiest...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.