diff --git a/ChangeLog b/ChangeLog index 4d3f5a421b..b27a09e142 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Thu Mar 27 20:44:22 2008 Tadayoshi Funaba + + * complex.c (f_lcm): removed. + + * rational.c (rb_lcm, rb_gcdlcm): added. + + * lib/complex.rb (gcd, lcm, gcdlcm): removed. + + * lib/rational.rb (gcd, lcm, gcdlcm): ditto. + Wed Mar 26 18:11:26 2008 Yukihiro Matsumoto * variable.c (rb_mod_constants): rdoc updated. a patch from diff --git a/complex.c b/complex.c index 58704110ce..1644746360 100644 --- a/complex.c +++ b/complex.c @@ -84,6 +84,22 @@ f_add(VALUE x, VALUE y) return r; } +inline static VALUE +f_cmp(VALUE x, VALUE y) +{ + VALUE r; + if (FIXNUM_P(x) && FIXNUM_P(y)) { + long c = FIX2LONG(x) - FIX2LONG(y); + if (c > 0) + c = 1; + else if (c < 0) + c = -1; + r = INT2FIX(c); + } else + r = rb_funcall(x, id_cmp, 1, y); + return r; +} + inline static VALUE f_div(VALUE x, VALUE y) { @@ -184,22 +200,6 @@ fun1(to_r) fun1(to_s) fun1(truncate) -inline static VALUE -f_cmp(VALUE x, VALUE y) -{ - VALUE r; - if (FIXNUM_P(x) && FIXNUM_P(y)) { - long c = FIX2LONG(x) - FIX2LONG(y); - if (c > 0) - c = 1; - else if (c < 0) - c = -1; - r = INT2FIX(c); - } else - r = rb_funcall(x, id_cmp, 1, y); - return r; -} - fun2(coerce) fun2(divmod) @@ -1016,22 +1016,13 @@ nucomp_inexact_p(VALUE self) return f_boolcast(!nucomp_exact_p(self)); } -extern VALUE rb_gcd(VALUE x, VALUE y); - -static VALUE -f_lcm(VALUE x, VALUE y) -{ - if (f_zero_p(x) || f_zero_p(y)) - return ZERO; - else - return f_abs(f_mul(f_div(x, rb_gcd(x, y)), y)); -} +extern VALUE rb_lcm(VALUE x, VALUE y); static VALUE nucomp_denominator(VALUE self) { get_dat1(self); - return f_lcm(f_denominator(dat->real), f_denominator(dat->image)); + return rb_lcm(f_denominator(dat->real), f_denominator(dat->image)); } static VALUE diff --git a/lib/complex.rb b/lib/complex.rb index 505b0120e3..9d926023a7 100644 --- a/lib/complex.rb +++ b/lib/complex.rb @@ -1,35 +1,3 @@ -class Integer - - def gcd(other) - min = self.abs - max = other.abs - while min > 0 - tmp = min - min = max % min - max = tmp - end - max - end - - def lcm(other) - if self.zero? or other.zero? - 0 - else - (self.div(self.gcd(other)) * other).abs - end - end - - def gcdlcm(other) - gcd = self.gcd(other) - if self.zero? or other.zero? - [gcd, 0] - else - [gcd, (self.div(gcd) * other).abs] - end - end - -end - module Math alias exp! exp diff --git a/lib/rational.rb b/lib/rational.rb index b12bf7ef38..87c5d3f111 100644 --- a/lib/rational.rb +++ b/lib/rational.rb @@ -15,35 +15,3 @@ class Bignum alias rpower ** end - -class Integer - - def gcd(other) - min = self.abs - max = other.abs - while min > 0 - tmp = min - min = max % min - max = tmp - end - max - end - - def lcm(other) - if self.zero? or other.zero? - 0 - else - (self.div(self.gcd(other)) * other).abs - end - end - - def gcdlcm(other) - gcd = self.gcd(other) - if self.zero? or other.zero? - [gcd, 0] - else - [gcd, (self.div(gcd) * other).abs] - end - end - -end diff --git a/rational.c b/rational.c index 93125fecfc..7165e93232 100644 --- a/rational.c +++ b/rational.c @@ -21,9 +21,9 @@ VALUE rb_cRational; -static ID id_Unify, id_cmp, id_coerce, id_convert, id_equal_p, id_expt, - id_floor, id_format,id_idiv, id_inspect, id_negate, id_new, id_new_bang, - id_to_f, id_to_i, id_to_s, id_truncate; +static ID id_Unify, id_abs, id_cmp, id_coerce, id_convert, id_equal_p, + id_expt, id_floor, id_format,id_idiv, id_inspect, id_negate, id_new, + id_new_bang, id_to_f, id_to_i, id_to_s, id_truncate; #define f_boolcast(x) ((x) ? Qtrue : Qfalse) @@ -67,6 +67,22 @@ f_add(VALUE x, VALUE y) return r; } +inline static VALUE +f_cmp(VALUE x, VALUE y) +{ + VALUE r; + if (FIXNUM_P(x) && FIXNUM_P(y)) { + long c = FIX2LONG(x) - FIX2LONG(y); + if (c > 0) + c = 1; + else if (c < 0) + c = -1; + r = INT2FIX(c); + } else + r = rb_funcall(x, id_cmp, 1, y); + return r; +} + inline static VALUE f_div(VALUE x, VALUE y) { @@ -149,6 +165,7 @@ f_sub(VALUE x, VALUE y) binop(xor, '^') +fun1(abs) fun1(floor) fun1(inspect) fun1(negate) @@ -157,22 +174,6 @@ fun1(to_i) fun1(to_s) fun1(truncate) -inline static VALUE -f_cmp(VALUE x, VALUE y) -{ - VALUE r; - if (FIXNUM_P(x) && FIXNUM_P(y)) { - long c = FIX2LONG(x) - FIX2LONG(y); - if (c > 0) - c = 1; - else if (c < 0) - c = -1; - r = INT2FIX(c); - } else - r = rb_funcall(x, id_cmp, 1, y); - return r; -} - fun2(coerce) inline static VALUE @@ -346,10 +347,13 @@ f_gcd(VALUE x, VALUE y) } #endif -VALUE -rb_gcd(VALUE x, VALUE y) +inline static VALUE +f_lcm(VALUE x, VALUE y) { - return f_gcd(x, y); + if (f_zero_p(x) || f_zero_p(y)) + return ZERO; + else + return f_abs(f_mul(f_div(x, f_gcd(x, y)), y)); } #define get_dat1(x) \ @@ -1208,6 +1212,48 @@ nurat_marshal_load(VALUE self, VALUE a) /* --- */ +VALUE +rb_gcd(VALUE self, VALUE other) +{ + switch (TYPE(other)) { + case T_FIXNUM: + case T_BIGNUM: + break; + default: + rb_raise(rb_eArgError, "not an integer"); + } + + return f_gcd(self, other); +} + +VALUE +rb_lcm(VALUE self, VALUE other) +{ + switch (TYPE(other)) { + case T_FIXNUM: + case T_BIGNUM: + break; + default: + rb_raise(rb_eArgError, "not an integer"); + } + + return f_lcm(self, other); +} + +VALUE +rb_gcdlcm(VALUE self, VALUE other) +{ + switch (TYPE(other)) { + case T_FIXNUM: + case T_BIGNUM: + break; + default: + rb_raise(rb_eArgError, "not an integer"); + } + + return rb_assoc_new(f_gcd(self, other), f_lcm(self, other)); +} + VALUE rb_rational_raw(VALUE x, VALUE y) { @@ -1494,6 +1540,7 @@ Init_Rational(void) assert(fprintf(stderr, "assert() is now active\n")); id_Unify = rb_intern("Unify"); + id_abs = rb_intern("abs"); id_cmp = rb_intern("<=>"); id_coerce = rb_intern("coerce"); id_convert = rb_intern("convert"); @@ -1583,6 +1630,10 @@ Init_Rational(void) /* --- */ + rb_define_method(rb_cInteger, "gcd", rb_gcd, 1); + rb_define_method(rb_cInteger, "lcm", rb_lcm, 1); + rb_define_method(rb_cInteger, "gcdlcm", rb_gcdlcm, 1); + rb_define_method(rb_cNilClass, "to_r", nilclass_to_r, 0); rb_define_method(rb_cInteger, "to_r", integer_to_r, 0); rb_define_method(rb_cFloat, "to_r", float_to_r, 0);