1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

merge revision(s) 9efd590a13d1e8b8a141c46eabb48c2a1c286d2b,a55eb9a2af7950d180d9d31ffde2bce66710f44f: [Backport #17572]

Rationalize floats in coerce [Bug #17572]

	---
	 rational.c | 16 +++++++++++-----
	 1 file changed, 11 insertions(+), 5 deletions(-)

	Make reciprocal properly of non-integral rational [Bug #17572]

	---
	 rational.c                 | 2 +-
	 test/ruby/test_rational.rb | 7 +++++++
	 2 files changed, 8 insertions(+), 1 deletion(-)
This commit is contained in:
NARUSE, Yui 2021-02-02 15:47:10 +09:00
parent c09f5ee344
commit d370cb62d2
3 changed files with 20 additions and 7 deletions

View file

@ -53,6 +53,7 @@ static ID id_abs, id_integer_p,
#define f_to_s rb_obj_as_string #define f_to_s rb_obj_as_string
static VALUE nurat_to_f(VALUE self); static VALUE nurat_to_f(VALUE self);
static VALUE float_to_r(VALUE self);
inline static VALUE inline static VALUE
f_add(VALUE x, VALUE y) f_add(VALUE x, VALUE y)
@ -1174,11 +1175,17 @@ nurat_coerce(VALUE self, VALUE other)
return rb_assoc_new(other, self); return rb_assoc_new(other, self);
} }
else if (RB_TYPE_P(other, T_COMPLEX)) { else if (RB_TYPE_P(other, T_COMPLEX)) {
if (k_exact_zero_p(RCOMPLEX(other)->imag)) if (!k_exact_zero_p(RCOMPLEX(other)->imag))
return rb_assoc_new(f_rational_new_bang1
(CLASS_OF(self), RCOMPLEX(other)->real), self);
else
return rb_assoc_new(other, rb_Complex(self, INT2FIX(0))); return rb_assoc_new(other, rb_Complex(self, INT2FIX(0)));
other = RCOMPLEX(other)->real;
if (RB_FLOAT_TYPE_P(other)) {
other = float_to_r(other);
RBASIC_SET_CLASS(other, CLASS_OF(self));
}
else {
other = f_rational_new_bang1(CLASS_OF(self), other);
}
return rb_assoc_new(other, self);
} }
rb_raise(rb_eTypeError, "%s can't be coerced into %s", rb_raise(rb_eTypeError, "%s can't be coerced into %s",
@ -1870,7 +1877,7 @@ VALUE
rb_rational_reciprocal(VALUE x) rb_rational_reciprocal(VALUE x)
{ {
get_dat1(x); get_dat1(x);
return f_rational_new_no_reduce2(CLASS_OF(x), dat->den, dat->num); return nurat_convert(CLASS_OF(x), dat->den, dat->num, FALSE);
} }
/* /*
@ -2062,7 +2069,6 @@ integer_denominator(VALUE self)
return INT2FIX(1); return INT2FIX(1);
} }
static VALUE float_to_r(VALUE self);
/* /*
* call-seq: * call-seq:
* flo.numerator -> integer * flo.numerator -> integer

View file

@ -613,6 +613,13 @@ class Rational_Test < Test::Unit::TestCase
assert_nothing_raised(TypeError, '[Bug #5020] [ruby-dev:44088]') do assert_nothing_raised(TypeError, '[Bug #5020] [ruby-dev:44088]') do
Rational(1,2).coerce(Complex(1,1)) Rational(1,2).coerce(Complex(1,1))
end end
assert_raise(ZeroDivisionError) do
1 / 0r.coerce(0+0i)[0]
end
assert_raise(ZeroDivisionError) do
1 / 0r.coerce(0.0+0i)[0]
end
end end
class ObjectX class ObjectX

View file

@ -12,7 +12,7 @@
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
#define RUBY_VERSION_TEENY 0 #define RUBY_VERSION_TEENY 0
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
#define RUBY_PATCHLEVEL 26 #define RUBY_PATCHLEVEL 27
#define RUBY_RELEASE_YEAR 2021 #define RUBY_RELEASE_YEAR 2021
#define RUBY_RELEASE_MONTH 2 #define RUBY_RELEASE_MONTH 2