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

ruby-changes:49820

From: shyouhei <ko1@a...>
Date: Fri, 19 Jan 2018 10:45:44 +0900 (JST)
Subject: [ruby-changes:49820] shyouhei:r61938 (trunk): INFINITY is float. That of double is HUGE_VAL.

shyouhei	2018-01-19 10:45:36 +0900 (Fri, 19 Jan 2018)

  New Revision: 61938

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=61938

  Log:
    INFINITY is float. That of double is HUGE_VAL.
    
    It seems HUGE_VAL is already used. Why not eliminate INTINITY.
    NAN is also float. That of double is called nan(). This is also
    fixed.
    
    Signed-off-by: Urabe, Shyouhei <shyouhei@r...>

  Modified files:
    trunk/array.c
    trunk/bignum.c
    trunk/configure.ac
    trunk/enum.c
    trunk/enumerator.c
    trunk/include/ruby/missing.h
    trunk/marshal.c
    trunk/math.c
    trunk/numeric.c
    trunk/rational.c
    trunk/vm_eval.c
Index: enumerator.c
===================================================================
--- enumerator.c	(revision 61937)
+++ enumerator.c	(revision 61938)
@@ -397,7 +397,7 @@ enumerator_initialize(int argc, VALUE *a https://github.com/ruby/ruby/blob/trunk/enumerator.c#L397
 	recv = generator_init(generator_allocate(rb_cGenerator), rb_block_proc());
 	if (argc) {
             if (NIL_P(argv[0]) || rb_respond_to(argv[0], id_call) ||
-                (RB_TYPE_P(argv[0], T_FLOAT) && RFLOAT_VALUE(argv[0]) == INFINITY)) {
+                (RB_TYPE_P(argv[0], T_FLOAT) && RFLOAT_VALUE(argv[0]) == HUGE_VAL)) {
                 size = argv[0];
             }
             else {
Index: bignum.c
===================================================================
--- bignum.c	(revision 61937)
+++ bignum.c	(revision 61938)
@@ -6130,7 +6130,7 @@ big_fdiv(VALUE x, VALUE y, long ey) https://github.com/ruby/ruby/blob/trunk/bignum.c#L6130
 #if SIZEOF_LONG > SIZEOF_INT
     {
 	/* Visual C++ can't be here */
-	if (l > INT_MAX) return INFINITY;
+	if (l > INT_MAX) return HUGE_VAL;
 	if (l < INT_MIN) return 0.0;
     }
 #endif
Index: math.c
===================================================================
--- math.c	(revision 61937)
+++ math.c	(revision 61938)
@@ -382,8 +382,8 @@ math_atanh(VALUE unused_obj, VALUE x) https://github.com/ruby/ruby/blob/trunk/math.c#L382
     /* check for domain error */
     if (d <  -1.0 || +1.0 <  d) domain_error("atanh");
     /* check for pole error */
-    if (d == -1.0) return DBL2NUM(-INFINITY);
-    if (d == +1.0) return DBL2NUM(+INFINITY);
+    if (d == -1.0) return DBL2NUM(-HUGE_VAL);
+    if (d == +1.0) return DBL2NUM(+HUGE_VAL);
     return DBL2NUM(atanh(d));
 }
 
@@ -488,7 +488,7 @@ math_log1(VALUE x) https://github.com/ruby/ruby/blob/trunk/math.c#L488
     /* check for domain error */
     if (d < 0.0) domain_error("log");
     /* check for pole error */
-    if (d == 0.0) return -INFINITY;
+    if (d == 0.0) return -HUGE_VAL;
 
     return log(d) + numbits * M_LN2; /* log(d * 2 ** numbits) */
 }
@@ -531,7 +531,7 @@ math_log2(VALUE unused_obj, VALUE x) https://github.com/ruby/ruby/blob/trunk/math.c#L531
     /* check for domain error */
     if (d < 0.0) domain_error("log2");
     /* check for pole error */
-    if (d == 0.0) return DBL2NUM(-INFINITY);
+    if (d == 0.0) return DBL2NUM(-HUGE_VAL);
 
     return DBL2NUM(log2(d) + numbits); /* log2(d * 2 ** numbits) */
 }
@@ -561,7 +561,7 @@ math_log10(VALUE unused_obj, VALUE x) https://github.com/ruby/ruby/blob/trunk/math.c#L561
     /* check for domain error */
     if (d < 0.0) domain_error("log10");
     /* check for pole error */
-    if (d == 0.0) return DBL2NUM(-INFINITY);
+    if (d == 0.0) return DBL2NUM(-HUGE_VAL);
 
     return DBL2NUM(log10(d) + numbits * log10(2)); /* log10(d * 2 ** numbits) */
 }
@@ -855,10 +855,10 @@ math_gamma(VALUE unused_obj, VALUE x) https://github.com/ruby/ruby/blob/trunk/math.c#L855
     /* check for domain error */
     if (isinf(d)) {
 	if (signbit(d)) domain_error("gamma");
-	return DBL2NUM(INFINITY);
+	return DBL2NUM(HUGE_VAL);
     }
     if (d == 0.0) {
-	return signbit(d) ? DBL2NUM(-INFINITY) : DBL2NUM(INFINITY);
+	return signbit(d) ? DBL2NUM(-HUGE_VAL) : DBL2NUM(HUGE_VAL);
     }
     if (d == floor(d)) {
 	if (d < 0.0) domain_error("gamma");
@@ -893,11 +893,11 @@ math_lgamma(VALUE unused_obj, VALUE x) https://github.com/ruby/ruby/blob/trunk/math.c#L893
     /* check for domain error */
     if (isinf(d)) {
 	if (signbit(d)) domain_error("lgamma");
-	return rb_assoc_new(DBL2NUM(INFINITY), INT2FIX(1));
+	return rb_assoc_new(DBL2NUM(HUGE_VAL), INT2FIX(1));
     }
     if (d == 0.0) {
 	VALUE vsign = signbit(d) ? INT2FIX(-1) : INT2FIX(+1);
-	return rb_assoc_new(DBL2NUM(INFINITY), vsign);
+	return rb_assoc_new(DBL2NUM(HUGE_VAL), vsign);
     }
     v = DBL2NUM(lgamma_r(d, &sign));
     return rb_assoc_new(v, INT2FIX(sign));
Index: vm_eval.c
===================================================================
--- vm_eval.c	(revision 61937)
+++ vm_eval.c	(revision 61938)
@@ -1060,7 +1060,7 @@ loop_stop(VALUE dummy, VALUE exc) https://github.com/ruby/ruby/blob/trunk/vm_eval.c#L1060
 static VALUE
 rb_f_loop_size(VALUE self, VALUE args, VALUE eobj)
 {
-    return DBL2NUM(INFINITY);
+    return DBL2NUM(HUGE_VAL);
 }
 
 /*
Index: enum.c
===================================================================
--- enum.c	(revision 61937)
+++ enum.c	(revision 61938)
@@ -2887,7 +2887,7 @@ enum_cycle_size(VALUE self, VALUE args, https://github.com/ruby/ruby/blob/trunk/enum.c#L2887
     size = enum_size(self, args, 0);
     if (NIL_P(size) || FIXNUM_ZERO_P(size)) return size;
 
-    if (NIL_P(n)) return DBL2NUM(INFINITY);
+    if (NIL_P(n)) return DBL2NUM(HUGE_VAL);
     if (mul <= 0) return INT2FIX(0);
     n = LONG2FIX(mul);
     return rb_funcallv(size, '*', 1, &n);
Index: rational.c
===================================================================
--- rational.c	(revision 61937)
+++ rational.c	(revision 61938)
@@ -1072,7 +1072,12 @@ nurat_expt(VALUE self, VALUE other) https://github.com/ruby/ruby/blob/trunk/rational.c#L1072
 		den = ONE;
 	    }
 	    if (RB_FLOAT_TYPE_P(num)) { /* infinity due to overflow */
-		if (RB_FLOAT_TYPE_P(den)) return DBL2NUM(NAN);
+		if (RB_FLOAT_TYPE_P(den))
+#ifdef HAVE_NANF
+		    return DBL2NUM(nan(""));
+#else
+		    return DBL2NUM((double)NAN);
+#endif
 		return num;
 	    }
 	    if (RB_FLOAT_TYPE_P(den)) { /* infinity due to overflow */
Index: numeric.c
===================================================================
--- numeric.c	(revision 61937)
+++ numeric.c	(revision 61938)
@@ -1795,7 +1795,7 @@ flo_next_float(VALUE vx) https://github.com/ruby/ruby/blob/trunk/numeric.c#L1795
 {
     double x, y;
     x = NUM2DBL(vx);
-    y = nextafter(x, INFINITY);
+    y = nextafter(x, HUGE_VAL);
     return DBL2NUM(y);
 }
 
@@ -1846,7 +1846,7 @@ flo_prev_float(VALUE vx) https://github.com/ruby/ruby/blob/trunk/numeric.c#L1846
 {
     double x, y;
     x = NUM2DBL(vx);
-    y = nextafter(x, -INFINITY);
+    y = nextafter(x, -HUGE_VAL);
     return DBL2NUM(y);
 }
 
@@ -2451,7 +2451,7 @@ ruby_float_step_size(double beg, double https://github.com/ruby/ruby/blob/trunk/numeric.c#L2451
 	return unit > 0 ? beg <= end : beg >= end;
     }
     if (unit == 0) {
-	return INFINITY;
+	return HUGE_VAL;
     }
     if (err>0.5) err=0.5;
     if (excl) {
@@ -2507,7 +2507,7 @@ ruby_num_interval_step_size(VALUE from, https://github.com/ruby/ruby/blob/trunk/numeric.c#L2507
 
 	diff = FIX2LONG(step);
 	if (diff == 0) {
-	    return DBL2NUM(INFINITY);
+	    return DBL2NUM(HUGE_VAL);
 	}
 	delta = FIX2LONG(to) - FIX2LONG(from);
 	if (diff < 0) {
@@ -2533,7 +2533,7 @@ ruby_num_interval_step_size(VALUE from, https://github.com/ruby/ruby/blob/trunk/numeric.c#L2533
 	VALUE result;
 	ID cmp = '>';
 	switch (rb_cmpint(rb_num_coerce_cmp(step, INT2FIX(0), id_cmp), step, INT2FIX(0))) {
-	  case 0: return DBL2NUM(INFINITY);
+	  case 0: return DBL2NUM(HUGE_VAL);
 	  case -1: cmp = '<'; break;
 	}
 	if (RTEST(rb_funcall(from, cmp, 1, to))) return INT2FIX(0);
@@ -2604,7 +2604,7 @@ num_step_scan_args(int argc, const VALUE https://github.com/ruby/ruby/blob/trunk/numeric.c#L2604
     }
     desc = num_step_negative_p(*step);
     if (NIL_P(*to)) {
-	*to = desc ? DBL2NUM(-INFINITY) : DBL2NUM(INFINITY);
+	*to = desc ? DBL2NUM(-HUGE_VAL) : DBL2NUM(HUGE_VAL);
     }
     return desc;
 }
@@ -3650,7 +3650,13 @@ rb_int_fdiv_double(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3650
     else if (RB_TYPE_P(x, T_BIGNUM)) {
         return rb_big_fdiv_double(x, y);
     }
-    return NAN;
+    else {
+#ifdef HAVE_NANF
+        return nan("");
+#else
+        return (double)NAN;
+#endif
+    }
 }
 
 /*
@@ -3967,7 +3973,7 @@ fix_pow(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3973
 	if (b == 1) return x;
 	if (a == 0) {
 	    if (b > 0) return INT2FIX(0);
-	    return DBL2NUM(INFINITY);
+	    return DBL2NUM(HUGE_VAL);
 	}
 	return int_pow(a, b);
     }
@@ -3987,7 +3993,7 @@ fix_pow(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3993
 	double dy = RFLOAT_VALUE(y);
 	if (dy == 0.0) return DBL2NUM(1.0);
 	if (a == 0) {
-	    return DBL2NUM(dy < 0 ? INFINITY : 0.0);
+	    return DBL2NUM(dy < 0 ? HUGE_VAL : 0.0);
 	}
 	if (a == 1) return DBL2NUM(1.0);
 	{
@@ -5555,11 +5561,17 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L5561
     /*
      *	An expression representing positive infinity.
      */
-    rb_define_const(rb_cFloat, "INFINITY", DBL2NUM(INFINITY));
+    rb_define_const(rb_cFloat, "INFINITY", DBL2NUM(HUGE_VAL));
     /*
      *	An expression representing a value which is "not a number".
      */
-    rb_define_const(rb_cFloat, "NAN", DBL2NUM(NAN));
+    rb_define_const(rb_cFloat, "NAN",
+#ifdef HAVE_NANF
+		    DBL2NUM(nan(""))
+#else
+		    DBL2NUM((double)NAN)
+#endif
+	);
 
     rb_define_method(rb_cFloat, "to_s", flo_to_s, 0);
     rb_define_alias(rb_cFloat, "inspect", "to_s");
Index: array.c
===================================================================
--- array.c	(revision 61937)
+++ array.c	(revision 61938)
@@ -5012,7 +5012,7 @@ rb_ary_cycle_size(VALUE self, VALUE args https://github.com/ruby/ruby/blob/trunk/array.c#L5012
 	n = RARRAY_AREF(args, 0);
     }
     if (RARRAY_LEN(self) == 0) return INT2FIX(0);
-    if (n == Qnil) return DBL2NUM(INFINITY);
+    if (n == Qnil) return DBL2NUM(HUGE_VAL);
     mul = NUM2LONG(n);
     if (mul <= 0) return INT2FIX(0);
     n = LONG2FIX(mul);
Index: marshal.c
===================================================================
--- marshal.c	(revision 61937)
+++ marshal.c	(revision 61938)
@@ -1678,13 +1678,18 @@ r_object0(struct load_arg *arg, int *ivp https://github.com/ruby/ruby/blob/trunk/marshal.c#L1678
 	    const char *ptr = RSTRING_PTR(str);
 
 	    if (strcmp(ptr, "nan") == 0) {
-		d = NAN;
+		d =
+#ifdef HAVE_NANF
+		    nan("");
+#else
+		    (double)NAN;
+#endif
 	    }
 	    else if (strcmp(ptr, "inf") == 0) {
-		d = INFINITY;
+		d = HUGE_VAL;
 	    }
 	    else if (strcmp(ptr, "-inf") == 0) {
-		d = -INFINITY;
+		d = -HUGE_VAL;
 	    }
 	    else {
 		char *e;
Index: include/ruby/missing.h
===================================================================
--- include/ruby/missing.h	(revision 61937)
+++ include/ruby/missing.h	(revision 61938)
@@ -155,6 +155,10 @@ RUBY_EXTERN const union bytesequence4_or https://github.com/ruby/ruby/blob/trunk/include/ruby/missing.h#L155
 # define NAN (rb_nan.float_value)
 #endif
 
+#ifndef HUGE_VAL
+# define HUGE_VAL ((double)INFINITY)
+#endif
+
 #ifndef isinf
 # ifndef HAVE_ISINF
 #  if defined(HAVE_FINITE) && defined(HAVE_ISNAN)
Index: configure.ac
===================================================================
--- configure.ac	(revision 61937)
+++ configure.ac	(revision 61938)
@@ -805,7 +805,7 @@ AS_IF([test "$GCC:${warnflags+set}:no" = https://github.com/ruby/ruby/blob/trunk/configure.ac#L805
 ])
 RUBY_TRY_CFLAGS(-Qunused-arguments, [RUBY_APPEND_OPTIONS(rb_cv_wsuppress_flags, -Qunused-arguments)])
 
-for n in infinity nan; do
+for n in huge_val infinity nan; do
     m=AS_TR_CPP($n)
     AC_CACHE_CHECK([whether $m is available without C99 option], rb_cv_$n,
 	[AC_COMPILE_IFELSE(
@@ -2393,6 +2393,7 @@ AC_CHECK_FUNCS(memmem) https://github.com/ruby/ruby/blob/trunk/configure.ac#L2393
 AC_CHECK_FUNCS(mkfifo)
 AC_CHECK_FUNCS(mknod)
 AC_CHECK_FUNCS(mktime)
+AC_CHECK_FUNCS(nanf) # We need nan(), but HAVE_NAN conflicts...
 AC_CHECK_FUNCS(openat)
 AC_CHECK_FUNCS(pipe2)
 AC_CHECK_FUNCS(poll)

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

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