* complex.c, rational.c: fixed indent.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16154 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
9f9a532d98
commit
f80616b6d2
237
complex.c
237
complex.c
@ -74,12 +74,14 @@ f_add(VALUE x, VALUE y)
|
|||||||
r = x;
|
r = x;
|
||||||
else
|
else
|
||||||
r = rb_funcall(x, '+', 1, y);
|
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;
|
r = y;
|
||||||
else
|
else
|
||||||
r = rb_funcall(x, '+', 1, y);
|
r = rb_funcall(x, '+', 1, y);
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
r = rb_funcall(x, '+', 1, y);
|
r = rb_funcall(x, '+', 1, y);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
@ -95,7 +97,8 @@ f_cmp(VALUE x, VALUE y)
|
|||||||
else if (c < 0)
|
else if (c < 0)
|
||||||
c = -1;
|
c = -1;
|
||||||
r = INT2FIX(c);
|
r = INT2FIX(c);
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
r = rb_funcall(x, id_cmp, 1, y);
|
r = rb_funcall(x, id_cmp, 1, y);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
@ -146,22 +149,26 @@ f_mul(VALUE x, VALUE y)
|
|||||||
r = rb_float_new(0.0);
|
r = rb_float_new(0.0);
|
||||||
else
|
else
|
||||||
r = ZERO;
|
r = ZERO;
|
||||||
} else if (_iy == 1)
|
}
|
||||||
|
else if (_iy == 1)
|
||||||
r = x;
|
r = x;
|
||||||
else
|
else
|
||||||
r = rb_funcall(x, '*', 1, y);
|
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);
|
r = rb_float_new(0.0);
|
||||||
else
|
else
|
||||||
r = ZERO;
|
r = ZERO;
|
||||||
} else if (_ix == 1)
|
}
|
||||||
|
else if (_ix == 1)
|
||||||
r = y;
|
r = y;
|
||||||
else
|
else
|
||||||
r = rb_funcall(x, '*', 1, y);
|
r = rb_funcall(x, '*', 1, y);
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
r = rb_funcall(x, '*', 1, y);
|
r = rb_funcall(x, '*', 1, y);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
@ -175,7 +182,8 @@ f_sub(VALUE x, VALUE y)
|
|||||||
r = x;
|
r = x;
|
||||||
else
|
else
|
||||||
r = rb_funcall(x, '-', 1, y);
|
r = rb_funcall(x, '-', 1, y);
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
r = rb_funcall(x, '-', 1, y);
|
r = rb_funcall(x, '-', 1, y);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
@ -403,13 +411,15 @@ nucomp_s_canonicalize_internal(VALUE klass, VALUE real, VALUE image)
|
|||||||
return nucomp_s_new_internal(klass,
|
return nucomp_s_new_internal(klass,
|
||||||
f_sub(real, dat->image),
|
f_sub(real, dat->image),
|
||||||
f_add(ZERO, dat->real));
|
f_add(ZERO, dat->real));
|
||||||
} else if (f_scalar_p(image)) {
|
}
|
||||||
|
else if (f_scalar_p(image)) {
|
||||||
get_dat1(real);
|
get_dat1(real);
|
||||||
|
|
||||||
return nucomp_s_new_internal(klass,
|
return nucomp_s_new_internal(klass,
|
||||||
dat->real,
|
dat->real,
|
||||||
f_add(dat->image, image));
|
f_add(dat->image, image));
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
get_dat2(real, image);
|
get_dat2(real, image);
|
||||||
|
|
||||||
return nucomp_s_new_internal(klass,
|
return nucomp_s_new_internal(klass,
|
||||||
@ -538,7 +548,8 @@ m_sqrt(VALUE x)
|
|||||||
return m_sqrt_bang(x);
|
return m_sqrt_bang(x);
|
||||||
else
|
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);
|
||||||
|
|
||||||
if (f_negative_p(dat->image))
|
if (f_negative_p(dat->image))
|
||||||
@ -582,23 +593,23 @@ nucomp_add(VALUE self, VALUE other)
|
|||||||
case T_BIGNUM:
|
case T_BIGNUM:
|
||||||
case T_FLOAT:
|
case T_FLOAT:
|
||||||
case T_RATIONAL:
|
case T_RATIONAL:
|
||||||
{
|
{
|
||||||
get_dat1(self);
|
get_dat1(self);
|
||||||
|
|
||||||
return f_complex_new2(CLASS_OF(self),
|
return f_complex_new2(CLASS_OF(self),
|
||||||
f_add(dat->real, other), dat->image);
|
f_add(dat->real, other), dat->image);
|
||||||
}
|
}
|
||||||
case T_COMPLEX:
|
case T_COMPLEX:
|
||||||
{
|
{
|
||||||
VALUE real, image;
|
VALUE real, image;
|
||||||
|
|
||||||
get_dat2(self, other);
|
get_dat2(self, other);
|
||||||
|
|
||||||
real = f_add(adat->real, bdat->real);
|
real = f_add(adat->real, bdat->real);
|
||||||
image = f_add(adat->image, bdat->image);
|
image = f_add(adat->image, bdat->image);
|
||||||
|
|
||||||
return f_complex_new2(CLASS_OF(self), real, image);
|
return f_complex_new2(CLASS_OF(self), real, image);
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return rb_num_coerce_bin(self, other, '+');
|
return rb_num_coerce_bin(self, other, '+');
|
||||||
}
|
}
|
||||||
@ -612,23 +623,23 @@ nucomp_sub(VALUE self, VALUE other)
|
|||||||
case T_BIGNUM:
|
case T_BIGNUM:
|
||||||
case T_FLOAT:
|
case T_FLOAT:
|
||||||
case T_RATIONAL:
|
case T_RATIONAL:
|
||||||
{
|
{
|
||||||
get_dat1(self);
|
get_dat1(self);
|
||||||
|
|
||||||
return f_complex_new2(CLASS_OF(self),
|
return f_complex_new2(CLASS_OF(self),
|
||||||
f_sub(dat->real, other), dat->image);
|
f_sub(dat->real, other), dat->image);
|
||||||
}
|
}
|
||||||
case T_COMPLEX:
|
case T_COMPLEX:
|
||||||
{
|
{
|
||||||
VALUE real, image;
|
VALUE real, image;
|
||||||
|
|
||||||
get_dat2(self, other);
|
get_dat2(self, other);
|
||||||
|
|
||||||
real = f_sub(adat->real, bdat->real);
|
real = f_sub(adat->real, bdat->real);
|
||||||
image = f_sub(adat->image, bdat->image);
|
image = f_sub(adat->image, bdat->image);
|
||||||
|
|
||||||
return f_complex_new2(CLASS_OF(self), real, image);
|
return f_complex_new2(CLASS_OF(self), real, image);
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return rb_num_coerce_bin(self, other, '-');
|
return rb_num_coerce_bin(self, other, '-');
|
||||||
}
|
}
|
||||||
@ -642,26 +653,26 @@ nucomp_mul(VALUE self, VALUE other)
|
|||||||
case T_BIGNUM:
|
case T_BIGNUM:
|
||||||
case T_FLOAT:
|
case T_FLOAT:
|
||||||
case T_RATIONAL:
|
case T_RATIONAL:
|
||||||
{
|
{
|
||||||
get_dat1(self);
|
get_dat1(self);
|
||||||
|
|
||||||
return f_complex_new2(CLASS_OF(self),
|
return f_complex_new2(CLASS_OF(self),
|
||||||
f_mul(dat->real, other),
|
f_mul(dat->real, other),
|
||||||
f_mul(dat->image, other));
|
f_mul(dat->image, other));
|
||||||
}
|
}
|
||||||
case T_COMPLEX:
|
case T_COMPLEX:
|
||||||
{
|
{
|
||||||
VALUE real, image;
|
VALUE real, image;
|
||||||
|
|
||||||
get_dat2(self, other);
|
get_dat2(self, other);
|
||||||
|
|
||||||
real = f_sub(f_mul(adat->real, bdat->real),
|
real = f_sub(f_mul(adat->real, bdat->real),
|
||||||
f_mul(adat->image, bdat->image));
|
f_mul(adat->image, bdat->image));
|
||||||
image = f_add(f_mul(adat->real, bdat->image),
|
image = f_add(f_mul(adat->real, bdat->image),
|
||||||
f_mul(adat->image, bdat->real));
|
f_mul(adat->image, bdat->real));
|
||||||
|
|
||||||
return f_complex_new2(CLASS_OF(self), real, image);
|
return f_complex_new2(CLASS_OF(self), real, image);
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return rb_num_coerce_bin(self, other, '*');
|
return rb_num_coerce_bin(self, other, '*');
|
||||||
}
|
}
|
||||||
@ -675,29 +686,29 @@ nucomp_div(VALUE self, VALUE other)
|
|||||||
case T_BIGNUM:
|
case T_BIGNUM:
|
||||||
case T_FLOAT:
|
case T_FLOAT:
|
||||||
case T_RATIONAL:
|
case T_RATIONAL:
|
||||||
{
|
{
|
||||||
get_dat1(self);
|
get_dat1(self);
|
||||||
|
|
||||||
return f_complex_new2(CLASS_OF(self),
|
return f_complex_new2(CLASS_OF(self),
|
||||||
f_div(dat->real, other),
|
f_div(dat->real, other),
|
||||||
f_div(dat->image, other));
|
f_div(dat->image, other));
|
||||||
}
|
}
|
||||||
case T_COMPLEX:
|
case T_COMPLEX:
|
||||||
{
|
{
|
||||||
get_dat2(self, other);
|
get_dat2(self, other);
|
||||||
|
|
||||||
if (TYPE(adat->real) == T_FLOAT ||
|
if (TYPE(adat->real) == T_FLOAT ||
|
||||||
TYPE(adat->image) == T_FLOAT ||
|
TYPE(adat->image) == T_FLOAT ||
|
||||||
TYPE(bdat->real) == T_FLOAT ||
|
TYPE(bdat->real) == T_FLOAT ||
|
||||||
TYPE(bdat->image) == T_FLOAT) {
|
TYPE(bdat->image) == T_FLOAT) {
|
||||||
VALUE magn = m_hypot(bdat->real, bdat->image);
|
VALUE magn = m_hypot(bdat->real, bdat->image);
|
||||||
VALUE tmp = f_complex_new_bang2(CLASS_OF(self),
|
VALUE tmp = f_complex_new_bang2(CLASS_OF(self),
|
||||||
f_div(bdat->real, magn),
|
f_div(bdat->real, magn),
|
||||||
f_div(bdat->image, magn));
|
f_div(bdat->image, magn));
|
||||||
return f_div(f_mul(self, f_conjugate(tmp)), magn);
|
return f_div(f_mul(self, f_conjugate(tmp)), magn);
|
||||||
}
|
}
|
||||||
return f_div(f_mul(self, f_conjugate(other)), f_abs2(other));
|
return f_div(f_mul(self, f_conjugate(other)), f_abs2(other));
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return rb_num_coerce_bin(self, other, '/');
|
return rb_num_coerce_bin(self, other, '/');
|
||||||
}
|
}
|
||||||
@ -759,37 +770,38 @@ nucomp_expt(VALUE self, VALUE other)
|
|||||||
n = f_sub(n, ONE);
|
n = f_sub(n, ONE);
|
||||||
}
|
}
|
||||||
return z;
|
return z;
|
||||||
} else {
|
}
|
||||||
|
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:
|
||||||
{
|
{
|
||||||
VALUE a, r, theta;
|
VALUE a, r, theta;
|
||||||
|
|
||||||
a = f_polar(self);
|
a = f_polar(self);
|
||||||
r = RARRAY_PTR(a)[0];
|
r = RARRAY_PTR(a)[0];
|
||||||
theta = RARRAY_PTR(a)[1];
|
theta = RARRAY_PTR(a)[1];
|
||||||
return nucomp_s_polar(CLASS_OF(self), f_expt(r, other),
|
return nucomp_s_polar(CLASS_OF(self), f_expt(r, other),
|
||||||
f_mul(theta, other));
|
f_mul(theta, other));
|
||||||
}
|
}
|
||||||
case T_COMPLEX:
|
case T_COMPLEX:
|
||||||
{
|
{
|
||||||
VALUE a, r, theta, ore, oim, nr, ntheta;
|
VALUE a, r, theta, ore, oim, nr, ntheta;
|
||||||
|
|
||||||
get_dat1(other);
|
get_dat1(other);
|
||||||
|
|
||||||
a = f_polar(self);
|
a = f_polar(self);
|
||||||
r = RARRAY_PTR(a)[0];
|
r = RARRAY_PTR(a)[0];
|
||||||
theta = RARRAY_PTR(a)[1];
|
theta = RARRAY_PTR(a)[1];
|
||||||
|
|
||||||
ore = dat->real;
|
ore = dat->real;
|
||||||
oim = dat->image;
|
oim = dat->image;
|
||||||
nr = m_exp_bang(f_sub(f_mul(ore, m_log_bang(r)),
|
nr = m_exp_bang(f_sub(f_mul(ore, m_log_bang(r)),
|
||||||
f_mul(oim, theta)));
|
f_mul(oim, theta)));
|
||||||
ntheta = f_add(f_mul(theta, ore), f_mul(oim, m_log_bang(r)));
|
ntheta = f_add(f_mul(theta, ore), f_mul(oim, m_log_bang(r)));
|
||||||
return nucomp_s_polar(CLASS_OF(self), nr, ntheta);
|
return nucomp_s_polar(CLASS_OF(self), nr, ntheta);
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return rb_num_coerce_bin(self, other, id_expt);
|
return rb_num_coerce_bin(self, other, id_expt);
|
||||||
}
|
}
|
||||||
@ -803,18 +815,18 @@ nucomp_equal_p(VALUE self, VALUE other)
|
|||||||
case T_BIGNUM:
|
case T_BIGNUM:
|
||||||
case T_FLOAT:
|
case T_FLOAT:
|
||||||
case T_RATIONAL:
|
case T_RATIONAL:
|
||||||
{
|
{
|
||||||
get_dat1(self);
|
get_dat1(self);
|
||||||
|
|
||||||
return f_boolcast(f_equal_p(dat->real, other) && f_zero_p(dat->image));
|
return f_boolcast(f_equal_p(dat->real, other) && f_zero_p(dat->image));
|
||||||
}
|
}
|
||||||
case T_COMPLEX:
|
case T_COMPLEX:
|
||||||
{
|
{
|
||||||
get_dat2(self, other);
|
get_dat2(self, other);
|
||||||
|
|
||||||
return f_boolcast(f_equal_p(adat->real, bdat->real) &&
|
return f_boolcast(f_equal_p(adat->real, bdat->real) &&
|
||||||
f_equal_p(adat->image, bdat->image));
|
f_equal_p(adat->image, bdat->image));
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return f_equal_p(other, self);
|
return f_equal_p(other, self);
|
||||||
}
|
}
|
||||||
@ -989,7 +1001,8 @@ nucomp_to_s(VALUE self)
|
|||||||
rb_str_cat2(s, "(");
|
rb_str_cat2(s, "(");
|
||||||
rb_str_concat(s, f_to_s(rezero ? dat->image : f_abs(dat->image)));
|
rb_str_concat(s, f_to_s(rezero ? dat->image : f_abs(dat->image)));
|
||||||
rb_str_cat2(s, ")i");
|
rb_str_cat2(s, ")i");
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
rb_str_concat(s, f_to_s(rezero ? dat->image : f_abs(dat->image)));
|
rb_str_concat(s, f_to_s(rezero ? dat->image : f_abs(dat->image)));
|
||||||
rb_str_cat2(s, "i");
|
rb_str_cat2(s, "i");
|
||||||
}
|
}
|
||||||
@ -1287,22 +1300,22 @@ nucomp_s_convert(int argc, VALUE *argv, VALUE klass)
|
|||||||
|
|
||||||
switch (TYPE(a1)) {
|
switch (TYPE(a1)) {
|
||||||
case T_COMPLEX:
|
case T_COMPLEX:
|
||||||
{
|
{
|
||||||
get_dat1(a1);
|
get_dat1(a1);
|
||||||
|
|
||||||
if (!k_float_p(dat->image) && f_zero_p(dat->image))
|
if (!k_float_p(dat->image) && f_zero_p(dat->image))
|
||||||
a1 = dat->real;
|
a1 = dat->real;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (TYPE(a2)) {
|
switch (TYPE(a2)) {
|
||||||
case T_COMPLEX:
|
case T_COMPLEX:
|
||||||
{
|
{
|
||||||
get_dat1(a2);
|
get_dat1(a2);
|
||||||
|
|
||||||
if (!k_float_p(dat->image) && f_zero_p(dat->image))
|
if (!k_float_p(dat->image) && f_zero_p(dat->image))
|
||||||
a2 = dat->real;
|
a2 = dat->real;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (TYPE(a1)) {
|
switch (TYPE(a1)) {
|
||||||
|
239
rational.c
239
rational.c
@ -23,7 +23,7 @@
|
|||||||
VALUE rb_cRational;
|
VALUE rb_cRational;
|
||||||
|
|
||||||
static ID id_Unify, id_abs, id_cmp, id_convert, id_equal_p,
|
static ID id_Unify, id_abs, id_cmp, id_convert, id_equal_p,
|
||||||
id_expt, id_floor, id_format,id_idiv, id_inspect, id_negate, id_new,
|
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;
|
id_new_bang, id_to_f, id_to_i, id_to_s, id_truncate;
|
||||||
|
|
||||||
#define f_boolcast(x) ((x) ? Qtrue : Qfalse)
|
#define f_boolcast(x) ((x) ? Qtrue : Qfalse)
|
||||||
@ -58,12 +58,14 @@ f_add(VALUE x, VALUE y)
|
|||||||
r = x;
|
r = x;
|
||||||
else
|
else
|
||||||
r = rb_funcall(x, '+', 1, y);
|
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;
|
r = y;
|
||||||
else
|
else
|
||||||
r = rb_funcall(x, '+', 1, y);
|
r = rb_funcall(x, '+', 1, y);
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
r = rb_funcall(x, '+', 1, y);
|
r = rb_funcall(x, '+', 1, y);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
@ -79,7 +81,8 @@ f_cmp(VALUE x, VALUE y)
|
|||||||
else if (c < 0)
|
else if (c < 0)
|
||||||
c = -1;
|
c = -1;
|
||||||
r = INT2FIX(c);
|
r = INT2FIX(c);
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
r = rb_funcall(x, id_cmp, 1, y);
|
r = rb_funcall(x, id_cmp, 1, y);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
@ -98,12 +101,12 @@ f_div(VALUE x, VALUE y)
|
|||||||
inline static VALUE
|
inline static VALUE
|
||||||
f_gt_p(VALUE x, VALUE y)
|
f_gt_p(VALUE x, VALUE y)
|
||||||
{
|
{
|
||||||
VALUE r;
|
VALUE r;
|
||||||
if (FIXNUM_P(x) && FIXNUM_P(y))
|
if (FIXNUM_P(x) && FIXNUM_P(y))
|
||||||
r = f_boolcast(FIX2LONG(x) > FIX2LONG(y));
|
r = f_boolcast(FIX2LONG(x) > FIX2LONG(y));
|
||||||
else
|
else
|
||||||
r = rb_funcall(x, '>', 1, y);
|
r = rb_funcall(x, '>', 1, y);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline static VALUE
|
inline static VALUE
|
||||||
@ -130,22 +133,26 @@ f_mul(VALUE x, VALUE y)
|
|||||||
r = rb_float_new(0.0);
|
r = rb_float_new(0.0);
|
||||||
else
|
else
|
||||||
r = ZERO;
|
r = ZERO;
|
||||||
} else if (_iy == 1)
|
}
|
||||||
|
else if (_iy == 1)
|
||||||
r = x;
|
r = x;
|
||||||
else
|
else
|
||||||
r = rb_funcall(x, '*', 1, y);
|
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);
|
r = rb_float_new(0.0);
|
||||||
else
|
else
|
||||||
r = ZERO;
|
r = ZERO;
|
||||||
} else if (_ix == 1)
|
}
|
||||||
|
else if (_ix == 1)
|
||||||
r = y;
|
r = y;
|
||||||
else
|
else
|
||||||
r = rb_funcall(x, '*', 1, y);
|
r = rb_funcall(x, '*', 1, y);
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
r = rb_funcall(x, '*', 1, y);
|
r = rb_funcall(x, '*', 1, y);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
@ -159,7 +166,8 @@ f_sub(VALUE x, VALUE y)
|
|||||||
r = x;
|
r = x;
|
||||||
else
|
else
|
||||||
r = rb_funcall(x, '-', 1, y);
|
r = rb_funcall(x, '-', 1, y);
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
r = rb_funcall(x, '-', 1, y);
|
r = rb_funcall(x, '-', 1, y);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
@ -635,7 +643,8 @@ f_addsub(VALUE self, VALUE anum, VALUE aden, VALUE bnum, VALUE bden, int k)
|
|||||||
num = f_idiv(c, g);
|
num = f_idiv(c, g);
|
||||||
a = f_idiv(bden, g);
|
a = f_idiv(bden, g);
|
||||||
den = f_mul(a, b);
|
den = f_mul(a, b);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
VALUE g = f_gcd(aden, bden);
|
VALUE g = f_gcd(aden, bden);
|
||||||
VALUE a = f_mul(anum, f_idiv(bden, g));
|
VALUE a = f_mul(anum, f_idiv(bden, g));
|
||||||
VALUE b = f_mul(bnum, f_idiv(aden, g));
|
VALUE b = f_mul(bnum, f_idiv(aden, g));
|
||||||
@ -661,23 +670,23 @@ nurat_add(VALUE self, VALUE other)
|
|||||||
switch (TYPE(other)) {
|
switch (TYPE(other)) {
|
||||||
case T_FIXNUM:
|
case T_FIXNUM:
|
||||||
case T_BIGNUM:
|
case T_BIGNUM:
|
||||||
{
|
{
|
||||||
get_dat1(self);
|
get_dat1(self);
|
||||||
|
|
||||||
return f_addsub(self,
|
return f_addsub(self,
|
||||||
dat->num, dat->den,
|
dat->num, dat->den,
|
||||||
other, ONE, '+');
|
other, ONE, '+');
|
||||||
}
|
}
|
||||||
case T_FLOAT:
|
case T_FLOAT:
|
||||||
return f_add(f_to_f(self), other);
|
return f_add(f_to_f(self), other);
|
||||||
case T_RATIONAL:
|
case T_RATIONAL:
|
||||||
{
|
{
|
||||||
get_dat2(self, other);
|
get_dat2(self, other);
|
||||||
|
|
||||||
return f_addsub(self,
|
return f_addsub(self,
|
||||||
adat->num, adat->den,
|
adat->num, adat->den,
|
||||||
bdat->num, bdat->den, '+');
|
bdat->num, bdat->den, '+');
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return rb_num_coerce_bin(self, other, '+');
|
return rb_num_coerce_bin(self, other, '+');
|
||||||
}
|
}
|
||||||
@ -689,23 +698,23 @@ nurat_sub(VALUE self, VALUE other)
|
|||||||
switch (TYPE(other)) {
|
switch (TYPE(other)) {
|
||||||
case T_FIXNUM:
|
case T_FIXNUM:
|
||||||
case T_BIGNUM:
|
case T_BIGNUM:
|
||||||
{
|
{
|
||||||
get_dat1(self);
|
get_dat1(self);
|
||||||
|
|
||||||
return f_addsub(self,
|
return f_addsub(self,
|
||||||
dat->num, dat->den,
|
dat->num, dat->den,
|
||||||
other, ONE, '-');
|
other, ONE, '-');
|
||||||
}
|
}
|
||||||
case T_FLOAT:
|
case T_FLOAT:
|
||||||
return f_sub(f_to_f(self), other);
|
return f_sub(f_to_f(self), other);
|
||||||
case T_RATIONAL:
|
case T_RATIONAL:
|
||||||
{
|
{
|
||||||
get_dat2(self, other);
|
get_dat2(self, other);
|
||||||
|
|
||||||
return f_addsub(self,
|
return f_addsub(self,
|
||||||
adat->num, adat->den,
|
adat->num, adat->den,
|
||||||
bdat->num, bdat->den, '-');
|
bdat->num, bdat->den, '-');
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return rb_num_coerce_bin(self, other, '-');
|
return rb_num_coerce_bin(self, other, '-');
|
||||||
}
|
}
|
||||||
@ -739,7 +748,8 @@ f_muldiv(VALUE self, VALUE anum, VALUE aden, VALUE bnum, VALUE bden, int k)
|
|||||||
|
|
||||||
num = f_imul(an / g1, bn / g2);
|
num = f_imul(an / g1, bn / g2);
|
||||||
den = f_imul(ad / g2, bd / g1);
|
den = f_imul(ad / g2, bd / g1);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
VALUE g1 = f_gcd(anum, bden);
|
VALUE g1 = f_gcd(anum, bden);
|
||||||
VALUE g2 = f_gcd(aden, bnum);
|
VALUE g2 = f_gcd(aden, bnum);
|
||||||
|
|
||||||
@ -755,23 +765,23 @@ nurat_mul(VALUE self, VALUE other)
|
|||||||
switch (TYPE(other)) {
|
switch (TYPE(other)) {
|
||||||
case T_FIXNUM:
|
case T_FIXNUM:
|
||||||
case T_BIGNUM:
|
case T_BIGNUM:
|
||||||
{
|
{
|
||||||
get_dat1(self);
|
get_dat1(self);
|
||||||
|
|
||||||
return f_muldiv(self,
|
return f_muldiv(self,
|
||||||
dat->num, dat->den,
|
dat->num, dat->den,
|
||||||
other, ONE, '*');
|
other, ONE, '*');
|
||||||
}
|
}
|
||||||
case T_FLOAT:
|
case T_FLOAT:
|
||||||
return f_mul(f_to_f(self), other);
|
return f_mul(f_to_f(self), other);
|
||||||
case T_RATIONAL:
|
case T_RATIONAL:
|
||||||
{
|
{
|
||||||
get_dat2(self, other);
|
get_dat2(self, other);
|
||||||
|
|
||||||
return f_muldiv(self,
|
return f_muldiv(self,
|
||||||
adat->num, adat->den,
|
adat->num, adat->den,
|
||||||
bdat->num, bdat->den, '*');
|
bdat->num, bdat->den, '*');
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return rb_num_coerce_bin(self, other, '*');
|
return rb_num_coerce_bin(self, other, '*');
|
||||||
}
|
}
|
||||||
@ -831,31 +841,31 @@ nurat_expt(VALUE self, VALUE other)
|
|||||||
switch (TYPE(other)) {
|
switch (TYPE(other)) {
|
||||||
case T_FIXNUM:
|
case T_FIXNUM:
|
||||||
case T_BIGNUM:
|
case T_BIGNUM:
|
||||||
{
|
{
|
||||||
VALUE num, den;
|
VALUE num, den;
|
||||||
|
|
||||||
get_dat1(self);
|
get_dat1(self);
|
||||||
|
|
||||||
switch (FIX2INT(f_cmp(other, ZERO))) {
|
switch (FIX2INT(f_cmp(other, ZERO))) {
|
||||||
case 1:
|
case 1:
|
||||||
num = f_expt(dat->num, other);
|
num = f_expt(dat->num, other);
|
||||||
den = f_expt(dat->den, other);
|
den = f_expt(dat->den, other);
|
||||||
break;
|
break;
|
||||||
case -1:
|
case -1:
|
||||||
num = f_expt(dat->den, f_negate(other));
|
num = f_expt(dat->den, f_negate(other));
|
||||||
den = f_expt(dat->num, f_negate(other));
|
den = f_expt(dat->num, f_negate(other));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
num = ONE;
|
num = ONE;
|
||||||
den = ONE;
|
den = ONE;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (f_negative_p(den)) { /* or use normal new */
|
if (f_negative_p(den)) { /* or use normal new */
|
||||||
num = f_negate(num);
|
num = f_negate(num);
|
||||||
den = f_negate(den);
|
den = f_negate(den);
|
||||||
}
|
}
|
||||||
return f_rational_new_bang2(CLASS_OF(self), num, den);
|
return f_rational_new_bang2(CLASS_OF(self), num, den);
|
||||||
}
|
}
|
||||||
case T_FLOAT:
|
case T_FLOAT:
|
||||||
case T_RATIONAL:
|
case T_RATIONAL:
|
||||||
return f_expt(f_to_f(self), other);
|
return f_expt(f_to_f(self), other);
|
||||||
@ -870,32 +880,33 @@ nurat_cmp(VALUE self, VALUE other)
|
|||||||
switch (TYPE(other)) {
|
switch (TYPE(other)) {
|
||||||
case T_FIXNUM:
|
case T_FIXNUM:
|
||||||
case T_BIGNUM:
|
case T_BIGNUM:
|
||||||
{
|
{
|
||||||
get_dat1(self);
|
get_dat1(self);
|
||||||
|
|
||||||
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
|
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);
|
||||||
case T_RATIONAL:
|
case T_RATIONAL:
|
||||||
{
|
{
|
||||||
VALUE num1, num2;
|
VALUE num1, num2;
|
||||||
|
|
||||||
get_dat2(self, other);
|
get_dat2(self, other);
|
||||||
|
|
||||||
if (FIXNUM_P(adat->num) && FIXNUM_P(adat->den) &&
|
if (FIXNUM_P(adat->num) && FIXNUM_P(adat->den) &&
|
||||||
FIXNUM_P(bdat->num) && FIXNUM_P(bdat->den)) {
|
FIXNUM_P(bdat->num) && FIXNUM_P(bdat->den)) {
|
||||||
num1 = f_imul(FIX2LONG(adat->num), FIX2LONG(bdat->den));
|
num1 = f_imul(FIX2LONG(adat->num), FIX2LONG(bdat->den));
|
||||||
num2 = f_imul(FIX2LONG(bdat->num), FIX2LONG(adat->den));
|
num2 = f_imul(FIX2LONG(bdat->num), FIX2LONG(adat->den));
|
||||||
} else {
|
}
|
||||||
num1 = f_mul(adat->num, bdat->den);
|
else {
|
||||||
num2 = f_mul(bdat->num, adat->den);
|
num1 = f_mul(adat->num, bdat->den);
|
||||||
}
|
num2 = f_mul(bdat->num, adat->den);
|
||||||
return f_cmp(f_sub(num1, num2), ZERO);
|
}
|
||||||
}
|
return f_cmp(f_sub(num1, num2), ZERO);
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return rb_num_coerce_bin(self, other, id_cmp);
|
return rb_num_coerce_bin(self, other, id_cmp);
|
||||||
}
|
}
|
||||||
@ -907,27 +918,27 @@ nurat_equal_p(VALUE self, VALUE other)
|
|||||||
switch (TYPE(other)) {
|
switch (TYPE(other)) {
|
||||||
case T_FIXNUM:
|
case T_FIXNUM:
|
||||||
case T_BIGNUM:
|
case T_BIGNUM:
|
||||||
{
|
{
|
||||||
get_dat1(self);
|
get_dat1(self);
|
||||||
|
|
||||||
if (!FIXNUM_P(dat->den))
|
if (!FIXNUM_P(dat->den))
|
||||||
return Qfalse;
|
return Qfalse;
|
||||||
if (FIX2LONG(dat->den) != 1)
|
if (FIX2LONG(dat->den) != 1)
|
||||||
return Qfalse;
|
return Qfalse;
|
||||||
if (f_equal_p(dat->num, other))
|
if (f_equal_p(dat->num, other))
|
||||||
return Qtrue;
|
return Qtrue;
|
||||||
else
|
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);
|
||||||
case T_RATIONAL:
|
case T_RATIONAL:
|
||||||
{
|
{
|
||||||
get_dat2(self, other);
|
get_dat2(self, other);
|
||||||
|
|
||||||
return f_boolcast(f_equal_p(adat->num, bdat->num) &&
|
return f_boolcast(f_equal_p(adat->num, bdat->num) &&
|
||||||
f_equal_p(adat->den, bdat->den));
|
f_equal_p(adat->den, bdat->den));
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return f_equal_p(other, self);
|
return f_equal_p(other, self);
|
||||||
}
|
}
|
||||||
@ -954,6 +965,7 @@ nurat_idiv(VALUE self, VALUE other)
|
|||||||
{
|
{
|
||||||
return f_floor(f_div(self, other));
|
return f_floor(f_div(self, other));
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
nurat_mod(VALUE self, VALUE other)
|
nurat_mod(VALUE self, VALUE other)
|
||||||
{
|
{
|
||||||
@ -1044,7 +1056,8 @@ nurat_round(VALUE self)
|
|||||||
num = f_add(f_mul(num, TWO), dat->den);
|
num = f_add(f_mul(num, TWO), dat->den);
|
||||||
den = f_mul(dat->den, TWO);
|
den = f_mul(dat->den, TWO);
|
||||||
return f_negate(f_idiv(num, den));
|
return f_negate(f_idiv(num, den));
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
VALUE num = f_add(f_mul(dat->num, TWO), dat->den);
|
VALUE num = f_add(f_mul(dat->num, TWO), dat->den);
|
||||||
VALUE den = f_mul(dat->den, TWO);
|
VALUE den = f_mul(dat->den, TWO);
|
||||||
return f_idiv(num, den);
|
return f_idiv(num, den);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user