diff options
author | marcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-03-14 06:10:01 +0000 |
---|---|---|
committer | marcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2012-03-14 06:10:01 +0000 |
commit | 4c2e2d8bdefa8cd43adf1e28358353c1ded53049 (patch) | |
tree | 223830f7eec30a6ea0b2ba335c17547f3f7c51b7 | |
parent | 092db4dcf0a6c9b0dc0408d4f29189754a07212e (diff) |
* numeric.c: fix flodivmod for cornercases [Bug #6044]
add ruby_float_mod
* insns.def (opt_mod): use ruby_float_mod
* internal.h: declare ruby_float_mod
* test/ruby/test_float.rb: tests for above
* test/ruby/envutil.rb: create helper assert_is_minus_zero
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35013 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 13 | ||||
-rw-r--r-- | insns.def | 18 | ||||
-rw-r--r-- | internal.h | 1 | ||||
-rw-r--r-- | numeric.c | 27 | ||||
-rw-r--r-- | test/ruby/envutil.rb | 4 | ||||
-rw-r--r-- | test/ruby/test_float.rb | 12 |
6 files changed, 48 insertions, 27 deletions
@@ -1,3 +1,16 @@ +Wed Mar 14 15:09:23 2012 Marc-Andre Lafortune <[email protected]> + + * numeric.c: fix flodivmod for cornercases [Bug #6044] + add ruby_float_mod + + * insns.def (opt_mod): use ruby_float_mod + + * internal.h: declare ruby_float_mod + + * test/ruby/test_float.rb: tests for above + + * test/ruby/envutil.rb: create helper assert_is_minus_zero + Wed Mar 14 10:44:35 2012 Nobuyoshi Nakada <[email protected]> * enumerator.c (lazy_grep_func): should use === instead of =~, as @@ -1618,23 +1618,7 @@ opt_mod else if (HEAP_CLASS_OF(recv) == rb_cFloat && HEAP_CLASS_OF(obj) == rb_cFloat && BASIC_OP_UNREDEFINED_P(BOP_MOD, FLOAT_REDEFINED_OP_FLAG)) { - double x = RFLOAT_VALUE(recv); - double y = RFLOAT_VALUE(obj); - double div, mod; - - { - double z; - - modf(x / y, &z); - mod = x - z * y; - } - - div = (x - mod) / y; - if (y * mod < 0) { - mod += y; - div -= 1.0; - } - val = DBL2NUM(mod); + val = DBL2NUM(ruby_float_mod(RFLOAT_VALUE(recv), RFLOAT_VALUE(obj))); } else { goto INSN_LABEL(normal_dispatch); diff --git a/internal.h b/internal.h index 64a67e194d..e5818429ea 100644 --- a/internal.h +++ b/internal.h @@ -136,6 +136,7 @@ void Init_newline(void); /* numeric.c */ int rb_num_to_uint(VALUE val, unsigned int *ret); int ruby_float_step(VALUE from, VALUE to, VALUE step, int excl); +double ruby_float_mod(double x, double y); /* object.c */ VALUE rb_obj_equal(VALUE obj1, VALUE obj2); @@ -817,7 +817,9 @@ flodivmod(double x, double y, double *divp, double *modp) #ifdef HAVE_FMOD mod = fmod(x, y); #else - { + if((x == 0.0) || (isinf(y) && !isinf(x))) + mod = x; + else { double z; modf(x/y, &z); @@ -836,6 +838,17 @@ flodivmod(double x, double y, double *divp, double *modp) if (divp) *divp = div; } +/* + * Returns the modulo of division of x by y. + * An error will be raised if y == 0. + */ + +double ruby_float_mod(double x, double y) { + double mod; + flodivmod(x, y, 0, &mod); + return mod; +} + /* * call-seq: @@ -851,7 +864,7 @@ flodivmod(double x, double y, double *divp, double *modp) static VALUE flo_mod(VALUE x, VALUE y) { - double fy, mod; + double fy; switch (TYPE(y)) { case T_FIXNUM: @@ -866,8 +879,7 @@ flo_mod(VALUE x, VALUE y) default: return rb_num_coerce_bin(x, y, '%'); } - flodivmod(RFLOAT_VALUE(x), fy, 0, &mod); - return DBL2NUM(mod); + return DBL2NUM(ruby_float_mod(RFLOAT_VALUE(x), fy)); } static VALUE @@ -2733,12 +2745,7 @@ fix_mod(VALUE x, VALUE y) x = rb_int2big(FIX2LONG(x)); return rb_big_modulo(x, y); case T_FLOAT: - { - double mod; - - flodivmod((double)FIX2LONG(x), RFLOAT_VALUE(y), 0, &mod); - return DBL2NUM(mod); - } + return DBL2NUM(ruby_float_mod((double)FIX2LONG(x), RFLOAT_VALUE(y))); default: return rb_num_coerce_bin(x, y, '%'); } diff --git a/test/ruby/envutil.rb b/test/ruby/envutil.rb index 80700d6dc5..3e0c1a3f00 100644 --- a/test/ruby/envutil.rb +++ b/test/ruby/envutil.rb @@ -212,6 +212,10 @@ module Test assert_equal([true, ""], [status.success?, err], message) assert_operator(after.fdiv(before), :<, limit, message) end + + def assert_is_minus_zero(f) + assert(1.0/f == -Float::INFINITY, "#{f} is not -0.0") + end end end end diff --git a/test/ruby/test_float.rb b/test/ruby/test_float.rb index 1935cfdd52..f561d00158 100644 --- a/test/ruby/test_float.rb +++ b/test/ruby/test_float.rb @@ -195,6 +195,18 @@ class TestFloat < Test::Unit::TestCase assert_raise(TypeError) { 2.0.send(:%, nil) } end + def test_modulo3 + bug6048 = '[ruby-core:42726]' + assert_equal(4.2, 4.2.send(:%, Float::INFINITY)) + assert_equal(4.2, 4.2 % Float::INFINITY) + assert_is_minus_zero(-0.0 % 4.2) + assert_is_minus_zero(-0.0.send :%, 4.2) + assert_raise(ZeroDivisionError) { 4.2.send(:%, 0.0) } + assert_raise(ZeroDivisionError) { 4.2 % 0.0 } + assert_raise(ZeroDivisionError) { 42.send(:%, 0) } + assert_raise(ZeroDivisionError) { 42 % 0 } + end + def test_divmod2 assert_equal([1.0, 0.0], 2.0.divmod(2)) assert_equal([1.0, 0.0], 2.0.divmod((2**32).coerce(2).first)) |