Project

General

Profile

Feature #15435 » use-predefined-infinity.patch

shuujii (Shuji KOBAYASHI), 12/19/2018 08:49 AM

View differences:

array.c
5423 5423
	n = RARRAY_AREF(args, 0);
5424 5424
    }
5425 5425
    if (RARRAY_LEN(self) == 0) return INT2FIX(0);
5426
    if (n == Qnil) return DBL2NUM(HUGE_VAL);
5426
    if (n == Qnil) return rb_float_positive_infinity;
5427 5427
    mul = NUM2LONG(n);
5428 5428
    if (mul <= 0) return INT2FIX(0);
5429 5429
    n = LONG2FIX(mul);
enum.c
2937 2937
    size = enum_size(self, args, 0);
2938 2938
    if (NIL_P(size) || FIXNUM_ZERO_P(size)) return size;
2939 2939

  
2940
    if (NIL_P(n)) return DBL2NUM(HUGE_VAL);
2940
    if (NIL_P(n)) return rb_float_positive_infinity;
2941 2941
    if (mul <= 0) return INT2FIX(0);
2942 2942
    n = LONG2FIX(mul);
2943 2943
    return rb_funcallv(size, '*', 1, &n);
enumerator.c
3167 3167
    }
3168 3168

  
3169 3169
    if (NIL_P(e)) {
3170
        return DBL2NUM(HUGE_VAL);
3170
        return rb_float_positive_infinity;
3171 3171
    }
3172 3172

  
3173 3173
    if (!rb_obj_is_kind_of(s, rb_cNumeric)) {
......
3175 3175
    }
3176 3176

  
3177 3177
    if (rb_equal(s, INT2FIX(0))) {
3178
        return DBL2NUM(HUGE_VAL);
3178
        return rb_float_positive_infinity;
3179 3179
    }
3180 3180

  
3181 3181
    len_1 = rb_int_idiv(rb_int_minus(e, b), s);
include/ruby/ruby.h
2052 2052

  
2053 2053
RUBY_EXTERN VALUE rb_stdin, rb_stdout, rb_stderr;
2054 2054

  
2055
RUBY_EXTERN VALUE rb_float_negative_infinity, rb_float_positive_infinity;
2056

  
2055 2057
static inline VALUE
2056 2058
rb_class_of(VALUE obj)
2057 2059
{
internal.h
1821 1821
    }
1822 1822
    /* out of range */
1823 1823
#endif
1824
    if (isinf(d)) {
1825
	return d < 0 ? rb_float_negative_infinity : rb_float_positive_infinity;
1826
    }
1824 1827
    return rb_float_new_in_heap(d);
1825 1828
}
1826 1829

  
math.c
382 382
    /* check for domain error */
383 383
    if (d <  -1.0 || +1.0 <  d) domain_error("atanh");
384 384
    /* check for pole error */
385
    if (d == -1.0) return DBL2NUM(-HUGE_VAL);
386
    if (d == +1.0) return DBL2NUM(+HUGE_VAL);
385
    if (d == -1.0) return rb_float_negative_infinity;
386
    if (d == +1.0) return rb_float_positive_infinity;
387 387
    return DBL2NUM(atanh(d));
388 388
}
389 389

  
......
538 538
    /* check for domain error */
539 539
    if (d < 0.0) domain_error("log2");
540 540
    /* check for pole error */
541
    if (d == 0.0) return DBL2NUM(-HUGE_VAL);
541
    if (d == 0.0) return rb_float_negative_infinity;
542 542

  
543 543
    return DBL2NUM(log2(d) + numbits); /* log2(d * 2 ** numbits) */
544 544
}
......
568 568
    /* check for domain error */
569 569
    if (d < 0.0) domain_error("log10");
570 570
    /* check for pole error */
571
    if (d == 0.0) return DBL2NUM(-HUGE_VAL);
571
    if (d == 0.0) return rb_float_negative_infinity;
572 572

  
573 573
    return DBL2NUM(log10(d) + numbits * log10(2)); /* log10(d * 2 ** numbits) */
574 574
}
......
869 869
    /* check for domain error */
870 870
    if (isinf(d)) {
871 871
	if (signbit(d)) domain_error("gamma");
872
	return DBL2NUM(HUGE_VAL);
872
	return rb_float_positive_infinity;
873 873
    }
874 874
    if (d == 0.0) {
875
	return signbit(d) ? DBL2NUM(-HUGE_VAL) : DBL2NUM(HUGE_VAL);
875
	return signbit(d) ? rb_float_negative_infinity : rb_float_positive_infinity;
876 876
    }
877 877
    if (d == floor(d)) {
878 878
	if (d < 0.0) domain_error("gamma");
......
907 907
    /* check for domain error */
908 908
    if (isinf(d)) {
909 909
	if (signbit(d)) domain_error("lgamma");
910
	return rb_assoc_new(DBL2NUM(HUGE_VAL), INT2FIX(1));
910
	return rb_assoc_new(rb_float_positive_infinity, INT2FIX(1));
911 911
    }
912 912
    if (d == 0.0) {
913 913
	VALUE vsign = signbit(d) ? INT2FIX(-1) : INT2FIX(+1);
914
	return rb_assoc_new(DBL2NUM(HUGE_VAL), vsign);
914
	return rb_assoc_new(rb_float_positive_infinity, vsign);
915 915
    }
916 916
    v = DBL2NUM(lgamma_r(d, &sign));
917 917
    return rb_assoc_new(v, INT2FIX(sign));
numeric.c
186 186
VALUE rb_eZeroDivError;
187 187
VALUE rb_eFloatDomainError;
188 188

  
189
VALUE rb_float_negative_infinity, rb_float_positive_infinity;
190

  
189 191
static ID id_to, id_by;
190 192

  
191 193
void
......
2549 2551

  
2550 2552
	diff = FIX2LONG(step);
2551 2553
	if (diff == 0) {
2552
	    return DBL2NUM(HUGE_VAL);
2554
	    return rb_float_positive_infinity;
2553 2555
	}
2554 2556
	delta = FIX2LONG(to) - FIX2LONG(from);
2555 2557
	if (diff < 0) {
......
2575 2577
	VALUE result;
2576 2578
	ID cmp = '>';
2577 2579
	switch (rb_cmpint(rb_num_coerce_cmp(step, INT2FIX(0), id_cmp), step, INT2FIX(0))) {
2578
	  case 0: return DBL2NUM(HUGE_VAL);
2580
	  case 0: return rb_float_positive_infinity;
2579 2581
	  case -1: cmp = '<'; break;
2580 2582
	}
2581 2583
	if (RTEST(rb_funcall(from, cmp, 1, to))) return INT2FIX(0);
......
2656 2658
    }
2657 2659
    desc = num_step_negative_p(*step);
2658 2660
    if (fix_nil && NIL_P(*to)) {
2659
        *to = desc ? DBL2NUM(-HUGE_VAL) : DBL2NUM(HUGE_VAL);
2661
        *to = desc ? rb_float_negative_infinity : rb_float_positive_infinity;
2660 2662
    }
2661 2663
    return desc;
2662 2664
}
......
4050 4052
	if (b == 1) return x;
4051 4053
	if (a == 0) {
4052 4054
	    if (b > 0) return INT2FIX(0);
4053
	    return DBL2NUM(HUGE_VAL);
4055
	    return rb_float_positive_infinity;
4054 4056
	}
4055 4057
	return int_pow(a, b);
4056 4058
    }
......
4078 4080
	double dy = RFLOAT_VALUE(y);
4079 4081
	if (dy == 0.0) return DBL2NUM(1.0);
4080 4082
	if (a == 0) {
4081
	    return DBL2NUM(dy < 0 ? HUGE_VAL : 0.0);
4083
	    return dy < 0 ? rb_float_positive_infinity : DBL2NUM(0.0);
4082 4084
	}
4083 4085
	if (a == 1) return DBL2NUM(1.0);
4084 4086
	{
......
5664 5666
    /*
5665 5667
     *	An expression representing positive infinity.
5666 5668
     */
5667
    rb_define_const(rb_cFloat, "INFINITY", DBL2NUM(HUGE_VAL));
5669
    rb_define_const(rb_cFloat, "INFINITY",
5670
                    rb_float_positive_infinity = rb_float_new_in_heap(HUGE_VAL));
5668 5671
    /*
5669 5672
     *	An expression representing a value which is "not a number".
5670 5673
     */
......
5715 5718

  
5716 5719
    id_to = rb_intern("to");
5717 5720
    id_by = rb_intern("by");
5721

  
5722
    rb_global_variable(&rb_float_negative_infinity);
5723
    rb_float_negative_infinity = rb_float_new_in_heap(-HUGE_VAL);
5718 5724
}
5719 5725

  
5720 5726
#undef rb_float_value
parse.y
10001 10001
	RCOMPLEX_SET_IMAG(lit, negate_lit(p, RCOMPLEX(lit)->imag));
10002 10002
	break;
10003 10003
      case T_FLOAT:
10004
	RFLOAT(lit)->float_value = -RFLOAT_VALUE(lit);
10004
	if (lit == rb_float_positive_infinity) {
10005
	    lit = rb_float_negative_infinity;
10006
	}
10007
	else {
10008
	    RFLOAT(lit)->float_value = -RFLOAT_VALUE(lit);
10009
	}
10005 10010
	break;
10006 10011
      unknown:
10007 10012
      default:
range.c
767 767
	    return ruby_num_interval_step_size(b, e, INT2FIX(1), EXCL(range));
768 768
        }
769 769
        if (NIL_P(e)) {
770
            return DBL2NUM(HUGE_VAL);
770
            return rb_float_positive_infinity;
771 771
        }
772 772
    }
773 773

  
rational.c
2442 2442
            VALUE mul;
2443 2443
            if (!FIXNUM_P(nexp)) {
2444 2444
              overflow:
2445
                return sign == '-' ? DBL2NUM(-HUGE_VAL) : DBL2NUM(HUGE_VAL);
2445
                return sign == '-' ? rb_float_negative_infinity : rb_float_positive_infinity;
2446 2446
            }
2447 2447
            mul = f_expt10(LONG2NUM(-FIX2LONG(nexp)));
2448 2448
            if (RB_FLOAT_TYPE_P(mul)) goto overflow;
test/ruby/test_float.rb
961 961
      end
962 962
    end;
963 963
  end
964

  
965
  def test_predefined_infinity
966
    assert_same(-Float::INFINITY, -1e1020)
967
    assert_same(-Float::INFINITY, -1*Float::INFINITY)
968
    assert_same(-Float::INFINITY, 1/-0.0)
969
    assert_same(Float::INFINITY, 1e1020)
970
    assert_same(Float::INFINITY, Float::INFINITY+1)
971
    assert_same(Float::INFINITY, 1/0.0)
972
  end
964 973
end
vm_eval.c
1068 1068
static VALUE
1069 1069
rb_f_loop_size(VALUE self, VALUE args, VALUE eobj)
1070 1070
{
1071
    return DBL2NUM(HUGE_VAL);
1071
    return rb_float_positive_infinity;
1072 1072
}
1073 1073

  
1074 1074
/*