Skip to content

Commit

Permalink
INumber::getPrecision instead of setPrecision, renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed May 5, 2024
1 parent 32e3ea6 commit 32a5143
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 113 deletions.
2 changes: 1 addition & 1 deletion include/fintamath/expressions/ExpressionFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@ Expression complexInf();

Expression solve(const Expression &rhs);

extern Expression approximate(const Expression &rhs, unsigned precision = Real::getPrecision());
extern Expression approximate(const Expression &rhs, unsigned precision = Real::getPrecisionStatic());

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

#include "fintamath/core/IMathObject.hpp"
Expand Down Expand Up @@ -48,7 +49,7 @@ class Complex : public INumberCRTP<Complex> {

std::unique_ptr<IMathObject> toMinimalObject() const override;

bool isPrecise() const noexcept override;
std::optional<unsigned> getPrecision() const noexcept override;

bool isComplex() const noexcept override;

Expand Down
5 changes: 3 additions & 2 deletions include/fintamath/numbers/INumber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <concepts>
#include <memory>
#include <optional>
#include <string>
#include <utility>

Expand All @@ -17,8 +18,8 @@ class INumber : public IComparable {
FINTAMATH_PARENT_CLASS_BODY(INumber, IComparable)

public:
virtual bool isPrecise() const noexcept {
return true;
virtual std::optional<unsigned> getPrecision() const noexcept {
return {};
}

virtual bool isComplex() const noexcept {
Expand Down
16 changes: 7 additions & 9 deletions include/fintamath/numbers/Real.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Real : public INumberCRTP<Real> {
using Backend = boost::multiprecision::mpfr_float;

struct ScopedSetPrecision final {
unsigned currPrecision = getPrecision();
unsigned currPrecision = getPrecisionStatic();

public:
explicit ScopedSetPrecision(unsigned precision);
Expand Down Expand Up @@ -56,8 +56,6 @@ class Real : public INumberCRTP<Real> {

std::string toString(unsigned precision) const;

bool isPrecise() const noexcept override;

int sign() const;

bool isZero() const;
Expand All @@ -68,15 +66,15 @@ class Real : public INumberCRTP<Real> {

const Backend &getBackend() const noexcept;

unsigned getOutputPrecision() const noexcept;
std::optional<unsigned> getPrecision() const noexcept override;

void setOutputPrecision(unsigned precision);
void setPrecision(unsigned precision);

static unsigned getCalculationPrecision() noexcept;
static unsigned getCalculationPrecisionStatic() noexcept;

static unsigned getPrecision() noexcept;
static unsigned getPrecisionStatic() noexcept;

static void setPrecision(unsigned precision);
static void setPrecisionStatic(unsigned precision);

protected:
bool equals(const Real &rhs) const override;
Expand Down Expand Up @@ -107,7 +105,7 @@ class Real : public INumberCRTP<Real> {
private:
Backend backend;

unsigned outputPrecision = getPrecision();
unsigned outputPrecision = getPrecisionStatic();
};

}
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/config/PrecisionConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace fintamath::detail {

PrecisionConfig::PrecisionConfig() {
constexpr unsigned defaultPrecision = 20;
Real::setPrecision(defaultPrecision);
Real::setPrecisionStatic(defaultPrecision);
}

}
10 changes: 5 additions & 5 deletions src/fintamath/expressions/IExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void IExpression::preSimplifyChild(ArgumentPtr &child) {
if (const auto constChild = cast<IConstant>(child)) {
const ArgumentPtr constVal = (*constChild)();

if (const auto num = cast<INumber>(constVal); num && !num->isPrecise()) {
if (const auto num = cast<INumber>(constVal); num && num->getPrecision()) {
child = constChild;
}
else {
Expand Down Expand Up @@ -205,7 +205,7 @@ std::unique_ptr<INumber> IExpression::convertToApproximated(const INumber &num,
const Integer & /*inPrecision*/,
const Integer & /*inMaxInt*/) {
auto res = cast<Real>(inRhs.clone());
res->setOutputPrecision(Real::getPrecision());
res->setPrecision(Real::getPrecisionStatic());
return res;
});

Expand Down Expand Up @@ -261,7 +261,7 @@ ArgumentPtr IExpression::callFunction(const IFunction &func, const ArgumentPtrVe
for (const auto &argPtr : argPtrs) {
args.emplace_back(*argPtr);

if (const auto num = cast<INumber>(argPtr); num && !num->isPrecise()) {
if (const auto num = cast<INumber>(argPtr); num && num->getPrecision()) {
areArgumentsPrecise = false;
}
}
Expand All @@ -273,7 +273,7 @@ ArgumentPtr IExpression::callFunction(const IFunction &func, const ArgumentPtrVe
ArgumentPtr res = func(args);

if (areArgumentsPrecise) {
if (const auto num = cast<INumber>(res); num && !num->isPrecise()) {
if (const auto num = cast<INumber>(res); num && num->getPrecision()) {
return {};
}
}
Expand Down Expand Up @@ -309,7 +309,7 @@ ArgumentPtr IExpression::approximate() const {
if (const auto childNum = cast<INumber>(child)) {
numberChildrenCount++;

if (!childNum->isPrecise()) {
if (childNum->getPrecision()) {
areNumberChilrenPrecise = false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/functions/logarithms/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ std::unique_ptr<IMathObject> Log::call(const ArgumentRefVector &argVect) const {
return Integer(0).clone();
}

if (lhs == rhs && lhs.isPrecise()) {
if (lhs == rhs && !lhs.getPrecision()) {
return Integer(1).clone();
}

Expand Down
19 changes: 17 additions & 2 deletions src/fintamath/numbers/Complex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,23 @@ std::unique_ptr<IMathObject> Complex::toMinimalObject() const {
return clone();
}

bool Complex::isPrecise() const noexcept {
return !is<Real>(re) && !is<Real>(im);
std::optional<unsigned> Complex::getPrecision() const noexcept {
std::optional<unsigned> rePrecision = re->getPrecision();
std::optional<unsigned> imPrecision = im->getPrecision();

if (rePrecision && imPrecision) {
return std::min(*rePrecision, *imPrecision);
}

if (rePrecision) {
return *rePrecision;
}

if (imPrecision) {
return *imPrecision;
}

return {};
}

bool Complex::isComplex() const noexcept {
Expand Down
4 changes: 2 additions & 2 deletions src/fintamath/numbers/NumberAbstract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ std::unique_ptr<IArithmetic> Rational::divideAbstract(const IArithmetic &rhs) co
//-------------------------------------------------------------------------------------//

bool Real::equalsAbstract(const IMathObject &rhs) const {
if (const auto *rhsNum = cast<INumber>(&rhs); rhsNum && rhsNum->isPrecise()) {
if (const auto *rhsNum = cast<INumber>(&rhs); rhsNum && !rhsNum->getPrecision()) {
return false;
}

Expand All @@ -65,7 +65,7 @@ std::strong_ordering Real::compareAbstract(const IComparable &rhs) const {
const auto res = INumberCRTP::compareAbstract(rhs);

if (res == std::strong_ordering::equal) {
if (const auto *rhsNum = cast<INumber>(&rhs); rhsNum && rhsNum->isPrecise()) {
if (const auto *rhsNum = cast<INumber>(&rhs); rhsNum && !rhsNum->getPrecision()) {
return sign() != 0 ? sign() <=> 0 : std::strong_ordering::greater;
}
}
Expand Down
20 changes: 8 additions & 12 deletions src/fintamath/numbers/Real.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ std::string Real::toString(unsigned precision) const {
return str;
}

bool Real::isPrecise() const noexcept {
return false;
}

int Real::sign() const {
if (mpfr_signbit(backend.backend().data())) {
return -1;
Expand All @@ -152,24 +148,24 @@ const Real::Backend &Real::getBackend() const noexcept {
return backend;
}

unsigned Real::getOutputPrecision() const noexcept {
std::optional<unsigned> Real::getPrecision() const noexcept {
return outputPrecision;
}

void Real::setOutputPrecision(const unsigned precision) {
void Real::setPrecision(const unsigned precision) {
assert(precision <= outputPrecision);
outputPrecision = precision;
}

unsigned Real::getCalculationPrecision() noexcept {
unsigned Real::getCalculationPrecisionStatic() noexcept {
return Backend::thread_default_precision();
}

unsigned Real::getPrecision() noexcept {
return (getCalculationPrecision() - precisionDelta) / precisionMultiplier;
unsigned Real::getPrecisionStatic() noexcept {
return (getCalculationPrecisionStatic() - precisionDelta) / precisionMultiplier;
}

void Real::setPrecision(unsigned precision) {
void Real::setPrecisionStatic(unsigned precision) {
if (precision == 0) {
precision++;
}
Expand Down Expand Up @@ -258,11 +254,11 @@ void Real::updatePrecision(const Real &rhs) {
}

Real::ScopedSetPrecision::ScopedSetPrecision(const unsigned precision) {
setPrecision(precision);
setPrecisionStatic(precision);
}

Real::ScopedSetPrecision::~ScopedSetPrecision() {
setPrecision(currPrecision);
setPrecisionStatic(currPrecision);
}

}
10 changes: 5 additions & 5 deletions src/fintamath/numbers/RealFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ bool isOverflow(const Real &rhs) {
return pow(powBase, precision);
});

return abs(rhs) > cache[Real::getPrecision()];
return abs(rhs) > cache[Real::getPrecisionStatic()];
}

bool isUnderflow(const Real &rhs) {
Expand All @@ -38,15 +38,15 @@ bool isUnderflow(const Real &rhs) {
return 1 / pow(powBase, precision);
});

return !rhs.isZero() && abs(rhs) < cache[Real::getPrecision()];
return !rhs.isZero() && abs(rhs) < cache[Real::getPrecisionStatic()];
}

bool isLogUnderflow(const Real &rhs) {
static Cache<unsigned, Real::Backend> cache([](const unsigned precision) {
return 1 / pow(precision, getE().getBackend());
});

return !rhs.isZero() && abs(rhs) < cache[Real::getPrecision()];
return !rhs.isZero() && abs(rhs) < cache[Real::getPrecisionStatic()];
}

std::string getExceptionMessage(const std::string_view message) {
Expand Down Expand Up @@ -517,7 +517,7 @@ const Real &getE() {
return Real(res);
});

return cache[Real::getCalculationPrecision()];
return cache[Real::getCalculationPrecisionStatic()];
}

const Real &getPi() {
Expand All @@ -527,7 +527,7 @@ const Real &getPi() {
return Real(res);
});

return cache[Real::getCalculationPrecision()];
return cache[Real::getCalculationPrecisionStatic()];
}

}
28 changes: 21 additions & 7 deletions tests/src/numbers/ComplexTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -975,13 +975,27 @@ TEST(ComplexTests, simplifyTest) {
EXPECT_EQ(Complex(Real("5.2"), Integer(0)).toMinimalObject()->toString(), "5.2");
}

TEST(ComplexTests, isPreciseTest) {
EXPECT_TRUE(Complex(1, 2).isPrecise());
EXPECT_TRUE(Complex(Rational(1, 2), Rational(1, 2)).isPrecise());

EXPECT_FALSE(Complex(Real(1), Real(1)).isPrecise());
EXPECT_FALSE(Complex(Real(1), Integer(1)).isPrecise());
EXPECT_FALSE(Complex(Integer(1), Real(1)).isPrecise());
TEST(ComplexTests, getPrecisionTest) {
EXPECT_FALSE(Complex(1, 2).getPrecision());
EXPECT_FALSE(Complex(Rational(1, 2), Rational(1, 2)).getPrecision());

EXPECT_EQ(*Complex(Real(1), Real(1)).getPrecision(), Real::getPrecisionStatic());
EXPECT_EQ(*Complex(Real(1), Integer(1)).getPrecision(), Real::getPrecisionStatic());
EXPECT_EQ(*Complex(Integer(1), Real(1)).getPrecision(), Real::getPrecisionStatic());

{
Real a = 1;
Real b = 2;
a.setPrecision(5);
EXPECT_EQ(*Complex(a, b).getPrecision(), 5);
}

{
Real a = 1;
Real b = 2;
b.setPrecision(5);
EXPECT_EQ(*Complex(a, b).getPrecision(), 5);
}
}

TEST(ComplexTests, isComplexTest) {
Expand Down
4 changes: 2 additions & 2 deletions tests/src/numbers/IntegerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,8 @@ TEST(IntegerTests, intOperatorTest) {
EXPECT_EQ(static_cast<int64_t>(Integer("-100000000000000000000000000000000000000000000000000")) + 1, -9223372036854775807);
}

TEST(IntegerTests, isPreciseTest) {
EXPECT_TRUE(Integer(1).isPrecise());
TEST(IntegerTests, getPrecisionTest) {
EXPECT_FALSE(Integer(1).getPrecision());
}

TEST(IntegerTests, isComplexTest) {
Expand Down
4 changes: 2 additions & 2 deletions tests/src/numbers/RationalTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,8 @@ TEST(RationalTests, signTest) {
EXPECT_EQ(Rational(2).sign(), 1);
}

TEST(RationalTests, isPreciseTest) {
EXPECT_TRUE(Rational(1, 2).isPrecise());
TEST(RationalTests, getPrecisionTest) {
EXPECT_FALSE(Rational(1, 2).getPrecision());
}

TEST(RationalTests, isComplexTest) {
Expand Down
Loading

0 comments on commit 32a5143

Please sign in to comment.