mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* 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/trunk@12698 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
b553a34c63
commit
a0d50fa3c4
3 changed files with 39 additions and 4 deletions
|
@ -1,3 +1,9 @@
|
||||||
|
Thu Jul 5 16:37:34 2007 NAKAMURA Usaku <usa@ruby-lang.org>
|
||||||
|
|
||||||
|
* 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 <nobu@ruby-lang.org>
|
Thu Jul 5 15:56:06 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* numeric.c (int_pow): even number multiplication never be negative.
|
* numeric.c (int_pow): even number multiplication never be negative.
|
||||||
|
|
11
numeric.c
11
numeric.c
|
@ -2281,7 +2281,10 @@ int_pow(long x, unsigned long y)
|
||||||
long z = 1;
|
long z = 1;
|
||||||
|
|
||||||
if (neg) x = -x;
|
if (neg) x = -x;
|
||||||
if (y & 1) z = x;
|
if (y & 1)
|
||||||
|
z = x;
|
||||||
|
else
|
||||||
|
neg = 0;
|
||||||
y &= ~1;
|
y &= ~1;
|
||||||
do {
|
do {
|
||||||
while (y % 2 == 0) {
|
while (y % 2 == 0) {
|
||||||
|
@ -2289,8 +2292,8 @@ int_pow(long x, unsigned long y)
|
||||||
if (x2 < x || !POSFIXABLE(x2)) {
|
if (x2 < x || !POSFIXABLE(x2)) {
|
||||||
VALUE v;
|
VALUE v;
|
||||||
bignum:
|
bignum:
|
||||||
v = rb_big_pow(rb_int2big(neg ? -x : x), LONG2NUM(y));
|
v = rb_big_pow(rb_int2big(x), LONG2NUM(y));
|
||||||
if (z != 1) v = rb_big_mul(rb_int2big(z), v);
|
if (z != 1) v = rb_big_mul(rb_int2big(neg ? -z : z), v);
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
x = x2;
|
x = x2;
|
||||||
|
@ -2304,7 +2307,7 @@ int_pow(long x, unsigned long y)
|
||||||
z = xz;
|
z = xz;
|
||||||
}
|
}
|
||||||
} while (--y);
|
} while (--y);
|
||||||
if (neg && (y & 1)) z = -z;
|
if (neg) z = -z;
|
||||||
return LONG2NUM(z);
|
return LONG2NUM(z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
26
test/ruby/test_fixnum.rb
Normal file
26
test/ruby/test_fixnum.rb
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue