Skip to content

Commit

Permalink
Rework all exceptions and fix negative zeroes in RealFunctions
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Apr 27, 2024
1 parent 7671a33 commit 7d7f792
Show file tree
Hide file tree
Showing 143 changed files with 4,981 additions and 1,919 deletions.
3 changes: 3 additions & 0 deletions include/fintamath/core/IArithmetic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
#include <string>
#include <utility>

#include <fmt/core.h>

#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectClass.hpp"
#include "fintamath/core/MathObjectUtils.hpp"
#include "fintamath/core/Parser.hpp"
#include "fintamath/exceptions/InvalidInputException.hpp"

namespace fintamath {

Expand Down
8 changes: 7 additions & 1 deletion include/fintamath/core/IArithmeticCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,13 @@ class IArithmeticCRTP_ : public IArithmetic {
return cast<IArithmetic>(res->toMinimalObject());
}

throw InvalidInputBinaryOperatorException(operStr, toString(), rhs.toString());
throw InvalidInputException(fmt::format(
R"(Invalid arguments of the {} operator ({} "{}" and {} "{}" are unconvertible to each other))",
operStr,
this->getClass()->getName(),
this->toString(),
rhs.getClass()->getName(),
rhs.toString()));
}

private:
Expand Down
2 changes: 2 additions & 0 deletions include/fintamath/core/IComparable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <string>
#include <utility>

#include <fmt/core.h>

#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/MathObjectClass.hpp"
#include "fintamath/core/MathObjectUtils.hpp"
Expand Down
7 changes: 6 additions & 1 deletion include/fintamath/core/IComparableCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ class IComparableCRTP_ : public IComparable {
return 0 <=> (rhs <=> *this);
}

throw InvalidInputBinaryOperatorException("<=>", toString(), rhs.toString());
throw InvalidInputException(fmt::format(
R"(Invalid arguments of the comparison operator ({} "{}" and {} "{}" are unconvertible to each other))",
this->getClass()->getName(),
this->toString(),
rhs.getClass()->getName(),
rhs.toString()));
}

private:
Expand Down
9 changes: 8 additions & 1 deletion include/fintamath/exceptions/Exception.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
#pragma once

#include <exception>
#include <string>
#include <utility>

namespace fintamath {

class Exception : public std::exception {
public:
explicit Exception(std::string inMessage) noexcept : message(std::move(inMessage)) {}

const char *what() const noexcept override {
return "Something went wrong...";
return message.data();
}

private:
std::string message;
};

}
64 changes: 2 additions & 62 deletions include/fintamath/exceptions/InvalidInputException.hpp
Original file line number Diff line number Diff line change
@@ -1,75 +1,15 @@
#pragma once

#include <cstdint>
#include <string>
#include <vector>
#include <utility>

#include "fintamath/exceptions/Exception.hpp"

namespace fintamath {

class InvalidInputException : public Exception {
public:
InvalidInputException() noexcept = default;

explicit InvalidInputException(const std::string &input) noexcept {
content += ": " + input;
}

const char *what() const noexcept override {
return content.c_str();
}

protected:
std::string content = "Invalid input";
};

class InvalidInputFunctionException final : public InvalidInputException {
public:
explicit InvalidInputFunctionException(const std::string &func, const std::vector<std::string> &argVect) noexcept {
content += ": " + func + "(";

if (!argVect.empty()) {
for (const auto &arg : argVect) {
content += arg + ',';
}

content.pop_back();
}

content += ")";
}

const char *what() const noexcept override {
return content.c_str();
}
};

class InvalidInputBinaryOperatorException final : public InvalidInputException {
public:
explicit InvalidInputBinaryOperatorException(const std::string &oper, const std::string &lhs, const std::string &rhs) noexcept {
content += ": (" + lhs + ")" + oper + "(" + rhs + ")";
}
};

class InvalidInputUnaryOperatorException final : public InvalidInputException {
public:
enum class Type : uint8_t {
Prefix,
Postfix,
};

public:
explicit InvalidInputUnaryOperatorException(const std::string &oper, const std::string &rhs, const Type type) noexcept {
switch (type) {
case Type::Prefix:
content += ": " + oper + "(" + rhs + ")";
break;
case Type::Postfix:
content += ": (" + rhs + ")" + oper;
break;
}
}
explicit InvalidInputException(std::string inMessage) noexcept : Exception(std::move(inMessage)) {}
};

}
64 changes: 2 additions & 62 deletions include/fintamath/exceptions/UndefinedException.hpp
Original file line number Diff line number Diff line change
@@ -1,75 +1,15 @@
#pragma once

#include <cstdint>
#include <string>
#include <vector>
#include <utility>

#include "fintamath/exceptions/Exception.hpp"

namespace fintamath {

class UndefinedException : public Exception {
public:
UndefinedException() noexcept = default;

explicit UndefinedException(const std::string &input) noexcept {
content += ": " + input;
}

const char *what() const noexcept override {
return content.c_str();
}

protected:
std::string content = "Undefined";
};

class UndefinedFunctionException final : public UndefinedException {
public:
explicit UndefinedFunctionException(const std::string &func, const std::vector<std::string> &argVect) noexcept {
content += ": " + func + "(";

if (!argVect.empty()) {
for (const auto &arg : argVect) {
content += arg + ',';
}

content.pop_back();
}

content += ")";
}

const char *what() const noexcept override {
return content.c_str();
}
};

class UndefinedBinaryOperatorException final : public UndefinedException {
public:
explicit UndefinedBinaryOperatorException(const std::string &oper, const std::string &lhs, const std::string &rhs) noexcept {
content += ": (" + lhs + ")" + oper + "(" + rhs + ")";
}
};

class UndefinedUnaryOperatorException final : public UndefinedException {
public:
enum class Type : uint8_t {
Prefix,
Postfix,
};

public:
explicit UndefinedUnaryOperatorException(const std::string &oper, const std::string &rhs, const Type type) noexcept {
switch (type) {
case Type::Prefix:
content += ": " + oper + "(" + rhs + ")";
break;
case Type::Postfix:
content += ": (" + rhs + ")" + oper;
break;
}
}
explicit UndefinedException(std::string inMessage) noexcept : Exception(std::move(inMessage)) {}
};

}
18 changes: 10 additions & 8 deletions include/fintamath/expressions/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ using TermVector = std::vector<Term>;

using FunctionTermStack = std::stack<FunctionTerm>;

using OperandStack = std::stack<std::unique_ptr<IMathObject>>;
using ObjectStack = std::stack<std::unique_ptr<IMathObject>>;

}

Expand Down Expand Up @@ -103,17 +103,17 @@ class Expression : public IExpressionCRTP<Expression> {

static detail::TermVector tokensToTerms(detail::TokenVector &tokens);

static detail::OperandStack termsToOperands(detail::TermVector &terms);
static detail::ObjectStack termsToObjects(detail::TermVector &terms);

static std::unique_ptr<IMathObject> operandsToObject(detail::OperandStack &operands);
static std::unique_ptr<IMathObject> objectsToExpr(detail::ObjectStack &objects);

static std::unique_ptr<IFunction> parseFunction(const std::string &str, size_t argNum);
static std::unique_ptr<IFunction> findFunction(const std::string &str, size_t argNum);

static std::unique_ptr<IOperator> parseOperator(const std::string &str, IOperator::Priority priority);
static std::unique_ptr<IOperator> findOperator(const std::string &str, IOperator::Priority priority);

static detail::Term parseTerm(const std::string &str);

static void moveFunctionTermsToOperands(detail::OperandStack &operands, detail::FunctionTermStack &functions, const IOperator *nextOper);
static void moveFunctionTermsToObjects(detail::ObjectStack &objects, detail::FunctionTermStack &functions, const IOperator *nextOper);

static void insertMultiplications(detail::TermVector &terms);

Expand All @@ -135,15 +135,15 @@ class Expression : public IExpressionCRTP<Expression> {

static void validateFunctionArgs(const IFunction &func, const ArgumentPtrVector &args);

static bool doesArgMatch(const MathObjectClass &expectedType, const ArgumentPtr &arg);
static std::pair<MathObjectClass, bool> doesArgMatch(const MathObjectClass &expectedClass, const ArgumentPtr &arg);

static ArgumentPtrVector unwrapComma(const ArgumentPtr &child);

static ArgumentPtr compress(const ArgumentPtr &child);

friend std::unique_ptr<IMathObject> detail::makeExpr(const IFunction &func, ArgumentPtrVector args);

friend std::unique_ptr<IMathObject> parseExpr(const std::string &str);
friend std::unique_ptr<IMathObject> parseRawExpr(const std::string &str);

friend Expression approximate(const Expression &rhs, unsigned precision);

Expand All @@ -159,6 +159,8 @@ class Expression : public IExpressionCRTP<Expression> {
mutable bool isSimplified = false;
};

std::unique_ptr<IMathObject> parseRawExpr(const std::string &str);

template <typename Function>
void Expression::registerExpressionConstructor(ExpressionConstructor constructor) {
getExpressionMaker()[Function::getClassStatic()] = [maker = std::move(constructor)](ArgumentPtrVector &&args) {
Expand Down
12 changes: 0 additions & 12 deletions include/fintamath/expressions/ExpressionParser.hpp

This file was deleted.

9 changes: 4 additions & 5 deletions include/fintamath/literals/Boolean.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@ class Boolean : public ILiteralCRTP<Boolean> {
public:
Boolean();

explicit Boolean(const std::string &str);

template <std::same_as<bool> Bool>
Boolean(const Bool val) : name(val ? trueStr : falseStr) {
}
Boolean(const Bool rhs) : val(rhs) {}

explicit Boolean(std::string_view str);

std::string toString() const override;

explicit operator bool() const;

private:
std::string name;
bool val;

static constexpr std::string_view trueStr = "True";

Expand Down
4 changes: 2 additions & 2 deletions include/fintamath/literals/Variable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class Variable : public ILiteralCRTP<Variable> {
FINTAMATH_CLASS_BODY(Variable, ILiteral)

public:
explicit Variable(std::string inName);
explicit Variable(std::string_view inName);

explicit Variable(std::string inName, Integer inIndex);
explicit Variable(std::string_view inName, Integer inIndex);

std::string toString() const override;

Expand Down
3 changes: 2 additions & 1 deletion include/fintamath/numbers/Integer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <cstdint>
#include <memory>
#include <string>
#include <string_view>

#include <boost/multiprecision/fwd.hpp>
#include <boost/multiprecision/gmp.hpp>
Expand All @@ -29,7 +30,7 @@ class Integer : public INumberCRTP<Integer> {

Integer(Backend inBackend);

explicit Integer(std::string str);
explicit Integer(std::string_view str);

std::string toString() const override;

Expand Down
9 changes: 5 additions & 4 deletions include/fintamath/numbers/IntegerFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ namespace fintamath {

using FactorToCountMap = std::unordered_map<Integer, Integer>;

// Use exponentiation by squaring with constant auxiliary memory (iterative version).
// https://en.wikipedia.org/wiki/Exponentiation_by_squaring#With_constant_auxiliary_memory.
template <std::derived_from<INumber> Lhs>
Lhs pow(const Lhs &lhs, Integer rhs) {
if (lhs == 0 && rhs == 0) {
throw UndefinedBinaryOperatorException("^", lhs.toString(), rhs.toString());
throw UndefinedException("pow({}, {}) is undefined (zero to the power of zero)");
}

if (rhs < 0) {
return pow(1 / lhs, -rhs);
}

// Use exponentiation by squaring with constant auxiliary memory (iterative version).
// https://en.wikipedia.org/wiki/Exponentiation_by_squaring#With_constant_auxiliary_memory.

Lhs res(1);
Lhs sqr = lhs;

Expand Down Expand Up @@ -60,6 +61,6 @@ FactorToCountMap factors(Integer rhs, Integer limit = -1);

Integer combinations(const Integer &totalNumber, const Integer &choosedNumber);

Integer multinomialCoefficient(const Integer &totalNumber, const std::vector<Integer> &groupNumbers);
Integer multinomialCoefficient(const std::vector<Integer> &groupNumbers);

}
2 changes: 1 addition & 1 deletion include/fintamath/numbers/Rational.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Rational : public INumberCRTP<Rational> {

explicit Rational(Integer inNumer, Integer inDenom);

explicit Rational(const std::string &str);
explicit Rational(std::string_view str);

std::string toString() const override;

Expand Down
Loading

0 comments on commit 7d7f792

Please sign in to comment.