Skip to content

Commit

Permalink
Improvements to power and multiply (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZCG-coder authored Feb 2, 2025
2 parents e4f2173 + df2a7fb commit 3edb3da
Show file tree
Hide file tree
Showing 77 changed files with 786 additions and 354 deletions.
10 changes: 6 additions & 4 deletions .idea/editor.xml

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

1 change: 1 addition & 0 deletions .idea/inspectionProfiles/Project_Default.xml

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

3 changes: 0 additions & 3 deletions .idea/vcs.xml

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

1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ PROJECT(Steppable)

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

FIND_PACKAGE(
Python
Expand Down
16 changes: 10 additions & 6 deletions include/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
namespace steppable::constants
{
/// @brief 100 digits of pi.
extern const std::string_view& PI;
constexpr const std::string_view PI =
"3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679";

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

/// @brief Pi divided by 2.
// Generated using Python:
Expand All @@ -55,7 +56,8 @@ namespace steppable::constants
// 5 | 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
// 6 | ) / Decimal(2)
// -------------------------------------------------------
extern const std::string_view& PI_OVER_2;
constexpr const std::string_view PI_OVER_2 =
"1.570796326794896619231321691639751442098584699687552910487472296153908203143104499314017412835292542";

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

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

extern const std::string_view& E;
constexpr const std::string_view E = "2.718281828459045090795598298427648842334747314453125";
} // namespace steppable::constants
4 changes: 2 additions & 2 deletions include/factors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace steppable::__internals::numUtils
* @param[in] base The base of the root.
* @return A result object containing the largest root factor of the number.
*/
ResultBool getRootFactor(const std::string& _number, const std::string& base = "2");
ResultBool<std::string> getRootFactor(const std::string& _number, const std::string& base = "2");

/**
* @brief Get the greatest root number less than or equal to the given number.
Expand All @@ -73,5 +73,5 @@ namespace steppable::__internals::numUtils
* @return StatusBool::CALCULATED_SIMPLIFIED_YES if the number is a root number,
* StatusBool::CALCULATED_SIMPLIFIED_NO otherwise.
*/
ResultBool isRoot(const std::string& _number, const std::string& base);
ResultBool<std::string> isRoot(const std::string& _number, const std::string& base);
} // namespace steppable::__internals::numUtils
10 changes: 6 additions & 4 deletions include/fn/calc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@
#include "output.hpp"

#include <string>
#include <util.hpp>

using namespace std::literals;

/**
* @namespace steppable::__internals
* @brief The namespace containing internal functions for the Steppable library.
*/
namespace steppable::__internals::arithmetic
namespace steppable::__internals::calc
{
/**
* @brief Represents the quotient and remainder of a division operation.
Expand Down Expand Up @@ -154,17 +155,18 @@ namespace steppable::__internals::arithmetic
* @param[in] steps The number of steps to perform the multiplication.
* @return The product of the two numbers as a string.
*/
std::string multiply(const std::string& a, const std::string& b, int steps = 2);
std::string multiply(const std::string& a, const std::string& b, int steps = 2, int decimals = MAX_DECIMALS);

/**
* @brief Raises a string representation of a number to a power.
*
* @param[in] _number The string representation of the number.
* @param[in] raiseTo The string representation of the power to raise the number to.
* @param[in] steps The number of steps to perform the power operation.
* @param[in] decimals The number of decimals to output from the power operation.
* @return The result of the power operation as a string.
*/
std::string power(const std::string& _number, const std::string& raiseTo, int steps = 2);
std::string power(const std::string& _number, const std::string& raiseTo, int steps = 2, int decimals = 8);

/**
* @brief Calculates e^x. Shorthand of power(x, E, 0);
Expand Down Expand Up @@ -572,4 +574,4 @@ namespace steppable::__internals::arithmetic
}
}

} // namespace steppable::__internals::arithmetic
} // namespace steppable::__internals::calc
4 changes: 2 additions & 2 deletions include/fn/root.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#include <string>

namespace steppable::__internals::arithmetic
namespace steppable::__internals::calc
{
/**
* @brief A struct to represent a surd.
Expand All @@ -38,4 +38,4 @@ namespace steppable::__internals::arithmetic
/// @brief The multiplier of the surd.
std::string multiplier;
};
} // namespace steppable::__internals::arithmetic
} // namespace steppable::__internals::calc
2 changes: 1 addition & 1 deletion include/testing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace steppable::testing
* @brief Constructs a new TestCase object with the given name.
* @param[in] testCaseName The name of the test case.
*/
explicit TestCase(const std::string& testCaseName);
explicit TestCase(std::string testCaseName);

/**
* @brief Asserts that two strings are equal.
Expand Down
64 changes: 64 additions & 0 deletions include/types/data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**************************************************************************************************
* Copyright (c) 2023-2025 NWSOFT *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy *
* of this software and associated documentation files (the "Software"), to deal *
* in the Software without restriction, including without limitation the rights *
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in all *
* copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
* SOFTWARE. *
**************************************************************************************************/

#pragma once

#include <string>

#include "util.hpp"

using namespace std::literals;
using namespace steppable::__internals::utils;

namespace steppable
{
template<typename BaseT, StringLiteral BaseTName>
class Data
{
BaseT value;

public:
std::string getDataType() { return BaseTName.value; }

Data(const BaseT object) : value(object) {} // NOLINT(*-explicit-constructor)
Data() : value() {}

Data& operator=(const BaseT& object)
{
value = object;
return *this;
}
};

enum class _Weekday : std::uint8_t
{
Sunday = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6,
};

using Weekday = Data<_Weekday, StringLiteral{"Weekday"}>;
} // namespace steppable
47 changes: 36 additions & 11 deletions include/types/result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@

#pragma once

#include <output.hpp>
#include <platform.hpp>
#include <string>
#include <util.hpp>
#include <utility>
#include <vector>

using namespace std::literals;
using namespace steppable::__internals::utils;

/**
* @namespace steppable::types
* @brief The namespace containing types used in the steppable calculator.
Expand All @@ -35,7 +41,7 @@ namespace steppable::types
/**
* @brief The status of the calculation.
*/
enum class Status
enum class Status : std::uint8_t
{
CALCULATED_SIMPLIFIED,
CALCULATED_UNSIMPLIFIED,
Expand All @@ -45,7 +51,7 @@ namespace steppable::types
/**
* @brief The status of a boolean calculation.
*/
enum class StatusBool
enum class StatusBool : std::uint8_t
{
CALCULATED_SIMPLIFIED_YES,
CALCULATED_SIMPLIFIED_NO,
Expand All @@ -60,18 +66,21 @@ namespace steppable::types
*
* @tparam StatusType The type of the status of the calculation.
*/
template<typename StatusType>
template<typename StatusType, typename ResultT, StringLiteral ResultTName>
class ResultBase
{
private:
/// @brief Whether the calculation is done.
StatusType done;

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

/// @brief The output of the calculation.
std::string out;
std::vector<std::string> outputs;

/// @brief The result of the calculation
/// @attention This is different from `inputs`, as it stores only the result of the operation.
ResultT result;

public:
ResultBase() = delete;
Expand All @@ -81,26 +90,42 @@ namespace steppable::types
*
* @param[in] _inputs The inputs to the calculation.
* @param[in] _out The output of the calculation.
* @param result The result of the calculation.
* @param[in] _done A flag indicating how the calculation is done.
*/
ResultBase(const std::vector<std::string>& _inputs, std::string _out, StatusType _done) :
done(_done), inputs(_inputs), out(std::move(_out))
ResultBase(const std::vector<std::string>& _inputs,
std::vector<std::string> _out,
ResultT result,
StatusType _done) :
done(_done), inputs(_inputs), outputs(std::move(_out)), result(result)
{
}

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

[[nodiscard("Result should be used")]] ResultT getResult() const { return result; }

/// @brief Gets the output of the calculation.
std::string getOutput() const { return out; }
[[nodiscard("Output should be used")]] std::string getOutput(size_t idx = 0) const
{
if (idx >= outputs.size())
{
output::error("getOutput"s, "Output index out of range"s);
programSafeExit(1);
}
return outputs[idx];
}

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

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

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

0 comments on commit 3edb3da

Please sign in to comment.