[前][次][番号順一覧][スレッド一覧]

ruby-changes:17081

From: mrkn <ko1@a...>
Date: Tue, 24 Aug 2010 01:22:18 +0900 (JST)
Subject: [ruby-changes:17081] Ruby:r29078 (trunk): * ext/bigdecimal/bigdecimal.c (BigDecimal_to_f): to_f must underflow when the exponent is less than DBL_MIN_10_EXP - BASE_FIG.

mrkn	2010-08-24 01:22:04 +0900 (Tue, 24 Aug 2010)

  New Revision: 29078

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=29078

  Log:
    * 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.

  Modified files:
    trunk/ChangeLog
    trunk/ext/bigdecimal/bigdecimal.c
    trunk/test/bigdecimal/test_bigdecimal.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 29077)
+++ ChangeLog	(revision 29078)
@@ -1,3 +1,10 @@
+Tue Aug 24 01:14:58 2010  Kenta Murata <mrkn@m...>
+
+	* 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.
+
 Mon Aug 23 23:14:21 2010  Tanaka Akira  <akr@f...>
 
 	* ext/pathname/pathname.c (path_each_line): Pathname#each_line
Index: ext/bigdecimal/bigdecimal.c
===================================================================
--- ext/bigdecimal/bigdecimal.c	(revision 29077)
+++ ext/bigdecimal/bigdecimal.c	(revision 29078)
@@ -513,25 +513,40 @@
     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);
 }
 
 
Index: test/bigdecimal/test_bigdecimal.rb
===================================================================
--- test/bigdecimal/test_bigdecimal.rb	(revision 29077)
+++ test/bigdecimal/test_bigdecimal.rb	(revision 29078)
@@ -343,6 +343,16 @@
     BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false)
     assert_kind_of(Float,   x .to_f)
     assert_kind_of(Float, (-x).to_f)
+
+    BigDecimal.mode(BigDecimal::EXCEPTION_UNDERFLOW, true)
+    assert_raise(FloatDomainError) {
+      BigDecimal("1e#{Float::MIN_10_EXP - 2*Float::DIG}").to_f }
+    assert_raise(FloatDomainError) {
+      BigDecimal("-1e#{Float::MIN_10_EXP - 2*Float::DIG}").to_f }
+
+    BigDecimal.mode(BigDecimal::EXCEPTION_UNDERFLOW, false)
+    assert_equal( 0.0, BigDecimal("1e#{Float::MIN_10_EXP - 2*Float::DIG}").to_f)
+    assert_equal(-0.0, BigDecimal("-1e#{Float::MIN_10_EXP - 2*Float::DIG}").to_f)
   end
 
   def test_coerce

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]