Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement pow operator for BigDecimal and BigRational #7860

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions spec/std/big/big_decimal_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ describe BigDecimal do
(3 / 2.to_big_d).should eq(BigDecimal.new("1.5"))
end

it "exponentiates" do
result = "12.34".to_big_d ** 5
result.should be_a(BigDecimal)
result.to_s.should eq("286138.1721051424")
end

it "can be converted from other types" do
1.to_big_d.should eq (BigDecimal.new(1))
"1.5".to_big_d.should eq (BigDecimal.new(15, 1))
Expand Down
17 changes: 17 additions & 0 deletions spec/std/big/big_rational_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,23 @@ describe BigRational do
(br(10, 3) >> 2).should eq(br(5, 6))
end

describe "#**" do
it "exponentiates with positive powers" do
result = br(17, 11) ** 5
result.should be_a(BigRational)
result.should eq(br(1419857, 161051))
end

it "exponentiates with negative powers" do
result = br(17, 11) ** -5
result.should eq(br(161051, 1419857))
end

it "cannot raise 0 to a negative power" do
expect_raises(DivisionByZeroError) { br(0, 1) ** -1 }
end
end

it "#ceil" do
br(2, 1).ceil.should eq(2)
br(21, 10).ceil.should eq(3)
Expand Down
14 changes: 14 additions & 0 deletions src/big/big_decimal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,20 @@ struct BigDecimal < Number
end
end

# Raises the decimal to the *other*th power
#
# ```
# require "big"
#
# BigDecimal.new(1234, 2) ** 2 # => 152.2756
# ```
def **(other : Int) : BigDecimal
if other < 0
raise ArgumentError.new("Negative exponent isn't supported")
end
BigDecimal.new(@value ** other, @scale * other)
end

def ceil : BigDecimal
mask = power_ten_to(@scale)
diff = (mask - @value % mask) % mask
Expand Down
17 changes: 17 additions & 0 deletions src/big/big_rational.cr
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,23 @@ struct BigRational < Number
BigRational.new { |mpq| LibGMP.mpq_neg(mpq, self) }
end

# Raises the rational to the *other*th power
#
# This will raise `DivisionByZeroError` if rational is 0 and *other* is negative.
#
# ```
# require "big"
#
# BigRational.new(2, 3) ** 2 # => 4/9
# BigRational.new(2, 3) ** -1 # => 3/2
# ```
def **(other : Int) : BigRational
if other < 0
return (self ** -other).inv
end
BigRational.new(numerator ** other, denominator ** other)
end

# Returns a new `BigRational` as 1/r.
#
# This will raise an exception if rational is 0.
Expand Down