diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-03-18 03:01:58 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-03-18 03:01:58 +0000 |
commit | 3bcf9fb53e4b9efabb15a3091fddfb68e5b6fbbe (patch) | |
tree | ecf199a6ec42abd624f2c79c8df435f8dd528764 | |
parent | 1e98a25e9fff7e4c9c858f67c9ca7c6b9ee793f8 (diff) | |
download | ruby-3bcf9fb53e4b9efabb15a3091fddfb68e5b6fbbe.tar.gz |
hash.c: same hash values with Float#hash
* hash.c (rb_any_hash): use same hash values with Float#hash so
that -0.0 and +0.0 will be identical.
[ruby-core:68541] [Bug #10979]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49999 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | hash.c | 5 | ||||
-rw-r--r-- | internal.h | 1 | ||||
-rw-r--r-- | numeric.c | 8 | ||||
-rw-r--r-- | test/ruby/test_float.rb | 8 |
5 files changed, 25 insertions, 3 deletions
@@ -1,3 +1,9 @@ +Wed Mar 18 12:01:53 2015 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * hash.c (rb_any_hash): use same hash values with Float#hash so + that -0.0 and +0.0 will be identical. + [ruby-core:68541] [Bug #10979] + Wed Mar 18 05:34:32 2015 Koichi Sasada <ko1@atdot.net> * string.c: introduce STR_FAKESTR to show string is FAKESTR or not. @@ -142,13 +142,16 @@ rb_any_hash(VALUE a) } else if (FLONUM_P(a)) { /* prevent pathological behavior: [Bug #10761] */ - a = (st_index_t)rb_float_value(a); + return rb_dbl_hash(rb_float_value(a)); } hnum = rb_objid_hash((st_index_t)a); } else if (BUILTIN_TYPE(a) == T_STRING) { hnum = rb_str_hash(a); } + else if (BUILTIN_TYPE(a) == T_FLOAT) { + return rb_dbl_hash(rb_float_value(a)); + } else { hval = rb_hash(a); hnum = FIX2LONG(hval); diff --git a/internal.h b/internal.h index 953db1b792..e6335c06ee 100644 --- a/internal.h +++ b/internal.h @@ -885,6 +885,7 @@ double ruby_float_mod(double x, double y); int rb_num_negative_p(VALUE); VALUE rb_int_succ(VALUE num); VALUE rb_int_pred(VALUE num); +VALUE rb_dbl_hash(double d); #if USE_FLONUM #define RUBY_BIT_ROTL(v, n) (((v) << (n)) | ((v) >> ((sizeof(v) * 8) - n))) @@ -1137,10 +1137,14 @@ flo_eq(VALUE x, VALUE y) static VALUE flo_hash(VALUE num) { - double d; + return rb_dbl_hash(RFLOAT_VALUE(num)); +} + +VALUE +rb_dbl_hash(double d) +{ st_index_t hash; - d = RFLOAT_VALUE(num); /* normalize -0.0 to 0.0 */ if (d == 0.0) d = 0.0; hash = rb_memhash(&d, sizeof(d)); diff --git a/test/ruby/test_float.rb b/test/ruby/test_float.rb index ac2d84352b..073e382777 100644 --- a/test/ruby/test_float.rb +++ b/test/ruby/test_float.rb @@ -672,4 +672,12 @@ class TestFloat < Test::Unit::TestCase assert_equal(0.0, z) assert_equal(-Float::INFINITY, 1.0/z) end + + def test_hash_0 + bug10979 = '[ruby-core:68541] [Bug #10979]' + assert_equal(+0.0.hash, -0.0.hash) + assert_operator(+0.0, :eql?, -0.0) + h = {0.0 => bug10979} + assert_equal(bug10979, h[-0.0]) + end end |