mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
added rb_gcd.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15807 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
1484d1c32a
commit
a3c76eb0c7
3 changed files with 20 additions and 75 deletions
12
ChangeLog
12
ChangeLog
|
@ -1,3 +1,15 @@
|
||||||
|
Wed Mar 19 22:27:41 2008 Tadayoshi Funaba <tadf@dotrb.org>
|
||||||
|
|
||||||
|
* rational.c: added rb_gcd.
|
||||||
|
|
||||||
|
* complex.c: use rb_gcd.
|
||||||
|
|
||||||
|
Wed Mar 19 18:37:00 2008 Tadayoshi Funaba <tadf@dotrb.org>
|
||||||
|
|
||||||
|
* complex.c: revert.
|
||||||
|
|
||||||
|
* rational.c: revert.
|
||||||
|
|
||||||
Wed Mar 19 17:31:20 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
|
Wed Mar 19 17:31:20 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
|
|
||||||
* eval_intern.h (TH_EXEC_TAG): need not to FLUSH_REGISTER_WINDOWS.
|
* eval_intern.h (TH_EXEC_TAG): need not to FLUSH_REGISTER_WINDOWS.
|
||||||
|
|
77
complex.c
77
complex.c
|
@ -813,80 +813,7 @@ nucomp_inexact_p(VALUE self)
|
||||||
return f_boolcast(!nucomp_exact_p(self));
|
return f_boolcast(!nucomp_exact_p(self));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline static long
|
extern VALUE rb_gcd(VALUE x, VALUE y);
|
||||||
i_gcd(long x, long y)
|
|
||||||
{
|
|
||||||
long b;
|
|
||||||
|
|
||||||
if (x < 0)
|
|
||||||
x = -x;
|
|
||||||
if (y < 0)
|
|
||||||
y = -y;
|
|
||||||
|
|
||||||
if (x == 0)
|
|
||||||
return y;
|
|
||||||
if (y == 0)
|
|
||||||
return x;
|
|
||||||
|
|
||||||
b = 0;
|
|
||||||
while ((x & 1) == 0 && (y & 1) == 0) {
|
|
||||||
b += 1;
|
|
||||||
x >>= 1;
|
|
||||||
y >>= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
while ((x & 1) == 0)
|
|
||||||
x >>= 1;
|
|
||||||
|
|
||||||
while ((y & 1) == 0)
|
|
||||||
y >>= 1;
|
|
||||||
|
|
||||||
while (x != y) {
|
|
||||||
if (y > x) {
|
|
||||||
long t;
|
|
||||||
t = x;
|
|
||||||
x = y;
|
|
||||||
y = t;
|
|
||||||
}
|
|
||||||
x -= y;
|
|
||||||
while ((x & 1) == 0)
|
|
||||||
x >>= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return x << b;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline static VALUE
|
|
||||||
f_gcd(VALUE x, VALUE y)
|
|
||||||
{
|
|
||||||
VALUE z;
|
|
||||||
|
|
||||||
if (FIXNUM_P(x) && FIXNUM_P(y))
|
|
||||||
return LONG2NUM(i_gcd(FIX2LONG(x), FIX2LONG(y)));
|
|
||||||
|
|
||||||
if (f_negative_p(x))
|
|
||||||
x = f_negate(x);
|
|
||||||
if (f_negative_p(y))
|
|
||||||
y = f_negate(y);
|
|
||||||
|
|
||||||
if (f_zero_p(x))
|
|
||||||
return y;
|
|
||||||
if (f_zero_p(y))
|
|
||||||
return x;
|
|
||||||
|
|
||||||
for (;;) {
|
|
||||||
if (FIXNUM_P(x)) {
|
|
||||||
if (FIX2INT(x) == 0)
|
|
||||||
return y;
|
|
||||||
if (FIXNUM_P(y))
|
|
||||||
return LONG2NUM(i_gcd(FIX2LONG(x), FIX2LONG(y)));
|
|
||||||
}
|
|
||||||
z = x;
|
|
||||||
x = f_mod(y, x);
|
|
||||||
y = z;
|
|
||||||
}
|
|
||||||
/* NOTREACHED */
|
|
||||||
}
|
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
f_lcm(VALUE x, VALUE y)
|
f_lcm(VALUE x, VALUE y)
|
||||||
|
@ -894,7 +821,7 @@ f_lcm(VALUE x, VALUE y)
|
||||||
if (f_zero_p(x) || f_zero_p(y))
|
if (f_zero_p(x) || f_zero_p(y))
|
||||||
return ZERO;
|
return ZERO;
|
||||||
else
|
else
|
||||||
return f_abs(f_mul(f_div(x, f_gcd(x, y)), y));
|
return f_abs(f_mul(f_div(x, rb_gcd(x, y)), y));
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
|
|
|
@ -133,6 +133,12 @@ f_gcd(VALUE x, VALUE y)
|
||||||
/* NOTREACHED */
|
/* NOTREACHED */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VALUE
|
||||||
|
rb_gcd(VALUE x, VALUE y)
|
||||||
|
{
|
||||||
|
return f_gcd(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
#define get_dat1(x) \
|
#define get_dat1(x) \
|
||||||
struct RRational *dat;\
|
struct RRational *dat;\
|
||||||
dat = ((struct RRational *)(x))
|
dat = ((struct RRational *)(x))
|
||||||
|
|
Loading…
Add table
Reference in a new issue