mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15844 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
69ad92d9ca
commit
d4f5cb67d4
5 changed files with 101 additions and 113 deletions
95
rational.c
95
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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue