Skip to content

Commit

Permalink
Fixed multiply (#16) (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZCG-coder committed May 2, 2024
2 parents ed7ec9e + bf6aed0 commit e28c0cd
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,4 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
/Steppable.build/
2 changes: 2 additions & 0 deletions .idea/editor.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 4 additions & 22 deletions src/multiply/multiplyReport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,29 +118,11 @@ std::string reportMultiply(const std::string& a,
out += std::to_string(i);

// Add the decimal point.
auto biggerNumber = (compare(a, b, 0) == "1") ? a : b;
auto smallerNumber = (compare(a, b, 0) == "0") ? a : b;
if (scale > 0)
auto aDecimals = aDecimal.length(), bDecimals = bDecimal.length();
if (aDecimals + bDecimals > 0)
{
auto numberIntegers = out.length() - (aDecimal.length() + bDecimal.length());
if (numberIntegers < out.length())
out.insert(numberIntegers, 1, '.');
}
else if (scale == 0)
{
auto numberIntegers = 0;
if ((compare(biggerNumber, "5", 0) == "0" and compare(add(biggerNumber, smallerNumber, 0), "7", 0) != "0") or
compare(smallerNumber, "1", 0) == "1")
numberIntegers++;

if (numberIntegers < out.size())
out.insert(numberIntegers + 1, 1, '.');
}
else if (scale < 0)
{
// auto numberIntegers = -scale;
out = "0" + out;
out.insert(out.length() + scale - 1, 1, '.');
out.insert(out.end() - aDecimals - bDecimals, '.');
out = removeTrailingZeros(out);
}

ss << standardizeNumber(out);
Expand Down
16 changes: 7 additions & 9 deletions src/power/power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,17 @@ namespace steppable::__internals::arithmetic
{
if (steps == 2)
return "Since the number is 1, the result is 1.";
else if (steps == 1)
return "1"s + symbols::makeSuperscript(static_cast<std::string>(raiseTo)) + " = 1";
else
return "1";
if (steps == 1)
return "1"s + symbols::makeSuperscript(raiseTo) + " = 1";
return "1";
}
else if (number == "0")
if (number == "0")
{
if (steps == 2)
return "Since the number is 0, the result is 0.";
else if (steps == 1)
return "0"s + symbols::makeSuperscript(static_cast<std::string>(raiseTo)) + " = 0";
else
return "0";
if (steps == 1)
return "0"s + symbols::makeSuperscript(raiseTo) + " = 0";
return "0";
}

auto numberNoTrailingZeros = removeTrailingZeros(number);
Expand Down
7 changes: 3 additions & 4 deletions src/power/powerReport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,13 @@ std::string reportPower(const std::string_view _number,
// Here, we attempt to give a quick answer, instead of doing pointless iterations.
if (numberOrig == "1")
goto finish;
else if (numberOrig == "0")
if (numberOrig == "0")
{
if (steps == 2)
return "Since the number is 0, the result is 0.";
else if (steps == 1)
if (steps == 1)
return "0"s + makeSuperscript(static_cast<std::string>(raiseTo)) + " = 0";
else
return "0";
return "0";
}

loop(raiseTo, [&](const auto& i) {
Expand Down
14 changes: 14 additions & 0 deletions tests/testPower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,18 @@ const auto& result = power(number, raiseTo, 0);

_.assertIsEqual(result, "52599132235830049");
SECTION_END()

SECTION(Power with Decimals)
const std::string_view &number = "47.5", &raiseTo = "10";
const auto& result = power(number, raiseTo, 0);

_.assertIsEqual(result, "58470404222497940.0634765625");
SECTION_END()

SECTION(Power with Decimals)
const std::string_view &number = "0.5", &raiseTo = "10";
const auto& result = power(number, raiseTo, 0);

_.assertIsEqual(result, "0.0009765625");
SECTION_END()
TEST_END()

0 comments on commit e28c0cd

Please sign in to comment.