Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion include/functions/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ BigInt pow(const BigInt& base, int exp) {
if (exp < 0) {
if (base == 0)
throw std::logic_error("Cannot divide by zero");
return abs(base) == 1 ? base : 0;
if (base == 1 || exp % 2 == 0)
return 1;
else if (base == -1)
return exp % 2 == 0 ? 1 : -1;
else
return 0;
}
if (exp == 0) {
if (base == 0)
Expand Down
2 changes: 2 additions & 0 deletions test/functions/math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ TEST_CASE("Base cases for pow()", "[functions][math][pow]") {
REQUIRE(pow("1", 0) == 1);
REQUIRE(pow("1", 1) == 1);

REQUIRE(pow("-1", -2) == 1);
REQUIRE(pow("-1", -1) == -1);
REQUIRE(pow("-1", 0) == 1);
REQUIRE(pow("-1", 1) == -1);
REQUIRE(pow("-1", 2) == 1);
}

TEST_CASE("pow() with BigInt base", "[functions][math][pow]") {
Expand Down