summaryrefslogtreecommitdiff
path: root/spec/ruby/core/math/gamma_spec.rb
blob: 386162a087518d03873b6be66cd73cbfa22db775 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require_relative '../../spec_helper'

describe "Math.gamma" do
  it "returns +infinity given 0" do
    Math.gamma(0).should == Float::INFINITY
  end

  platform_is_not :windows do
    # https://bugs.ruby-lang.org/issues/12249
    it "returns -infinity given -0.0" do
      Math.gamma(-0.0).should == -Float::INFINITY
    end
  end

  it "returns Math.sqrt(Math::PI) given 0.5" do
    Math.gamma(0.5).should be_close(Math.sqrt(Math::PI), TOLERANCE)
  end

  # stop at n == 23 because 23! cannot be exactly represented by IEEE 754 double
  it "returns exactly (n-1)! given n for n between 2 and 23" do
    fact = 1
    2.upto(23) do |n|
      fact *= (n - 1)
      Math.gamma(n).should == fact
    end
  end

  it "returns approximately (n-1)! given n for n between 24 and 30" do
    fact2 = 1124000727777607680000  # 22!
    24.upto(30) do |n|
      fact2 *= n - 1
      # compare only the first 12 places, tolerate the rest
      Math.gamma(n).should be_close(fact2, fact2.to_s[12..-1].to_i)
    end
  end

  it "returns good numerical approximation for gamma(3.2)" do
    Math.gamma(3.2).should be_close(2.423965, TOLERANCE)
  end

  it "returns good numerical approximation for gamma(-2.15)" do
    Math.gamma(-2.15).should be_close(-2.999619, TOLERANCE)
  end

  it "returns good numerical approximation for gamma(0.00001)" do
    Math.gamma(0.00001).should be_close(99999.422794, TOLERANCE)
  end

  it "returns good numerical approximation for gamma(-0.00001)" do
    Math.gamma(-0.00001).should be_close(-100000.577225, TOLERANCE)
  end

  it "raises Math::DomainError given -1" do
    -> { Math.gamma(-1) }.should raise_error(Math::DomainError)
  end

  # See https://bugs.ruby-lang.org/issues/10642
  it "returns +infinity given +infinity" do
    Math.gamma(infinity_value).infinite?.should == 1
  end

  it "raises Math::DomainError given negative infinity" do
    -> { Math.gamma(-Float::INFINITY) }.should raise_error(Math::DomainError)
  end

  it "returns NaN given NaN" do
    Math.gamma(nan_value).nan?.should be_true
  end
end