Skip to content

Commit

Permalink
rediscovered Diophantine equation problem in float conversion to bina…
Browse files Browse the repository at this point in the history
…ry rational
  • Loading branch information
Ravenwater committed Nov 14, 2024
1 parent da74726 commit 9f96c99
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
21 changes: 16 additions & 5 deletions include/universal/number/rational/rational_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,16 +337,27 @@ class rational {
uint64_t e{ 0 }, f{ 0 };
bool s{ false };
extractFields(rhs, s, e, f, bits);

int exponent = static_cast<int>(e - ieee754_parameter<Real>::bias);
if (e == 0) { // subnormal
}
else { // normal
uint64_t a = f | ieee754_parameter<Real>::hmask;
uint64_t b = ieee754_parameter<Real>::hmask;
int exponent = static_cast<int>(e - ieee754_parameter<Real>::bias);
// std::cout << "exponent = " << exponent << '\n';
// std::cout << "a = " << to_binary(a) << '\n';
// std::cout << "b = " << to_binary(b) << '\n';

//std::cout << "exponent = " << exponent << '\n';
//std::cout << "a = " << to_binary(a) << '\n';
//std::cout << "b = " << to_binary(b) << '\n';

// remove any redundancy in the representation
uint64_t rr{ 0 }, aa{ a }, bb{ b };
while (aa % bb > 0ull) {
rr = aa % bb;
aa = bb;
bb = rr;
}
a /= bb;
b /= bb;

if (exponent == 0 && a == b) {
n = 1;
d = 1;
Expand Down
11 changes: 7 additions & 4 deletions static/rational/conversion/assignment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,30 @@ try {

std::string test_suite = "rational float assignment validation";
std::string test_tag = "assignment";
bool reportTestCases = true;
bool reportTestCases = false;
int nrOfFailedTestCases = 0;

ReportTestSuiteHeader(test_suite, reportTestCases);

#if MANUAL_TESTING

rb64 a,b;
rb16 a,b;
a.set(0x02, 0x0A);
std::cout << to_binary(a) << '\n';
double da = double(a);
b = da;
std::cout << to_binary(da) << " : " << da << '\n';
std::cout << a << '\n';
std::cout << b << '\n';
std::cout << to_binary(a) << " : " << a << '\n';
std::cout << to_binary(b) << " : " << b << '\n';

Ranges(1.0f);

// manual exhaustive test
//
nrOfFailedTestCases += ReportTestResult(ValidateAssignment<rb8>(reportTestCases), type_tag(rb8()), test_tag);
nrOfFailedTestCases += ReportTestResult(ValidateAssignment<rb16>(reportTestCases), type_tag(rb16()), test_tag);
// nrOfFailedTestCases += ReportTestResult(ValidateAssignment<rb32>(reportTestCases), type_tag(rb32()), test_tag);
nrOfFailedTestCases += ReportTestResult(ValidateAssignment<rb64>(reportTestCases), type_tag(rb64()), test_tag);

ReportTestSuiteResults(test_suite, nrOfFailedTestCases);
return EXIT_SUCCESS;
Expand Down

0 comments on commit 9f96c99

Please sign in to comment.