Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support BigFloat#** for all Int::Primitive arguments (crystal-lan…
Browse files Browse the repository at this point in the history
HertzDevil authored and Blacksmoke16 committed Dec 11, 2023

Verified

This commit was signed with the committer’s verified signature.
agrare Adam Grare
1 parent 5c24129 commit 4c1f839
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions spec/std/big/big_float_spec.cr
Original file line number Diff line number Diff line change
@@ -186,6 +186,9 @@ describe "BigFloat" do
it { ("-0.05".to_big_f ** 10.to_big_i).should be_close("0.00000000000009765625".to_big_f, 1e-12) }
it { ("0.1234567890".to_big_f ** 3.to_big_i).should be_close("0.001881676371789154860897069".to_big_f, 1e-12) }

it { ("10".to_big_f ** -5).should be_close("0.00001".to_big_f, 1e-12) }
it { ("0.1".to_big_f ** -5).should be_close("100000".to_big_f, 1e-12) }

it { ("10".to_big_f ** (-5).to_big_i).should be_close("0.00001".to_big_f, 1e-12) }
it { ("0.1".to_big_f ** (-5).to_big_i).should be_close("100000".to_big_f, 1e-12) }
it { ("0".to_big_f ** 1.to_big_i).should eq(0.to_big_f) }
13 changes: 12 additions & 1 deletion src/big/big_float.cr
Original file line number Diff line number Diff line change
@@ -171,7 +171,18 @@ struct BigFloat < Float
end

def **(other : Int) : BigFloat
BigFloat.new { |mpf| LibGMP.mpf_pow_ui(mpf, self, other.to_u64) }
# there is no BigFloat::Infinity
if zero? && other < 0
raise ArgumentError.new "Cannot raise 0 to a negative power"
end

Int.primitive_ui_check(other) do |ui, neg_ui, big_i|
{
ui: BigFloat.new { |mpf| LibGMP.mpf_pow_ui(mpf, self, {{ ui }}) },
neg_ui: BigFloat.new { |mpf| LibGMP.mpf_pow_ui(mpf, self, {{ neg_ui }}); LibGMP.mpf_ui_div(mpf, 1, mpf) },
big_i: self ** {{ big_i }},
}
end
end

def abs : BigFloat

0 comments on commit 4c1f839

Please sign in to comment.