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

ruby-changes:14367

From: naruse <ko1@a...>
Date: Tue, 29 Dec 2009 16:05:58 +0900 (JST)
Subject: [ruby-changes:14367] Ruby:r26197 (trunk): Add Float::INFINITY and Float::NAN.

naruse	2009-12-29 16:05:39 +0900 (Tue, 29 Dec 2009)

  New Revision: 26197

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

  Log:
    Add Float::INFINITY and Float::NAN.
    
    * numeric.c (Init_Numeric): Add Float::INFINITY and Float::NAN.
      [ruby-dev:1657] [ruby-dev:4760] [ruby-list:7023] [ruby-list:46690]
      [ruby-core:26632] [ruby-talk:41352] [ruby-talk:203333]
    
    * include/ruby/defines.h (INFINITY): defined.
    
    * include/ruby/defines.h (NAN): defined.
    
    * include/ruby/util.h (ruby_div0): removed.
    
    * numeric.c (fix_pow): use INFINITY and NAN instead of ruby_div0(1.0).
    
    * marshal.c (r_object0): ditto.
    
    * bignum.c (big_fdiv): ditto.

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c
    trunk/include/ruby/defines.h
    trunk/include/ruby/util.h
    trunk/marshal.c
    trunk/numeric.c
    trunk/test/ruby/test_float.rb

Index: include/ruby/defines.h
===================================================================
--- include/ruby/defines.h	(revision 26196)
+++ include/ruby/defines.h	(revision 26197)
@@ -99,6 +99,20 @@
 # define BDIGIT_DBL_SIGNED long
 #endif
 
+#ifdef INFINITY
+# define HAVE_INFINITY
+#else
+extern const unsigned char rb_infinity[];
+# define INFINITY (*(double *)rb_infinity)
+#endif
+
+#ifdef NAN
+# define HAVE_NAN
+#else
+extern const unsigned char rb_nan[];
+# define NAN (*(double *)rb_nan)
+#endif
+
 #ifdef __CYGWIN__
 #undef _WIN32
 #endif
Index: include/ruby/util.h
===================================================================
--- include/ruby/util.h	(revision 26196)
+++ include/ruby/util.h	(revision 26197)
@@ -74,12 +74,6 @@
 #pragma warning(push)
 #pragma warning(disable:4723)
 #endif
-static inline double
-ruby_div0(double x)
-{
-    double t = 0.0;
-    return x / t;
-}
 #if defined _MSC_VER && _MSC_VER >= 1300
 #pragma warning(pop)
 #endif
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 26196)
+++ ChangeLog	(revision 26197)
@@ -1,3 +1,23 @@
+Tue Dec 29 16:03:33 2009  NARUSE, Yui  <naruse@r...>
+
+	* numeric.c (Init_Numeric): Add Float::INFINITY and Float::NAN.
+	  [ruby-dev:1657] [ruby-dev:4760] [ruby-list:7023]
+	  [ruby-list:46690]
+	  [ruby-core:26632] [ruby-talk:41352] [ruby-talk:203333]
+
+	* include/ruby/defines.h (INFINITY): defined.
+
+	* include/ruby/defines.h (NAN): defined.
+
+	* include/ruby/util.h (ruby_div0): removed.
+
+	* numeric.c (fix_pow): use INFINITY and NAN
+	  instead of ruby_div0(1.0).
+
+	* marshal.c (r_object0): ditto.
+
+	* bignum.c (big_fdiv): ditto.
+
 Tue Dec 29 10:36:23 2009  Nobuyoshi Nakada  <nobu@r...>
 
 	* lib/rexml/parsers/baseparser.rb (REXML::Parsers::BaseParser::STANDALONE):
Index: numeric.c
===================================================================
--- numeric.c	(revision 26196)
+++ numeric.c	(revision 26197)
@@ -63,6 +63,20 @@
 #define DBL_EPSILON 2.2204460492503131e-16
 #endif
 
+#ifdef HAVE_INFINITY
+#elif BYTE_ORDER == LITTLE_ENDIAN
+const unsigned char rb_infinity[] = "\x00\x00\x00\x00\x00\x00\xf0\x7f";
+#else
+const unsigned char rb_infinity[] = "\x7f\xf0\x00\x00\x00\x00\x00\x00";
+#endif
+
+#ifdef HAVE_NAN
+#elif BYTE_ORDER == LITTLE_ENDIAN
+const unsigned char rb_nan[] = "\x00\x00\x00\x00\x00\x00\xf8\x7f";
+#else
+const unsigned char rb_nan[] = "\x7f\xf8\x00\x00\x00\x00\x00\x00";
+#endif
+
 extern double round(double);
 
 #ifndef HAVE_ROUND
@@ -2467,8 +2481,6 @@
     return LONG2NUM(z);
 }
 
-#define infinite_value() ruby_div0(1.0)
-
 /*
  *  call-seq:
  *    fix ** numeric  ->  numeric_result
@@ -2496,7 +2508,7 @@
 	if (b == 1) return x;
 	if (a == 0) {
 	    if (b > 0) return INT2FIX(0);
-	    return DBL2NUM(infinite_value());
+	    return DBL2NUM(INFINITY);
 	}
 	if (a == 1) return INT2FIX(1);
 	if (a == -1) {
@@ -2524,7 +2536,7 @@
       case T_FLOAT:
 	if (RFLOAT_VALUE(y) == 0.0) return DBL2NUM(1.0);
 	if (a == 0) {
-	    return DBL2NUM(RFLOAT_VALUE(y) < 0 ? infinite_value() : 0.0);
+	    return DBL2NUM(RFLOAT_VALUE(y) < 0 ? INFINITY : 0.0);
 	}
 	if (a == 1) return DBL2NUM(1.0);
 	{
@@ -3305,6 +3317,8 @@
     rb_define_const(rb_cFloat, "MIN", DBL2NUM(DBL_MIN));
     rb_define_const(rb_cFloat, "MAX", DBL2NUM(DBL_MAX));
     rb_define_const(rb_cFloat, "EPSILON", DBL2NUM(DBL_EPSILON));
+    rb_define_const(rb_cFloat, "INFINITY", DBL2NUM(INFINITY));
+    rb_define_const(rb_cFloat, "NAN", DBL2NUM(NAN));
 
     rb_define_method(rb_cFloat, "to_s", flo_to_s, 0);
     rb_define_method(rb_cFloat, "coerce", flo_coerce, 1);
Index: bignum.c
===================================================================
--- bignum.c	(revision 26196)
+++ bignum.c	(revision 26197)
@@ -2485,7 +2485,7 @@
 #if SIZEOF_LONG > SIZEOF_INT
 	{
 	    /* Visual C++ can't be here */
-	    if (l > INT_MAX) return DBL2NUM(ruby_div0(1.0));
+	    if (l > INT_MAX) return DBL2NUM(INFINITY);
 	    if (l < INT_MIN) return DBL2NUM(0.0);
 	}
 #endif
Index: marshal.c
===================================================================
--- marshal.c	(revision 26196)
+++ marshal.c	(revision 26197)
@@ -1322,8 +1322,6 @@
     return rb_obj_alloc(klass);
 }
 
-#define div0(x) ruby_div0(x)
-
 static int
 has_encoding(struct load_arg *arg)
 {
@@ -1449,13 +1447,13 @@
 	    const char *ptr = RSTRING_PTR(str);
 
 	    if (strcmp(ptr, "nan") == 0) {
-		d = div0(0.0);
+		d = NAN;
 	    }
 	    else if (strcmp(ptr, "inf") == 0) {
-		d = div0(+1.0);
+		d = INFINITY;
 	    }
 	    else if (strcmp(ptr, "-inf") == 0) {
-		d = div0(-1.0);
+		d = -INFINITY;
 	    }
 	    else {
 		char *e;
Index: test/ruby/test_float.rb
===================================================================
--- test/ruby/test_float.rb	(revision 26196)
+++ test/ruby/test_float.rb	(revision 26197)
@@ -24,7 +24,7 @@
     assert_equal(false, (x >= y))
   end
   def test_nan
-    nan = 0.0/0
+    nan = Float::NAN
     nan_test(nan, nan)
     nan_test(nan, 0)
     nan_test(nan, 1)
@@ -118,7 +118,7 @@
   end
 
   def test_to_s
-    inf = 1.0 / 0.0
+    inf = Float::INFINITY
     assert_equal("Infinity", inf.to_s)
     assert_equal("-Infinity", (-inf).to_s)
     assert_equal("NaN", (inf / inf).to_s)
@@ -171,7 +171,7 @@
     assert_equal([1.0, 0.0], 2.0.divmod(2.0))
     assert_raise(TypeError) { 2.0.divmod(nil) }
 
-    inf = 1.0 / 0.0
+    inf = Float::INFINITY
     assert_raise(ZeroDivisionError) {inf.divmod(0)}
 
     a, b = (2.0**32).divmod(1.0)
@@ -186,8 +186,8 @@
   end
 
   def test_eql
-    inf = 1.0 / 0.0
-    nan = inf / inf
+    inf = Float::INFINITY
+    nan = Float::NAN
     assert(1.0.eql?(1.0))
     assert(inf.eql?(inf))
     assert(!(nan.eql?(nan)))
@@ -200,8 +200,8 @@
   end
 
   def test_cmp
-    inf = 1.0 / 0.0
-    nan = inf / inf
+    inf = Float::INFINITY
+    nan = Float::NAN
     assert_equal(0, 1.0 <=> 1.0)
     assert_equal(1, 1.0 <=> 0.0)
     assert_equal(-1, 1.0 <=> 2.0)
@@ -232,14 +232,14 @@
   end
 
   def test_infinite_p
-    inf = 1.0 / 0.0
+    inf = Float::INFINITY
     assert(1, inf.infinite?)
     assert(1, (-inf).infinite?)
     assert_nil(1.0.infinite?)
   end
 
   def test_finite_p
-    inf = 1.0 / 0.0
+    inf = Float::INFINITY
     assert(!(inf.finite?))
     assert(!((-inf).finite?))
     assert(1.0.finite?)
@@ -266,7 +266,7 @@
     assert_equal(-2, (-2.0).round)
     assert_equal(-2, (-2.0).truncate)
 
-    inf = 1.0/0.0
+    inf = Float::INFINITY
     assert_raise(FloatDomainError) { inf.floor }
     assert_raise(FloatDomainError) { inf.ceil }
     assert_raise(FloatDomainError) { inf.round }
@@ -413,7 +413,7 @@
     assert(Float("1e10_00").infinite?)
     assert_raise(TypeError) { Float(nil) }
     o = Object.new
-    def o.to_f; inf = 1.0/0.0; inf/inf; end
+    def o.to_f; inf = Float::INFINITY; inf/inf; end
     assert(Float(o).nan?)
   end
 

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

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