From 42b4e02760c725e1a426b6845695ecdbc8fe3e3b Mon Sep 17 00:00:00 2001 From: usa Date: Thu, 5 Jul 2007 07:45:02 +0000 Subject: * numeric.c (int_pow): fix previous nubu's commit. * test/ruby/test_fixnum.rb: new test. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@12699 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ numeric.c | 7 +++++-- test/ruby/test_fixnum.rb | 26 ++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 test/ruby/test_fixnum.rb diff --git a/ChangeLog b/ChangeLog index bdab52522e..e42280b9d1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Thu Jul 5 16:44:28 2007 NAKAMURA Usaku + + * numeric.c (int_pow): fix previous nubu's commit. + + * test/ruby/test_fixnum.rb: new test. + Thu Jul 5 15:56:06 2007 Nobuyoshi Nakada * numeric.c (int_pow): even number multiplication never be negative. diff --git a/numeric.c b/numeric.c index 7402f1527c..f0b827aacc 100644 --- a/numeric.c +++ b/numeric.c @@ -2188,7 +2188,10 @@ int_pow(x, y) long z = 1; if (neg) x = -x; - if (y & 1) z = x; + if (y & 1) + z = x; + else + neg = 0; y &= ~1; do { while (y % 2 == 0) { @@ -2209,7 +2212,7 @@ int_pow(x, y) z = xz; } } while (--y); - if (neg && (y & 1)) z = -z; + if (neg) z = -z; return LONG2NUM(z); } diff --git a/test/ruby/test_fixnum.rb b/test/ruby/test_fixnum.rb new file mode 100644 index 0000000000..57999d4b03 --- /dev/null +++ b/test/ruby/test_fixnum.rb @@ -0,0 +1,26 @@ +require 'test/unit' + +class TestFixnum < Test::Unit::TestCase + def setup + @verbose = $VERBOSE + $VERBOSE = nil + end + + def teardown + $VERBOSE = @verbose + end + + def test_pow + [1, 2, 2**64, 2**63*3, 2**64*3].each do |y| + [1, 3].each do |x| + z1 = x**y + z2 = (-x)**y + if y % 2 == 1 + assert_equal(z2, -z1) + else + assert_equal(z2, z1) + end + end + end + end +end -- cgit v1.2.3