Skip to content

Commit

Permalink
unifying the to_components formatting between dd and qd types
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravenwater committed Aug 29, 2024
1 parent 0f7fda7 commit 6f82d37
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/universal/number/dd/dd_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down
3 changes: 2 additions & 1 deletion include/universal/number/qd/qd_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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') << '.';
Expand All @@ -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';
}

Expand Down
6 changes: 5 additions & 1 deletion static/dd/api/constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
}


Expand Down
30 changes: 29 additions & 1 deletion static/qd/api/experiments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <universal/number/qd/qd.hpp>
// types to compare to
#include <universal/number/cfloat/cfloat.hpp>
#include <universal/number/dd/dd.hpp>
#include <universal/verification/test_suite.hpp>
//#include <universal/numerics/error_free_ops.hpp> // integral part of double-double and quad-double but can be used standalone
#include <universal/common/string_utils.hpp>
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6f82d37

Please sign in to comment.