зеркало из https://github.com/github/ruby.git
rb_rational_raw: make a denominator always positive
This commit is contained in:
Родитель
73618d84e8
Коммит
47465ab1cc
|
@ -29,9 +29,17 @@ gcd_gmp(VALUE x, VALUE y)
|
|||
#define gcd_gmp rb_f_notimplement
|
||||
#endif
|
||||
|
||||
static VALUE
|
||||
s_rational_raw(VALUE klass, VALUE x, VALUE y)
|
||||
{
|
||||
return rb_rational_raw(x, y);
|
||||
}
|
||||
|
||||
void
|
||||
Init_rational(VALUE klass)
|
||||
{
|
||||
rb_define_method(rb_cInteger, "gcd_normal", gcd_normal, 1);
|
||||
rb_define_method(rb_cInteger, "gcd_gmp", gcd_gmp, 1);
|
||||
|
||||
rb_define_singleton_method(rb_cRational, "raw", s_rational_raw, 2);
|
||||
}
|
||||
|
|
|
@ -1927,6 +1927,10 @@ rb_gcdlcm(VALUE self, VALUE other)
|
|||
VALUE
|
||||
rb_rational_raw(VALUE x, VALUE y)
|
||||
{
|
||||
if (INT_NEGATIVE_P(y)) {
|
||||
x = rb_int_uminus(x);
|
||||
y = rb_int_uminus(y);
|
||||
}
|
||||
return nurat_s_new_internal(rb_cRational, x, y);
|
||||
}
|
||||
|
||||
|
|
|
@ -29,4 +29,18 @@ class TestRational < Test::Unit::TestCase
|
|||
rescue NotImplementedError
|
||||
end
|
||||
end
|
||||
|
||||
def test_rb_rational_raw
|
||||
rat = Rational.raw(1, 2)
|
||||
assert_equal(1, rat.numerator)
|
||||
assert_equal(2, rat.denominator)
|
||||
|
||||
rat = Rational.raw(-1, 2)
|
||||
assert_equal(-1, rat.numerator)
|
||||
assert_equal(2, rat.denominator)
|
||||
|
||||
rat = Rational.raw(1, -2)
|
||||
assert_equal(-1, rat.numerator)
|
||||
assert_equal(2, rat.denominator)
|
||||
end
|
||||
end
|
||||
|
|
Загрузка…
Ссылка в новой задаче