Skip to content

Commit

Permalink
NegativeOrZero
Browse files Browse the repository at this point in the history
  • Loading branch information
ohhmm committed Sep 16, 2024
1 parent 35aee8d commit c5872ae
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions omnn/math/Integer.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ class Integer
/// </summary>
/// <returns></returns>
Valuable IntMod_IsNegativeOrZero() const override { return ca() <= 0 ? 0 : 1; }
Valuable NegativeOrZero() const override { return IntMod_IsNegativeOrZero(); }

/// <summary>
/// Operator 'less' then value to which a param expression is to be evaluated
Expand Down
16 changes: 16 additions & 0 deletions omnn/math/Valuable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2550,6 +2550,13 @@ bool Valuable::SerializedStrEqual(const std::string_view& s) const {
return IntMod_Sign().sq();
}

Valuable Valuable::IntMod_IsNegativeOrZero() const {
if (exp)
return exp->IntMod_IsNegativeOrZero();
else
return Equals(0) || ((*this - 1) % *this).Equals(-1);
}

Valuable Valuable::IntMod_Negative() const
{
if (exp)
Expand Down Expand Up @@ -2614,6 +2621,15 @@ bool Valuable::SerializedStrEqual(const std::string_view& s) const {
// which seem less valid but lets check
}

Valuable Valuable::NegativeOrZero() const
{
if (exp)
return exp->NegativeOrZero();
else {
return Equals(Minimum(operator-()));
}
}

Valuable Valuable::Less(const Valuable& than) const
{
return LessOrEqual(than) / Equals(than);
Expand Down
9 changes: 8 additions & 1 deletion omnn/math/Valuable.h
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ class Valuable
/// (x-1)%x is x-1 for positive integers
/// </summary>
/// <returns></returns>
virtual Valuable IntMod_IsNegativeOrZero() const { return Equals(0) || ((*this - 1) % *this).Equals(-1); }
virtual Valuable IntMod_IsNegativeOrZero() const;

/// <summary>
/// Operator 'less' then value to which a param expression is to be evaluated
Expand All @@ -610,6 +610,13 @@ class Valuable
/// <returns>An expression that equals zero only when the object is less then param</returns>
virtual Valuable IntMod_Less(const Valuable& than) const;


/// <summary>
/// (this < 0) - constraint negative
/// </summary>
/// <returns>constraint to negative values: expression that equals zero for given negative *this values</returns>
virtual Valuable NegativeOrZero() const;

/// <summary>
/// Operator 'less' then value to which a param expression is to be evaluated
/// </summary>
Expand Down
44 changes: 44 additions & 0 deletions omnn/math/test/logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,50 @@ BOOST_AUTO_TEST_CASE(Min_operator_test) {
}
}

BOOST_AUTO_TEST_CASE(NegativeOrZero_operator_test) {
DECL_VARS(X);
auto NegativeOrZeroOperatorExpression = X.NegativeOrZero();
std::cout << "X<=0 : " << NegativeOrZeroOperatorExpression << std::endl;
for (auto x = 10; x--> -10;) {
auto isLessEq = x <= 0;
auto negativeOrZeroOperatorInstantiation = NegativeOrZeroOperatorExpression;
Valuable::vars_cont_t evalMap = {{X, x}};
std::cout << '\n' << x << "<=0 = ";
negativeOrZeroOperatorInstantiation.eval(evalMap);
std::cout << " expression that must be equal to zero when true: " << negativeOrZeroOperatorInstantiation
<< std::endl;

negativeOrZeroOperatorInstantiation.optimize();
std::cout << std::endl << "Is " << x << "<=0 : " << negativeOrZeroOperatorInstantiation << std::endl;
bool b = {};
auto boolLessOrEqualOp = negativeOrZeroOperatorInstantiation.ToBool();
BOOST_TEST(boolLessOrEqualOp == isLessEq);
boolLessOrEqualOp.eval(evalMap);
BOOST_TEST(boolLessOrEqualOp == isLessEq);
if (boolLessOrEqualOp == true) {
BOOST_TEST(boolLessOrEqualOp.IsInt());
BOOST_TEST(negativeOrZeroOperatorInstantiation.IsInt());
b = negativeOrZeroOperatorInstantiation.IsInt() && negativeOrZeroOperatorInstantiation.ca() == 0;
std::cout << std::endl << x << "<=0 : " << b << std::endl;
} else if (boolLessOrEqualOp == false) {
BOOST_TEST(boolLessOrEqualOp.IsInt());
b = negativeOrZeroOperatorInstantiation == 0;
std::cout << std::endl
<< x << "<=0 : " << b << ' ' << negativeOrZeroOperatorInstantiation << " != 0" << std::endl;
} else {
std::cout << std::endl << x << "<=0 : " << boolLessOrEqualOp << std::endl;
BOOST_TEST(!"boolLessOp must have boolean value");
}
BOOST_TEST(boolLessOrEqualOp == b);

auto ok = b == isLessEq;
if (!ok) {
std::cout << "X=" << x << " Y=0 " << ok << " bool: " << b << std::endl;
BOOST_TEST(ok);
}
}
}

BOOST_AUTO_TEST_CASE(LessOrEqual_operator_test) {
DECL_VARS(X, Y);
auto LE = X.LessOrEqual(Y);
Expand Down

0 comments on commit c5872ae

Please sign in to comment.