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

ruby-changes:15258

From: naruse <ko1@a...>
Date: Thu, 1 Apr 2010 13:33:03 +0900 (JST)
Subject: [ruby-changes:15258] Ruby:r27140 (trunk): * object.c (rb_cstr_to_dbl): return 0.0 if hexadecimal and

naruse	2010-04-01 13:32:43 +0900 (Thu, 01 Apr 2010)

  New Revision: 27140

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

  Log:
    * object.c (rb_cstr_to_dbl): return 0.0 if hexadecimal and
      baccheck is FALSE: Float("0x1p+0") works, but "0x1p+0".to_f
      doesn't. [ruby-dev:40650]
    
    * util.c (ruby_strtod): allow hexdecimal integers.

  Modified files:
    trunk/ChangeLog
    trunk/object.c
    trunk/test/ruby/test_float.rb
    trunk/test/ruby/test_string.rb
    trunk/util.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 27139)
+++ ChangeLog	(revision 27140)
@@ -1,3 +1,11 @@
+Thu Apr  1 13:24:12 2010  NARUSE, Yui  <naruse@r...>
+
+	* object.c (rb_cstr_to_dbl): return 0.0 if hexadecimal and
+	  baccheck is FALSE: Float("0x1p+0") works, but "0x1p+0".to_f
+	  doesn't. [ruby-dev:40650]
+
+	* util.c (ruby_strtod): allow hexdecimal integers.
+
 Thu Apr  1 13:20:50 2010  NARUSE, Yui  <naruse@r...>
 
 	* lib/scanf.rb: support %a format. [ruby-dev:40650]
Index: object.c
===================================================================
--- object.c	(revision 27139)
+++ object.c	(revision 27140)
@@ -2246,6 +2246,11 @@
     if (!p) return 0.0;
     q = p;
     while (ISSPACE(*p)) p++;
+
+    if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
+	return 0.0;
+    }
+
     d = strtod(p, &end);
     if (errno == ERANGE) {
 	OutOfRange();
@@ -2284,6 +2289,11 @@
 	}
 	*n = '\0';
 	p = buf;
+
+	if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
+	    return 0.0;
+	}
+
 	d = strtod(p, &end);
 	if (errno == ERANGE) {
 	    OutOfRange();
Index: util.c
===================================================================
--- util.c	(revision 27139)
+++ util.c	(revision 27140)
@@ -2124,24 +2124,21 @@
 		}
 	    }
 
-	    if (*s != 'P' && *s != 'p') {
-		s = s0;
-		goto ret;
-	    }
+	    if (*s == 'P' || *s == 'p') {
+		dsign = 0x2C - *++s; /* +: 2B, -: 2D */
+		if (abs(dsign) == 1) s++;
+		else dsign = 1;
 
-	    dsign = 0x2C - *++s; /* +: 2B, -: 2D */
-	    if (abs(dsign) != 1) {
-		s = s0;
-		goto ret;
+		for (nd = 0; (c = *s) >= '0' && c <= '9'; s++) {
+		    nd *= 10;
+		    nd += c;
+		    nd -= '0';
+		}
+		dval(rv) = ldexp(adj, nd * dsign);
 	    }
-
-	    for (nd = 0, s++; (c = *s) >= '0' && c <= '9'; s++) {
-		nd *= 10;
-		nd += c;
-		nd -= '0';
+	    else {
+		dval(rv) = adj;
 	    }
-
-	    dval(rv) = ldexp(adj, nd * dsign);
 	    goto ret;
 	}
         nz0 = 1;
Index: test/ruby/test_float.rb
===================================================================
--- test/ruby/test_float.rb	(revision 27139)
+++ test/ruby/test_float.rb	(revision 27140)
@@ -87,6 +87,18 @@
     assert_raise(ArgumentError){Float("1__1")}
     # add expected behaviour here.
     assert_equal(10, Float("1_0"))
+
+    assert_equal([ 0.0].pack('G'), [Float(" 0x0p+0").to_f].pack('G'))
+    assert_equal([-0.0].pack('G'), [Float("-0x0p+0").to_f].pack('G'))
+    assert_equal(255.0,     Float("0Xff"))
+    assert_equal(255.5,     Float("0Xff.8"))
+    assert_equal(1.0,       Float("0X1.P+0"))
+    assert_equal(1024.0,    Float("0x1p10"))
+    assert_equal(1024.0,    Float("0x1p+10"))
+    assert_equal(0.0009765625, Float("0x1p-10"))
+    assert_equal(2.6881171418161356e+43, Float("0x1.3494a9b171bf5p+144"))
+    assert_equal(-3.720075976020836e-44, Float("-0x1.a8c1f14e2af5dp-145"))
+ 
   end
 
   def test_divmod
Index: test/ruby/test_string.rb
===================================================================
--- test/ruby/test_string.rb	(revision 27139)
+++ test/ruby/test_string.rb	(revision 27140)
@@ -1398,7 +1398,10 @@
     assert_equal([-0.0].pack('G'), [S("-0.0").to_f].pack('G'))
     assert_equal([ 0.0].pack('G'), [S(" 0x0p+0").to_f].pack('G'))
     assert_equal([-0.0].pack('G'), [S("-0x0p+0").to_f].pack('G'))
+    assert_equal(255.0,     S("0Xff").to_f)
+    assert_equal(255.5,     S("0Xff.8").to_f)
     assert_equal(1.0,       S("0X1.P+0").to_f)
+    assert_equal(1024.0,    S("0x1p10").to_f)
     assert_equal(1024.0,    S("0x1p+10").to_f)
     assert_equal(0.0009765625, S("0x1p-10").to_f)
     assert_equal(2.6881171418161356e+43, S("0x1.3494a9b171bf5p+144").to_f)

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

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