Skip to content

Commit 1a65666

Browse files
committed
Using Result to save calculation time of root.
- Added ResultBase, Result and ResultBool objects. - getRootFactors uses ResultBool to return the root of the factor along with the factor. - Fixed a bug in multiply that moves more decimal places than needed if the input b is a decimal.
1 parent bf4840d commit 1a65666

File tree

6 files changed

+68
-28
lines changed

6 files changed

+68
-28
lines changed

include/factors.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@
2222

2323
#pragma once
2424

25+
#include "types/result.hpp"
26+
2527
#include <string>
2628
#include <vector>
2729

30+
using namespace steppable::types;
31+
2832
namespace steppable::__internals::numUtils
2933
{
3034
/**
@@ -40,9 +44,9 @@ namespace steppable::__internals::numUtils
4044
*
4145
* @param[in] _number The number to get the largest root factor of.
4246
* @param[in] base The base of the root.
43-
* @return The largest root factor of the number.
47+
* @return A result object containing the largest root factor of the number.
4448
*/
45-
std::string getRootFactor(const std::string& _number, const std::string& base = "2");
49+
ResultBool getRootFactor(const std::string& _number, const std::string& base = "2");
4650

4751
/**
4852
* @brief Get the greatest root number less than or equal to the given number.
@@ -66,7 +70,8 @@ namespace steppable::__internals::numUtils
6670
*
6771
* @param[in] _number The number to check.
6872
* @param[in] base The base of the root.
69-
* @return True if the number is a root number, false otherwise.
73+
* @return StatusBool::CALCULATED_SIMPLIFIED_YES if the number is a root number,
74+
* StatusBool::CALCULATED_SIMPLIFIED_NO otherwise.
7075
*/
71-
bool isRoot(const std::string& _number, const std::string& base);
76+
ResultBool isRoot(const std::string& _number, const std::string& base);
7277
} // namespace steppable::__internals::numUtils

include/types/result.hpp

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,37 @@ namespace steppable::types
3535
/**
3636
* @brief The status of the calculation.
3737
*/
38-
enum Status
38+
enum class Status
3939
{
4040
CALCULATED_SIMPLIFIED,
4141
CALCULATED_UNSIMPLIFIED,
4242
MATH_ERROR
4343
};
4444

4545
/**
46-
* @brief The result of a calculation.
46+
* @brief The status of a boolean calculation.
4747
*/
48-
class Result
48+
enum class StatusBool
49+
{
50+
CALCULATED_SIMPLIFIED_YES,
51+
CALCULATED_SIMPLIFIED_NO,
52+
CALCULATED_UNSIMPLIFIED_YES,
53+
CALCULATED_UNSIMPLIFIED_NO,
54+
MATH_ERROR
55+
};
56+
57+
/**
58+
* @brief A base class for a result of a calculation. You should use the `Result` and `ResultBool` aliases instead
59+
* of this class, which has the `Status` and `StatusBool` enums as the status type respectively.
60+
*
61+
* @tparam StatusType The type of the status of the calculation.
62+
*/
63+
template<typename StatusType>
64+
class ResultBase
4965
{
5066
private:
5167
/// @brief Whether the calculation is done.
52-
Status done;
68+
StatusType done;
5369

5470
/// @brief The inputs to the calculation.
5571
std::vector<std::string> inputs;
@@ -58,7 +74,7 @@ namespace steppable::types
5874
std::string out;
5975

6076
public:
61-
Result() = delete;
77+
ResultBase() = delete;
6278

6379
/**
6480
* @brief Constructs a new result object.
@@ -67,12 +83,24 @@ namespace steppable::types
6783
* @param[in] _out The output of the calculation.
6884
* @param[in] _done A flag indicating how the calculation is done.
6985
*/
70-
Result(const std::vector<std::string>& _inputs, std::string _out, Status _done) :
86+
ResultBase(const std::vector<std::string>& _inputs, std::string _out, StatusType _done) :
7187
done(_done), inputs(_inputs), out(std::move(_out))
7288
{
7389
}
7490

7591
/// @brief Gets how the calculation is done.
76-
Status getStatus() { return done; }
92+
StatusType getStatus() const { return done; }
93+
94+
/// @brief Gets the output of the calculation.
95+
std::string getOutput() const { return out; }
96+
97+
/// @brief Gets the inputs to the calculation.
98+
std::vector<std::string> getInputs() const { return inputs; }
7799
};
100+
101+
/// @brief An alias for a result of a calculation. This represents a calculation with a `Status` status.
102+
using Result = ResultBase<Status>;
103+
104+
/// @brief An alias for a result of a boolean calculation.
105+
using ResultBool = ResultBase<StatusBool>;
78106
} // namespace steppable::types

src/factors.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
#include "factors.hpp"
2424

2525
#include "fn/basicArithm.hpp"
26+
#include "types/result.hpp"
2627

2728
#include <algorithm>
2829
#include <vector>
2930

3031
using namespace steppable::__internals::arithmetic;
32+
using namespace steppable::types;
3133

3234
namespace steppable::__internals::numUtils
3335
{
@@ -52,17 +54,20 @@ namespace steppable::__internals::numUtils
5254
return factors;
5355
}
5456

55-
std::string getRootFactor(const std::string& _number, const std::string& base)
57+
ResultBool getRootFactor(const std::string& _number, const std::string& base)
5658
{
5759
auto factors = getFactors(_number);
5860
// Reverse the factors
5961
std::reverse(factors.begin(), factors.end());
6062
// Get the largest root factor
6163
for (const auto& factor : factors)
62-
if (isRoot(factor, base))
63-
return factor;
64+
{
65+
auto rootResult = isRoot(factor, base);
66+
if (rootResult.getStatus() == StatusBool::CALCULATED_SIMPLIFIED_YES)
67+
return { { _number, base, rootResult.getOutput() }, factor, StatusBool::CALCULATED_SIMPLIFIED_YES };
68+
}
6469
// The number has no root factors
65-
return "1";
70+
return { { _number, base }, "1", StatusBool::CALCULATED_SIMPLIFIED_YES };
6671
}
6772

6873
std::string getGreatestRootNum(const std::string& _number, const std::string& base)
@@ -77,10 +82,12 @@ namespace steppable::__internals::numUtils
7782
return factors.size() == 2; // Only 1 and the number itself are factors ==> prime!
7883
}
7984

80-
bool isRoot(const std::string& _number, const std::string& base)
85+
ResultBool isRoot(const std::string& _number, const std::string& base)
8186
{
8287
auto iRoot = rootIntPart(_number, base);
8388
auto rootNum = power(iRoot, base, 0);
84-
return compare(rootNum, _number, 0) == "2";
89+
if (compare(rootNum, _number, 0) == "2")
90+
return { { _number, base }, iRoot, StatusBool::CALCULATED_SIMPLIFIED_YES };
91+
return { { _number, base }, "1", StatusBool::CALCULATED_SIMPLIFIED_NO };
8592
}
8693
} // namespace steppable::__internals::numUtils

src/multiply/multiply.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ namespace steppable::__internals::arithmetic
5050
{
5151
auto a = static_cast<std::string>(_a);
5252
auto b = static_cast<std::string>(_b);
53+
const auto& [splitNumberArray, aIsNegative, bIsNegative] = splitNumber(a, b, false, false);
54+
bool resultIsNegative = false;
55+
const auto& [aInteger, aDecimal, bInteger, bDecimal] = splitNumberArray;
5356
std::stringstream out;
5457
if (isZeroString(a) or isZeroString(b))
5558
{
@@ -79,20 +82,17 @@ namespace steppable::__internals::arithmetic
7982
{
8083
if (steps == 2)
8184
out << "Since " << a << " is a power of 10, we can move the decimal places to obtain the result.\n";
82-
out << moveDecimalPlaces(b, static_cast<long long>(a.length() - 1));
85+
out << moveDecimalPlaces(b, static_cast<long long>(aInteger.length() - 1));
8386
return out.str();
8487
}
8588
if (isPowerOfTen(b))
8689
{
8790
if (steps == 2)
8891
out << "Since " << b << " is a power of 10, we can move the decimal places to obtain the result.\n";
89-
out << moveDecimalPlaces(a, static_cast<long long>(b.length() - 1));
92+
out << moveDecimalPlaces(a, static_cast<long long>(bInteger.length() - 1));
9093
return out.str();
9194
}
9295

93-
const auto& [splitNumberArray, aIsNegative, bIsNegative] = splitNumber(a, b, false, false);
94-
bool resultIsNegative = false;
95-
const auto& [aInteger, aDecimal, bInteger, bDecimal] = splitNumberArray;
9696
const std::string& aStr = aInteger + aDecimal;
9797
const std::string bStr = bInteger + bDecimal;
9898
std::vector<std::vector<int>> prodDigits;

src/root/root.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ namespace steppable::__internals::arithmetic
117117
Surd rootSurd(const std::string& _number, const std::string& base)
118118
{
119119
auto largestRootFactor = numUtils::getRootFactor(_number, base);
120-
auto radicand = roundDown(divide(_number, largestRootFactor, 0, 1));
121-
auto multiplier = rootIntPart(largestRootFactor, base);
120+
auto radicand = roundDown(divide(_number, largestRootFactor.getOutput(), 0, 1));
121+
auto multiplier = largestRootFactor.getInputs()[2];
122122

123123
return { radicand, multiplier };
124124
}

tests/testFactors.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ _.assertIsEqual(getGreatestRootNum("5"), "4");
4949
SECTION_END()
5050

5151
SECTION(Greatest Root Factor Test)
52-
_.assertIsEqual(getRootFactor("24", "2"), "4");
53-
_.assertIsEqual(getRootFactor("13", "2"), "1");
54-
_.assertIsEqual(getRootFactor("27", "2"), "9");
55-
_.assertIsEqual(getRootFactor("72", "2"), "36");
52+
_.assertIsEqual(getRootFactor("24", "2").getOutput(), "4");
53+
_.assertIsEqual(getRootFactor("13", "2").getOutput(), "1");
54+
_.assertIsEqual(getRootFactor("27", "2").getOutput(), "9");
55+
_.assertIsEqual(getRootFactor("72", "2").getOutput(), "36");
5656
SECTION_END()
5757

5858
TEST_END()

0 commit comments

Comments
 (0)