diff options
author | Kenta Murata <mrkn@users.noreply.github.com> | 2020-12-09 18:48:59 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-09 18:48:59 +0900 |
commit | fad3023e94c45e7f03478732f7641b6f39ba9d12 (patch) | |
tree | a53e562ae444a5b3f8d76308af62afd2f5c5c985 /rational.c | |
parent | cacdf2681dcc7c431973f33a18979b10566ec2f9 (diff) |
Fix ArithmeticSequence#last and ArithmeticSequence#each for non-integer sequences (#3870)
[Bug #17218]
[ruby-core:100312]
Notes
Notes:
Merged-By: mrkn <mrkn@ruby-lang.org>
Diffstat (limited to 'rational.c')
-rw-r--r-- | rational.c | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/rational.c b/rational.c index bb7c66c52b..ae134da79f 100644 --- a/rational.c +++ b/rational.c @@ -894,8 +894,8 @@ rb_rational_mul(VALUE self, VALUE other) * Rational(9, 8) / 4 #=> (9/32) * Rational(20, 9) / 9.8 #=> 0.22675736961451246 */ -static VALUE -nurat_div(VALUE self, VALUE other) +VALUE +rb_rational_div(VALUE self, VALUE other) { if (RB_INTEGER_TYPE_P(other)) { if (f_zero_p(other)) @@ -947,10 +947,10 @@ nurat_fdiv(VALUE self, VALUE other) { VALUE div; if (f_zero_p(other)) - return nurat_div(self, rb_float_new(0.0)); + return rb_rational_div(self, rb_float_new(0.0)); if (FIXNUM_P(other) && other == LONG2FIX(1)) return nurat_to_f(self); - div = nurat_div(self, other); + div = rb_rational_div(self, other); if (RB_TYPE_P(div, T_RATIONAL)) return nurat_to_f(div); if (RB_FLOAT_TYPE_P(div)) @@ -1377,7 +1377,7 @@ f_round_common(int argc, VALUE *argv, VALUE self, VALUE (*func)(VALUE)) s = (*func)(s); - s = nurat_div(f_rational_new_bang1(CLASS_OF(self), s), b); + s = rb_rational_div(f_rational_new_bang1(CLASS_OF(self), s), b); if (RB_TYPE_P(s, T_RATIONAL) && FIX2INT(rb_int_cmp(n, ONE)) < 0) s = nurat_truncate(s); @@ -1385,6 +1385,18 @@ f_round_common(int argc, VALUE *argv, VALUE self, VALUE (*func)(VALUE)) return s; } +VALUE +rb_rational_floor(VALUE self, int ndigits) +{ + if (ndigits == 0) { + return nurat_floor(self); + } + else { + VALUE n = INT2NUM(ndigits); + return f_round_common(1, &n, self, nurat_floor); + } +} + /* * call-seq: * rat.floor([ndigits]) -> integer or rational @@ -2013,7 +2025,7 @@ rb_numeric_quo(VALUE x, VALUE y) } x = rb_convert_type(x, T_RATIONAL, "Rational", "to_r"); - return nurat_div(x, y); + return rb_rational_div(x, y); } VALUE @@ -2751,8 +2763,8 @@ Init_Rational(void) rb_define_method(rb_cRational, "+", rb_rational_plus, 1); rb_define_method(rb_cRational, "-", rb_rational_minus, 1); rb_define_method(rb_cRational, "*", rb_rational_mul, 1); - rb_define_method(rb_cRational, "/", nurat_div, 1); - rb_define_method(rb_cRational, "quo", nurat_div, 1); + rb_define_method(rb_cRational, "/", rb_rational_div, 1); + rb_define_method(rb_cRational, "quo", rb_rational_div, 1); rb_define_method(rb_cRational, "fdiv", nurat_fdiv, 1); rb_define_method(rb_cRational, "**", nurat_expt, 1); |