1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* complex.c: refactoring.

* rational.c: ditto.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17109 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
tadf 2008-06-12 12:41:17 +00:00
parent 4214475331
commit 2b66844f48
3 changed files with 85 additions and 180 deletions

View file

@ -1,3 +1,9 @@
Thu Jun 12 21:39:55 2008 Tadayoshi Funaba <tadf@dotrb.org>
* complex.c: refactoring.
* rational.c: ditto.
Thu Jun 12 17:11:24 2008 Yukihiro Matsumoto <matz@ruby-lang.org> Thu Jun 12 17:11:24 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
* regint.h: undefine USE_CAPTURE_HISTORY which is mentioned as * regint.h: undefine USE_CAPTURE_HISTORY which is mentioned as

131
complex.c
View file

@ -68,72 +68,53 @@ m_##n(VALUE x, VALUE y)\
inline static VALUE inline static VALUE
f_add(VALUE x, VALUE y) f_add(VALUE x, VALUE y)
{ {
VALUE r;
if (FIXNUM_P(y)) { if (FIXNUM_P(y)) {
if (FIX2LONG(y) == 0) if (FIX2LONG(y) == 0)
r = x; return x;
else
r = rb_funcall(x, '+', 1, y);
} }
else if (FIXNUM_P(x)) { else if (FIXNUM_P(x)) {
if (FIX2LONG(x) == 0) if (FIX2LONG(x) == 0)
r = y; return y;
else
r = rb_funcall(x, '+', 1, y);
} }
else return rb_funcall(x, '+', 1, y);
r = rb_funcall(x, '+', 1, y);
return r;
} }
inline static VALUE inline static VALUE
f_cmp(VALUE x, VALUE y) f_cmp(VALUE x, VALUE y)
{ {
VALUE r;
if (FIXNUM_P(x) && FIXNUM_P(y)) { if (FIXNUM_P(x) && FIXNUM_P(y)) {
long c = FIX2LONG(x) - FIX2LONG(y); long c = FIX2LONG(x) - FIX2LONG(y);
if (c > 0) if (c > 0)
c = 1; c = 1;
else if (c < 0) else if (c < 0)
c = -1; c = -1;
r = INT2FIX(c); return INT2FIX(c);
} }
else return rb_funcall(x, id_cmp, 1, y);
r = rb_funcall(x, id_cmp, 1, y);
return r;
} }
inline static VALUE inline static VALUE
f_div(VALUE x, VALUE y) f_div(VALUE x, VALUE y)
{ {
VALUE r;
if (FIXNUM_P(y) && FIX2LONG(y) == 1) if (FIXNUM_P(y) && FIX2LONG(y) == 1)
r = x; return x;
else return rb_funcall(x, '/', 1, y);
r = rb_funcall(x, '/', 1, y);
return r;
} }
inline static VALUE inline static VALUE
f_gt_p(VALUE x, VALUE y) f_gt_p(VALUE x, VALUE y)
{ {
VALUE r;
if (FIXNUM_P(x) && FIXNUM_P(y)) if (FIXNUM_P(x) && FIXNUM_P(y))
r = f_boolcast(FIX2LONG(x) > FIX2LONG(y)); return f_boolcast(FIX2LONG(x) > FIX2LONG(y));
else return rb_funcall(x, '>', 1, y);
r = rb_funcall(x, '>', 1, y);
return r;
} }
inline static VALUE inline static VALUE
f_lt_p(VALUE x, VALUE y) f_lt_p(VALUE x, VALUE y)
{ {
VALUE r;
if (FIXNUM_P(x) && FIXNUM_P(y)) if (FIXNUM_P(x) && FIXNUM_P(y))
r = f_boolcast(FIX2LONG(x) < FIX2LONG(y)); return f_boolcast(FIX2LONG(x) < FIX2LONG(y));
else return rb_funcall(x, '<', 1, y);
r = rb_funcall(x, '<', 1, y);
return r;
} }
binop(mod, '%') binop(mod, '%')
@ -141,51 +122,38 @@ binop(mod, '%')
inline static VALUE inline static VALUE
f_mul(VALUE x, VALUE y) f_mul(VALUE x, VALUE y)
{ {
VALUE r;
if (FIXNUM_P(y)) { if (FIXNUM_P(y)) {
long _iy = FIX2LONG(y); long _iy = FIX2LONG(y);
if (_iy == 0) { if (_iy == 0) {
if (TYPE(x) == T_FLOAT) if (TYPE(x) == T_FLOAT)
r = rb_float_new(0.0); return rb_float_new(0.0);
else else
r = ZERO; return ZERO;
} }
else if (_iy == 1) else if (_iy == 1)
r = x; return x;
else
r = rb_funcall(x, '*', 1, y);
} }
else if (FIXNUM_P(x)) { else if (FIXNUM_P(x)) {
long _ix = FIX2LONG(x); long _ix = FIX2LONG(x);
if (_ix == 0) { if (_ix == 0) {
if (TYPE(y) == T_FLOAT) if (TYPE(y) == T_FLOAT)
r = rb_float_new(0.0); return rb_float_new(0.0);
else else
r = ZERO; return ZERO;
} }
else if (_ix == 1) else if (_ix == 1)
r = y; return y;
else
r = rb_funcall(x, '*', 1, y);
} }
else return rb_funcall(x, '*', 1, y);
r = rb_funcall(x, '*', 1, y);
return r;
} }
inline static VALUE inline static VALUE
f_sub(VALUE x, VALUE y) f_sub(VALUE x, VALUE y)
{ {
VALUE r; if (FIXNUM_P(y))
if (FIXNUM_P(y)) {
if (FIX2LONG(y) == 0) if (FIX2LONG(y) == 0)
r = x; return x;
else return rb_funcall(x, '-', 1, y);
r = rb_funcall(x, '-', 1, y);
}
else
r = rb_funcall(x, '-', 1, y);
return r;
} }
binop(xor, '^') binop(xor, '^')
@ -213,12 +181,9 @@ fun2(divmod)
inline static VALUE inline static VALUE
f_equal_p(VALUE x, VALUE y) f_equal_p(VALUE x, VALUE y)
{ {
VALUE r;
if (FIXNUM_P(x) && FIXNUM_P(y)) if (FIXNUM_P(x) && FIXNUM_P(y))
r = f_boolcast(FIX2LONG(x) == FIX2LONG(y)); return f_boolcast(FIX2LONG(x) == FIX2LONG(y));
else return rb_funcall(x, id_equal_p, 1, y);
r = rb_funcall(x, id_equal_p, 1, y);
return r;
} }
fun2(expt) fun2(expt)
@ -228,34 +193,25 @@ fun2(quo)
inline static VALUE inline static VALUE
f_negative_p(VALUE x) f_negative_p(VALUE x)
{ {
VALUE r;
if (FIXNUM_P(x)) if (FIXNUM_P(x))
r = f_boolcast(FIX2LONG(x) < 0); return f_boolcast(FIX2LONG(x) < 0);
else return rb_funcall(x, '<', 1, ZERO);
r = rb_funcall(x, '<', 1, ZERO);
return r;
} }
inline static VALUE inline static VALUE
f_zero_p(VALUE x) f_zero_p(VALUE x)
{ {
VALUE r;
if (FIXNUM_P(x)) if (FIXNUM_P(x))
r = f_boolcast(FIX2LONG(x) == 0); return f_boolcast(FIX2LONG(x) == 0);
else return rb_funcall(x, id_equal_p, 1, ZERO);
r = rb_funcall(x, id_equal_p, 1, ZERO);
return r;
} }
inline static VALUE inline static VALUE
f_one_p(VALUE x) f_one_p(VALUE x)
{ {
VALUE r;
if (FIXNUM_P(x)) if (FIXNUM_P(x))
r = f_boolcast(FIX2LONG(x) == 1); return f_boolcast(FIX2LONG(x) == 1);
else return rb_funcall(x, id_equal_p, 1, ONE);
r = rb_funcall(x, id_equal_p, 1, ONE);
return r;
} }
inline static VALUE inline static VALUE
@ -517,12 +473,11 @@ m_cos(VALUE x)
if (f_generic_p(x)) if (f_generic_p(x))
return m_cos_bang(x); return m_cos_bang(x);
else return f_complex_new2(rb_cComplex,
return f_complex_new2(rb_cComplex, f_mul(m_cos_bang(dat->real),
f_mul(m_cos_bang(dat->real), m_cosh_bang(dat->image)),
m_cosh_bang(dat->image)), f_mul(f_negate(m_sin_bang(dat->real)),
f_mul(f_negate(m_sin_bang(dat->real)), m_sinh_bang(dat->image)));
m_sinh_bang(dat->image)));
} }
static VALUE static VALUE
@ -532,12 +487,11 @@ m_sin(VALUE x)
if (f_generic_p(x)) if (f_generic_p(x))
return m_sin_bang(x); return m_sin_bang(x);
else return f_complex_new2(rb_cComplex,
return f_complex_new2(rb_cComplex, f_mul(m_sin_bang(dat->real),
f_mul(m_sin_bang(dat->real), m_cosh_bang(dat->image)),
m_cosh_bang(dat->image)), f_mul(m_cos_bang(dat->real),
f_mul(m_cos_bang(dat->real), m_sinh_bang(dat->image)));
m_sinh_bang(dat->image)));
} }
static VALUE static VALUE
@ -546,8 +500,7 @@ m_sqrt(VALUE x)
if (f_generic_p(x)) { if (f_generic_p(x)) {
if (!f_negative_p(x)) if (!f_negative_p(x))
return m_sqrt_bang(x); return m_sqrt_bang(x);
else return f_complex_new2(rb_cComplex, ZERO, m_sqrt_bang(f_negate(x)));
return f_complex_new2(rb_cComplex, ZERO, m_sqrt_bang(f_negate(x)));
} }
else { else {
get_dat1(x); get_dat1(x);
@ -771,9 +724,7 @@ nucomp_expt(VALUE self, VALUE other)
} }
return z; return z;
} }
else { return f_expt(f_div(f_to_r(ONE), self), f_negate(other));
return f_expt(f_div(f_to_r(ONE), self), f_negate(other));
}
case T_FLOAT: case T_FLOAT:
case T_RATIONAL: case T_RATIONAL:
{ {

View file

@ -52,72 +52,53 @@ f_##n(VALUE x, VALUE y)\
inline static VALUE inline static VALUE
f_add(VALUE x, VALUE y) f_add(VALUE x, VALUE y)
{ {
VALUE r;
if (FIXNUM_P(y)) { if (FIXNUM_P(y)) {
if (FIX2LONG(y) == 0) if (FIX2LONG(y) == 0)
r = x; return x;
else
r = rb_funcall(x, '+', 1, y);
} }
else if (FIXNUM_P(x)) { else if (FIXNUM_P(x)) {
if (FIX2LONG(x) == 0) if (FIX2LONG(x) == 0)
r = y; return y;
else
r = rb_funcall(x, '+', 1, y);
} }
else return rb_funcall(x, '+', 1, y);
r = rb_funcall(x, '+', 1, y);
return r;
} }
inline static VALUE inline static VALUE
f_cmp(VALUE x, VALUE y) f_cmp(VALUE x, VALUE y)
{ {
VALUE r;
if (FIXNUM_P(x) && FIXNUM_P(y)) { if (FIXNUM_P(x) && FIXNUM_P(y)) {
long c = FIX2LONG(x) - FIX2LONG(y); long c = FIX2LONG(x) - FIX2LONG(y);
if (c > 0) if (c > 0)
c = 1; c = 1;
else if (c < 0) else if (c < 0)
c = -1; c = -1;
r = INT2FIX(c); return INT2FIX(c);
} }
else return rb_funcall(x, id_cmp, 1, y);
r = rb_funcall(x, id_cmp, 1, y);
return r;
} }
inline static VALUE inline static VALUE
f_div(VALUE x, VALUE y) f_div(VALUE x, VALUE y)
{ {
VALUE r;
if (FIXNUM_P(y) && FIX2LONG(y) == 1) if (FIXNUM_P(y) && FIX2LONG(y) == 1)
r = x; return x;
else return rb_funcall(x, '/', 1, y);
r = rb_funcall(x, '/', 1, y);
return r;
} }
inline static VALUE inline static VALUE
f_gt_p(VALUE x, VALUE y) f_gt_p(VALUE x, VALUE y)
{ {
VALUE r;
if (FIXNUM_P(x) && FIXNUM_P(y)) if (FIXNUM_P(x) && FIXNUM_P(y))
r = f_boolcast(FIX2LONG(x) > FIX2LONG(y)); return f_boolcast(FIX2LONG(x) > FIX2LONG(y));
else return rb_funcall(x, '>', 1, y);
r = rb_funcall(x, '>', 1, y);
return r;
} }
inline static VALUE inline static VALUE
f_lt_p(VALUE x, VALUE y) f_lt_p(VALUE x, VALUE y)
{ {
VALUE r;
if (FIXNUM_P(x) && FIXNUM_P(y)) if (FIXNUM_P(x) && FIXNUM_P(y))
r = f_boolcast(FIX2LONG(x) < FIX2LONG(y)); return f_boolcast(FIX2LONG(x) < FIX2LONG(y));
else return rb_funcall(x, '<', 1, y);
r = rb_funcall(x, '<', 1, y);
return r;
} }
binop(mod, '%') binop(mod, '%')
@ -125,51 +106,38 @@ binop(mod, '%')
inline static VALUE inline static VALUE
f_mul(VALUE x, VALUE y) f_mul(VALUE x, VALUE y)
{ {
VALUE r;
if (FIXNUM_P(y)) { if (FIXNUM_P(y)) {
long _iy = FIX2LONG(y); long _iy = FIX2LONG(y);
if (_iy == 0) { if (_iy == 0) {
if (TYPE(x) == T_FLOAT) if (TYPE(x) == T_FLOAT)
r = rb_float_new(0.0); return rb_float_new(0.0);
else else
r = ZERO; return ZERO;
} }
else if (_iy == 1) else if (_iy == 1)
r = x; return x;
else
r = rb_funcall(x, '*', 1, y);
} }
else if (FIXNUM_P(x)) { else if (FIXNUM_P(x)) {
long _ix = FIX2LONG(x); long _ix = FIX2LONG(x);
if (_ix == 0) { if (_ix == 0) {
if (TYPE(y) == T_FLOAT) if (TYPE(y) == T_FLOAT)
r = rb_float_new(0.0); return rb_float_new(0.0);
else else
r = ZERO; return ZERO;
} }
else if (_ix == 1) else if (_ix == 1)
r = y; return y;
else
r = rb_funcall(x, '*', 1, y);
} }
else return rb_funcall(x, '*', 1, y);
r = rb_funcall(x, '*', 1, y);
return r;
} }
inline static VALUE inline static VALUE
f_sub(VALUE x, VALUE y) f_sub(VALUE x, VALUE y)
{ {
VALUE r; if (FIXNUM_P(y))
if (FIXNUM_P(y)) {
if (FIX2LONG(y) == 0) if (FIX2LONG(y) == 0)
r = x; return x;
else return rb_funcall(x, '-', 1, y);
r = rb_funcall(x, '-', 1, y);
}
else
r = rb_funcall(x, '-', 1, y);
return r;
} }
binop(xor, '^') binop(xor, '^')
@ -186,12 +154,9 @@ fun1(truncate)
inline static VALUE inline static VALUE
f_equal_p(VALUE x, VALUE y) f_equal_p(VALUE x, VALUE y)
{ {
VALUE r;
if (FIXNUM_P(x) && FIXNUM_P(y)) if (FIXNUM_P(x) && FIXNUM_P(y))
r = f_boolcast(FIX2LONG(x) == FIX2LONG(y)); return f_boolcast(FIX2LONG(x) == FIX2LONG(y));
else return rb_funcall(x, id_equal_p, 1, y);
r = rb_funcall(x, id_equal_p, 1, y);
return r;
} }
fun2(expt) fun2(expt)
@ -200,34 +165,25 @@ fun2(idiv)
inline static VALUE inline static VALUE
f_negative_p(VALUE x) f_negative_p(VALUE x)
{ {
VALUE r;
if (FIXNUM_P(x)) if (FIXNUM_P(x))
r = f_boolcast(FIX2LONG(x) < 0); return f_boolcast(FIX2LONG(x) < 0);
else return rb_funcall(x, '<', 1, ZERO);
r = rb_funcall(x, '<', 1, ZERO);
return r;
} }
inline static VALUE inline static VALUE
f_zero_p(VALUE x) f_zero_p(VALUE x)
{ {
VALUE r;
if (FIXNUM_P(x)) if (FIXNUM_P(x))
r = f_boolcast(FIX2LONG(x) == 0); return f_boolcast(FIX2LONG(x) == 0);
else return rb_funcall(x, id_equal_p, 1, ZERO);
r = rb_funcall(x, id_equal_p, 1, ZERO);
return r;
} }
inline static VALUE inline static VALUE
f_one_p(VALUE x) f_one_p(VALUE x)
{ {
VALUE r;
if (FIXNUM_P(x)) if (FIXNUM_P(x))
r = f_boolcast(FIX2LONG(x) == 1); return f_boolcast(FIX2LONG(x) == 1);
else return rb_funcall(x, id_equal_p, 1, ONE);
r = rb_funcall(x, id_equal_p, 1, ONE);
return r;
} }
inline static VALUE inline static VALUE
@ -359,8 +315,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 return f_abs(f_mul(f_div(x, f_gcd(x, y)), y));
return f_abs(f_mul(f_div(x, f_gcd(x, y)), y));
} }
#define get_dat1(x) \ #define get_dat1(x) \
@ -471,8 +426,7 @@ nurat_s_canonicalize_internal(VALUE klass, VALUE num, VALUE den)
if (f_one_p(den) && f_unify_p(klass)) if (f_one_p(den) && f_unify_p(klass))
return num; return num;
else return nurat_s_new_internal(klass, num, den);
return nurat_s_new_internal(klass, num, den);
} }
inline static VALUE inline static VALUE
@ -490,8 +444,7 @@ nurat_s_canonicalize_internal_no_reduce(VALUE klass, VALUE num, VALUE den)
if (f_equal_p(den, ONE) && f_unify_p(klass)) if (f_equal_p(den, ONE) && f_unify_p(klass))
return num; return num;
else return nurat_s_new_internal(klass, num, den);
return nurat_s_new_internal(klass, num, den);
} }
#if 0 #if 0
@ -877,8 +830,7 @@ nurat_cmp(VALUE self, VALUE other)
if (FIXNUM_P(dat->den) && FIX2LONG(dat->den) == 1) if (FIXNUM_P(dat->den) && FIX2LONG(dat->den) == 1)
return f_cmp(dat->num, other); return f_cmp(dat->num, other);
else return f_cmp(self, f_rational_new_bang1(CLASS_OF(self), other));
return f_cmp(self, f_rational_new_bang1(CLASS_OF(self), other));
} }
case T_FLOAT: case T_FLOAT:
return f_cmp(f_to_f(self), other); return f_cmp(f_to_f(self), other);
@ -919,8 +871,7 @@ nurat_equal_p(VALUE self, VALUE other)
return Qfalse; return Qfalse;
if (f_equal_p(dat->num, other)) if (f_equal_p(dat->num, other))
return Qtrue; return Qtrue;
else return Qfalse;
return Qfalse;
} }
case T_FLOAT: case T_FLOAT:
return f_equal_p(f_to_f(self), other); return f_equal_p(f_to_f(self), other);
@ -1001,8 +952,7 @@ nurat_abs(VALUE self)
{ {
if (!f_negative_p(self)) if (!f_negative_p(self))
return self; return self;
else return f_negate(self);
return f_negate(self);
} }
#if 0 #if 0
@ -1160,9 +1110,8 @@ nurat_to_s(VALUE self)
if (f_one_p(dat->den)) if (f_one_p(dat->den))
return f_to_s(dat->num); return f_to_s(dat->num);
else return rb_funcall(rb_mKernel, id_format, 3,
return rb_funcall(rb_mKernel, id_format, 3, rb_str_new2("%d/%d"), dat->num, dat->den);
rb_str_new2("%d/%d"), dat->num, dat->den);
} }
static VALUE static VALUE
@ -1472,8 +1421,7 @@ nurat_s_convert(int argc, VALUE *argv, VALUE klass)
case T_RATIONAL: case T_RATIONAL:
if (NIL_P(a2) || f_zero_p(a2)) if (NIL_P(a2) || f_zero_p(a2))
return a1; return a1;
else return f_div(a1, a2);
return f_div(a1, a2);
} }
switch (TYPE(a2)) { switch (TYPE(a2)) {