summaryrefslogtreecommitdiff
path: root/spec/ruby
diff options
context:
space:
mode:
authorYusuke Endoh <[email protected]>2024-11-08 14:46:35 +0900
committerYusuke Endoh <[email protected]>2024-11-08 19:48:56 +0900
commit45cd4a8296814f3b082dfb906cdef29974726731 (patch)
treef0bb06b401fd95c09e9b44281473fafe5621c67b /spec/ruby
parentf7b334e002eba25e386917337771b65bed5297f8 (diff)
Do not round `a**b` to infinity
... instead, just calculate the value unless it is too big. Also, this change raises an ArgumentError if it is expected to exceed 16 GB in a 64-bit environment. (It is possible to calculate it straightforward, but it would likely be out-of-memory, so I didn't think it would make sense.) [Feature #20811]
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/12033
Diffstat (limited to 'spec/ruby')
-rw-r--r--spec/ruby/core/integer/shared/exponent.rb40
-rw-r--r--spec/ruby/shared/rational/exponent.rb102
2 files changed, 102 insertions, 40 deletions
diff --git a/spec/ruby/core/integer/shared/exponent.rb b/spec/ruby/core/integer/shared/exponent.rb
index 15df518b7e..5ef6d686d8 100644
--- a/spec/ruby/core/integer/shared/exponent.rb
+++ b/spec/ruby/core/integer/shared/exponent.rb
@@ -48,10 +48,18 @@ describe :integer_exponent, shared: true do
(-1).send(@method, 4611686018427387905).should eql(-1)
end
- it "returns Float::INFINITY when the number is too big" do
- -> {
- 2.send(@method, 427387904).should == Float::INFINITY
- }.should complain(/warning: in a\*\*b, b may be too big/)
+ ruby_version_is ""..."3.4" do
+ it "returns Float::INFINITY when the number is too big" do
+ -> {
+ 2.send(@method, 427387904).should == Float::INFINITY
+ }.should complain(/warning: in a\*\*b, b may be too big/)
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "raises an ArgumentError when the number is too big" do
+ -> { 100000000.send(@method, 1000000000) }.should raise_error(ArgumentError)
+ end
end
it "raises a ZeroDivisionError for 0 ** -1" do
@@ -108,13 +116,23 @@ describe :integer_exponent, shared: true do
-> { @bignum.send(@method, :symbol) }.should raise_error(TypeError)
end
- it "switch to a Float when the values is too big" do
- flt = nil
- -> {
- flt = @bignum.send(@method, @bignum)
- }.should complain(/warning: in a\*\*b, b may be too big/)
- flt.should be_kind_of(Float)
- flt.infinite?.should == 1
+ ruby_version_is ""..."3.4" do
+ it "switch to a Float when the values is too big" do
+ flt = nil
+ -> {
+ flt = @bignum.send(@method, @bignum)
+ }.should complain(/warning: in a\*\*b, b may be too big/)
+ flt.should be_kind_of(Float)
+ flt.infinite?.should == 1
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "does not switch to a Float when the values is too big" do
+ -> {
+ @bignum.send(@method, @bignum)
+ }.should raise_error(ArgumentError)
+ end
end
it "returns a complex number when negative and raised to a fractional power" do
diff --git a/spec/ruby/shared/rational/exponent.rb b/spec/ruby/shared/rational/exponent.rb
index b0e9b23574..2145d6bace 100644
--- a/spec/ruby/shared/rational/exponent.rb
+++ b/spec/ruby/shared/rational/exponent.rb
@@ -84,47 +84,91 @@ describe :rational_exponent, shared: true do
(Rational(-1) ** bignum_value(3)).should eql(Rational(-1))
end
- it "returns positive Infinity when self is > 1" do
- -> {
- (Rational(2) ** bignum_value).infinite?.should == 1
- }.should complain(/warning: in a\*\*b, b may be too big/)
- -> {
- (Rational(fixnum_max) ** bignum_value).infinite?.should == 1
- }.should complain(/warning: in a\*\*b, b may be too big/)
- end
-
- it "returns 0.0 when self is > 1 and the exponent is negative" do
- -> {
- (Rational(2) ** -bignum_value).should eql(0.0)
- }.should complain(/warning: in a\*\*b, b may be too big/)
- -> {
- (Rational(fixnum_max) ** -bignum_value).should eql(0.0)
- }.should complain(/warning: in a\*\*b, b may be too big/)
- end
-
- # Fails on linux due to pow() bugs in glibc: http://sources.redhat.com/bugzilla/show_bug.cgi?id=3866
- platform_is_not :linux do
- it "returns positive Infinity when self < -1" do
+ ruby_version_is ""..."3.4" do
+ it "returns positive Infinity when self is > 1" do
-> {
- (Rational(-2) ** bignum_value).infinite?.should == 1
+ (Rational(2) ** bignum_value).infinite?.should == 1
}.should complain(/warning: in a\*\*b, b may be too big/)
-> {
- (Rational(-2) ** (bignum_value + 1)).infinite?.should == 1
- }.should complain(/warning: in a\*\*b, b may be too big/)
- -> {
- (Rational(fixnum_min) ** bignum_value).infinite?.should == 1
+ (Rational(fixnum_max) ** bignum_value).infinite?.should == 1
}.should complain(/warning: in a\*\*b, b may be too big/)
end
- it "returns 0.0 when self is < -1 and the exponent is negative" do
+ it "returns 0.0 when self is > 1 and the exponent is negative" do
-> {
- (Rational(-2) ** -bignum_value).should eql(0.0)
+ (Rational(2) ** -bignum_value).should eql(0.0)
}.should complain(/warning: in a\*\*b, b may be too big/)
-> {
- (Rational(fixnum_min) ** -bignum_value).should eql(0.0)
+ (Rational(fixnum_max) ** -bignum_value).should eql(0.0)
}.should complain(/warning: in a\*\*b, b may be too big/)
end
end
+
+ ruby_version_is "3.4" do
+ it "raises an ArgumentError when self is > 1" do
+ -> {
+ (Rational(2) ** bignum_value)
+ }.should raise_error(ArgumentError)
+ -> {
+ (Rational(fixnum_max) ** bignum_value)
+ }.should raise_error(ArgumentError)
+ end
+
+ it "raises an ArgumentError when self is > 1 and the exponent is negative" do
+ -> {
+ (Rational(2) ** -bignum_value)
+ }.should raise_error(ArgumentError)
+ -> {
+ (Rational(fixnum_max) ** -bignum_value)
+ }.should raise_error(ArgumentError)
+ end
+ end
+
+ # Fails on linux due to pow() bugs in glibc: http://sources.redhat.com/bugzilla/show_bug.cgi?id=3866
+ platform_is_not :linux do
+ ruby_version_is ""..."3.4" do
+ it "returns positive Infinity when self < -1" do
+ -> {
+ (Rational(-2) ** bignum_value).infinite?.should == 1
+ }.should complain(/warning: in a\*\*b, b may be too big/)
+ -> {
+ (Rational(-2) ** (bignum_value + 1)).infinite?.should == 1
+ }.should complain(/warning: in a\*\*b, b may be too big/)
+ -> {
+ (Rational(fixnum_min) ** bignum_value).infinite?.should == 1
+ }.should complain(/warning: in a\*\*b, b may be too big/)
+ end
+
+ it "returns 0.0 when self is < -1 and the exponent is negative" do
+ -> {
+ (Rational(-2) ** -bignum_value).should eql(0.0)
+ }.should complain(/warning: in a\*\*b, b may be too big/)
+ -> {
+ (Rational(fixnum_min) ** -bignum_value).should eql(0.0)
+ }.should complain(/warning: in a\*\*b, b may be too big/)
+ end
+ end
+
+ ruby_version_is "3.4" do
+ it "returns positive Infinity when self < -1" do
+ -> {
+ (Rational(-2) ** bignum_value)
+ }.should raise_error(ArgumentError)
+ -> {
+ (Rational(fixnum_min) ** bignum_value)
+ }.should raise_error(ArgumentError)
+ end
+
+ it "returns 0.0 when self is < -1 and the exponent is negative" do
+ -> {
+ (Rational(-2) ** -bignum_value)
+ }.should raise_error(ArgumentError)
+ -> {
+ (Rational(fixnum_min) ** -bignum_value)
+ }.should raise_error(ArgumentError)
+ end
+ end
+ end
end
describe "when passed Float" do