Permalink
Browse files

Merge pull request #1340 from jfirebaugh/Fixnum#exponent

Spec and fix Rational(0,1) with negative exponents
  • Loading branch information...
2 parents 018a9de + ccfd067 commit 103241b28332b1a7b57eda5baedff69f9507d612 @davorb davorb committed Oct 23, 2011
@@ -222,8 +222,14 @@ def divide (a)
#
def ** (other)
if other.kind_of?(Rational)
+ if self == 0 && other < 0 && other.denominator == 1
+ raise ZeroDivisionError, "divided by 0"
+ end
Float(self) ** other
elsif other.kind_of?(Integer)
+ if self == 0 && other < 0
+ raise ZeroDivisionError, "divided by 0"
+ end
if other > 0
num = @numerator ** other
den = @denominator ** other
@@ -13,7 +13,7 @@
it_behaves_like(:rational_exponent_float, :**)
end
- describe "Rational#** when pass Integer" do
+ describe "Rational#** when passed Integer" do
it_behaves_like(:rational_exponent_int, :**)
end
end
@@ -13,7 +13,7 @@
it_behaves_like(:rational_exponent_float, :**)
end
- describe "Rational#** when pass Integer" do
+ describe "Rational#** when passed Integer" do
it_behaves_like(:rational_exponent_int, :**)
end
end
@@ -141,4 +141,46 @@
(rational ** obj).should == :result
end
+
+ ruby_version_is ""..."1.9" do
+ it "returns Rational(1, 0) for Rational(0, 1) passed a negative Integer" do
+ [-1, -4, -9999].each do |exponent|
+ result = (Rational(0, 1) ** exponent)
+ result.numerator.should eql(1)
+ result.denominator.should eql(0)
+ end
+ end
+
+ conflicts_with :Prime do
+ it "returns Infinity for Rational(0, 1) passed a negative Rational" do
+ [Rational(-1,1), Rational(-3,1), Rational(-3,2)].each do |exponent|
+ (Rational(0, 1) ** exponent).infinite?.should == 1
+ end
+ end
+ end
+ end
+
+ ruby_version_is "1.9" do
+ it "raises ZeroDivisionError for Rational(0, 1) passed a negative Integer" do
+ [-1, -4, -9999].each do |exponent|
+ lambda { Rational(0, 1) ** exponent }.should raise_error(ZeroDivisionError, "divided by 0")
+ end
+ end
+
+ it "raises ZeroDivisionError for Rational(0, 1) passed a negative Rational with denominator 1" do
+ [Rational(-1,1), Rational(-3,1)].each do |exponent|
+ lambda { Rational(0, 1) ** exponent }.should raise_error(ZeroDivisionError, "divided by 0")
+ end
+ end
+
+ it "returns Infinity for Rational(0, 1) passed a negative Rational with denominator not 1" do
+ (Rational(0, 1) ** Rational(-3,2)).infinite?.should == 1
+ end
+ end
+
+ it "returns Infinity for Rational(0, 1) passed a negative Float" do
+ [-1.0, -3.0, -3.14].each do |exponent|
+ (Rational(0, 1) ** exponent).infinite?.should == 1
+ end
+ end
end
@@ -1,2 +1 @@
-fails:Fixnum#** raises a ZeroDivisionError for 0**-1
fails:Fixnum#** returns a complex number when negative and raised to a fractional power

0 comments on commit 103241b

Please sign in to comment.