# Boost :

From: Paul A Bristow (pbristow_at_[hidden])
Date: 2004-07-27 05:50:27

Good.

The code below works for me - and I will advise any problems I encounter.

is about the number of decimal places.

The formula below generates the values W Kahan, of IEEE754 fame, gives (for
example 17 for 64-bit double).

To amplify my reply to Vicotr Wagner, the following example showing the
effect of a few least significant bit changes and that you need precision 17
to show this reliably.

double d = 1.;
for (int i = 1; i < 10; i++)
{
d = _nextafter(d, +999.); // increment least siginificant bit.
cout << i << ' ' << setw(20) << setprecision(15) << d << ' '<< setw(20) <<
setprecision(16) << setw(20)<< d << ' ' << setprecision(17) << d << endl;
}

Outputs
bits changed precision 15 precision 16 precision 17

1 1 1 1.0000000000000002
2 1 1 1.0000000000000004
3 1 1.000000000000001 1.0000000000000007
4 1 1.000000000000001 1.0000000000000009
5 1 1.000000000000001 1.0000000000000011
6 1 1.000000000000001 1.0000000000000013
7 1 1.000000000000002 1.0000000000000016
8 1 1.000000000000002 1.0000000000000018
9 1 1.000000000000002 1.000000000000002

However, I note that this involves lots of calls to ostr.precision() and
perhaps it can be don't more efficiently by a single call when ostream just
after constructed. But it works and the cost is small.

Paul

| -----Original Message-----
| From: boost-bounces_at_[hidden]
| [mailto:boost-bounces_at_[hidden]] On Behalf Of Gennadiy Rozental
| Sent: 27 July 2004 07:12
| To: boost_at_[hidden]
| Subject: [boost] Re: Unit test could usefully output more
| significant digitstolog files?
|
| > template<typename T>
| > struct print_log_value {
| > void operator()( std::ostream& ostr, T const& t )
| > {
| > if(std::numeric_limits<T>::is_specialized &&
| > { // Show all possibly significant digits (for example,
| 17 for 64-bit
| > double).
| > ostr.precision(2 + std::numeric_limits<T>::digits * 301/1000);
| > }
| > ostr << t; // by default print the value.
| > }
| > };
|
|
| Actually it's in my TODO list.
|
| I consider using this solution after release.
|