mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
rational.c: refactoring
* rational.c: use RB_INTEGER_TYPE_P, RB_FLOAT_TYPE_P, and FIXNUM_ZERO_P. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56761 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
e7a7583029
commit
b5be78c6bf
1 changed files with 18 additions and 18 deletions
36
rational.c
36
rational.c
|
@ -64,9 +64,9 @@ f_##n(VALUE x, VALUE y)\
|
||||||
inline static VALUE
|
inline static VALUE
|
||||||
f_add(VALUE x, VALUE y)
|
f_add(VALUE x, VALUE y)
|
||||||
{
|
{
|
||||||
if (FIXNUM_P(y) && FIX2LONG(y) == 0)
|
if (FIXNUM_P(y) && FIXNUM_ZERO_P(y))
|
||||||
return x;
|
return x;
|
||||||
else if (FIXNUM_P(x) && FIX2LONG(x) == 0)
|
else if (FIXNUM_P(x) && FIXNUM_ZERO_P(x))
|
||||||
return y;
|
return y;
|
||||||
return rb_funcall(x, '+', 1, y);
|
return rb_funcall(x, '+', 1, y);
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ f_div(VALUE x, VALUE y)
|
||||||
{
|
{
|
||||||
if (FIXNUM_P(y) && FIX2LONG(y) == 1)
|
if (FIXNUM_P(y) && FIX2LONG(y) == 1)
|
||||||
return x;
|
return x;
|
||||||
if (FIXNUM_P(x) || RB_TYPE_P(x, T_BIGNUM))
|
if (RB_INTEGER_TYPE_P(x))
|
||||||
return rb_int_div(x, y);
|
return rb_int_div(x, y);
|
||||||
return rb_funcall(x, '/', 1, y);
|
return rb_funcall(x, '/', 1, y);
|
||||||
}
|
}
|
||||||
|
@ -124,7 +124,7 @@ f_mul(VALUE x, VALUE y)
|
||||||
inline static VALUE
|
inline static VALUE
|
||||||
f_sub(VALUE x, VALUE y)
|
f_sub(VALUE x, VALUE y)
|
||||||
{
|
{
|
||||||
if (FIXNUM_P(y) && FIX2LONG(y) == 0)
|
if (FIXNUM_P(y) && FIXNUM_ZERO_P(y))
|
||||||
return x;
|
return x;
|
||||||
return rb_funcall(x, '-', 1, y);
|
return rb_funcall(x, '-', 1, y);
|
||||||
}
|
}
|
||||||
|
@ -132,7 +132,7 @@ f_sub(VALUE x, VALUE y)
|
||||||
inline static VALUE
|
inline static VALUE
|
||||||
f_abs(VALUE x)
|
f_abs(VALUE x)
|
||||||
{
|
{
|
||||||
if (FIXNUM_P(x) || RB_TYPE_P(x, T_BIGNUM))
|
if (RB_INTEGER_TYPE_P(x))
|
||||||
return rb_int_abs(x);
|
return rb_int_abs(x);
|
||||||
return rb_funcall(x, id_abs, 0);
|
return rb_funcall(x, id_abs, 0);
|
||||||
}
|
}
|
||||||
|
@ -164,12 +164,12 @@ inline static int
|
||||||
f_zero_p(VALUE x)
|
f_zero_p(VALUE x)
|
||||||
{
|
{
|
||||||
if (RB_INTEGER_TYPE_P(x)) {
|
if (RB_INTEGER_TYPE_P(x)) {
|
||||||
return x == LONG2FIX(0);
|
return FIXNUM_ZERO_P(x);
|
||||||
}
|
}
|
||||||
else if (RB_TYPE_P(x, T_RATIONAL)) {
|
else if (RB_TYPE_P(x, T_RATIONAL)) {
|
||||||
VALUE num = RRATIONAL(x)->num;
|
VALUE num = RRATIONAL(x)->num;
|
||||||
|
|
||||||
return num == LONG2FIX(0);
|
return FIXNUM_ZERO_P(num);
|
||||||
}
|
}
|
||||||
return RTEST(rb_funcall(x, id_eqeq_p, 1, ZERO));
|
return RTEST(rb_funcall(x, id_eqeq_p, 1, ZERO));
|
||||||
}
|
}
|
||||||
|
@ -317,7 +317,7 @@ f_gcd_normal(VALUE x, VALUE y)
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (FIXNUM_P(x)) {
|
if (FIXNUM_P(x)) {
|
||||||
if (FIX2LONG(x) == 0)
|
if (FIXNUM_ZERO_P(x))
|
||||||
return y;
|
return y;
|
||||||
if (FIXNUM_P(y))
|
if (FIXNUM_P(y))
|
||||||
return LONG2NUM(i_gcd(FIX2LONG(x), FIX2LONG(y)));
|
return LONG2NUM(i_gcd(FIX2LONG(x), FIX2LONG(y)));
|
||||||
|
@ -737,7 +737,7 @@ rb_rational_plus(VALUE self, VALUE other)
|
||||||
dat->den);
|
dat->den);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (RB_TYPE_P(other, T_FLOAT)) {
|
else if (RB_FLOAT_TYPE_P(other)) {
|
||||||
return DBL2NUM(nurat_to_double(self) + RFLOAT_VALUE(other));
|
return DBL2NUM(nurat_to_double(self) + RFLOAT_VALUE(other));
|
||||||
}
|
}
|
||||||
else if (RB_TYPE_P(other, T_RATIONAL)) {
|
else if (RB_TYPE_P(other, T_RATIONAL)) {
|
||||||
|
@ -1040,7 +1040,7 @@ nurat_expt(VALUE self, VALUE other)
|
||||||
rb_warn("in a**b, b may be too big");
|
rb_warn("in a**b, b may be too big");
|
||||||
return rb_float_pow(nurat_to_f(self), other);
|
return rb_float_pow(nurat_to_f(self), other);
|
||||||
}
|
}
|
||||||
else if (RB_TYPE_P(other, T_FLOAT) || RB_TYPE_P(other, T_RATIONAL)) {
|
else if (RB_FLOAT_TYPE_P(other) || RB_TYPE_P(other, T_RATIONAL)) {
|
||||||
return rb_float_pow(nurat_to_f(self), other);
|
return rb_float_pow(nurat_to_f(self), other);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -1132,8 +1132,8 @@ nurat_eqeq_p(VALUE self, VALUE other)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (RB_FLOAT_TYPE_P(other)) {
|
else if (RB_FLOAT_TYPE_P(other)) {
|
||||||
return f_boolcast(rb_dbl_cmp(nurat_to_double(self), RFLOAT_VALUE(other))
|
const double d = nurat_to_double(self);
|
||||||
== INT2FIX(0));
|
return f_boolcast(FIXNUM_ZERO_P(rb_dbl_cmp(d, RFLOAT_VALUE(other))));
|
||||||
}
|
}
|
||||||
else if (RB_TYPE_P(other, T_RATIONAL)) {
|
else if (RB_TYPE_P(other, T_RATIONAL)) {
|
||||||
{
|
{
|
||||||
|
@ -1905,7 +1905,7 @@ numeric_denominator(VALUE self)
|
||||||
static VALUE
|
static VALUE
|
||||||
numeric_quo(VALUE x, VALUE y)
|
numeric_quo(VALUE x, VALUE y)
|
||||||
{
|
{
|
||||||
if (RB_TYPE_P(y, T_FLOAT)) {
|
if (RB_FLOAT_TYPE_P(y)) {
|
||||||
return rb_funcall(x, rb_intern("fdiv"), 1, y);
|
return rb_funcall(x, rb_intern("fdiv"), 1, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2414,7 +2414,7 @@ string_to_r_strict(VALUE self)
|
||||||
self);
|
self);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (RB_TYPE_P(num, T_FLOAT))
|
if (RB_FLOAT_TYPE_P(num))
|
||||||
rb_raise(rb_eFloatDomainError, "Infinity");
|
rb_raise(rb_eFloatDomainError, "Infinity");
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
@ -2463,7 +2463,7 @@ string_to_r(VALUE self)
|
||||||
|
|
||||||
(void)parse_rat(s, 0, &num);
|
(void)parse_rat(s, 0, &num);
|
||||||
|
|
||||||
if (RB_TYPE_P(num, T_FLOAT))
|
if (RB_FLOAT_TYPE_P(num))
|
||||||
rb_raise(rb_eFloatDomainError, "Infinity");
|
rb_raise(rb_eFloatDomainError, "Infinity");
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
@ -2475,7 +2475,7 @@ rb_cstr_to_rat(const char *s, int strict) /* for complex's internal */
|
||||||
|
|
||||||
(void)parse_rat(s, strict, &num);
|
(void)parse_rat(s, strict, &num);
|
||||||
|
|
||||||
if (RB_TYPE_P(num, T_FLOAT))
|
if (RB_FLOAT_TYPE_P(num))
|
||||||
rb_raise(rb_eFloatDomainError, "Infinity");
|
rb_raise(rb_eFloatDomainError, "Infinity");
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
@ -2503,14 +2503,14 @@ nurat_s_convert(int argc, VALUE *argv, VALUE klass)
|
||||||
backref = rb_backref_get();
|
backref = rb_backref_get();
|
||||||
rb_match_busy(backref);
|
rb_match_busy(backref);
|
||||||
|
|
||||||
if (RB_TYPE_P(a1, T_FLOAT)) {
|
if (RB_FLOAT_TYPE_P(a1)) {
|
||||||
a1 = float_to_r(a1);
|
a1 = float_to_r(a1);
|
||||||
}
|
}
|
||||||
else if (RB_TYPE_P(a1, T_STRING)) {
|
else if (RB_TYPE_P(a1, T_STRING)) {
|
||||||
a1 = string_to_r_strict(a1);
|
a1 = string_to_r_strict(a1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (RB_TYPE_P(a2, T_FLOAT)) {
|
if (RB_FLOAT_TYPE_P(a2)) {
|
||||||
a2 = float_to_r(a2);
|
a2 = float_to_r(a2);
|
||||||
}
|
}
|
||||||
else if (RB_TYPE_P(a2, T_STRING)) {
|
else if (RB_TYPE_P(a2, T_STRING)) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue