mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* numeric.c (fix_pow): integer power calculation: 0**n => 0,
1**n => 1, -1**n => 1 (n: even) / -1 (n: odd). * test/ruby/test_fixnum.rb (TestFixnum::test_pow): update test suite. pow(-3, 2^64) gives NaN when pow(3, 2^64) gives Inf. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@12808 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
8c3546c744
commit
217db90e87
3 changed files with 45 additions and 6 deletions
|
@ -1,3 +1,11 @@
|
||||||
|
Tue Jul 17 00:50:53 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
|
|
||||||
|
* numeric.c (fix_pow): integer power calculation: 0**n => 0,
|
||||||
|
1**n => 1, -1**n => 1 (n: even) / -1 (n: odd).
|
||||||
|
|
||||||
|
* test/ruby/test_fixnum.rb (TestFixnum::test_pow): update test
|
||||||
|
suite. pow(-3, 2^64) gives NaN when pow(3, 2^64) gives Inf.
|
||||||
|
|
||||||
Mon Jul 16 23:07:51 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
|
Mon Jul 16 23:07:51 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
|
|
||||||
* lib/base64.rb (Base64::b64encode): should not specify /o option
|
* lib/base64.rb (Base64::b64encode): should not specify /o option
|
||||||
|
|
41
numeric.c
41
numeric.c
|
@ -2218,6 +2218,15 @@ int_pow(x, y)
|
||||||
return LONG2NUM(z);
|
return LONG2NUM(z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
int_even_p(VALUE num)
|
||||||
|
{
|
||||||
|
if (rb_funcall(num, '%', 1, INT2FIX(2)) == INT2FIX(0)) {
|
||||||
|
return Qtrue;
|
||||||
|
}
|
||||||
|
return Qfalse;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* fix ** other => Numeric
|
* fix ** other => Numeric
|
||||||
|
@ -2234,23 +2243,45 @@ static VALUE
|
||||||
fix_pow(x, y)
|
fix_pow(x, y)
|
||||||
VALUE x, y;
|
VALUE x, y;
|
||||||
{
|
{
|
||||||
|
long a = FIX2LONG(x);
|
||||||
|
|
||||||
if (FIXNUM_P(y)) {
|
if (FIXNUM_P(y)) {
|
||||||
long a, b;
|
long b;
|
||||||
|
|
||||||
b = FIX2LONG(y);
|
b = FIX2LONG(y);
|
||||||
if (b == 0) return INT2FIX(1);
|
if (b == 0) return INT2FIX(1);
|
||||||
if (b == 1) return x;
|
if (b == 1) return x;
|
||||||
a = FIX2LONG(x);
|
a = FIX2LONG(x);
|
||||||
if (a == 0) return INT2FIX(0);
|
if (a == 0) return INT2FIX(0);
|
||||||
|
if (a == 1) return INT2FIX(1);
|
||||||
|
if (a == -1) {
|
||||||
|
if (b % 2 == 0)
|
||||||
|
return INT2FIX(1);
|
||||||
|
else
|
||||||
|
return INT2FIX(-1);
|
||||||
|
}
|
||||||
if (b > 0) {
|
if (b > 0) {
|
||||||
return int_pow(a, b);
|
return int_pow(a, b);
|
||||||
}
|
}
|
||||||
return rb_float_new(pow((double)a, (double)b));
|
return rb_float_new(pow((double)a, (double)b));
|
||||||
} else if (TYPE(y) == T_FLOAT) {
|
|
||||||
long a = FIX2LONG(x);
|
|
||||||
return rb_float_new(pow((double)a, RFLOAT(y)->value));
|
|
||||||
}
|
}
|
||||||
return rb_num_coerce_bin(x, y);
|
switch (TYPE(y)) {
|
||||||
|
case T_BIGNUM:
|
||||||
|
if (a == 0) return INT2FIX(0);
|
||||||
|
if (a == 1) return INT2FIX(1);
|
||||||
|
if (a == -1) {
|
||||||
|
if (int_even_p(y)) return INT2FIX(1);
|
||||||
|
else return INT2FIX(-1);
|
||||||
|
}
|
||||||
|
x = rb_int2big(FIX2LONG(x));
|
||||||
|
return rb_big_pow(x, y);
|
||||||
|
case T_FLOAT:
|
||||||
|
if (a == 0) return rb_float_new(0.0);
|
||||||
|
if (a == 1) return rb_float_new(1.0);
|
||||||
|
return rb_float_new(pow((double)a, RFLOAT(y)->value));
|
||||||
|
default:
|
||||||
|
return rb_num_coerce_bin(x, y);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -12,7 +12,7 @@ class TestFixnum < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_pow
|
def test_pow
|
||||||
[1, 2, 2**64, 2**63*3, 2**64*3].each do |y|
|
[1, 2, 2**64, 2**63*3, 2**64*3].each do |y|
|
||||||
[1, 3].each do |x|
|
[-1, 0, 1].each do |x|
|
||||||
z1 = x**y
|
z1 = x**y
|
||||||
z2 = (-x)**y
|
z2 = (-x)**y
|
||||||
if y % 2 == 1
|
if y % 2 == 1
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue