summaryrefslogtreecommitdiff
path: root/complex.c
diff options
context:
space:
mode:
authorTakashi Kokubun <takashikkbn@gmail.com>2022-07-21 09:23:58 -0700
committerTakashi Kokubun <takashikkbn@gmail.com>2022-07-21 09:42:04 -0700
commit5b21e94bebed90180d8ff63dad03b8b948361089 (patch)
treef9f7196d84b51b7a3a8001658e4391a63b71c396 /complex.c
parent3ff53c8e04ecc91e0190de6d5950ecce2a2ea188 (diff)
downloadruby-5b21e94bebed90180d8ff63dad03b8b948361089.tar.gz
Expand tabs [ci skip]
[Misc #18891]
Diffstat (limited to 'complex.c')
-rw-r--r--complex.c650
1 files changed, 325 insertions, 325 deletions
diff --git a/complex.c b/complex.c
index 92f4e812d6..d625ced7fa 100644
--- a/complex.c
+++ b/complex.c
@@ -97,7 +97,7 @@ inline static VALUE
f_div(VALUE x, VALUE y)
{
if (FIXNUM_P(y) && FIX2LONG(y) == 1)
- return x;
+ return x;
return rb_funcall(x, '/', 1, y);
}
@@ -152,7 +152,7 @@ f_sub(VALUE x, VALUE y)
{
if (FIXNUM_ZERO_P(y) &&
LIKELY(rb_method_basic_definition_p(CLASS_OF(x), idMINUS))) {
- return x;
+ return x;
}
return rb_funcall(x, '-', 1, y);
}
@@ -262,7 +262,7 @@ inline static VALUE
f_to_i(VALUE x)
{
if (RB_TYPE_P(x, T_STRING))
- return rb_str_to_inum(x, 10, 0);
+ return rb_str_to_inum(x, 10, 0);
return rb_funcall(x, id_to_i, 0);
}
@@ -270,7 +270,7 @@ inline static VALUE
f_to_f(VALUE x)
{
if (RB_TYPE_P(x, T_STRING))
- return DBL2NUM(rb_str_to_dbl(x, 0));
+ return DBL2NUM(rb_str_to_dbl(x, 0));
return rb_funcall(x, id_to_f, 0);
}
@@ -280,9 +280,9 @@ inline static int
f_eqeq_p(VALUE x, VALUE y)
{
if (FIXNUM_P(x) && FIXNUM_P(y))
- return x == y;
+ return x == y;
else if (RB_FLOAT_TYPE_P(x) || RB_FLOAT_TYPE_P(y))
- return NUM2DBL(x) == NUM2DBL(y);
+ return NUM2DBL(x) == NUM2DBL(y);
return (int)rb_equal(x, y);
}
@@ -349,7 +349,7 @@ f_finite_p(VALUE x)
return TRUE;
}
else if (RB_FLOAT_TYPE_P(x)) {
- return isfinite(RFLOAT_VALUE(x));
+ return isfinite(RFLOAT_VALUE(x));
}
return RTEST(rb_funcallv(x, id_finite_p, 0, 0));
}
@@ -361,7 +361,7 @@ f_infinite_p(VALUE x)
return FALSE;
}
else if (RB_FLOAT_TYPE_P(x)) {
- return isinf(RFLOAT_VALUE(x));
+ return isinf(RFLOAT_VALUE(x));
}
return RTEST(rb_funcallv(x, id_infinite_p, 0, 0));
}
@@ -425,10 +425,10 @@ inline static void
nucomp_real_check(VALUE num)
{
if (!RB_INTEGER_TYPE_P(num) &&
- !RB_FLOAT_TYPE_P(num) &&
- !RB_TYPE_P(num, T_RATIONAL)) {
- if (!k_numeric_p(num) || !f_real_p(num))
- rb_raise(rb_eTypeError, "not a real");
+ !RB_FLOAT_TYPE_P(num) &&
+ !RB_TYPE_P(num, T_RATIONAL)) {
+ if (!k_numeric_p(num) || !f_real_p(num))
+ rb_raise(rb_eTypeError, "not a real");
}
}
@@ -439,28 +439,28 @@ nucomp_s_canonicalize_internal(VALUE klass, VALUE real, VALUE imag)
complex_r = RB_TYPE_P(real, T_COMPLEX);
complex_i = RB_TYPE_P(imag, T_COMPLEX);
if (!complex_r && !complex_i) {
- return nucomp_s_new_internal(klass, real, imag);
+ return nucomp_s_new_internal(klass, real, imag);
}
else if (!complex_r) {
- get_dat1(imag);
+ get_dat1(imag);
- return nucomp_s_new_internal(klass,
- f_sub(real, dat->imag),
- f_add(ZERO, dat->real));
+ return nucomp_s_new_internal(klass,
+ f_sub(real, dat->imag),
+ f_add(ZERO, dat->real));
}
else if (!complex_i) {
- get_dat1(real);
+ get_dat1(real);
- return nucomp_s_new_internal(klass,
- dat->real,
- f_add(dat->imag, imag));
+ return nucomp_s_new_internal(klass,
+ dat->real,
+ f_add(dat->imag, imag));
}
else {
- get_dat2(real, imag);
+ get_dat2(real, imag);
- return nucomp_s_new_internal(klass,
- f_sub(adat->real, bdat->imag),
- f_add(adat->imag, bdat->real));
+ return nucomp_s_new_internal(klass,
+ f_sub(adat->real, bdat->imag),
+ f_add(adat->imag, bdat->real));
}
}
@@ -480,13 +480,13 @@ nucomp_s_new(int argc, VALUE *argv, VALUE klass)
switch (rb_scan_args(argc, argv, "11", &real, &imag)) {
case 1:
- nucomp_real_check(real);
- imag = ZERO;
- break;
+ nucomp_real_check(real);
+ imag = ZERO;
+ break;
default:
- nucomp_real_check(real);
- nucomp_real_check(imag);
- break;
+ nucomp_real_check(real);
+ nucomp_real_check(imag);
+ break;
}
return nucomp_s_canonicalize_internal(klass, real, imag);
@@ -580,14 +580,14 @@ static VALUE
m_cos(VALUE x)
{
if (!RB_TYPE_P(x, T_COMPLEX))
- return m_cos_bang(x);
+ return m_cos_bang(x);
{
- get_dat1(x);
- return f_complex_new2(rb_cComplex,
- f_mul(m_cos_bang(dat->real),
- m_cosh_bang(dat->imag)),
- f_mul(f_negate(m_sin_bang(dat->real)),
- m_sinh_bang(dat->imag)));
+ get_dat1(x);
+ return f_complex_new2(rb_cComplex,
+ f_mul(m_cos_bang(dat->real),
+ m_cosh_bang(dat->imag)),
+ f_mul(f_negate(m_sin_bang(dat->real)),
+ m_sinh_bang(dat->imag)));
}
}
@@ -595,14 +595,14 @@ static VALUE
m_sin(VALUE x)
{
if (!RB_TYPE_P(x, T_COMPLEX))
- return m_sin_bang(x);
+ return m_sin_bang(x);
{
- get_dat1(x);
- return f_complex_new2(rb_cComplex,
- f_mul(m_sin_bang(dat->real),
- m_cosh_bang(dat->imag)),
- f_mul(m_cos_bang(dat->real),
- m_sinh_bang(dat->imag)));
+ get_dat1(x);
+ return f_complex_new2(rb_cComplex,
+ f_mul(m_sin_bang(dat->real),
+ m_cosh_bang(dat->imag)),
+ f_mul(m_cos_bang(dat->real),
+ m_sinh_bang(dat->imag)));
}
}
@@ -612,38 +612,38 @@ f_complex_polar(VALUE klass, VALUE x, VALUE y)
assert(!RB_TYPE_P(x, T_COMPLEX));
assert(!RB_TYPE_P(y, T_COMPLEX));
if (f_zero_p(x) || f_zero_p(y)) {
- return nucomp_s_new_internal(klass, x, RFLOAT_0);
+ return nucomp_s_new_internal(klass, x, RFLOAT_0);
}
if (RB_FLOAT_TYPE_P(y)) {
- const double arg = RFLOAT_VALUE(y);
- if (arg == M_PI) {
- x = f_negate(x);
- y = RFLOAT_0;
- }
- else if (arg == M_PI_2) {
- y = x;
- x = RFLOAT_0;
- }
- else if (arg == M_PI_2+M_PI) {
- y = f_negate(x);
- x = RFLOAT_0;
- }
- else if (RB_FLOAT_TYPE_P(x)) {
- const double abs = RFLOAT_VALUE(x);
- const double real = abs * cos(arg), imag = abs * sin(arg);
- x = DBL2NUM(real);
- y = DBL2NUM(imag);
- }
- else {
+ const double arg = RFLOAT_VALUE(y);
+ if (arg == M_PI) {
+ x = f_negate(x);
+ y = RFLOAT_0;
+ }
+ else if (arg == M_PI_2) {
+ y = x;
+ x = RFLOAT_0;
+ }
+ else if (arg == M_PI_2+M_PI) {
+ y = f_negate(x);
+ x = RFLOAT_0;
+ }
+ else if (RB_FLOAT_TYPE_P(x)) {
+ const double abs = RFLOAT_VALUE(x);
+ const double real = abs * cos(arg), imag = abs * sin(arg);
+ x = DBL2NUM(real);
+ y = DBL2NUM(imag);
+ }
+ else {
const double ax = sin(arg), ay = cos(arg);
y = f_mul(x, DBL2NUM(ax));
x = f_mul(x, DBL2NUM(ay));
- }
- return nucomp_s_new_internal(klass, x, y);
+ }
+ return nucomp_s_new_internal(klass, x, y);
}
return nucomp_s_canonicalize_internal(klass,
- f_mul(x, m_cos(y)),
- f_mul(x, m_sin(y)));
+ f_mul(x, m_cos(y)),
+ f_mul(x, m_sin(y)));
}
#ifdef HAVE___COSPI
@@ -665,12 +665,12 @@ rb_dbl_complex_new_polar_pi(double abs, double ang)
int pos = fr == +0.5;
if (pos || fr == -0.5) {
- if ((modf(fi / 2.0, &fi) != fr) ^ pos) abs = -abs;
- return rb_complex_new(RFLOAT_0, DBL2NUM(abs));
+ if ((modf(fi / 2.0, &fi) != fr) ^ pos) abs = -abs;
+ return rb_complex_new(RFLOAT_0, DBL2NUM(abs));
}
else if (fr == 0.0) {
- if (modf(fi / 2.0, &fi) != 0.0) abs = -abs;
- return DBL2NUM(abs);
+ if (modf(fi / 2.0, &fi) != 0.0) abs = -abs;
+ return DBL2NUM(abs);
}
else {
const double real = abs * cospi(ang), imag = abs * sinpi(ang);
@@ -696,12 +696,12 @@ nucomp_s_polar(int argc, VALUE *argv, VALUE klass)
switch (rb_scan_args(argc, argv, "11", &abs, &arg)) {
case 1:
- nucomp_real_check(abs);
- return nucomp_s_new_internal(klass, abs, ZERO);
+ nucomp_real_check(abs);
+ return nucomp_s_new_internal(klass, abs, ZERO);
default:
- nucomp_real_check(abs);
- nucomp_real_check(arg);
- break;
+ nucomp_real_check(abs);
+ nucomp_real_check(arg);
+ break;
}
if (RB_TYPE_P(abs, T_COMPLEX)) {
get_dat1(abs);
@@ -760,7 +760,7 @@ rb_complex_uminus(VALUE self)
{
get_dat1(self);
return f_complex_new2(CLASS_OF(self),
- f_negate(dat->real), f_negate(dat->imag));
+ f_negate(dat->real), f_negate(dat->imag));
}
/*
@@ -779,20 +779,20 @@ VALUE
rb_complex_plus(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_COMPLEX)) {
- VALUE real, imag;
+ VALUE real, imag;
- get_dat2(self, other);
+ get_dat2(self, other);
- real = f_add(adat->real, bdat->real);
- imag = f_add(adat->imag, bdat->imag);
+ real = f_add(adat->real, bdat->real);
+ imag = f_add(adat->imag, bdat->imag);
- return f_complex_new2(CLASS_OF(self), real, imag);
+ return f_complex_new2(CLASS_OF(self), real, imag);
}
if (k_numeric_p(other) && f_real_p(other)) {
- get_dat1(self);
+ get_dat1(self);
- return f_complex_new2(CLASS_OF(self),
- f_add(dat->real, other), dat->imag);
+ return f_complex_new2(CLASS_OF(self),
+ f_add(dat->real, other), dat->imag);
}
return rb_num_coerce_bin(self, other, '+');
}
@@ -813,20 +813,20 @@ VALUE
rb_complex_minus(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_COMPLEX)) {
- VALUE real, imag;
+ VALUE real, imag;
- get_dat2(self, other);
+ get_dat2(self, other);
- real = f_sub(adat->real, bdat->real);
- imag = f_sub(adat->imag, bdat->imag);
+ real = f_sub(adat->real, bdat->real);
+ imag = f_sub(adat->imag, bdat->imag);
- return f_complex_new2(CLASS_OF(self), real, imag);
+ return f_complex_new2(CLASS_OF(self), real, imag);
}
if (k_numeric_p(other) && f_real_p(other)) {
- get_dat1(self);
+ get_dat1(self);
- return f_complex_new2(CLASS_OF(self),
- f_sub(dat->real, other), dat->imag);
+ return f_complex_new2(CLASS_OF(self),
+ f_sub(dat->real, other), dat->imag);
}
return rb_num_coerce_bin(self, other, '-');
}
@@ -836,10 +836,10 @@ safe_mul(VALUE a, VALUE b, int az, int bz)
{
double v;
if (!az && bz && RB_FLOAT_TYPE_P(a) && (v = RFLOAT_VALUE(a), !isnan(v))) {
- a = signbit(v) ? DBL2NUM(-1.0) : DBL2NUM(1.0);
+ a = signbit(v) ? DBL2NUM(-1.0) : DBL2NUM(1.0);
}
if (!bz && az && RB_FLOAT_TYPE_P(b) && (v = RFLOAT_VALUE(b), !isnan(v))) {
- b = signbit(v) ? DBL2NUM(-1.0) : DBL2NUM(1.0);
+ b = signbit(v) ? DBL2NUM(-1.0) : DBL2NUM(1.0);
}
return f_mul(a, b);
}
@@ -873,47 +873,47 @@ VALUE
rb_complex_mul(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_COMPLEX)) {
- VALUE real, imag;
- get_dat2(self, other);
+ VALUE real, imag;
+ get_dat2(self, other);
comp_mul(adat->real, adat->imag, bdat->real, bdat->imag, &real, &imag);
- return f_complex_new2(CLASS_OF(self), real, imag);
+ return f_complex_new2(CLASS_OF(self), real, imag);
}
if (k_numeric_p(other) && f_real_p(other)) {
- get_dat1(self);
+ get_dat1(self);
- return f_complex_new2(CLASS_OF(self),
- f_mul(dat->real, other),
- f_mul(dat->imag, other));
+ return f_complex_new2(CLASS_OF(self),
+ f_mul(dat->real, other),
+ f_mul(dat->imag, other));
}
return rb_num_coerce_bin(self, other, '*');
}
inline static VALUE
f_divide(VALUE self, VALUE other,
- VALUE (*func)(VALUE, VALUE), ID id)
+ VALUE (*func)(VALUE, VALUE), ID id)
{
if (RB_TYPE_P(other, T_COMPLEX)) {
VALUE r, n, x, y;
- int flo;
- get_dat2(self, other);
+ int flo;
+ get_dat2(self, other);
- flo = (RB_FLOAT_TYPE_P(adat->real) || RB_FLOAT_TYPE_P(adat->imag) ||
- RB_FLOAT_TYPE_P(bdat->real) || RB_FLOAT_TYPE_P(bdat->imag));
+ flo = (RB_FLOAT_TYPE_P(adat->real) || RB_FLOAT_TYPE_P(adat->imag) ||
+ RB_FLOAT_TYPE_P(bdat->real) || RB_FLOAT_TYPE_P(bdat->imag));
- if (f_gt_p(f_abs(bdat->real), f_abs(bdat->imag))) {
- r = (*func)(bdat->imag, bdat->real);
- n = f_mul(bdat->real, f_add(ONE, f_mul(r, r)));
+ if (f_gt_p(f_abs(bdat->real), f_abs(bdat->imag))) {
+ r = (*func)(bdat->imag, bdat->real);
+ n = f_mul(bdat->real, f_add(ONE, f_mul(r, r)));
x = (*func)(f_add(adat->real, f_mul(adat->imag, r)), n);
y = (*func)(f_sub(adat->imag, f_mul(adat->real, r)), n);
- }
- else {
- r = (*func)(bdat->real, bdat->imag);
- n = f_mul(bdat->imag, f_add(ONE, f_mul(r, r)));
+ }
+ else {
+ r = (*func)(bdat->real, bdat->imag);
+ n = f_mul(bdat->imag, f_add(ONE, f_mul(r, r)));
x = (*func)(f_add(f_mul(adat->real, r), adat->imag), n);
y = (*func)(f_sub(f_mul(adat->imag, r), adat->real), n);
- }
+ }
if (!flo) {
x = rb_rational_canonicalize(x);
y = rb_rational_canonicalize(y);
@@ -922,7 +922,7 @@ f_divide(VALUE self, VALUE other,
}
if (k_numeric_p(other) && f_real_p(other)) {
VALUE x, y;
- get_dat1(self);
+ get_dat1(self);
x = rb_rational_canonicalize((*func)(dat->real, other));
y = rb_rational_canonicalize((*func)(dat->imag, other));
return f_complex_new2(CLASS_OF(self), x, y);
@@ -986,31 +986,31 @@ VALUE
rb_complex_pow(VALUE self, VALUE other)
{
if (k_numeric_p(other) && k_exact_zero_p(other))
- return f_complex_new_bang1(CLASS_OF(self), ONE);
+ return f_complex_new_bang1(CLASS_OF(self), ONE);
if (RB_TYPE_P(other, T_RATIONAL) && RRATIONAL(other)->den == LONG2FIX(1))
- other = RRATIONAL(other)->num; /* c14n */
+ other = RRATIONAL(other)->num; /* c14n */
if (RB_TYPE_P(other, T_COMPLEX)) {
- get_dat1(other);
+ get_dat1(other);
- if (k_exact_zero_p(dat->imag))
- other = dat->real; /* c14n */
+ if (k_exact_zero_p(dat->imag))
+ other = dat->real; /* c14n */
}
if (RB_TYPE_P(other, T_COMPLEX)) {
- VALUE r, theta, nr, ntheta;
+ VALUE r, theta, nr, ntheta;
- get_dat1(other);
+ get_dat1(other);
- r = f_abs(self);
- theta = f_arg(self);
+ r = f_abs(self);
+ theta = f_arg(self);
- nr = m_exp_bang(f_sub(f_mul(dat->real, m_log_bang(r)),
- f_mul(dat->imag, theta)));
- ntheta = f_add(f_mul(theta, dat->real),
- f_mul(dat->imag, m_log_bang(r)));
- return f_complex_polar(CLASS_OF(self), nr, ntheta);
+ nr = m_exp_bang(f_sub(f_mul(dat->real, m_log_bang(r)),
+ f_mul(dat->imag, theta)));
+ ntheta = f_add(f_mul(theta, dat->real),
+ f_mul(dat->imag, m_log_bang(r)));
+ return f_complex_polar(CLASS_OF(self), nr, ntheta);
}
if (FIXNUM_P(other)) {
long n = FIX2LONG(other);
@@ -1051,19 +1051,19 @@ rb_complex_pow(VALUE self, VALUE other)
}
}
return nucomp_s_new_internal(CLASS_OF(self), zr, zi);
- }
+ }
}
if (k_numeric_p(other) && f_real_p(other)) {
- VALUE r, theta;
+ VALUE r, theta;
- if (RB_BIGNUM_TYPE_P(other))
- rb_warn("in a**b, b may be too big");
+ if (RB_BIGNUM_TYPE_P(other))
+ rb_warn("in a**b, b may be too big");
- r = f_abs(self);
- theta = f_arg(self);
+ r = f_abs(self);
+ theta = f_arg(self);
- return f_complex_polar(CLASS_OF(self), f_expt(r, other),
- f_mul(theta, other));
+ return f_complex_polar(CLASS_OF(self), f_expt(r, other),
+ f_mul(theta, other));
}
return rb_num_coerce_bin(self, other, id_expt);
}
@@ -1084,15 +1084,15 @@ static VALUE
nucomp_eqeq_p(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_COMPLEX)) {
- get_dat2(self, other);
+ get_dat2(self, other);
- return RBOOL(f_eqeq_p(adat->real, bdat->real) &&
- f_eqeq_p(adat->imag, bdat->imag));
+ return RBOOL(f_eqeq_p(adat->real, bdat->real) &&
+ f_eqeq_p(adat->imag, bdat->imag));
}
if (k_numeric_p(other) && f_real_p(other)) {
- get_dat1(self);
+ get_dat1(self);
- return RBOOL(f_eqeq_p(dat->real, other) && f_zero_p(dat->imag));
+ return RBOOL(f_eqeq_p(dat->real, other) && f_zero_p(dat->imag));
}
return RBOOL(f_eqeq_p(other, self));
}
@@ -1139,12 +1139,12 @@ static VALUE
nucomp_coerce(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_COMPLEX))
- return rb_assoc_new(other, self);
+ return rb_assoc_new(other, self);
if (k_numeric_p(other) && f_real_p(other))
return rb_assoc_new(f_complex_new_bang1(CLASS_OF(self), other), self);
rb_raise(rb_eTypeError, "%"PRIsVALUE" can't be coerced into %"PRIsVALUE,
- rb_obj_class(other), rb_obj_class(self));
+ rb_obj_class(other), rb_obj_class(self));
return Qnil;
}
@@ -1164,16 +1164,16 @@ rb_complex_abs(VALUE self)
get_dat1(self);
if (f_zero_p(dat->real)) {
- VALUE a = f_abs(dat->imag);
- if (RB_FLOAT_TYPE_P(dat->real) && !RB_FLOAT_TYPE_P(dat->imag))
- a = f_to_f(a);
- return a;
+ VALUE a = f_abs(dat->imag);
+ if (RB_FLOAT_TYPE_P(dat->real) && !RB_FLOAT_TYPE_P(dat->imag))
+ a = f_to_f(a);
+ return a;
}
if (f_zero_p(dat->imag)) {
- VALUE a = f_abs(dat->real);
- if (!RB_FLOAT_TYPE_P(dat->real) && RB_FLOAT_TYPE_P(dat->imag))
- a = f_to_f(a);
- return a;
+ VALUE a = f_abs(dat->real);
+ if (!RB_FLOAT_TYPE_P(dat->real) && RB_FLOAT_TYPE_P(dat->imag))
+ a = f_to_f(a);
+ return a;
}
return rb_math_hypot(dat->real, dat->imag);
}
@@ -1192,7 +1192,7 @@ nucomp_abs2(VALUE self)
{
get_dat1(self);
return f_add(f_mul(dat->real, dat->real),
- f_mul(dat->imag, dat->imag));
+ f_mul(dat->imag, dat->imag));
}
/*
@@ -1313,10 +1313,10 @@ nucomp_numerator(VALUE self)
cd = nucomp_denominator(self);
return f_complex_new2(CLASS_OF(self),
- f_mul(f_numerator(dat->real),
- f_div(cd, f_denominator(dat->real))),
- f_mul(f_numerator(dat->imag),
- f_div(cd, f_denominator(dat->imag))));
+ f_mul(f_numerator(dat->real),
+ f_div(cd, f_denominator(dat->real))),
+ f_mul(f_numerator(dat->imag),
+ f_div(cd, f_denominator(dat->imag))));
}
/* :nodoc: */
@@ -1346,11 +1346,11 @@ static VALUE
nucomp_eql_p(VALUE self, VALUE other)
{
if (RB_TYPE_P(other, T_COMPLEX)) {
- get_dat2(self, other);
+ get_dat2(self, other);
- return RBOOL((CLASS_OF(adat->real) == CLASS_OF(bdat->real)) &&
- (CLASS_OF(adat->imag) == CLASS_OF(bdat->imag)) &&
- f_eqeq_p(self, other));
+ return RBOOL((CLASS_OF(adat->real) == CLASS_OF(bdat->real)) &&
+ (CLASS_OF(adat->imag) == CLASS_OF(bdat->imag)) &&
+ f_eqeq_p(self, other));
}
return Qfalse;
@@ -1360,8 +1360,8 @@ inline static int
f_signbit(VALUE x)
{
if (RB_FLOAT_TYPE_P(x)) {
- double f = RFLOAT_VALUE(x);
- return !isnan(f) && signbit(f);
+ double f = RFLOAT_VALUE(x);
+ return !isnan(f) && signbit(f);
}
return f_negative_p(x);
}
@@ -1387,7 +1387,7 @@ f_format(VALUE self, VALUE (*func)(VALUE))
rb_str_concat(s, (*func)(f_abs(dat->imag)));
if (!rb_isdigit(RSTRING_PTR(s)[RSTRING_LEN(s) - 1]))
- rb_str_cat2(s, "*");
+ rb_str_cat2(s, "*");
rb_str_cat2(s, "i");
return s;
@@ -1470,7 +1470,7 @@ rb_complex_infinite_p(VALUE self)
get_dat1(self);
if (!f_infinite_p(dat->real) && !f_infinite_p(dat->imag)) {
- return Qnil;
+ return Qnil;
}
return ONE;
}
@@ -1513,7 +1513,7 @@ nucomp_marshal_load(VALUE self, VALUE a)
{
Check_Type(a, T_ARRAY);
if (RARRAY_LEN(a) != 2)
- rb_raise(rb_eArgError, "marshaled complex must have an array whose length is 2 but %ld", RARRAY_LEN(a));
+ rb_raise(rb_eArgError, "marshaled complex must have an array whose length is 2 but %ld", RARRAY_LEN(a));
rb_ivar_set(self, id_i_real, RARRAY_AREF(a, 0));
rb_ivar_set(self, id_i_imag, RARRAY_AREF(a, 1));
return self;
@@ -1575,8 +1575,8 @@ nucomp_to_i(VALUE self)
get_dat1(self);
if (!k_exact_zero_p(dat->imag)) {
- rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Integer",
- self);
+ rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Integer",
+ self);
}
return f_to_i(dat->real);
}
@@ -1598,8 +1598,8 @@ nucomp_to_f(VALUE self)
get_dat1(self);
if (!k_exact_zero_p(dat->imag)) {
- rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Float",
- self);
+ rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Float",
+ self);
}
return f_to_f(dat->real);
}
@@ -1623,8 +1623,8 @@ nucomp_to_r(VALUE self)
get_dat1(self);
if (!k_exact_zero_p(dat->imag)) {
- rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Rational",
- self);
+ rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Rational",
+ self);
}
return f_to_r(dat->real);
}
@@ -1703,14 +1703,14 @@ issign(int c)
static int
read_sign(const char **s,
- char **b)
+ char **b)
{
int sign = '?';
if (issign(**s)) {
- sign = **b = **s;
- (*s)++;
- (*b)++;
+ sign = **b = **s;
+ (*s)++;
+ (*b)++;
}
return sign;
}
@@ -1723,32 +1723,32 @@ isdecimal(int c)
static int
read_digits(const char **s, int strict,
- char **b)
+ char **b)
{
int us = 1;
if (!isdecimal(**s))
- return 0;
+ return 0;
while (isdecimal(**s) || **s == '_') {
- if (**s == '_') {
- if (strict) {
- if (us)
- return 0;
- }
- us = 1;
- }
- else {
- **b = **s;
- (*b)++;
- us = 0;
- }
- (*s)++;
+ if (**s == '_') {
+ if (strict) {
+ if (us)
+ return 0;
+ }
+ us = 1;
+ }
+ else {
+ **b = **s;
+ (*b)++;
+ us = 0;
+ }
+ (*s)++;
}
if (us)
- do {
- (*s)--;
- } while (**s == '_');
+ do {
+ (*s)--;
+ } while (**s == '_');
return 1;
}
@@ -1760,70 +1760,70 @@ islettere(int c)
static int
read_num(const char **s, int strict,
- char **b)
+ char **b)
{
if (**s != '.') {
- if (!read_digits(s, strict, b))
- return 0;
+ if (!read_digits(s, strict, b))
+ return 0;
}
if (**s == '.') {
- **b = **s;
- (*s)++;
- (*b)++;
- if (!read_digits(s, strict, b)) {
- (*b)--;
- return 0;
- }
+ **b = **s;
+ (*s)++;
+ (*b)++;
+ if (!read_digits(s, strict, b)) {
+ (*b)--;
+ return 0;
+ }
}
if (islettere(**s)) {
- **b = **s;
- (*s)++;
- (*b)++;
- read_sign(s, b);
- if (!read_digits(s, strict, b)) {
- (*b)--;
- return 0;
- }
+ **b = **s;
+ (*s)++;
+ (*b)++;
+ read_sign(s, b);
+ if (!read_digits(s, strict, b)) {
+ (*b)--;
+ return 0;
+ }
}
return 1;
}
inline static int
read_den(const char **s, int strict,
- char **b)
+ char **b)
{
if (!read_digits(s, strict, b))
- return 0;
+ return 0;
return 1;
}
static int
read_rat_nos(const char **s, int strict,
- char **b)
+ char **b)
{
if (!read_num(s, strict, b))
- return 0;
+ return 0;
if (**s == '/') {
- **b = **s;
- (*s)++;
- (*b)++;
- if (!read_den(s, strict, b)) {
- (*b)--;
- return 0;
- }
+ **b = **s;
+ (*s)++;
+ (*b)++;
+ if (!read_den(s, strict, b)) {
+ (*b)--;
+ return 0;
+ }
}
return 1;
}
static int
read_rat(const char **s, int strict,
- char **b)
+ char **b)
{
read_sign(s, b);
if (!read_rat_nos(s, strict, b))
- return 0;
+ return 0;
return 1;
}
@@ -1831,22 +1831,22 @@ inline static int
isimagunit(int c)
{
return (c == 'i' || c == 'I' ||
- c == 'j' || c == 'J');
+ c == 'j' || c == 'J');
}
static VALUE
str2num(char *s)
{
if (strchr(s, '/'))
- return rb_cstr_to_rat(s, 0);
+ return rb_cstr_to_rat(s, 0);
if (strpbrk(s, ".eE"))
- return DBL2NUM(rb_cstr_to_dbl(s, 0));
+ return DBL2NUM(rb_cstr_to_dbl(s, 0));
return rb_cstr_to_inum(s, 10, 0);
}
static int
read_comp(const char **s, int strict,
- VALUE *ret, char **b)
+ VALUE *ret, char **b)
{
char *bb;
int sign;
@@ -1857,72 +1857,72 @@ read_comp(const char **s, int strict,
sign = read_sign(s, b);
if (isimagunit(**s)) {
- (*s)++;
- num = INT2FIX((sign == '-') ? -1 : + 1);
- *ret = rb_complex_new2(ZERO, num);
- return 1; /* e.g. "i" */
+ (*s)++;
+ num = INT2FIX((sign == '-') ? -1 : + 1);
+ *ret = rb_complex_new2(ZERO, num);
+ return 1; /* e.g. "i" */
}
if (!read_rat_nos(s, strict, b)) {
- **b = '\0';
- num = str2num(bb);
- *ret = rb_complex_new2(num, ZERO);
- return 0; /* e.g. "-" */
+ **b = '\0';
+ num = str2num(bb);
+ *ret = rb_complex_new2(num, ZERO);
+ return 0; /* e.g. "-" */
}
**b = '\0';
num = str2num(bb);
if (isimagunit(**s)) {
- (*s)++;
- *ret = rb_complex_new2(ZERO, num);
- return 1; /* e.g. "3i" */
+ (*s)++;
+ *ret = rb_complex_new2(ZERO, num);
+ return 1; /* e.g. "3i" */
}
if (**s == '@') {
- int st;
-
- (*s)++;
- bb = *b;
- st = read_rat(s, strict, b);
- **b = '\0';
- if (strlen(bb) < 1 ||
- !isdecimal(*(bb + strlen(bb) - 1))) {
- *ret = rb_complex_new2(num, ZERO);
- return 0; /* e.g. "1@-" */
- }
- num2 = str2num(bb);
- *ret = rb_complex_new_polar(num, num2);
- if (!st)
- return 0; /* e.g. "1@2." */
- else
- return 1; /* e.g. "1@2" */
+ int st;
+
+ (*s)++;
+ bb = *b;
+ st = read_rat(s, strict, b);
+ **b = '\0';
+ if (strlen(bb) < 1 ||
+ !isdecimal(*(bb + strlen(bb) - 1))) {
+ *ret = rb_complex_new2(num, ZERO);
+ return 0; /* e.g. "1@-" */
+ }
+ num2 = str2num(bb);
+ *ret = rb_complex_new_polar(num, num2);
+ if (!st)
+ return 0; /* e.g. "1@2." */
+ else
+ return 1; /* e.g. "1@2" */
}
if (issign(**s)) {
- bb = *b;
- sign = read_sign(s, b);
- if (isimagunit(**s))
- num2 = INT2FIX((sign == '-') ? -1 : + 1);
- else {
- if (!read_rat_nos(s, strict, b)) {
- *ret = rb_complex_new2(num, ZERO);
- return 0; /* e.g. "1+xi" */
- }
- **b = '\0';
- num2 = str2num(bb);
- }
- if (!isimagunit(**s)) {
- *ret = rb_complex_new2(num, ZERO);
- return 0; /* e.g. "1+3x" */
- }
- (*s)++;
- *ret = rb_complex_new2(num, num2);
- return 1; /* e.g. "1+2i" */
+ bb = *b;
+ sign = read_sign(s, b);
+ if (isimagunit(**s))
+ num2 = INT2FIX((sign == '-') ? -1 : + 1);
+ else {
+ if (!read_rat_nos(s, strict, b)) {
+ *ret = rb_complex_new2(num, ZERO);
+ return 0; /* e.g. "1+xi" */
+ }
+ **b = '\0';
+ num2 = str2num(bb);
+ }
+ if (!isimagunit(**s)) {
+ *ret = rb_complex_new2(num, ZERO);
+ return 0; /* e.g. "1+3x" */
+ }
+ (*s)++;
+ *ret = rb_complex_new2(num, num2);
+ return 1; /* e.g. "1+2i" */
}
/* !(@, - or +) */
{
- *ret = rb_complex_new2(num, ZERO);
- return 1; /* e.g. "3" */
+ *ret = rb_complex_new2(num, ZERO);
+ return 1; /* e.g. "3" */
}
}
@@ -1930,7 +1930,7 @@ inline static void
skip_ws(const char **s)
{
while (isspace((unsigned char)**s))
- (*s)++;
+ (*s)++;
}
static int
@@ -1971,22 +1971,22 @@ string_to_c_strict(VALUE self, int raise)
if (!s || memchr(s, '\0', RSTRING_LEN(self))) {
if (!raise) return Qnil;
- rb_raise(rb_eArgError, "string contains null byte");
+ rb_raise(rb_eArgError, "string contains null byte");
}
if (s && s[RSTRING_LEN(self)]) {
- rb_str_modify(self);
- s = RSTRING_PTR(self);
- s[RSTRING_LEN(self)] = '\0';
+ rb_str_modify(self);
+ s = RSTRING_PTR(self);
+ s[RSTRING_LEN(self)] = '\0';
}
if (!s)
- s = (char *)"";
+ s = (char *)"";
if (!parse_comp(s, 1, &num)) {
if (!raise) return Qnil;
- rb_raise(rb_eArgError, "invalid value for convert(): %+"PRIsVALUE,
- self);
+ rb_raise(rb_eArgError, "invalid value for convert(): %+"PRIsVALUE,
+ self);
}
return num;
@@ -2026,13 +2026,13 @@ string_to_c(VALUE self)
s = RSTRING_PTR(self);
if (s && s[RSTRING_LEN(self)]) {
- rb_str_modify(self);
- s = RSTRING_PTR(self);
- s[RSTRING_LEN(self)] = '\0';
+ rb_str_modify(self);
+ s = RSTRING_PTR(self);
+ s[RSTRING_LEN(self)] = '\0';
}
if (!s)
- s = (char *)"";
+ s = (char *)"";
(void)parse_comp(s, 0, &num);
@@ -2050,64 +2050,64 @@ nucomp_convert(VALUE klass, VALUE a1, VALUE a2, int raise)
{
if (NIL_P(a1) || NIL_P(a2)) {
if (!raise) return Qnil;
- rb_raise(rb_eTypeError, "can't convert nil into Complex");
+ rb_raise(rb_eTypeError, "can't convert nil into Complex");
}
if (RB_TYPE_P(a1, T_STRING)) {
- a1 = string_to_c_strict(a1, raise);
+ a1 = string_to_c_strict(a1, raise);
if (NIL_P(a1)) return Qnil;
}
if (RB_TYPE_P(a2, T_STRING)) {
- a2 = string_to_c_strict(a2, raise);
+ a2 = string_to_c_strict(a2, raise);
if (NIL_P(a2)) return Qnil;
}
if (RB_TYPE_P(a1, T_COMPLEX)) {
- {
- get_dat1(a1);
+ {
+ get_dat1(a1);
- if (k_exact_zero_p(dat->imag))
- a1 = dat->real;
- }
+ if (k_exact_zero_p(dat->imag))
+ a1 = dat->real;
+ }
}
if (RB_TYPE_P(a2, T_COMPLEX)) {
- {
- get_dat1(a2);
+ {
+ get_dat1(a2);
- if (k_exact_zero_p(dat->imag))
- a2 = dat->real;
- }
+ if (k_exact_zero_p(dat->imag))
+ a2 = dat->real;
+ }
}
if (RB_TYPE_P(a1, T_COMPLEX)) {
- if (a2 == Qundef || (k_exact_zero_p(a2)))
- return a1;
+ if (a2 == Qundef || (k_exact_zero_p(a2)))
+ return a1;
}
if (a2 == Qundef) {
- if (k_numeric_p(a1) && !f_real_p(a1))
- return a1;
- /* should raise exception for consistency */
- if (!k_numeric_p(a1)) {
+ if (k_numeric_p(a1) && !f_real_p(a1))
+ return a1;
+ /* should raise exception for consistency */
+ if (!k_numeric_p(a1)) {
if (!raise)
return rb_protect(to_complex, a1, NULL);
- return to_complex(a1);
+ return to_complex(a1);
}
}
else {
- if ((k_numeric_p(a1) && k_numeric_p(a2)) &&
- (!f_real_p(a1) || !f_real_p(a2)))
- return f_add(a1,
- f_mul(a2,
- f_complex_new_bang2(rb_cComplex, ZERO, ONE)));
+ if ((k_numeric_p(a1) && k_numeric_p(a2)) &&
+ (!f_real_p(a1) || !f_real_p(a2)))
+ return f_add(a1,
+ f_mul(a2,
+ f_complex_new_bang2(rb_cComplex, ZERO, ONE)));
}
{
int argc;
- VALUE argv2[2];
- argv2[0] = a1;
+ VALUE argv2[2];
+ argv2[0] = a1;
if (a2 == Qundef) {
argv2[1] = Qnil;
argc = 1;
@@ -2118,7 +2118,7 @@ nucomp_convert(VALUE klass, VALUE a1, VALUE a2, int raise)
argv2[1] = a2;
argc = 2;
}
- return nucomp_s_new(argc, argv2, klass);
+ return nucomp_s_new(argc, argv2, klass);
}
}
@@ -2255,9 +2255,9 @@ static VALUE
float_arg(VALUE self)
{
if (isnan(RFLOAT_VALUE(self)))
- return self;
+ return self;
if (f_tpositive_p(self))
- return INT2FIX(0);
+ return INT2FIX(0);
return rb_const_get(rb_mMath, id_PI);
}
@@ -2429,7 +2429,7 @@ Init_Complex(void)
* The imaginary unit.
*/
rb_define_const(rb_cComplex, "I",
- f_complex_new_bang2(rb_cComplex, ZERO, ONE));
+ f_complex_new_bang2(rb_cComplex, ZERO, ONE));
#if !USE_FLONUM
rb_gc_register_mark_object(RFLOAT_0 = DBL2NUM(0.0));