diff options
author | tadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-08-29 13:41:41 +0000 |
---|---|---|
committer | tadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-08-29 13:41:41 +0000 |
commit | 5185955f3f64d53f55e34bfe4eaf059b7b347fc4 (patch) | |
tree | 6ffaa4e868c32e6a179f7051fac15048a7f938b3 /rational.c | |
parent | fa4c17e71283e72100c12b8824aafe176372241d (diff) | |
download | ruby-5185955f3f64d53f55e34bfe4eaf059b7b347fc4.tar.gz |
* 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
Diffstat (limited to 'rational.c')
-rw-r--r-- | rational.c | 40 |
1 files changed, 8 insertions, 32 deletions
diff --git a/rational.c b/rational.c index 1a0f4cbabc..e0fe2a20cd 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); |