Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documenting source code changes to handle trailing zeros and other input formatting corner cases #23

Open
maxieds opened this issue Nov 2, 2020 · 0 comments

Comments

@maxieds
Copy link

maxieds commented Nov 2, 2020

@ceheitsch
As requested, this is a follow up to my re-opening the pull request #12 to amend the sources mentioned to still have errors by @spoznan in issue #16. Basically, the new source to the function changed is as follows:

Rational get_rational_from_word(std::string word) {
        Rational result;
        std::size_t decimalpoint = word.find('.');
        bool negative = (word.find('-') != std::string::npos);
        if (decimalpoint != std::string::npos) {
            std::string intpart = word.substr(0, decimalpoint);
            std::string fracpart = word.substr(decimalpoint + 1);

            if (intpart == "") intpart = "0";
            Integer theint (intpart, 10);
            theint = abs(theint);

            // Carve out the fractional part. Surprisingly fiddly!
            // Handle a corner case where the fractional part is all zeros:
            int fracdenom = 1;
            int lastNonZeroPos = fracpart.find_last_of("0");
            while(lastNonZeroPos + 1 == fracpart.length() && lastNonZeroPos != std::string::npos) {
                 if(lastNonZeroPos == 0) {
                      fracpart = "";
                      break;
                 }
                 fracpart = fracpart.substr(0, lastNonZeroPos);
                 lastNonZeroPos = fracpart.find_last_of("0");
            } 
            fracdenom = pow(10, fracpart.length());
            if(fracpart == "") {
                 fracpart = "0";
            }
            Integer fracval (fracpart, 10);
            Rational thefrac (fracval, fracdenom);
            thefrac.canonicalize();

            result = theint + thefrac;
            if (negative) result *= -1;
        } else {
            Rational thevalue (word);
            result = thevalue;
        }
        result.canonicalize();
        return result;
    };

Compiling the new source on math-mulberry allows me to do the tests and ensure they pass as follows:

$ ./RunPMFECommand.sh pmfe-tests -a -s -o pmfe-tests-full-output-2020.11.02-v1.txt

  REQUIRE( seq.len() == 72 )
with expansion:
  72 == 72

-------------------------------------------------------------------------------
O. nivara tRNA (old) MFE
  onivara_old_33, d1
-------------------------------------------------------------------------------
src/test-pmfe.cc:589
...............................................................................

src/test-pmfe.cc:1167:
PASSED:
  REQUIRE( energy == pmfe::Rational(-861, 5) )
with expansion:
  -861/5 == -861/5

src/test-pmfe.cc:1171:
PASSED:
  REQUIRE( scored_structure.old_string() == "(.....(...)....(....).................................................)." )
with expansion:
  "(.....(...)....(....).................................................)."
  ==
  "(.....(...)....(....).................................................)."

-------------------------------------------------------------------------------
O. nivara tRNA (old) MFE
-------------------------------------------------------------------------------
src/test-pmfe.cc:589
...............................................................................

src/test-pmfe.cc:595:
PASSED:
  REQUIRE( seq.len() == 72 )
with expansion:
  72 == 72

-------------------------------------------------------------------------------
O. nivara tRNA (old) MFE
  onivara_old_34, d1
-------------------------------------------------------------------------------
src/test-pmfe.cc:589
...............................................................................

src/test-pmfe.cc:1184:
PASSED:
  REQUIRE( energy == pmfe::Rational(-567,10) )
with expansion:
  -567/10 == -567/10

src/test-pmfe.cc:1188:
PASSED:
  REQUIRE( scored_structure.old_string() == "((((((((......((....)).....................(....)..............))))))))." )
with expansion:
  "((((((((......((....)).....................(....)..............))))))))."
  ==
  "((((((((......((....)).....................(....)..............))))))))."

===============================================================================
All tests passed (174 assertions in 24 test cases)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant