diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-01-28 16:34:11 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-01-28 16:34:11 +0000 |
commit | d1499525d5ea9d0b2d372406d414ea3679a939fb (patch) | |
tree | ba9ef000bc59f7a8ce7e6e1dad59ef137ec3b49a /ext/bigdecimal | |
parent | a715d95a8221ae80015ced9d80bf96c4d8b5b6e9 (diff) |
* ext/bigdecimal/bigdecimal.c (BigDecimal_to_i): update RDoc to
denote that #to_i raises FloatDomainError for Inf and NaN.
* ext/bigdecimal/bigdecimal.c (BigDecimal_to_i): fast #to_i using
BigDecimal_split().
* bignum.c (conv_digit): use faster ISDIGIT() assuming ASCII.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26461 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/bigdecimal')
-rw-r--r-- | ext/bigdecimal/bigdecimal.c | 45 |
1 files changed, 19 insertions, 26 deletions
diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index c6ffe98896..3dd62f11fb 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -473,9 +473,11 @@ BigDecimal_check_num(Real *p) } } +static VALUE BigDecimal_split(VALUE self); + /* Returns the value as an integer (Fixnum or Bignum). * - * If the BigNumber is infinity or NaN, returns nil. + * If the BigNumber is infinity or NaN, raises FloatDomainError. */ static VALUE BigDecimal_to_i(VALUE self) @@ -497,31 +499,24 @@ BigDecimal_to_i(VALUE self) e = VpGetSign(p)*p->frac[0]; return INT2FIX(e); } - str = rb_str_new(0, e+nf+2); - psz = RSTRING_PTR(str); + else { + VALUE a = BigDecimal_split(self); + VALUE digits = RARRAY_PTR(a)[1]; + VALUE numerator = rb_funcall(digits, rb_intern("to_i"), 0); + int dpower = e - RSTRING_LEN(digits); - n = (e+nf-1)/nf; - pch = psz; - if(VpGetSign(p)<0) *pch++ = '-'; - for(i=0;i<n;++i) { - b = VpBaseVal()/10; - if(i>=(int)p->Prec) { - while(b) { - *pch++ = '0'; - b /= 10; - } - continue; - } - v = p->frac[i]; - while(b) { - j = v/b; - *pch++ = (char)(j + '0'); - v -= j*b; - b /= 10; - } + if (VpGetSign(p) < 0) { + numerator = rb_funcall(numerator, '*', 1, INT2FIX(-1)); + } + if (dpower < 0) { + return rb_funcall(numerator, rb_intern("div"), 1, + rb_funcall(INT2FIX(10), rb_intern("**"), 1, + INT2FIX(-dpower))); + } + return rb_funcall(numerator, '*', 1, + rb_funcall(INT2FIX(10), rb_intern("**"), 1, + INT2FIX(dpower))); } - *pch++ = 0; - return rb_cstr2inum(psz,10); } /* Returns a new Float object having approximately the same value as the @@ -556,8 +551,6 @@ BigDecimal_to_f(VALUE self) } -static VALUE BigDecimal_split(VALUE self); - /* Converts a BigDecimal to a Rational. */ static VALUE |