summaryrefslogtreecommitdiff
path: root/ext/bigdecimal
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-08-23 16:22:04 +0000
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-08-23 16:22:04 +0000
commitaf8cae1e7fe0a5ee0b9404aaf8ed7c9888272d09 (patch)
tree691a59654bd5dff36bfb54318ff4fe40c732d276 /ext/bigdecimal
parent526d246f715a1a0046942f942173c2d9447e78a5 (diff)
* ext/bigdecimal/bigdecimal.c (BigDecimal_to_f): to_f must underflow when the exponent is less than DBL_MIN_10_EXP - BASE_FIG.
* test/bigdecimal/test_bigdecimal.rb (test_to_f): added test for the above changes. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29078 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/bigdecimal')
-rw-r--r--ext/bigdecimal/bigdecimal.c33
1 files changed, 24 insertions, 9 deletions
diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index c674c95e608..7109f8c2421 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -513,25 +513,40 @@ BigDecimal_to_f(VALUE self)
ENTER(1);
Real *p;
double d;
- ssize_t e;
+ SIGNED_VALUE e;
char *buf;
volatile VALUE str;
GUARD_OBJ(p, GetVpValue(self, 1));
- if (VpVtoD(&d, &e, p) != 1) return rb_float_new(d);
- if (e > (ssize_t)(DBL_MAX_10_EXP+BASE_FIG)) goto erange;
+ if (VpVtoD(&d, &e, p) != 1)
+ return rb_float_new(d);
+ if (e > (SIGNED_VALUE)(DBL_MAX_10_EXP+BASE_FIG))
+ goto overflow;
+ if (e < (SIGNED_VALUE)(DBL_MIN_10_EXP-BASE_FIG))
+ goto underflow;
+
str = rb_str_new(0, VpNumOfChars(p,"E"));
buf = RSTRING_PTR(str);
VpToString(p, buf, 0, 0);
errno = 0;
d = strtod(buf, 0);
- if (errno == ERANGE) {
- erange:
- VpException(VP_EXCEPTION_OVERFLOW,"BigDecimal to Float conversion",0);
- if (d > 0.0) d = VpGetDoublePosInf();
- else d = VpGetDoubleNegInf();
- }
+ if (errno == ERANGE)
+ goto overflow;
return rb_float_new(d);
+
+overflow:
+ VpException(VP_EXCEPTION_OVERFLOW, "BigDecimal to Float conversion", 0);
+ if (d > 0.0)
+ return rb_float_new(VpGetDoublePosInf());
+ else
+ return rb_float_new(VpGetDoubleNegInf());
+
+underflow:
+ VpException(VP_EXCEPTION_UNDERFLOW, "BigDecimal to Float conversion", 0);
+ if (d > 0.0)
+ return rb_float_new(0.0);
+ else
+ return rb_float_new(-0.0);
}