mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
rb_rational_raw: make a denominator always positive
This commit is contained in:
parent
73618d84e8
commit
47465ab1cc
3 changed files with 26 additions and 0 deletions
|
@ -29,9 +29,17 @@ gcd_gmp(VALUE x, VALUE y)
|
||||||
#define gcd_gmp rb_f_notimplement
|
#define gcd_gmp rb_f_notimplement
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
s_rational_raw(VALUE klass, VALUE x, VALUE y)
|
||||||
|
{
|
||||||
|
return rb_rational_raw(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Init_rational(VALUE klass)
|
Init_rational(VALUE klass)
|
||||||
{
|
{
|
||||||
rb_define_method(rb_cInteger, "gcd_normal", gcd_normal, 1);
|
rb_define_method(rb_cInteger, "gcd_normal", gcd_normal, 1);
|
||||||
rb_define_method(rb_cInteger, "gcd_gmp", gcd_gmp, 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
|
VALUE
|
||||||
rb_rational_raw(VALUE x, VALUE y)
|
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);
|
return nurat_s_new_internal(rb_cRational, x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,4 +29,18 @@ class TestRational < Test::Unit::TestCase
|
||||||
rescue NotImplementedError
|
rescue NotImplementedError
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue