ruby-changes:8685
From: yugui <ko1@a...>
Date: Wed, 12 Nov 2008 00:21:37 +0900 (JST)
Subject: [ruby-changes:8685] Ruby:r20220 (ruby_1_9_1): * ext/bigdecimal/bigdecimal.c (BigDecimal_to_r): moved from
yugui 2008-11-12 00:21:15 +0900 (Wed, 12 Nov 2008) New Revision: 20220 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=20220 Log: * ext/bigdecimal/bigdecimal.c (BigDecimal_to_r): moved from bigdecimal/util, converted into C. [ruby-dev:36805] Modified files: branches/ruby_1_9_1/ChangeLog branches/ruby_1_9_1/ext/bigdecimal/bigdecimal.c branches/ruby_1_9_1/ext/bigdecimal/lib/bigdecimal/util.rb Index: ruby_1_9_1/ChangeLog =================================================================== --- ruby_1_9_1/ChangeLog (revision 20219) +++ ruby_1_9_1/ChangeLog (revision 20220) @@ -40,6 +40,11 @@ * test/ruby/test_transcode.rb: added tests for the above (from Yoshihiro Kambayashi) +Tue Nov 11 13:20:23 2008 Yukihiro Matsumoto <matz@r...> + + * ext/bigdecimal/bigdecimal.c (BigDecimal_to_r): moved from + bigdecimal/util, converted into C. [ruby-dev:36805] + Tue Nov 11 07:55:57 2008 Tadayoshi Funaba <tadf@d...> * lib/date.rb (inspect): changed the format. Index: ruby_1_9_1/ext/bigdecimal/bigdecimal.c =================================================================== --- ruby_1_9_1/ext/bigdecimal/bigdecimal.c (revision 20219) +++ ruby_1_9_1/ext/bigdecimal/bigdecimal.c (revision 20220) @@ -606,6 +606,41 @@ return rb_float_new(d); } + +static VALUE BigDecimal_split(VALUE self); + +/* Converts a BigDecimal to a Rational. + */ +static VALUE +BigDecimal_to_r(VALUE self) +{ + Real *p; + S_LONG sign, power, denomi_power; + VALUE a, digits, numerator; + + p = GetVpValue(self,1); + sign = VpGetSign(p); + power = VpExponent10(p); + a = BigDecimal_split(self); + digits = RARRAY_PTR(a)[1]; + denomi_power = power - RSTRING_LEN(digits); + numerator = rb_funcall(digits, rb_intern("to_i"), 0); + + if (sign < 0) { + numerator = rb_funcall(numerator, '*', 1, INT2FIX(-1)); + } + if (denomi_power < 0) { + return rb_Rational(numerator, + rb_funcall(INT2FIX(10), rb_intern("**"), 1, + INT2FIX(-denomi_power))); + } + else { + return rb_Rational1(rb_funcall(numerator, '*', 1, + rb_funcall(INT2FIX(10), rb_intern("**"), 1, + INT2FIX(denomi_power)))); + } +} + /* The coerce method provides support for Ruby type coercion. It is not * enabled by default. * @@ -1918,6 +1953,7 @@ rb_define_method(rb_cBigDecimal, "to_s", BigDecimal_to_s, -1); rb_define_method(rb_cBigDecimal, "to_i", BigDecimal_to_i, 0); rb_define_method(rb_cBigDecimal, "to_int", BigDecimal_to_i, 0); + rb_define_method(rb_cBigDecimal, "to_r", BigDecimal_to_r, 0); rb_define_method(rb_cBigDecimal, "split", BigDecimal_split, 0); rb_define_method(rb_cBigDecimal, "+", BigDecimal_add, 1); rb_define_method(rb_cBigDecimal, "-", BigDecimal_sub, 1); Index: ruby_1_9_1/ext/bigdecimal/lib/bigdecimal/util.rb =================================================================== --- ruby_1_9_1/ext/bigdecimal/lib/bigdecimal/util.rb (revision 20219) +++ ruby_1_9_1/ext/bigdecimal/lib/bigdecimal/util.rb (revision 20220) @@ -39,18 +39,6 @@ i + "." + ("0"*(-z)) + f end end - - # Converts a BigDecimal to a Rational. - def to_r - sign,digits,base,power = self.split - numerator = sign*digits.to_i - denomi_power = power - digits.size # base is always 10 - if denomi_power < 0 - Rational(numerator,base ** (-denomi_power)) - else - Rational(numerator * (base ** denomi_power),1) - end - end end class Rational < Numeric -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/