Skip to content

Commit 3edb3da

Browse files
authored
Improvements to power and multiply (#55)
2 parents e4f2173 + df2a7fb commit 3edb3da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+786
-354
lines changed

.idea/editor.xml

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ PROJECT(Steppable)
2525

2626
# Ensure that Python is available to run the development scripts, and build with bindings.
2727
SET(Python_FIND_VIRTUALENV FIRST)
28+
SET(Python3_FIND_VIRTUALENV FIRST)
2829

2930
FIND_PACKAGE(
3031
Python

include/constants.hpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
namespace steppable::constants
3232
{
3333
/// @brief 100 digits of pi.
34-
extern const std::string_view& PI;
34+
constexpr const std::string_view PI =
35+
"3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679";
3536

3637
/// @brief Pi multiplied by 2.
3738
// Generated using Python:
@@ -43,7 +44,7 @@ namespace steppable::constants
4344
// 5 | 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
4445
// 6 | ) * Decimal(2)
4546
// -------------------------------------------------------
46-
extern const std::string_view& TWO_PI;
47+
constexpr const std::string_view TWO_PI = "6.283185307179586231995926937088370323181152343750";
4748

4849
/// @brief Pi divided by 2.
4950
// Generated using Python:
@@ -55,7 +56,8 @@ namespace steppable::constants
5556
// 5 | 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
5657
// 6 | ) / Decimal(2)
5758
// -------------------------------------------------------
58-
extern const std::string_view& PI_OVER_2;
59+
constexpr const std::string_view PI_OVER_2 =
60+
"1.570796326794896619231321691639751442098584699687552910487472296153908203143104499314017412835292542";
5961

6062
/// @brief Pi divided by 180 (to convert degrees to radians), correct to 100 decimal places.
6163
// Generated using Python:
@@ -67,7 +69,8 @@ namespace steppable::constants
6769
// 5 | 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
6870
// 6 | ) / Decimal(180)
6971
// -------------------------------------------------------
70-
extern const std::string_view& PI_OVER_180;
72+
constexpr const std::string_view PI_OVER_180 =
73+
"0.01745329251994329508887757482524547311994764539930555555555555555555555555555555555555555555555555556";
7174

7275
/// @brief Pi divided by 200 (to convert grads to radians), correct to 100 decimal places.
7376
// Generated using Python:
@@ -79,7 +82,8 @@ namespace steppable::constants
7982
// 5 | 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
8083
// 6 | ) / Decimal(200)
8184
// -------------------------------------------------------
82-
extern const std::string_view& PI_OVER_200;
85+
constexpr const std::string_view PI_OVER_200 =
86+
"0.01570796326794896619231321691639716312084074699687552942986246296153903203140449499314017412671058534";
8387

84-
extern const std::string_view& E;
88+
constexpr const std::string_view E = "2.718281828459045090795598298427648842334747314453125";
8589
} // namespace steppable::constants

include/factors.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace steppable::__internals::numUtils
4646
* @param[in] base The base of the root.
4747
* @return A result object containing the largest root factor of the number.
4848
*/
49-
ResultBool getRootFactor(const std::string& _number, const std::string& base = "2");
49+
ResultBool<std::string> getRootFactor(const std::string& _number, const std::string& base = "2");
5050

5151
/**
5252
* @brief Get the greatest root number less than or equal to the given number.
@@ -73,5 +73,5 @@ namespace steppable::__internals::numUtils
7373
* @return StatusBool::CALCULATED_SIMPLIFIED_YES if the number is a root number,
7474
* StatusBool::CALCULATED_SIMPLIFIED_NO otherwise.
7575
*/
76-
ResultBool isRoot(const std::string& _number, const std::string& base);
76+
ResultBool<std::string> isRoot(const std::string& _number, const std::string& base);
7777
} // namespace steppable::__internals::numUtils

include/fn/calc.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@
3939
#include "output.hpp"
4040

4141
#include <string>
42+
#include <util.hpp>
4243

4344
using namespace std::literals;
4445

4546
/**
4647
* @namespace steppable::__internals
4748
* @brief The namespace containing internal functions for the Steppable library.
4849
*/
49-
namespace steppable::__internals::arithmetic
50+
namespace steppable::__internals::calc
5051
{
5152
/**
5253
* @brief Represents the quotient and remainder of a division operation.
@@ -154,17 +155,18 @@ namespace steppable::__internals::arithmetic
154155
* @param[in] steps The number of steps to perform the multiplication.
155156
* @return The product of the two numbers as a string.
156157
*/
157-
std::string multiply(const std::string& a, const std::string& b, int steps = 2);
158+
std::string multiply(const std::string& a, const std::string& b, int steps = 2, int decimals = MAX_DECIMALS);
158159

159160
/**
160161
* @brief Raises a string representation of a number to a power.
161162
*
162163
* @param[in] _number The string representation of the number.
163164
* @param[in] raiseTo The string representation of the power to raise the number to.
164165
* @param[in] steps The number of steps to perform the power operation.
166+
* @param[in] decimals The number of decimals to output from the power operation.
165167
* @return The result of the power operation as a string.
166168
*/
167-
std::string power(const std::string& _number, const std::string& raiseTo, int steps = 2);
169+
std::string power(const std::string& _number, const std::string& raiseTo, int steps = 2, int decimals = 8);
168170

169171
/**
170172
* @brief Calculates e^x. Shorthand of power(x, E, 0);
@@ -572,4 +574,4 @@ namespace steppable::__internals::arithmetic
572574
}
573575
}
574576

575-
} // namespace steppable::__internals::arithmetic
577+
} // namespace steppable::__internals::calc

include/fn/root.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
#include <string>
2626

27-
namespace steppable::__internals::arithmetic
27+
namespace steppable::__internals::calc
2828
{
2929
/**
3030
* @brief A struct to represent a surd.
@@ -38,4 +38,4 @@ namespace steppable::__internals::arithmetic
3838
/// @brief The multiplier of the surd.
3939
std::string multiplier;
4040
};
41-
} // namespace steppable::__internals::arithmetic
41+
} // namespace steppable::__internals::calc

include/testing.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ namespace steppable::testing
112112
* @brief Constructs a new TestCase object with the given name.
113113
* @param[in] testCaseName The name of the test case.
114114
*/
115-
explicit TestCase(const std::string& testCaseName);
115+
explicit TestCase(std::string testCaseName);
116116

117117
/**
118118
* @brief Asserts that two strings are equal.

include/types/data.hpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**************************************************************************************************
2+
* Copyright (c) 2023-2025 NWSOFT *
3+
* *
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy *
5+
* of this software and associated documentation files (the "Software"), to deal *
6+
* in the Software without restriction, including without limitation the rights *
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
8+
* copies of the Software, and to permit persons to whom the Software is *
9+
* furnished to do so, subject to the following conditions: *
10+
* *
11+
* The above copyright notice and this permission notice shall be included in all *
12+
* copies or substantial portions of the Software. *
13+
* *
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
20+
* SOFTWARE. *
21+
**************************************************************************************************/
22+
23+
#pragma once
24+
25+
#include <string>
26+
27+
#include "util.hpp"
28+
29+
using namespace std::literals;
30+
using namespace steppable::__internals::utils;
31+
32+
namespace steppable
33+
{
34+
template<typename BaseT, StringLiteral BaseTName>
35+
class Data
36+
{
37+
BaseT value;
38+
39+
public:
40+
std::string getDataType() { return BaseTName.value; }
41+
42+
Data(const BaseT object) : value(object) {} // NOLINT(*-explicit-constructor)
43+
Data() : value() {}
44+
45+
Data& operator=(const BaseT& object)
46+
{
47+
value = object;
48+
return *this;
49+
}
50+
};
51+
52+
enum class _Weekday : std::uint8_t
53+
{
54+
Sunday = 0,
55+
Monday = 1,
56+
Tuesday = 2,
57+
Wednesday = 3,
58+
Thursday = 4,
59+
Friday = 5,
60+
Saturday = 6,
61+
};
62+
63+
using Weekday = Data<_Weekday, StringLiteral{"Weekday"}>;
64+
} // namespace steppable

include/types/result.hpp

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@
2222

2323
#pragma once
2424

25+
#include <output.hpp>
26+
#include <platform.hpp>
2527
#include <string>
28+
#include <util.hpp>
2629
#include <utility>
2730
#include <vector>
2831

32+
using namespace std::literals;
33+
using namespace steppable::__internals::utils;
34+
2935
/**
3036
* @namespace steppable::types
3137
* @brief The namespace containing types used in the steppable calculator.
@@ -35,7 +41,7 @@ namespace steppable::types
3541
/**
3642
* @brief The status of the calculation.
3743
*/
38-
enum class Status
44+
enum class Status : std::uint8_t
3945
{
4046
CALCULATED_SIMPLIFIED,
4147
CALCULATED_UNSIMPLIFIED,
@@ -45,7 +51,7 @@ namespace steppable::types
4551
/**
4652
* @brief The status of a boolean calculation.
4753
*/
48-
enum class StatusBool
54+
enum class StatusBool : std::uint8_t
4955
{
5056
CALCULATED_SIMPLIFIED_YES,
5157
CALCULATED_SIMPLIFIED_NO,
@@ -60,18 +66,21 @@ namespace steppable::types
6066
*
6167
* @tparam StatusType The type of the status of the calculation.
6268
*/
63-
template<typename StatusType>
69+
template<typename StatusType, typename ResultT, StringLiteral ResultTName>
6470
class ResultBase
6571
{
66-
private:
6772
/// @brief Whether the calculation is done.
6873
StatusType done;
6974

7075
/// @brief The inputs to the calculation.
7176
std::vector<std::string> inputs;
7277

7378
/// @brief The output of the calculation.
74-
std::string out;
79+
std::vector<std::string> outputs;
80+
81+
/// @brief The result of the calculation
82+
/// @attention This is different from `inputs`, as it stores only the result of the operation.
83+
ResultT result;
7584

7685
public:
7786
ResultBase() = delete;
@@ -81,26 +90,42 @@ namespace steppable::types
8190
*
8291
* @param[in] _inputs The inputs to the calculation.
8392
* @param[in] _out The output of the calculation.
93+
* @param result The result of the calculation.
8494
* @param[in] _done A flag indicating how the calculation is done.
8595
*/
86-
ResultBase(const std::vector<std::string>& _inputs, std::string _out, StatusType _done) :
87-
done(_done), inputs(_inputs), out(std::move(_out))
96+
ResultBase(const std::vector<std::string>& _inputs,
97+
std::vector<std::string> _out,
98+
ResultT result,
99+
StatusType _done) :
100+
done(_done), inputs(_inputs), outputs(std::move(_out)), result(result)
88101
{
89102
}
90103

91104
/// @brief Gets how the calculation is done.
92105
StatusType getStatus() const { return done; }
93106

107+
[[nodiscard("Result should be used")]] ResultT getResult() const { return result; }
108+
94109
/// @brief Gets the output of the calculation.
95-
std::string getOutput() const { return out; }
110+
[[nodiscard("Output should be used")]] std::string getOutput(size_t idx = 0) const
111+
{
112+
if (idx >= outputs.size())
113+
{
114+
output::error("getOutput"s, "Output index out of range"s);
115+
programSafeExit(1);
116+
}
117+
return outputs[idx];
118+
}
96119

97120
/// @brief Gets the inputs to the calculation.
98-
std::vector<std::string> getInputs() const { return inputs; }
121+
[[nodiscard("Inputs should be used")]] std::vector<std::string> getInputs() const { return inputs; }
99122
};
100123

101124
/// @brief An alias for a result of a calculation. This represents a calculation with a `Status` status.
102-
using Result = ResultBase<Status>;
125+
template<typename ResultT>
126+
using Result = ResultBase<Status, ResultT, StringLiteral{ "str" }>;
103127

104128
/// @brief An alias for a result of a boolean calculation.
105-
using ResultBool = ResultBase<StatusBool>;
129+
template<typename ResultT>
130+
using ResultBool = ResultBase<StatusBool, ResultT, StringLiteral{ "bool" }>;
106131
} // namespace steppable::types

0 commit comments

Comments
 (0)