473,804 Members | 3,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

scientific computation (more)

Thanks for those who answered my question previously.

Everytime I want to output high precision numbers, I use the following
code:

cout << setprecision (9) << f << endl;

where f is some double number.

However, I don't want to add "setprecisi on (9)" in every "cout" and
"iofstream" in order to output/input high precision number, because I
need to modify my codes substantially in this case.

What can I do such that my program can output high precision numbers
without modifying the codes substantially?

Thanks.

Sep 26 '06 #1
1 2160
Wing wrote:
Thanks for those who answered my question previously.

Everytime I want to output high precision numbers, I use the following
code:

cout << setprecision (9) << f << endl;

where f is some double number.

However, I don't want to add "setprecisi on (9)" in every "cout" and
"iofstream" in order to output/input high precision number, because I
need to modify my codes substantially in this case.

What can I do such that my program can output high precision numbers
without modifying the codes substantially?

Thanks.
You could define your own version of cout in the following way:

#include <iostream>
#include <ostream>
#include <iomanip>

class CCustomOutputSt ream
{
public:
CCustomOutputSt ream (std::ostream& p_refUnderlying OStream, int
p_iDefaultPreci sion)
: m_pInternalOStr eam (&p_refUnderlyi ngOStream),
m_iDefaultPreci sion (p_iDefaultPrec ision),
m_NextPrecision (-1)
{
if (p_iDefaultPrec ision < 0 || p_iDefaultPreci sion 20)
throw exception ();
}

CCustomOutputSt ream& operator<< (std::ostream& (__cdecl
*_F)(std::ostre am&))
{
// Invoke the passed I/O function for the internal stream.
_F (*m_pInternalOS tream);
return *this;
}
CCustomOutputSt ream& operator<< (std::ios& (__cdecl *_F)(std::ios&) )
{
// Invoke the passed I/O function for the internal stream.
_F (*m_pInternalOS tream);
return *this;
}

/*! \brief This method can be used to access the internal stream.
*/
std::ostream* GetInternalStre am ()
{
return m_pInternalOStr eam;
}

protected:
/*! \brief The internal output stream.
*/
std::ostream* m_pInternalOStr eam;

/*! \brief If the user has provided a precision, we will use this
* value for the next floating point value.
* If this value is -1, we should use the default precision for the
* next floating point value.
*/
int m_NextPrecision ;

/*! \brief The default precision that should be used for floating
* point values.
*/
int m_iDefaultPreci sion;

public:
/*! \brief This is how outputting data is done in general: Just
* forward the call to the internal stream.
*/
template <class T>
CCustomOutputSt ream& operator<< (T p_RHS)
{
// Forward the call to the internal output stream.
*m_pInternalOSt ream << p_RHS;
return *this;
}

/*! \brief Template specialization for the setprecision manipulator.
*/
template<>
CCustomOutputSt ream& operator<< (std::_Smanip<s td::streamsize>
p_Manipulator)
{
// If the manipulator function is sbfun (the function that sets
// the precision of a given stream),
// we remember the argument, so that the next print of a floating
// point variable is done with this
// precision (Had we not remembered this, we would print the next
// floating point value with our internal
// default precision.
if (p_Manipulator. _Pf == std::setprecisi on (0)._Pf)
{
m_NextPrecision = p_Manipulator._ Manarg;
}
return *this;
}

/*! \brief Template specialization for floating point values.
*/
template<>
CCustomOutputSt ream& operator<< (float p_RHS)
{
// If the precision was explicitely set by a previous call
// to setprecision, we use this precision. Else
// we use the standard precision of this custom stream.
if (m_NextPrecisio n 0)
{
m_pInternalOStr eam->precision (m_NextPrecisio n);
m_NextPrecision = -1;
}
else
m_pInternalOStr eam->precision (m_iDefaultPrec ision);

// Print the float value with the set precision.
*m_pInternalOSt ream << p_RHS;
return *this;
}

template<>
CCustomOutputSt ream& operator<< (double p_RHS)
{
// If the precision was explicitely set by a previous call to
// setprecision, we use this precision. Else
// we use the standard precision of this custom stream.
if (m_NextPrecisio n 0)
{
m_pInternalOStr eam->precision (m_NextPrecisio n);
m_NextPrecision = -1;
}
else
m_pInternalOStr eam->precision (m_iDefaultPrec ision);

// Print the float value with the set precision.
*m_pInternalOSt ream << p_RHS;
return *this;
}
};

CCustomOutputSt ream Mycout (std::cout, 7);

int main ()
{
Mycout << "Pi with default precision: "
<< 3.1415926535897 << std::endl
<< "Pi with precision 2: "
<< std::setprecisi on (2) << 3.1415926535897 << std::endl
<< "Pi with default precision: "
<< 3.1415926535897 << std::endl;

return 0;
}

All you have to do is replacing of cout by Mycout (which can be done
with replace-in-files tools). If you use output streams to files, you
have to create one custom output stream for each such stream. If you use
the internals of a stream (like failbit), you may need to add these
features to CCustomOutputSt ream, so that you don't have to retrieve the
internal stream every time. CCustomOutputSt ream is also not a template
that can work with UNICODE strings (I didn't want to spend to much time
on this example).

Regards,
Stuart
Sep 26 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
3007
by: Michael Gorbach | last post by:
I was asked this summer to write a monte carlo code to simulation magnetic nanoparticles. For the nonphysicists, basicly it is a simulation where most of the time is taken up by looping through each pair in an array of 500 or so particles, in order to calculate interaction potential. I wrote what i have so far in csharp because i wanted to learn it and though it would give me some good experience. I am now beginning to understand that .net...
53
4395
by: Michael Tobis | last post by:
Someone asked me to write a brief essay regarding the value-add proposition for Python in the Fortran community. Slightly modified to remove a few climatology-related specifics, here it is. I would welcome comments and corrections, and would be happy to contribute some version of this to the Python website if it is of interest. ===
0
1669
by: Juan R. | last post by:
I have updated some basic requirements for a generic mathematical markup language for scientific requirements at the next link. http://canonicalscience.blogspot.com/2006/04/scientific-language-canonml-is.html] Some requirements fit into the XML model and could be considered for debate for the future mathML specifications. Other requirements do not fit and will be developed in alternative mathematical approaches to those from the w3c...
3
2648
by: Wing | last post by:
Hello eveyone, I am programming some scientific computation based on C++. I would like to set every output/input (e.g. cout, io file stream) to be a high precision. What C++ command should I use? Thanks so much.
0
9706
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
9579
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
10332
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
10077
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...
1
7620
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
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4300
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
3820
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.