473,663 Members | 2,800 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1841
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(b y 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 "Accelerate d 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
9599
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 char*? string s1; char s2; cin >> setw(4) >> s1; cin >> setw(4) >> s2; (I've found there to be a difference between compilers -- on some,
14
7839
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 std::string. I had something working with std::string. I simply treated it as an STL container, and iterated over its elements. The results were a bit confusing to me. Some of the stuff was printing out as 1 or 2 characters hex numbers, as I...
2
3256
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
6795
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
8859
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 To do this (I mean to obtain constant field length for data of the same type, even if strings are of different lengths) I tried to use setw(X) from <iomanip.h>, so I wrote something like this:
1
1513
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 or natural number and I like it to print in nice human read format. cout << setw(20) << left << setprecision(5) << a;
10
2257
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 extract 1, 255 and 18 from 01ff12 (as the components of a simple RGB colour, in my application). I was hoping to find something like std::setw for istreams and use it
3
1876
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 = cout.width(); // get the default width cout << 1.1 << setw(2) << 1.2 << setw(s) << endl;
16
4858
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 way seems to somehow get how many characters my counter is, and then set setw for the first part to that length+1.
0
8436
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8771
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8634
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7371
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4349
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.