summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-05-19 12:19:46 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-05-19 12:19:46 +0000
commit5d58894c1e1064d0592ddedf2428f6c13eb5da08 (patch)
tree17a9c984e7c2e4a04a8cb02a32d72f972e8171fa /numeric.c
parent621b15ced94ebf5053bee4c0687ad88fe5b6b49e (diff)
merge revision(s) 27890:
* numeric.c (rb_num2ulong): use rb_big2ulong for data from Bignum. Without this 32bit integer on 32bit environment can't converted into long. This fixes 1) and 2) of [ruby-dev:41289] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_2@27898 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c29
1 files changed, 27 insertions, 2 deletions
diff --git a/numeric.c b/numeric.c
index 51b1da81b5..ef9bdce489 100644
--- a/numeric.c
+++ b/numeric.c
@@ -1720,10 +1720,35 @@ rb_num2long(VALUE val)
VALUE
rb_num2ulong(VALUE val)
{
- if (TYPE(val) == T_BIGNUM) {
+ again:
+ if (NIL_P(val)) {
+ rb_raise(rb_eTypeError, "no implicit conversion from nil to integer");
+ }
+
+ if (FIXNUM_P(val)) return FIX2LONG(val); /* this is FIX2LONG, inteneded */
+
+ switch (TYPE(val)) {
+ case T_FLOAT:
+ if (RFLOAT_VALUE(val) <= (double)LONG_MAX
+ && RFLOAT_VALUE(val) >= (double)LONG_MIN) {
+ return (RFLOAT_VALUE(val));
+ }
+ else {
+ char buf[24];
+ char *s;
+
+ snprintf(buf, sizeof(buf), "%-.10g", RFLOAT_VALUE(val));
+ if ((s = strchr(buf, ' ')) != 0) *s = '\0';
+ rb_raise(rb_eRangeError, "float %s out of range of integer", buf);
+ }
+
+ case T_BIGNUM:
return rb_big2ulong(val);
+
+ default:
+ val = rb_to_int(val);
+ goto again;
}
- return (VALUE)rb_num2long(val);
}
#if SIZEOF_INT < SIZEOF_VALUE