473,396 Members | 1,590 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,396 software developers and data experts.

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 "setprecision (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 2141
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 "setprecision (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 CCustomOutputStream
{
public:
CCustomOutputStream (std::ostream& p_refUnderlyingOStream, int
p_iDefaultPrecision)
: m_pInternalOStream (&p_refUnderlyingOStream),
m_iDefaultPrecision (p_iDefaultPrecision),
m_NextPrecision (-1)
{
if (p_iDefaultPrecision < 0 || p_iDefaultPrecision 20)
throw exception ();
}

CCustomOutputStream& operator<< (std::ostream& (__cdecl
*_F)(std::ostream&))
{
// Invoke the passed I/O function for the internal stream.
_F (*m_pInternalOStream);
return *this;
}
CCustomOutputStream& operator<< (std::ios& (__cdecl *_F)(std::ios&))
{
// Invoke the passed I/O function for the internal stream.
_F (*m_pInternalOStream);
return *this;
}

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

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

/*! \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_iDefaultPrecision;

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

/*! \brief Template specialization for the setprecision manipulator.
*/
template<>
CCustomOutputStream& operator<< (std::_Smanip<std::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::setprecision (0)._Pf)
{
m_NextPrecision = p_Manipulator._Manarg;
}
return *this;
}

/*! \brief Template specialization for floating point values.
*/
template<>
CCustomOutputStream& 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_NextPrecision 0)
{
m_pInternalOStream->precision (m_NextPrecision);
m_NextPrecision = -1;
}
else
m_pInternalOStream->precision (m_iDefaultPrecision);

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

template<>
CCustomOutputStream& 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_NextPrecision 0)
{
m_pInternalOStream->precision (m_NextPrecision);
m_NextPrecision = -1;
}
else
m_pInternalOStream->precision (m_iDefaultPrecision);

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

CCustomOutputStream Mycout (std::cout, 7);

int main ()
{
Mycout << "Pi with default precision: "
<< 3.1415926535897 << std::endl
<< "Pi with precision 2: "
<< std::setprecision (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 CCustomOutputStream, so that you don't have to retrieve the
internal stream every time. CCustomOutputStream 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
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...
53
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...
0
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. ...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.