Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions ext/bigdecimal/bigdecimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2043,14 +2043,14 @@ BigDecimal_divremain(VALUE self, VALUE r, Real **dv, Real **rv)
b = GetVpValue(r, 0);
}

if (!b) return DoSomeOne(self, r, rb_intern("remainder"));
if (!b) return Qfalse;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think using Qfalse and Qtrue instead of false and true, but this pattern is frequently used in bigdecimal.c even if the function name starts with BigDecimal_.
But this pattern is used in is_zero, is_one and BigDecimal_DoDivmod.
I think it's better to refactor in a separate pull request

SAVE(b);

if (VpIsPosInf(b) || VpIsNegInf(b)) {
GUARD_OBJ(*dv, NewZeroWrapLimited(1, 1));
VpSetZero(*dv, 1);
*rv = a;
return Qnil;
return Qtrue;
}

mx = (a->MaxPrec + b->MaxPrec) *VpBaseFig();
Expand All @@ -2074,7 +2074,7 @@ BigDecimal_divremain(VALUE self, VALUE r, Real **dv, Real **rv)

*dv = d;
*rv = ff;
return Qnil;
return Qtrue;
}

/* call-seq:
Expand All @@ -2087,11 +2087,11 @@ BigDecimal_divremain(VALUE self, VALUE r, Real **dv, Real **rv)
static VALUE
BigDecimal_remainder(VALUE self, VALUE r) /* remainder */
{
VALUE f;
Real *d, *rv = 0;
f = BigDecimal_divremain(self, r, &d, &rv);
if (!NIL_P(f)) return f;
return VpCheckGetValue(rv);
if (BigDecimal_divremain(self, r, &d, &rv)) {
return VpCheckGetValue(rv);
}
return DoSomeOne(self, r, rb_intern("remainder"));
Comment on lines +2091 to +2094
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same interface as BigDecimal_DoDivmod

        if (BigDecimal_DoDivmod(self, b, &div, &mod)) {
            return BigDecimal_to_i(VpCheckGetValue(div));
        }
        return DoSomeOne(self, b, rb_intern("div"));

}

/* call-seq:
Expand Down
6 changes: 6 additions & 0 deletions test/bigdecimal/test_bigdecimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,12 @@ def test_remainder_with_rational
assert_kind_of(BigDecimal, BigDecimal("3").remainder(1.quo(3)))
end

def test_remainder_coerce
o = Object.new
def o.coerce(x); [x, BigDecimal("3")]; end
assert_equal(BigDecimal("1.1"), BigDecimal("7.1").remainder(o))
end

def test_divmod
x = BigDecimal((2**100).to_s)
assert_equal([(x / 3).floor, 1], x.divmod(3))
Expand Down