Browse Source

Improve BigNumber handle (BigFloat/BigInt patch)

pull/2/head
Arthur POULET 5 years ago
parent
commit
af4fc3d1b3
No known key found for this signature in database GPG Key ID: 7C42395DCA99B923
  1. 5
      CHANGELOG.md
  2. 5
      spec/binomial_distribution.cr
  3. 7
      spec/math/coef_binomial.cr
  4. 14
      spec/normale_distribution.cr
  5. 31
      src/stats/math/big_number.cr
  6. 5
      src/stats/math/numeric_value.cr

5
CHANGELOG.md

@ -1,3 +1,8 @@
# v0.1.6
- improve factorial (handle BigInt)
- improve distributions (handle BigFloat and BigInt)
- more tests
# v0.1.5
- add specs for every function
- improve file architecture

5
spec/binomial_distribution.cr

@ -38,4 +38,9 @@ describe BinomialDistribution do
b.distribute([2, 1]).should eq 0.75
end
it "with BitNumber" do
BinomialDistribution.new(
BigInt.new(1), BigFloat.new(1)).should be_a(BinomialDistribution)
end
end

7
spec/math/coef_binomial.cr

@ -19,4 +19,11 @@ describe Math do
20.times {|i| Math.coef_binomial(i + 1, i + 1).should eq 1}
end
it "coef_binomial with BigInt" do
Math.coef_binomial(BigInt.new(5), BigInt.new(2)).should eq 10
Math.coef_binomial(
BigInt.new("fffffffffffffffffffffffffffffffe", 16),
BigInt.new("ffffffffffffffffffffffffffffffff", 16)).should eq 0
end
end

14
spec/normale_distribution.cr

@ -1,9 +1,17 @@
describe NormaleDistribution do
it "simple instances" do
it "instances" do
NormaleDistribution::Persistant.new
NormaleDistribution::Persistant.new(standard_deviation: 1).standard_deviation.should eq(1)
NormaleDistribution::Persistant.new(esperance: 1).esperance.should eq(1)
NormaleDistribution::Persistant.new(standard_deviation: 12).standard_deviation.should eq(12)
NormaleDistribution::Persistant.new(esperance: 14).esperance.should eq(14)
end
it "instances with BigNumber" do
n = NormaleDistribution::Persistant.new(
standard_deviation: BigInt.new(1),
esperance: BigFloat.new(1))
n.standard_deviation.should eq(1)
n.esperance.should eq(1)
end
it "QI" do

31
src/stats/math/big_number.cr

@ -0,0 +1,31 @@
require "big_int"
require "big_float"
{% for klass in [Float32, Float64, Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64] %}
struct {{klass}}
{% for op in ["+", "-", "*", "%", "/"] %}
{% for other_klass in [BigFloat, BigInt] %}
def {{op.id}}(other : {{other_klass}})
{{other_klass}}.new(self) {{op.id}} other
end
{% end %}
{% end %}
def unsafe_div(other : BigInt)
BigInt.new(self) / other
end
end
{% end %}
struct BigInt
{% for op in ["+", "-", "*", "%", "/"] %}
def {{op.id}}(other : Float::Primitive)
self {{op.id}} BigFloat.new(other)
end
{% end %}
def /(other : BigFloat)
self / other
end
end

5
src/stats/math/numeric_value.cr

@ -1,9 +1,10 @@
abstract struct Int
alias All = Int::Primitive #| BigInt
alias All = Int::Primitive | BigInt
end
require "big_float"
abstract struct Float
alias All = Float::Primitive #| BigFloat | BigRational
alias All = Float::Primitive | BigFloat #| BigRational
end
abstract struct Number

Loading…
Cancel
Save