* 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
Родитель 4214475331
Коммит 2b66844f48
3 изменённых файлов: 85 добавлений и 180 удалений

Просмотреть файл

@ -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>
* regint.h: undefine USE_CAPTURE_HISTORY which is mentioned as

107
complex.c
Просмотреть файл

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

Просмотреть файл

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