Skip to content

Commit e39a314

Browse files
committed
unifying the std::ostream precision articulation
1 parent f2400de commit e39a314

21 files changed

+147
-14
lines changed

static/dd/api/attributes.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ try {
7272

7373
ReportTestSuiteHeader(test_suite, reportTestCases);
7474

75+
auto defaultPrecision = std::cout.precision();
76+
7577
/////////////////////////////////////////////////////////////////////////////////////
7678
//// doubledouble attribute functions
7779

@@ -124,6 +126,8 @@ try {
124126
std::cout << '\n';
125127
}
126128

129+
std::cout << std::setprecision(defaultPrecision);
130+
127131
ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
128132
return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS);
129133
}

static/dd/api/experiments.cpp

+33-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace sw {
2222
void Progression(double v) {
2323
using namespace sw::universal;
2424

25-
auto oldPrec = std::cout.precision();
25+
auto defaultPrecision = std::cout.precision();
2626
float f{ float(v) };
2727
std::cout << std::setprecision(7);
2828
std::cout << to_binary(f, true) << " : " << f << '\n';
@@ -34,7 +34,7 @@ namespace sw {
3434
dd a{ v };
3535
std::cout << std::setprecision(35);
3636
std::cout << to_binary(a, true) << " : " << a << '\n';
37-
std::cout << std::setprecision(oldPrec);
37+
std::cout << std::setprecision(defaultPrecision);
3838
}
3939

4040
void dd_binary(dd const& v) {
@@ -82,9 +82,37 @@ try {
8282
using namespace sw::universal;
8383

8484
std::string test_suite = "double-double (dd) experiments";
85+
bool reportTestCases = true;
8586
int nrOfFailedTestCases = 0;
8687

87-
auto oldPrec = std::cout.precision();
88+
ReportTestSuiteHeader(test_suite, reportTestCases);
89+
90+
auto defaultPrecision = std::cout.precision();
91+
92+
std::cout << "+ ---------- - unevaluated pairs------------ +\n";
93+
{
94+
// what is the value that adds a delta one below the least significant fraction bit of the high double?
95+
// dd = high + lo
96+
// = 1*2^0 + 1*2^-53
97+
// = 1.0e00 + 1.0elog10(2^-53)
98+
double high{ std::pow(2.0, 0.0) };
99+
ReportValue(high, "2^0");
100+
double low{ std::pow(2.0, -53.0) };
101+
ReportValue(low, "2^-53");
102+
std::cout << std::log10(low) << '\n';
103+
double exponent = -std::ceil(std::abs(std::log10(low)));
104+
std::cout << "exponent : " << exponent << '\n';
105+
106+
std::cout << std::setprecision(32);
107+
for (int i = 0; i < 54; ++i) {
108+
low = (std::pow(2.0, -53.0 - double(i)));
109+
dd a(high, low);
110+
std::cout << a << '\n';
111+
}
112+
std::cout << std::setprecision(defaultPrecision);
113+
}
114+
115+
return 0;
88116

89117
std::cout << "Smallest normal number progressions\n";
90118
{
@@ -144,7 +172,7 @@ try {
144172

145173
}
146174

147-
std::cout << "+--------- doubledouble subnormal behavior --------+\n";
175+
std::cout << "+--------- double-double subnormal behavior --------+\n";
148176
{
149177
constexpr double smallestNormal = std::numeric_limits<double>::min();
150178
ReportValue(smallestNormal, "smallest normal");
@@ -183,7 +211,7 @@ try {
183211
std::cout << "to_string(precision=4) format : " << a.to_string(4) << '\n';
184212
}
185213

186-
std::cout << std::setprecision(oldPrec);
214+
std::cout << std::setprecision(defaultPrecision);
187215

188216
ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
189217
return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS);

static/dd/api/traits.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ try {
2020

2121
ReportTestSuiteHeader(test_suite, reportTestCases);
2222

23+
auto defaultPrecision = std::cout.precision();
24+
2325
/////////////////////////////////////////////////////////////////////////////////////
2426
//// bfloat type attribute functions
2527

@@ -69,6 +71,8 @@ try {
6971
std::cout << symmetry_range< dd >() << '\n';
7072
}
7173

74+
std::cout << std::setprecision(defaultPrecision);
75+
7276
ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
7377
return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS);
7478
}

static/dd/arithmetic/addition.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ try {
4141

4242
#if MANUAL_TESTING
4343

44+
auto defaultPrecision = std::cout.precision();
45+
4446
dd a, b, c, ulpAtOne, onePlusUlp, onePlusHalfUlp;
4547

4648
a = 1.0;
@@ -61,6 +63,8 @@ try {
6163
std::cout << "1.0 + ulp(1.0) : " << to_pair(onePlusUlp) << '\n';
6264
std::cout << "1.0 + ulp(0.5) : " << to_pair(onePlusHalfUlp) << '\n';
6365

66+
std::cout << std::setprecision(defaultPrecision);
67+
6468
ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
6569
return EXIT_SUCCESS; // ignore failures
6670
#else // !MANUAL_TESTING

static/dd/arithmetic/arithmetic.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ try {
257257

258258
#if MANUAL_TESTING
259259

260+
auto defaultPrecision = std::cout.precision();
261+
260262
// doubledouble addition
261263
std::cout << "two sum\n";
262264
TraceTwoSum(ulp(pow(0.5, 10.0)));
@@ -347,6 +349,8 @@ try {
347349
std::cout << "Test divisional identities\n";
348350
TestRandomDivisionalIdentities(1);
349351

352+
std::cout << std::setprecision(defaultPrecision);
353+
350354
ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
351355
return EXIT_SUCCESS; // ignore failures
352356
#else // !MANUAL_TESTING

static/dd/arithmetic/division.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ try {
4040

4141
#if MANUAL_TESTING
4242

43+
auto defaultPrecision = std::cout.precision();
4344

45+
// test code
46+
47+
std::cout << std::setprecision(defaultPrecision);
4448

4549
ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
4650
return EXIT_SUCCESS; // ignore failures

static/dd/arithmetic/multiplication.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ try {
4141

4242
#if MANUAL_TESTING
4343

44+
auto defaultPrecision = std::cout.precision();
45+
4446
dd a, b, c, d;
4547

4648
constexpr unsigned labelWidth = 40;
@@ -71,6 +73,8 @@ try {
7173
d *= b;
7274
}
7375

76+
std::cout << std::setprecision(defaultPrecision);
77+
7478
ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
7579
return EXIT_SUCCESS; // ignore failures
7680
#else // !MANUAL_TESTING

static/dd/arithmetic/subtraction.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ try {
4141

4242
#if MANUAL_TESTING
4343

44+
auto defaultPrecision = std::cout.precision();
45+
4446
dd a, b, c, ulpAtOne, oneMinusUlp, oneMinusHalfUlp;
4547

4648
a = 1.0;
@@ -61,6 +63,8 @@ try {
6163
std::cout << "1.0 - ulp(1.0) : " << to_pair(oneMinusUlp) << '\n';
6264
std::cout << "1.0 - ulp(0.5) : " << to_pair(oneMinusHalfUlp) << '\n';
6365

66+
std::cout << std::setprecision(defaultPrecision);
67+
6468
ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
6569
return EXIT_SUCCESS; // ignore failures
6670
#else // !MANUAL_TESTING

static/dd/conversion/conversion.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ try {
3737

3838
#if MANUAL_TESTING
3939

40+
auto defaultPrecision = std::cout.precision();
41+
4042
uint64_t u64;
4143
int64_t i64;
4244

@@ -69,6 +71,8 @@ try {
6971
ReportValue(a, "0x7FFF'FFFF'FFFF'FFFF", 35, 32);
7072
}
7173

74+
std::cout << std::setprecision(defaultPrecision);
75+
7276
ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
7377
return EXIT_SUCCESS; // ignore failures
7478
#else // !MANUAL_TESTING

static/dd/conversion/to_string.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ try {
145145

146146
#if MANUAL_TESTING
147147

148+
auto defaultPrecision = std::cout.precision();
149+
148150
ScanTest(0); // reference native double
149151
ScanTest(1); // comparative double-double
150152

@@ -169,6 +171,7 @@ try {
169171
std::cout << "std::cout:\t" << f << "\n\n";
170172
}
171173

174+
std::cout << std::setprecision(defaultPrecision);
172175

173176
ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
174177
return EXIT_SUCCESS; // ignore failures

static/dd/logic/logic.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ try {
4141

4242
#if MANUAL_TESTING
4343

44+
auto defaultPrecision = std::cout.precision();
45+
46+
// test code
47+
48+
std::cout << std::setprecision(defaultPrecision);
4449

4550
ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
4651
return EXIT_SUCCESS; // ignore failures

static/dd/math/exponent.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ try {
5656
ReportTestSuiteHeader(test_suite, reportTestCases);
5757

5858
#if MANUAL_TESTING
59-
// generate individual testcases to hand trace/debug
59+
60+
auto defaultPrecision = std::cout.precision();
61+
6062
GenerateTestCase(4.0);
6163

6264
auto oldPrec = std::cout.precision();
@@ -69,6 +71,8 @@ try {
6971
}
7072
std::cout << std::setprecision(oldPrec);
7173

74+
std::cout << std::setprecision(defaultPrecision);
75+
7276
ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
7377
return EXIT_SUCCESS; // ignore errors
7478
#else

static/dd/math/hyperbolic.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ try {
3939

4040
#if MANUAL_TESTING
4141

42+
auto defaultPrecision = std::cout.precision();
43+
4244
std::cout << "ALL HYPERBOLIC FUNCTIONS ARE SHIMS TO DOUBLE\n";
4345

4446
{
@@ -65,6 +67,8 @@ try {
6567
std::cout << "atanh( " << x << " ) = " << atanh(x) << '\n';
6668
}
6769

70+
std::cout << std::setprecision(defaultPrecision);
71+
6872
ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
6973
return EXIT_SUCCESS; // ignore errors
7074
#else

static/dd/math/hypot.cpp

+21-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99
#include <universal/number/dd/dd.hpp>
1010
#include <universal/verification/test_suite.hpp>
1111

12+
namespace sw {
13+
namespace universal {
14+
15+
template<typename Real>
16+
void ReportTriangle(const Real& x, const Real y, int precision = 16) { // default precision is for double
17+
using std::hypot;
18+
auto defaultPrecision = std::cout.precision();
19+
std::cout << "hypot( " << x << ", " << y << ") = " << std::setprecision(precision) << hypot(x, y) << std::setprecision(defaultPrecision) << '\n';
20+
}
21+
}
22+
}
1223
// Regression testing guards: typically set by the cmake configuration, but MANUAL_TESTING is an override
1324
#define MANUAL_TESTING 1
1425
// REGRESSION_LEVEL_OVERRIDE is set by the cmake file to drive a specific regression intensity
@@ -38,10 +49,19 @@ try {
3849

3950
#if MANUAL_TESTING
4051

52+
auto defaultPrecision = std::cout.precision();
53+
4154
dd x{ 3.0 }, y{ 4.0 };
4255

43-
std::cout << "hypot( " << x << ", " << y << ") = " << hypot(x, y) << '\n';
56+
ReportTriangle(x, y);
57+
58+
// skinny triangle
59+
x = 1.0e-5; y = 1.0e5;
60+
61+
// TBD: shim converts to double which can't capture the skinny triangle details
62+
ReportTriangle(x, y, 32);
4463

64+
std::cout << std::setprecision(defaultPrecision);
4565

4666
ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
4767
return EXIT_SUCCESS; // ignore errors

static/dd/math/logarithm.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,9 @@ try {
263263
std::cerr << test_tag << '\n';
264264

265265
#if MANUAL_TESTING
266-
// generate individual testcases to hand trace/debug
266+
267+
auto defaultPrecision = std::cout.precision();
268+
267269
GenerateLogTestCase(1.0);
268270
GenerateLogTestCase(std::numbers::e);
269271
GenerateLogTestCase(pow(std::numbers::e, 2.0));
@@ -306,6 +308,8 @@ try {
306308
nrOfFailedTestCases += ReportTestResult(VerifyLog1pFunction<dd>(reportTestCases, maxError), "double-double", test_id);
307309
}
308310

311+
std::cout << std::setprecision(defaultPrecision);
312+
309313
ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
310314
return EXIT_SUCCESS; // ignore errors
311315
#else

static/dd/math/next.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,16 @@ try {
5757
ReportTestSuiteHeader(test_suite, reportTestCases);
5858

5959
#if MANUAL_TESTING
60-
// generate individual testcases to hand trace/debug
60+
61+
auto defaultPrecision = std::cout.precision();
62+
6163
GenerateTestCase(1.0, 2.0);
6264

6365

6466
//nrOfFailedTestCases += ReportTestResult(VerifyNextFunction<dd>("Manual Testing", reportTestCases), "dd", test_tag);
6567

68+
std::cout << std::setprecision(defaultPrecision);
69+
6670
ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
6771
return EXIT_SUCCESS; // ignore errors
6872
#else

static/dd/math/pow.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ try {
5757
ReportTestSuiteHeader(test_suite, reportTestCases);
5858

5959
#if MANUAL_TESTING
60-
// generate individual testcases to hand trace/debug
60+
61+
auto defaultPrecision = std::cout.precision();
62+
6163
GenerateTestCase(4.0, 2.0);
6264

6365
dd a{ 1.0 };
@@ -71,6 +73,7 @@ try {
7173
ReportValue(pow(a, i), tag);
7274
}
7375

76+
std::cout << std::setprecision(defaultPrecision);
7477

7578
ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
7679
return EXIT_SUCCESS; // ignore errors

static/dd/math/sqrt.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,18 @@ try {
7878
ReportTestSuiteHeader(test_suite, reportTestCases);
7979

8080
#if MANUAL_TESTING
81-
// generate individual testcases to hand trace/debug
81+
82+
auto defaultPrecision = std::cout.precision();
83+
8284
GenerateSqrtTestCase<double>(1.0);
8385
GenerateSqrtTestCase<double>(1024.0 * 1024.0);
8486
constexpr double minpos = std::numeric_limits<double>::min();
8587
GenerateSqrtTestCase<double>(minpos);
8688
constexpr double maxpos = std::numeric_limits<double>::max();
8789
GenerateSqrtTestCase<double>(maxpos);
8890

91+
std::cout << std::setprecision(defaultPrecision);
92+
8993
ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
9094
return EXIT_SUCCESS; // ignore errors
9195
#else

0 commit comments

Comments
 (0)