summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog21
-rw-r--r--complex.c37
-rw-r--r--numeric.c3
-rw-r--r--rational.c40
-rw-r--r--test/ruby/test_complex.rb98
-rw-r--r--test/ruby/test_rational.rb190
6 files changed, 265 insertions, 124 deletions
diff --git a/ChangeLog b/ChangeLog
index 9c11e8546..6468c7c66 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,24 @@
+Fri Aug 29 22:29:41 2008 Tadayoshi Funaba <tadf@dotrb.org>
+
+ * 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.
+
Fri Aug 29 19:46:02 2008 Koichi Sasada <ko1@atdot.net>
* gc.c, include/ruby/ruby.h: rename T_DEFERRED to T_ZOMBIE.
diff --git a/complex.c b/complex.c
index da8945ea9..39e8ad2f8 100644
--- a/complex.c
+++ b/complex.c
@@ -758,6 +758,13 @@ nucomp_arg(VALUE self)
}
static VALUE
+nucomp_rect(VALUE self)
+{
+ get_dat1(self);
+ return rb_assoc_new(dat->real, dat->image);
+}
+
+static VALUE
nucomp_polar(VALUE self)
{
return rb_assoc_new(f_abs(self), f_arg(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);
@@ -1297,6 +1302,12 @@ numeric_arg(VALUE self)
}
static VALUE
+numeric_rect(VALUE self)
+{
+ return rb_assoc_new(self, ZERO);
+}
+
+static VALUE
numeric_polar(VALUE self)
{
return rb_assoc_new(f_abs(self), f_arg(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);
diff --git a/numeric.c b/numeric.c
index 25e169acb..8e6ab6d46 100644
--- a/numeric.c
+++ b/numeric.c
@@ -3142,6 +3142,7 @@ Init_Numeric(void)
rb_define_method(rb_cNumeric, "modulo", num_modulo, 1);
rb_define_method(rb_cNumeric, "remainder", num_remainder, 1);
rb_define_method(rb_cNumeric, "abs", num_abs, 0);
+ rb_define_method(rb_cNumeric, "magnitude", num_abs, 0);
rb_define_method(rb_cNumeric, "to_int", num_to_int, 0);
rb_define_method(rb_cNumeric, "scalar?", num_scalar_p, 0);
@@ -3203,6 +3204,7 @@ Init_Numeric(void)
rb_define_method(rb_cFixnum, "**", fix_pow, 1);
rb_define_method(rb_cFixnum, "abs", fix_abs, 0);
+ rb_define_method(rb_cFixnum, "magnitude", fix_abs, 0);
rb_define_method(rb_cFixnum, "==", fix_equal, 1);
rb_define_method(rb_cFixnum, "<=>", fix_cmp, 1);
@@ -3270,6 +3272,7 @@ Init_Numeric(void)
rb_define_method(rb_cFloat, "hash", flo_hash, 0);
rb_define_method(rb_cFloat, "to_f", flo_to_f, 0);
rb_define_method(rb_cFloat, "abs", flo_abs, 0);
+ rb_define_method(rb_cFloat, "magnitude", flo_abs, 0);
rb_define_method(rb_cFloat, "zero?", flo_zero_p, 0);
rb_define_method(rb_cFloat, "to_i", flo_truncate, 0);
diff --git a/rational.c b/rational.c
index 1a0f4cbab..e0fe2a20c 100644
--- a/rational.c
+++ b/rational.c
@@ -227,8 +227,6 @@ k_rational_p(VALUE x)
inline static long
i_gcd(long x, long y)
{
- long b;
-
if (x < 0)
x = -x;
if (y < 0)
@@ -239,32 +237,12 @@ i_gcd(long x, long y)
if (y == 0)
return x;
- b = 0;
- while ((x & 1) == 0 && (y & 1) == 0) {
- b += 1;
- x >>= 1;
- y >>= 1;
- }
-
- while ((x & 1) == 0)
- x >>= 1;
-
- while ((y & 1) == 0)
- y >>= 1;
-
- while (x != y) {
- if (y > x) {
- long t;
- t = x;
- x = y;
- y = t;
- }
- x -= y;
- while ((x & 1) == 0)
- x >>= 1;
+ while (x > 0) {
+ long t = x;
+ x = y % x;
+ y = t;
}
-
- return x << b;
+ return y;
}
inline static VALUE
@@ -1229,10 +1207,11 @@ float_to_r(VALUE self)
static VALUE rat_pat, an_e_pat, a_dot_pat, underscores_pat, an_underscore;
+#define WS "\\s*"
#define DIGITS "(?:\\d(?:_\\d|\\d)*)"
#define NUMERATOR "(?:" DIGITS "?\\.)?" DIGITS "(?:[eE][-+]?" DIGITS ")?"
#define DENOMINATOR DIGITS
-#define PATTERN "\\A([-+])?(" NUMERATOR ")(?:\\/(" DENOMINATOR "))?"
+#define PATTERN "\\A" WS "([-+])?(" NUMERATOR ")(?:\\/(" DENOMINATOR "))?" WS
static void
make_patterns(void)
@@ -1261,9 +1240,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)
@@ -1283,7 +1259,7 @@ string_to_r_internal(VALUE self)
{
VALUE s, m;
- s = f_strip(self);
+ s = self;
if (RSTRING_LEN(s) == 0)
return rb_assoc_new(Qnil, self);
diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb
index b66186451..786451feb 100644
--- a/test/ruby/test_complex.rb
+++ b/test/ruby/test_complex.rb
@@ -7,6 +7,10 @@ class Complex_Test < Test::Unit::TestCase
def test_compsub
c = ComplexSub.__send__(:new, 1)
cc = ComplexSub.__send__(:convert, 1)
+
+ assert_kind_of(Numeric, c)
+ assert_kind_of(Numeric, cc)
+
if defined?(ComplexSub::Unify)
assert_instance_of(Fixnum, c)
assert_instance_of(Fixnum, cc)
@@ -181,10 +185,12 @@ class Complex_Test < Test::Unit::TestCase
assert_equal(4, c.real)
assert_equal(5, c.image)
- c = Complex(-0.0,-0.0)
+ if -0.0.to_s == '-0.0'
+ c = Complex(-0.0,-0.0)
- assert_equal('-0.0', c.real.to_s)
- assert_equal('-0.0', c.image.to_s)
+ assert_equal('-0.0', c.real.to_s)
+ assert_equal('-0.0', c.image.to_s)
+ end
c = Complex.__send__(:new, 4)
@@ -198,11 +204,13 @@ class Complex_Test < Test::Unit::TestCase
assert_equal(5, c.image)
assert_equal(c.imag, c.image)
- c = Complex.__send__(:new, -0.0,-0.0)
+ if -0.0.to_s == '-0.0'
+ c = Complex.__send__(:new, -0.0,-0.0)
- assert_equal('-0.0', c.real.to_s)
- assert_equal('-0.0', c.image.to_s)
- assert_equal(c.imag.to_s, c.image.to_s)
+ assert_equal('-0.0', c.real.to_s)
+ assert_equal('-0.0', c.image.to_s)
+ assert_equal(c.imag.to_s, c.image.to_s)
+ end
c = Complex.__send__(:new!, 4)
@@ -275,6 +283,15 @@ class Complex_Test < Test::Unit::TestCase
assert_equal(Complex(1,1), Complex(1,1).nonzero?)
end
+ def rect
+ assert_equal([1,2], Complex.rectangular(1,2).rectangular)
+ assert_equal([1,2], Complex.rect(1,2).rect)
+ end
+
+ def polar
+ assert_equal([1,2], Complex.polar(1,2).polar)
+ end
+
def test_uplus
assert_equal(Complex(1), +Complex(1))
assert_equal(Complex(-1), +Complex(-1))
@@ -503,10 +520,23 @@ class Complex_Test < Test::Unit::TestCase
assert_equal(false, Complex(1) == '')
end
+ def test_unify
+ if defined?(Complex::Unify)
+ assert_instance_of(Fixnum, Complex(1,2) + Complex(-1,-2))
+ assert_instance_of(Fixnum, Complex(1,2) - Complex(1,2))
+ assert_instance_of(Fixnum, Complex(1,2) * 0)
+ assert_instance_of(Fixnum, Complex(1,2) / Complex(1,2))
+ assert_instance_of(Fixnum, Complex(1,2).div(Complex(1,2)))
+ assert_instance_of(Fixnum, Complex(1,2).quo(Complex(1,2)))
+# assert_instance_of(Fixnum, Complex(1,2) ** 0) # mathn's bug
+ end
+ end
+
def test_math
c = Complex(1,2)
assert_in_delta(2.236, c.abs, 0.001)
+ assert_in_delta(2.236, c.magnitude, 0.001)
assert_equal(5, c.abs2)
assert_equal(c.abs, Math.sqrt(c * c.conj))
@@ -516,6 +546,7 @@ class Complex_Test < Test::Unit::TestCase
assert_in_delta(1.107, c.arg, 0.001)
assert_in_delta(1.107, c.angle, 0.001)
+ assert_in_delta(1.107, c.phase, 0.001)
r = c.polar
assert_in_delta(2.236, r[0], 0.001)
@@ -598,6 +629,7 @@ class Complex_Test < Test::Unit::TestCase
def test_parse
assert_equal(Complex(0), ''.to_c)
assert_equal(Complex(0), ' '.to_c)
+ assert_equal(Complex(5), "\f\n\r\t\v5\0".to_c)
assert_equal(Complex(5), '5'.to_c)
assert_equal(Complex(-5), '-5'.to_c)
assert_equal(Complex(5,3), '5+3i'.to_c)
@@ -705,6 +737,7 @@ class Complex_Test < Test::Unit::TestCase
assert_raise(ArgumentError){ Complex('5+3_i')}
assert_raise(ArgumentError){ Complex('5+3i_')}
assert_raise(ArgumentError){ Complex('5+3ix')}
+ assert_raise(ArgumentError){ Complex("5\0")}
if defined?(Rational) && defined?(''.to_r)
assert_equal(Complex(Rational(1,5)), '1/5'.to_c)
@@ -818,17 +851,36 @@ class Complex_Test < Test::Unit::TestCase
assert_equal(0, 1.1.image)
assert_equal(0, 1.1.imag)
+ assert_equal(1, 1.magnitude)
+ assert_equal(1, -1.magnitude)
+ assert_equal(1, 1.0.magnitude)
+ assert_equal(1, -1.0.magnitude)
+
assert_equal(0, 1.arg)
assert_equal(0, 1.angle)
+ assert_equal(0, 1.phase)
assert_equal(0, 1.0.arg)
assert_equal(0, 1.0.angle)
+ assert_equal(0, 1.0.phase)
assert_equal(Math::PI, -1.arg)
assert_equal(Math::PI, -1.angle)
+ assert_equal(Math::PI, -1.phase)
assert_equal(Math::PI, -1.0.arg)
assert_equal(Math::PI, -1.0.angle)
+ assert_equal(Math::PI, -1.0.phase)
+
+ assert_equal([1,0], 1.rect)
+ assert_equal([-1,0], -1.rect)
+ assert_equal([1,0], 1.rectangular)
+ assert_equal([-1,0], -1.rectangular)
+
+ assert_equal([1.0,0], 1.0.rect)
+ assert_equal([-1.0,0], -1.0.rect)
+ assert_equal([1.0,0], 1.0.rectangular)
+ assert_equal([-1.0,0], -1.0.rectangular)
assert_equal([1,0], 1.polar)
assert_equal([1, Math::PI], -1.polar)
@@ -846,46 +898,14 @@ class Complex_Test < Test::Unit::TestCase
assert_equal(1.1, 1.1.conj)
assert_equal(-1.1, -1.1.conj)
- assert_equal(1, 1.numerator)
- assert_equal(9, 9.numerator)
- assert_equal(1, 1.denominator)
- assert_equal(1, 9.denominator)
-
- if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID')
- assert_equal(1.0, 1.0.numerator)
- assert_equal(9.0, 9.0.numerator)
- assert_equal(1.0, 1.0.denominator)
- assert_equal(1.0, 9.0.denominator)
- end
-
-=begin
- if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID')
- assert_equal(Rational(1,9), 9.reciprocal)
- assert_equal(Rational(1,9), 9.0.reciprocal)
- assert_equal(Rational(1,9), 9.inverse)
- assert_equal(Rational(1,9), 9.0.inverse)
- end
-=end
-
if defined?(Rational)
- assert_equal(Rational(1,2), 1.quo(2))
- assert_equal(Rational(5000000000), 10000000000.quo(2))
- assert_equal(0.5, 1.0.quo(2))
- assert_equal(Rational(1,4), Rational(1,2).quo(2))
assert_equal(Complex(Rational(1,2),Rational(1)), Complex(1,2).quo(2))
else
- assert_equal(0.5, 1.quo(2))
- assert_equal(5000000000.0, 10000000000.quo(2))
- assert_equal(0.5, 1.0.quo(2))
assert_equal(Complex(0.5,1.0), Complex(1,2).quo(2))
end
=begin
if defined?(Rational) && !Rational.instance_variable_get('@RCS_ID')
- assert_equal(Rational(1,2), 1.rdiv(2))
- assert_equal(Rational(5000000000), 10000000000.rdiv(2))
- assert_equal(Rational(1,2), 1.0.rdiv(2))
- assert_equal(Rational(1,4), Rational(1,2).rdiv(2))
assert_equal(Complex(Rational(1,2),Rational(1)), Complex(1,2).quo(2))
end
=end
diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb
index eb52e3e19..b979da91d 100644
--- a/test/ruby/test_rational.rb
+++ b/test/ruby/test_rational.rb
@@ -7,6 +7,10 @@ class Rational_Test < Test::Unit::TestCase
def test_ratsub
c = RationalSub.__send__(:new, 1)
cc = RationalSub.__send__(:convert, 1)
+
+ assert_kind_of(Numeric, c)
+ assert_kind_of(Numeric, cc)
+
if defined?(RationalSub::Unify)
assert_instance_of(Fixnum, c)
assert_instance_of(Fixnum, cc)
@@ -29,6 +33,21 @@ class Rational_Test < Test::Unit::TestCase
end
end
+ def test_eql_p
+ c = Rational(0)
+ c2 = Rational(0)
+ c3 = Rational(1)
+
+ assert_equal(true, c.eql?(c2))
+ assert_equal(false, c.eql?(c3))
+
+ if defined?(Rational::Unify)
+ assert_equal(true, c.eql?(0))
+ else
+ assert_equal(false, c.eql?(0))
+ end
+ end
+
def test_hash
assert_instance_of(Fixnum, Rational(1,2).hash)
@@ -180,10 +199,16 @@ class Rational_Test < Test::Unit::TestCase
c = Rational(Rational(1,2),Rational(1,2))
assert_equal(Rational.__send__(:new, 1), c)
- assert_equal(Rational.__send__(:new, 1),Rational(1))
- assert_equal(1.1.to_r,Rational(1.1))
- assert_equal(Rational.__send__(:new, 1),Rational('1'))
+ assert_equal(Rational.__send__(:new, 3),Rational(3))
+ assert_equal(Rational.__send__(:new, 1),Rational(3,3))
+ assert_equal(3.3.to_r,Rational(3.3))
+ assert_equal(1,Rational(3.3,3.3))
+ assert_equal(Rational.__send__(:new, 3),Rational('3'))
+ assert_equal(Rational.__send__(:new, 1),Rational('3.0','3.0'))
+ assert_equal(Rational.__send__(:new, 1),Rational('3/3','3/3'))
assert_raise(ArgumentError){Rational(nil)}
+ assert_raise(ArgumentError){Rational('')}
+ assert_raise(ArgumentError){Rational(Object.new)}
end
def test_attr
@@ -335,6 +360,9 @@ class Rational_Test < Test::Unit::TestCase
assert_equal(Rational(1,4), c / 2)
assert_equal(0.25, c / 2.0)
+
+ assert_raise(ZeroDivisionError){Rational(1, 3) / 0}
+ assert_raise(ZeroDivisionError){Rational(1, 3) / Rational(0)}
end
def assert_eql(exp, act, *args)
@@ -387,6 +415,41 @@ class Rational_Test < Test::Unit::TestCase
end
end
+ def test_modulo
+ c = Rational(1,2)
+ c2 = Rational(2,3)
+
+ assert_eql(Rational(1,2), c.modulo(c2))
+ assert_eql(Rational(1,2), c.modulo(2))
+ assert_eql(0.5, c.modulo(2.0))
+
+ c = Rational(301,100)
+ c2 = Rational(7,5)
+
+ assert_equal(Rational(21,100), c.modulo(c2))
+ assert_equal(Rational(-119,100), c.modulo(-c2))
+ assert_equal(Rational(119,100), (-c).modulo(c2))
+ assert_equal(Rational(-21,100), (-c).modulo(-c2))
+
+ c = Rational(301,100)
+ c2 = Rational(2)
+
+ assert_equal(Rational(101,100), c.modulo(c2))
+ assert_equal(Rational(-99,100), c.modulo(-c2))
+ assert_equal(Rational(99,100), (-c).modulo(c2))
+ assert_equal(Rational(-101,100), (-c).modulo(-c2))
+
+ unless defined?(Rational::Unify)
+ c = Rational(11)
+ c2 = Rational(3)
+
+ assert_equal(2, c.modulo(c2))
+ assert_equal(-1, c.modulo(-c2))
+ assert_equal(1, (-c).modulo(c2))
+ assert_equal(-2, (-c).modulo(-c2))
+ end
+ end
+
def test_divmod
c = Rational(1,2)
c2 = Rational(2,3)
@@ -457,7 +520,44 @@ class Rational_Test < Test::Unit::TestCase
assert_equal(3, (-c).quot(-c2))
end
end
+=end
+
+ def test_remainder
+ c = Rational(1,2)
+ c2 = Rational(2,3)
+
+ assert_eql(Rational(1,2), c.remainder(c2))
+ assert_eql(Rational(1,2), c.remainder(2))
+ assert_eql(0.5, c.remainder(2.0))
+
+ c = Rational(301,100)
+ c2 = Rational(7,5)
+
+ assert_equal(Rational(21,100), c.remainder(c2))
+ assert_equal(Rational(21,100), c.remainder(-c2))
+ assert_equal(Rational(-21,100), (-c).remainder(c2))
+ assert_equal(Rational(-21,100), (-c).remainder(-c2))
+
+ c = Rational(301,100)
+ c2 = Rational(2)
+
+ assert_equal(Rational(101,100), c.remainder(c2))
+ assert_equal(Rational(101,100), c.remainder(-c2))
+ assert_equal(Rational(-101,100), (-c).remainder(c2))
+ assert_equal(Rational(-101,100), (-c).remainder(-c2))
+
+ unless defined?(Rational::Unify)
+ c = Rational(11)
+ c2 = Rational(3)
+
+ assert_equal(2, c.remainder(c2))
+ assert_equal(2, c.remainder(-c2))
+ assert_equal(-2, (-c).remainder(c2))
+ assert_equal(-2, (-c).remainder(-c2))
+ end
+ end
+=begin
def test_quotrem
c = Rational(1,2)
c2 = Rational(2,3)
@@ -696,6 +796,33 @@ class Rational_Test < Test::Unit::TestCase
end
end
+ def test_math
+ assert_equal(Rational(1,2), Rational(1,2).abs)
+ assert_equal(Rational(1,2), Rational(-1,2).abs)
+ if defined?(Complex) && !Complex.instance_variable_get('@RCS_ID')
+ assert_equal(Rational(1,2), Rational(1,2).magnitude)
+ assert_equal(Rational(1,2), Rational(-1,2).magnitude)
+ end
+
+ assert_equal(1, Rational(1,2).numerator)
+ assert_equal(2, Rational(1,2).denominator)
+ end
+
+ def test_trunc
+ [[Rational(13, 5), [ 2, 3, 2, 3]], # 2.6
+ [Rational(5, 2), [ 2, 3, 2, 3]], # 2.5
+ [Rational(12, 5), [ 2, 3, 2, 2]], # 2.4
+ [Rational(-12,5), [-3, -2, -2, -2]], # -2.4
+ [Rational(-5, 2), [-3, -2, -2, -3]], # -2.5
+ [Rational(-13, 5), [-3, -2, -2, -3]], # -2.6
+ ].each do |i, a|
+ assert_equal(a[0], i.floor)
+ assert_equal(a[1], i.ceil)
+ assert_equal(a[2], i.truncate)
+ assert_equal(a[3], i.round)
+ end
+ end
+
def test_to_s
c = Rational(1,2)
@@ -739,6 +866,7 @@ class Rational_Test < Test::Unit::TestCase
def test_parse
assert_equal(Rational(0), ''.to_r)
assert_equal(Rational(0), ' '.to_r)
+ assert_equal(Rational(5), "\f\n\r\t\v5\0".to_r)
assert_equal(Rational(5), '5'.to_r)
assert_equal(Rational(-5), '-5'.to_r)
assert_equal(Rational(5,3), '5/3'.to_r)
@@ -816,6 +944,7 @@ class Rational_Test < Test::Unit::TestCase
assert_raise(ArgumentError){ Rational('5/3_')}
assert_raise(ArgumentError){ Rational('5/3.3')}
assert_raise(ArgumentError){ Rational('5/3x')}
+ assert_raise(ArgumentError){ Rational("5\0")}
end
=begin
@@ -885,48 +1014,27 @@ class Rational_Test < Test::Unit::TestCase
assert_eql(c, c.prec(Rational))
end
+ def test_gcdlcm
+ assert_equal(7, 91.gcd(-49))
+ assert_equal(5, 5.gcd(0))
+ assert_equal(5, 0.gcd(5))
+ assert_equal(70, 14.lcm(35))
+ assert_equal(0, 5.lcm(0))
+ assert_equal(0, 0.lcm(5))
+ assert_equal([5,0], 0.gcdlcm(5))
+ assert_equal([5,0], 5.gcdlcm(0))
+
+ assert_equal(1, 1073741827.gcd(1073741789))
+ assert_equal(1152921470247108503, 1073741827.lcm(1073741789))
+
+ assert_equal(1, 1073741789.gcd(1073741827))
+ assert_equal(1152921470247108503, 1073741789.lcm(1073741827))
+ end
+
def test_supp
assert_equal(true, 1.scalar?)
assert_equal(true, 1.1.scalar?)
- if defined?(Complex)
- assert_equal(1, 1.real)
- assert_equal(0, 1.image)
- assert_equal(0, 1.imag)
-
- assert_equal(1.1, 1.1.real)
- assert_equal(0, 1.1.image)
- assert_equal(0, 1.1.imag)
-
- assert_equal(0, 1.arg)
- assert_equal(0, 1.angle)
-
- assert_equal(0, 1.0.arg)
- assert_equal(0, 1.0.angle)
-
- assert_equal(Math::PI, -1.arg)
- assert_equal(Math::PI, -1.angle)
-
- assert_equal(Math::PI, -1.0.arg)
- assert_equal(Math::PI, -1.0.angle)
-
- assert_equal([1,0], 1.polar)
- assert_equal([1, Math::PI], -1.polar)
-
- assert_equal([1.0,0], 1.0.polar)
- assert_equal([1.0, Math::PI], -1.0.polar)
-
- assert_equal(1, 1.conjugate)
- assert_equal(-1, -1.conjugate)
- assert_equal(1, 1.conj)
- assert_equal(-1, -1.conj)
-
- assert_equal(1.1, 1.1.conjugate)
- assert_equal(-1.1, -1.1.conjugate)
- assert_equal(1.1, 1.1.conj)
- assert_equal(-1.1, -1.1.conj)
- end
-
assert_equal(1, 1.numerator)
assert_equal(9, 9.numerator)
assert_equal(1, 1.denominator)