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

Rationalize floats in coerce [Bug #17572]

This commit is contained in:
Nobuyoshi Nakada 2021-01-23 11:04:45 +09:00
parent 9de68a5221
commit 9efd590a13
No known key found for this signature in database
GPG key ID: 7CD2805BFA3770C6

View file

@ -51,6 +51,7 @@ static ID id_abs, id_integer_p,
#define f_to_s rb_obj_as_string
static VALUE nurat_to_f(VALUE self);
static VALUE float_to_r(VALUE self);
inline static VALUE
f_add(VALUE x, VALUE y)
@ -1172,11 +1173,17 @@ nurat_coerce(VALUE self, VALUE other)
return rb_assoc_new(other, self);
}
else if (RB_TYPE_P(other, T_COMPLEX)) {
if (k_exact_zero_p(RCOMPLEX(other)->imag))
return rb_assoc_new(f_rational_new_bang1
(CLASS_OF(self), RCOMPLEX(other)->real), self);
else
if (!k_exact_zero_p(RCOMPLEX(other)->imag))
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",
@ -2060,7 +2067,6 @@ integer_denominator(VALUE self)
return INT2FIX(1);
}
static VALUE float_to_r(VALUE self);
/*
* call-seq:
* flo.numerator -> integer