Skip to content

Commit

Permalink
Modulo for rationals
Browse files Browse the repository at this point in the history
  • Loading branch information
ohhmm committed Feb 13, 2025
1 parent 476faef commit 4ee4518
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
16 changes: 16 additions & 0 deletions omnn/math/Modulo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@ void Modulo::optimize() {
}
}

if (IsModulo()) {
if (!_1.IsInt() || !_2.IsInt()) {
if (_1.IsRational() == YesNoMaybe::Yes && _2.IsRational() == YesNoMaybe::Yes) {
auto dividend = static_cast<a_rational>(_1);
auto divisor = static_cast<a_rational>(_2);
auto common_denominator = boost::lcm(boost::multiprecision::denominator(dividend),
boost::multiprecision::denominator(divisor));
dividend *= common_denominator;
divisor *= common_denominator;
Valuable result = boost::multiprecision::numerator(dividend) % boost::multiprecision::numerator(divisor);
result /= Valuable(std::move(common_denominator));
Become(std::move(result));
}
}
}

CHECK_OPTIMIZATION_CACHE
if (IsModulo()) {
hash = _1.Hash() ^ _2.Hash();
Expand Down
4 changes: 4 additions & 0 deletions omnn/math/test/Modulo_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ BOOST_AUTO_TEST_CASE(NegativeFractionModulo_test) {
_2 = -700;
mod = _1 % _2;
BOOST_TEST(mod == _1);

_1 = "((-1) % ((2 / 3)))"_v.Optimized();
_2 = "-1/3"_v;
BOOST_TEST(_1 == _2);
}

BOOST_AUTO_TEST_CASE(ModIntLess_comparator_test) {
Expand Down

0 comments on commit 4ee4518

Please sign in to comment.