ruby-changes:4354
From: ko1@a...
Date: Thu, 27 Mar 2008 20:48:18 +0900 (JST)
Subject: [ruby-changes:4354] tadf - Ruby:r15844 (trunk): * complex.c (f_lcm): removed.
tadf 2008-03-27 20:48:00 +0900 (Thu, 27 Mar 2008) New Revision: 15844 Modified files: trunk/ChangeLog trunk/complex.c trunk/lib/complex.rb trunk/lib/rational.rb trunk/rational.c Log: * 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. http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/lib/complex.rb?r1=15844&r2=15843&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/complex.c?r1=15844&r2=15843&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=15844&r2=15843&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/lib/rational.rb?r1=15844&r2=15843&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/rational.c?r1=15844&r2=15843&diff_format=u Index: complex.c =================================================================== --- complex.c (revision 15843) +++ complex.c (revision 15844) @@ -85,6 +85,22 @@ } 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) { VALUE r; @@ -184,22 +200,6 @@ 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 @@ return f_boolcast(!nucomp_exact_p(self)); } -extern VALUE rb_gcd(VALUE x, VALUE y); +extern VALUE rb_lcm(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)); -} - -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 Index: ChangeLog =================================================================== --- ChangeLog (revision 15843) +++ ChangeLog (revision 15844) @@ -1,3 +1,13 @@ +Thu Mar 27 20:44:22 2008 Tadayoshi Funaba <tadf@d...> + + * 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 <matz@r...> * variable.c (rb_mod_constants): rdoc updated. a patch from Index: lib/rational.rb =================================================================== --- lib/rational.rb (revision 15843) +++ lib/rational.rb (revision 15844) @@ -15,35 +15,3 @@ 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 Index: lib/complex.rb =================================================================== --- lib/complex.rb (revision 15843) +++ lib/complex.rb (revision 15844) @@ -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 Index: rational.c =================================================================== --- rational.c (revision 15843) +++ rational.c (revision 15844) @@ -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) @@ -68,6 +68,22 @@ } 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) { VALUE r; @@ -149,6 +165,7 @@ binop(xor, '^') +fun1(abs) fun1(floor) fun1(inspect) fun1(negate) @@ -157,22 +174,6 @@ 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 @@ } #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) \ @@ -1209,6 +1213,48 @@ /* --- */ 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) { return nurat_s_new_internal(rb_cRational, x, y); @@ -1494,6 +1540,7 @@ 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 @@ /* --- */ + 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); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/