Skip to content

Commit

Permalink
Remove Real::isNearZero
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Jul 31, 2023
1 parent 7a66a65 commit 6c54a30
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 45 deletions.
4 changes: 0 additions & 4 deletions include/fintamath/numbers/Real.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ class Real : public INumberCRTP<Real> {

int sign() const;

bool isNearZero() const;

const boost::multiprecision::cpp_dec_float_100 &getBackend() const;

static MathObjectTypeId getTypeIdStatic() {
Expand Down Expand Up @@ -75,8 +73,6 @@ class Real : public INumberCRTP<Real> {
boost::multiprecision::cpp_dec_float_100 backend;

uint8_t ouputPrecision = FINTAMATH_PRECISION;

static const boost::multiprecision::cpp_dec_float_100 DELTA;
};

}
17 changes: 1 addition & 16 deletions src/fintamath/numbers/Real.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ using namespace boost::multiprecision;

namespace fintamath {

static cpp_dec_float_100 initDelta() {
static const cpp_dec_float_100 delta = pow(cpp_dec_float_100(10), -FINTAMATH_PRECISION);
return delta;
}

const cpp_dec_float_100 Real::DELTA = initDelta();

Real::Real() = default;

Real::Real(const Real &rhs) = default;
Expand Down Expand Up @@ -125,16 +118,12 @@ int Real::sign() const {
return backend.sign();
}

bool Real::isNearZero() const {
return abs(backend) < DELTA;
}

const cpp_dec_float_100 &Real::getBackend() const {
return backend;
}

bool Real::equals(const Real &rhs) const {
return (*this - rhs).isNearZero();
return backend == rhs.backend;
}

bool Real::less(const Real &rhs) const {
Expand Down Expand Up @@ -167,10 +156,6 @@ Real &Real::multiply(const Real &rhs) {
}

Real &Real::divide(const Real &rhs) {
if (rhs.isNearZero()) {
throw UndefinedBinaryOperatorException("/", toString(), rhs.toString());
}

backend /= rhs.backend;
return *this;
}
Expand Down
27 changes: 2 additions & 25 deletions src/fintamath/numbers/RealFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,10 @@ Real abs(const Real &rhs) {
}

Real sqrt(const Real &rhs) {
if (rhs < 0) {
throw UndefinedFunctionException("sqrt", {rhs.toString()});
}

return {sqrt(rhs.getBackend())};
}

Real pow(const Real &lhs, const Real &rhs) {
if (lhs.isNearZero() && rhs.isNearZero()) {
throw UndefinedBinaryOperatorException("^", lhs.toString(), rhs.toString());
}
if (lhs < 0 && !is<Integer>(rhs.toMinimalObject())) { // TODO: do not use toMinimalObject
throw UndefinedBinaryOperatorException("^", lhs.toString(), rhs.toString());
}

cpp_dec_float_100 res(pow(lhs.getBackend(), rhs.getBackend()));

if (res.backend().isinf() || res.backend().isnan()) {
Expand Down Expand Up @@ -73,23 +62,11 @@ Real lg(const Real &rhs) {
}

Real sin(const Real &rhs) {
Real res(sin(rhs.getBackend()));

if (res.isNearZero()) {
return 0;
}

return res;
return {sin(rhs.getBackend())};
}

Real cos(const Real &rhs) {
Real res(cos(rhs.getBackend()));

if (res.isNearZero()) {
return 0;
}

return res;
return {cos(rhs.getBackend())};
}

Real tan(const Real &rhs) {
Expand Down

0 comments on commit 6c54a30

Please sign in to comment.