* complex.c ({nucomp,numeric}_rect): new.

* complex.c: added some aliases
	  (::rectangular, ::rect, #rectangular, #rect, #phase,
	  #magnitude).

	* complex.c (string_to_c_internal): should not strip any null
	  bytes.

	* rational.c (string_to_r_internal): ditto.

	* rational.c (i_gcd): reverted to nurat 0.0.2's one.

	* numeric.c: added an alias (#magnitude).

	* test/ruby/test_complex.rb: added assertions.

	* test/ruby/test_rational.rb: ditto.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18925 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
tadf 2008-08-29 13:41:41 +00:00
parent fa4c17e712
commit 5185955f3f
6 changed files with 265 additions and 124 deletions

View file

@ -757,6 +757,13 @@ nucomp_arg(VALUE self)
return m_atan2_bang(dat->image, dat->real);
}
static VALUE
nucomp_rect(VALUE self)
{
get_dat1(self);
return rb_assoc_new(dat->real, dat->image);
}
static VALUE
nucomp_polar(VALUE self)
{
@ -1004,14 +1011,15 @@ numeric_to_c(VALUE self)
static VALUE comp_pat0, comp_pat1, comp_pat2, a_slash, a_dot_and_an_e,
null_string, underscores_pat, an_underscore;
#define WS "\\s*"
#define DIGITS "(?:\\d(?:_\\d|\\d)*)"
#define NUMERATOR "(?:" DIGITS "?\\.)?" DIGITS "(?:[eE][-+]?" DIGITS ")?"
#define DENOMINATOR DIGITS
#define NUMBER "[-+]?" NUMERATOR "(?:\\/" DENOMINATOR ")?"
#define NUMBERNOS NUMERATOR "(?:\\/" DENOMINATOR ")?"
#define PATTERN0 "\\A(" NUMBER ")@(" NUMBER ")"
#define PATTERN1 "\\A([-+])?(" NUMBER ")?[iIjJ]"
#define PATTERN2 "\\A(" NUMBER ")(([-+])(" NUMBERNOS ")?[iIjJ])?"
#define PATTERN0 "\\A" WS "(" NUMBER ")@(" NUMBER ")" WS
#define PATTERN1 "\\A" WS "([-+])?(" NUMBER ")?[iIjJ]" WS
#define PATTERN2 "\\A" WS "(" NUMBER ")(([-+])(" NUMBERNOS ")?[iIjJ])?" WS
static void
make_patterns(void)
@ -1049,9 +1057,6 @@ make_patterns(void)
rb_global_variable(&an_underscore);
}
#define id_strip rb_intern("strip")
#define f_strip(x) rb_funcall(x, id_strip, 0)
#define id_match rb_intern("match")
#define f_match(x,y) rb_funcall(x, id_match, 1, y)
@ -1078,7 +1083,7 @@ string_to_c_internal(VALUE self)
{
VALUE s;
s = f_strip(self);
s = self;
if (RSTRING_LEN(s) == 0)
return rb_assoc_new(Qnil, self);
@ -1296,6 +1301,12 @@ numeric_arg(VALUE self)
return rb_const_get(rb_mMath, id_PI);
}
static VALUE
numeric_rect(VALUE self)
{
return rb_assoc_new(self, ZERO);
}
static VALUE
numeric_polar(VALUE self)
{
@ -1366,10 +1377,8 @@ Init_Complex(void)
rb_funcall(rb_cComplex, rb_intern("private_class_method"), 1,
ID2SYM(rb_intern("new")));
#if 0
rb_define_singleton_method(rb_cComplex, "rect", nucomp_s_new, -1);
rb_define_singleton_method(rb_cComplex, "rectangular", nucomp_s_new, -1);
#endif
rb_define_singleton_method(rb_cComplex, "rect", nucomp_s_new, -1);
rb_define_singleton_method(rb_cComplex, "polar", nucomp_s_polar, 2);
rb_define_global_function(COMPLEX_NAME, nucomp_f_complex, -1);
@ -1408,12 +1417,13 @@ Init_Complex(void)
rb_define_method(rb_cComplex, "coerce", nucomp_coerce, 1);
rb_define_method(rb_cComplex, "abs", nucomp_abs, 0);
#if 0
rb_define_method(rb_cComplex, "magnitude", nucomp_abs, 0);
#endif
rb_define_method(rb_cComplex, "abs2", nucomp_abs2, 0);
rb_define_method(rb_cComplex, "arg", nucomp_arg, 0);
rb_define_method(rb_cComplex, "angle", nucomp_arg, 0);
rb_define_method(rb_cComplex, "phase", nucomp_arg, 0);
rb_define_method(rb_cComplex, "rectangular", nucomp_rect, 0);
rb_define_method(rb_cComplex, "rect", nucomp_rect, 0);
rb_define_method(rb_cComplex, "polar", nucomp_polar, 0);
rb_define_method(rb_cComplex, "conjugate", nucomp_conjugate, 0);
rb_define_method(rb_cComplex, "conj", nucomp_conjugate, 0);
@ -1465,6 +1475,9 @@ Init_Complex(void)
rb_define_method(rb_cNumeric, "imag", numeric_image, 0);
rb_define_method(rb_cNumeric, "arg", numeric_arg, 0);
rb_define_method(rb_cNumeric, "angle", numeric_arg, 0);
rb_define_method(rb_cNumeric, "phase", numeric_arg, 0);
rb_define_method(rb_cNumeric, "rectangular", numeric_rect, 0);
rb_define_method(rb_cNumeric, "rect", numeric_rect, 0);
rb_define_method(rb_cNumeric, "polar", numeric_polar, 0);
rb_define_method(rb_cNumeric, "conjugate", numeric_conjugate, 0);
rb_define_method(rb_cNumeric, "conj", numeric_conjugate, 0);