Skip to content

Commit

Permalink
changed long to long long to make test work cross platform
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Yankowitz authored and Benjamin Yankowitz committed Feb 14, 2024
1 parent aa82bb0 commit 0f94211
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
24 changes: 12 additions & 12 deletions src/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#include <Oasis/Subtract.hpp>
#include <Oasis/Variable.hpp>

std::vector<long> getAllFactors(long n)
std::vector<long long> getAllFactors(long long n)
{
std::vector<long> answer;
for (long i = 1; i * i <= n; i++) {
std::vector<long long> answer;
for (long long i = 1; i * i <= n; i++) {
if (n % i == 0) {
answer.push_back(i);
}
Expand All @@ -23,7 +23,7 @@ std::vector<long> getAllFactors(long n)
return answer;
}

long gcf(long a, long b)
long long gcf(long long a, long long b)
{
if (b > a) {
std::swap(a, b);
Expand Down Expand Up @@ -58,7 +58,7 @@ auto Expression::FindZeros() const -> std::vector<std::unique_ptr<Expression>>
termsE.push_back(Copy());
}
std::string varName = "";
std::vector<long> termsC;
std::vector<long long> termsC;
for (const auto& i : termsE) {
double coefficent;
std::string variableName;
Expand Down Expand Up @@ -101,16 +101,16 @@ auto Expression::FindZeros() const -> std::vector<std::unique_ptr<Expression>>
termsC.pop_back();
}
std::reverse(termsC.begin(), termsC.end());
std::vector<long> q = getAllFactors(termsC.front());
std::vector<long> p = getAllFactors(termsC.back());
std::vector<long long> q = getAllFactors(termsC.front());
std::vector<long long> p = getAllFactors(termsC.back());
for (auto pv : p) {
for (auto qv : q) {
if (gcf(pv, qv) == 1) {
for (long sign : { -1, 1 }) {
long mpv = pv * sign;
std::vector<long> newTermsC;
long h = 0;
for (long i : termsC) {
for (long long sign : { -1, 1 }) {
long long mpv = pv * sign;
std::vector<long long> newTermsC;
long long h = 0;
for (long long i : termsC) {
h *= mpv;
if (h % qv != 0) {
break;
Expand Down
19 changes: 9 additions & 10 deletions tests/PolynomialTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,55 @@

TEST_CASE("7th degree polynomial with rational roots", "[factor][duplicateRoot]")
{
// 19891950 - 160023895 x + 475755646 x^2 - 621674764 x^3 + 253625736 x^4 + 167604471 x^5 - 159891300 x^6 + 24097500 x^7
Oasis::Add add {
Oasis::Add {
Oasis::Add {
Oasis::Real { 19'891'950 },
Oasis::Real { 222'126'775 },
Oasis::Multiply {
Oasis::Real { -160'023'895 },
Oasis::Real { -1'989'731'815 },
Oasis::Variable { "x" } },
},
Oasis::Add {
Oasis::Multiply {
Oasis::Real { 475'755'646 },
Oasis::Real { 6'674'884'402 },
Oasis::Exponent {
Oasis::Variable { "x" },
Oasis::Real { 2 } } },
Oasis::Multiply {
Oasis::Real { -621'674'764 },
Oasis::Real { -9'967'889'122 },
Oasis::Exponent {
Oasis::Variable { "x" },
Oasis::Real { 3 } } },
} },
Oasis::Add {
Oasis::Add {
Oasis::Multiply {
Oasis::Real { 253'625'736 },
Oasis::Real { 5'069'070'055 },
Oasis::Exponent {
Oasis::Variable { "x" },
Oasis::Real { 4 } } },
Oasis::Multiply {
Oasis::Real { 167'604'471 },
Oasis::Real { 2'370'752'969 },
Oasis::Exponent {
Oasis::Variable { "x" },
Oasis::Real { 5 } } },
},
Oasis::Add {
Oasis::Multiply {
Oasis::Real { -159'891'300 },
Oasis::Real { -2'841'027'600 },
Oasis::Exponent {
Oasis::Variable { "x" },
Oasis::Real { 6 } } },
Oasis::Multiply {
Oasis::Real { 24'097'500 },
Oasis::Real { 446'760'000 },
Oasis::Exponent {
Oasis::Variable { "x" },
Oasis::Real { 7 } } },
} }
};
auto zeros = add.FindZeros();
REQUIRE(zeros.size() == 6);
std::set<std::tuple<long, long>> goalSet = { std::tuple(1, 3), std::tuple(6, 7), std::tuple(13, 17), std::tuple(-5, 3), std::tuple(101, 150), std::tuple(5, 1) };
std::set<std::tuple<long, long>> goalSet = { std::tuple(1, 3), std::tuple(67, 73), std::tuple(13, 17), std::tuple(-5, 3), std::tuple(101, 200), std::tuple(5, 1) };
for (auto& i : zeros) {
auto divideCase = Oasis::Divide<Oasis::Real>::Specialize(*i);
REQUIRE(divideCase != nullptr);
Expand Down

0 comments on commit 0f94211

Please sign in to comment.