From 6f82d37b9e72de214684633d5770bb2c0a69a52e Mon Sep 17 00:00:00 2001 From: Ravenwater Date: Thu, 29 Aug 2024 08:46:57 -0400 Subject: [PATCH] unifying the to_components formatting between dd and qd types --- include/universal/number/dd/dd_impl.hpp | 1 + include/universal/number/qd/qd_impl.hpp | 3 ++- static/dd/api/constants.cpp | 6 ++++- static/qd/api/experiments.cpp | 30 ++++++++++++++++++++++++- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/include/universal/number/dd/dd_impl.hpp b/include/universal/number/dd/dd_impl.hpp index 4c1f91641..9d4816664 100644 --- a/include/universal/number/dd/dd_impl.hpp +++ b/include/universal/number/dd/dd_impl.hpp @@ -1038,6 +1038,7 @@ inline std::string to_components(const dd& number, bool nibbleMarker = false) { mask >>= 1; } + s << std::scientific << std::showpos << std::setprecision(15); // we are printing a double s << " : " << number[i] << " : binary scale " << scale(number[i]) << '\n'; } diff --git a/include/universal/number/qd/qd_impl.hpp b/include/universal/number/qd/qd_impl.hpp index a77859a41..489739c89 100644 --- a/include/universal/number/qd/qd_impl.hpp +++ b/include/universal/number/qd/qd_impl.hpp @@ -1286,7 +1286,7 @@ inline std::string to_components(const qd& number, bool nibbleMarker = false) { decoder.d = number[i]; std::string label = "x[" + std::to_string(i) + "]"; - s << std::setw(20) << label << " : "; + s << label << " : "; s << "0b"; // print sign bit s << (decoder.parts.sign ? '1' : '0') << '.'; @@ -1311,6 +1311,7 @@ inline std::string to_components(const qd& number, bool nibbleMarker = false) { mask >>= 1; } + s << std::scientific << std::showpos << std::setprecision(15); // we are printing a double s << " : " << number[i] << " : binary scale " << scale(number[i]) << '\n'; } diff --git a/static/dd/api/constants.cpp b/static/dd/api/constants.cpp index 04aafdf54..d063cc18e 100644 --- a/static/dd/api/constants.cpp +++ b/static/dd/api/constants.cpp @@ -202,7 +202,7 @@ try { a = _third; b = _third2; ReportValue(a, "0.3333....", 35, 32); - ReportValue(b, "0.3333....", 35, 32); + ReportValue(b, "0.3333....*2^-53", 35, 32); c = a + b; ReportValue(c, "0.3333....", 35, 32); std::cout << to_pair(c) << '\n'; @@ -218,6 +218,10 @@ try { dd f(0.3333333333333333, 1.8503717077085935e-17); ReportValue(f, "0.3333....", 35, 32); std::cout << to_pair(f) << '\n'; + + dd g = reciprocal(dd(3.0)); + ReportValue(g, "1/3", 35, 32); + std::cout << to_pair(g) << '\n'; } diff --git a/static/qd/api/experiments.cpp b/static/qd/api/experiments.cpp index 6405a671d..cc50fd510 100644 --- a/static/qd/api/experiments.cpp +++ b/static/qd/api/experiments.cpp @@ -10,9 +10,11 @@ // minimum set of include files to reflect source code dependencies // Configure the qd template environment // enable/disable arithmetic exceptions -#define DOUBLEDOUBLE_THROW_ARITHMETIC_EXCEPTION 0 +#define QUADDOUBLE_THROW_ARITHMETIC_EXCEPTION 0 #include +// types to compare to #include +#include #include //#include // integral part of double-double and quad-double but can be used standalone #include @@ -117,6 +119,32 @@ try { ReportValue(c, "c = a + b", 20, 32); } + std::cout << "+---------- to_binary and to_components -----+\n"; + { + std::cout << std::setprecision(64); + qd a("0.1"), b = 1.0 / qd(3.0); + + std::cout << a << '\n'; + std::cout << to_components(a) << '\n'; + std::cout << b << '\n'; + std::cout << to_components(b) << '\n'; + + std::cout << std::setprecision(defaultPrecision); + } + + { + std::cout << std::setprecision(32); + dd a("0.1"), b = 1.0 / dd(3.0); + + std::cout << a << '\n'; + std::cout << to_components(a) << '\n'; + std::cout << b << '\n'; + std::cout << to_components(b) << '\n'; + + std::cout << std::setprecision(defaultPrecision); + } + + std::cout << std::setprecision(defaultPrecision); ReportTestSuiteResults(test_suite, nrOfFailedTestCases);