diff options
author | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2021-04-05 01:22:11 +0000 |
---|---|---|
committer | usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2021-04-05 01:22:11 +0000 |
commit | 1a38986aedf63f676efded3e834c959059d21760 (patch) | |
tree | 5539fa810727b12004aed15c8f3cc7d7cb056345 /rational.c | |
parent | a5272e643121a2e5f51f3cfb2f0899520afafd27 (diff) |
merge revision(s) 254bed302752a401b5fcc3b6c65a9c93711d91d6,fad3023e94c45e7f03478732f7641b6f39ba9d12,3156fb0f2c3ebf8229f392c8502c08fe165ab181: [Backport #17218]
Renamed `nurat_sub` compliant with `rb_rational_plus`
Fix ArithmeticSequence#last and ArithmeticSequence#each for
non-integer sequences (#3870)
[Bug #17218]
[ruby-core:100312]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_6@67936 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'rational.c')
-rw-r--r-- | rational.c | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/rational.c b/rational.c index 1038059e4f..5058ce1bfa 100644 --- a/rational.c +++ b/rational.c @@ -781,8 +781,8 @@ rb_rational_plus(VALUE self, VALUE other) * Rational(9, 8) - 4 #=> (-23/8) * Rational(20, 9) - 9.8 #=> -7.577777777777778 */ -static VALUE -nurat_sub(VALUE self, VALUE other) +VALUE +rb_rational_minus(VALUE self, VALUE other) { if (RB_INTEGER_TYPE_P(other)) { { @@ -919,8 +919,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)) @@ -972,10 +972,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)) @@ -1410,7 +1410,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); @@ -1418,6 +1418,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) else { x = rb_convert_type(x, T_RATIONAL, "Rational", "to_r"); } - return nurat_div(x, y); + return rb_rational_div(x, y); } VALUE @@ -2735,10 +2747,10 @@ Init_Rational(void) rb_define_method(rb_cRational, "-@", rb_rational_uminus, 0); rb_define_method(rb_cRational, "+", rb_rational_plus, 1); - rb_define_method(rb_cRational, "-", nurat_sub, 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); |