Skip to content

Commit

Permalink
add negative exponential support to BigDecimal (#10892)
Browse files Browse the repository at this point in the history
Co-authored-by: Johannes Müller <[email protected]>
  • Loading branch information
stakach and straight-shoota authored Jul 24, 2021
1 parent e97096a commit 1db5d45
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
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 @@ -212,6 +212,12 @@ describe BigDecimal do
result.to_s.should eq("286138.1721051424")
end

it "exponentiates with negative powers" do
result = "2.0".to_big_d ** -1
result.should be_a(BigDecimal)
result.to_s.should eq("0.5")
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
4 changes: 1 addition & 3 deletions src/big/big_decimal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,7 @@ struct BigDecimal < Number
# BigDecimal.new(1234, 2) ** 2 # => 152.2756
# ```
def **(other : Int) : BigDecimal
if other < 0
raise ArgumentError.new("Negative exponent isn't supported")
end
return (to_big_r ** other).to_big_d if other < 0
BigDecimal.new(@value ** other, @scale * other)
end

Expand Down

0 comments on commit 1db5d45

Please sign in to comment.