Skip to content

Commit a61e64a

Browse files
committed
rational conversion error scan experiments
1 parent e8dca3e commit a61e64a

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

static/rational/conversion/conversion.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,39 @@ namespace sw { namespace universal {
4949
reportConversionError(reportRoundTrip<rb64, Real>(1, 5));
5050
}
5151

52+
template<typename Real>
53+
void roundingError(uint64_t a, uint64_t b) {
54+
constexpr unsigned nbits = sizeof(Real) * 8;
55+
blockbinary<nbits> numerator{static_cast<int64_t>(a)}, denominator{ static_cast<int64_t>(b)};
56+
int WIDTH = sizeof(Real)*8 + 5;
57+
58+
auto precision = std::cout.precision();
59+
std::cout << std::setprecision(std::numeric_limits<Real>::max_digits10);
60+
61+
for (unsigned i = 0; i < ieee754_parameter<Real>::fbits+1; ++i) {
62+
double v = double(numerator) / double(denominator);
63+
std::cout << std::setw(WIDTH) << to_binary(numerator) << std::setw(WIDTH) << to_binary(denominator) << " : " << v << '\n';
64+
65+
numerator >>= 1;
66+
denominator >>= 1;
67+
}
68+
69+
std::cout << std::setprecision(precision);
70+
}
71+
72+
template<typename Real>
73+
void scaleRoundingError(Real fp) {
74+
fp = Real(0.2);
75+
bool s;
76+
uint64_t e, f, bits;
77+
extractFields(fp, s, e, f, bits);
78+
uint64_t a = f | ieee754_parameter<Real>::hmask;
79+
uint64_t b = ieee754_parameter<Real>::hmask;
80+
b <<= 3; // associated with the ratio that yields 0.2
81+
82+
roundingError<Real>(a, b);
83+
84+
}
5285
} }
5386

5487
// Regression testing guards: typically set by the cmake configuration, but MANUAL_TESTING is an override
@@ -86,6 +119,9 @@ try {
86119
Experiment<float>();
87120
Experiment<double>();
88121

122+
scaleRoundingError<float>(0.2f);
123+
scaleRoundingError<double>(0.2);
124+
89125
ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
90126
return EXIT_SUCCESS;
91127
#else

0 commit comments

Comments
 (0)