aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/library/bigdecimal/shared/power.rb
blob: a4848fb2e2b6c4cdd02bf7ed6ea0673c1af70df3 (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
70
71
72
require 'bigdecimal'

describe :bigdecimal_power, shared: true do
  it "powers of self" do
    e3_minus = BigDecimal("3E-20001")
    e3_minus_power_2 = BigDecimal("9E-40002")
    e3_plus = BigDecimal("3E20001")
    e2_plus = BigDecimal("2E40001")
    e5_minus = BigDecimal("5E-40002")
    e = BigDecimal("1.00000000000000000000123456789")
    one = BigDecimal("1")
    ten = BigDecimal("10")
    # The tolerance is dependent upon the size of BASE_FIG
    tolerance = BigDecimal("1E-70")
    ten_powers = BigDecimal("1E10000")
    pi = BigDecimal("3.14159265358979")
    e3_minus.send(@method, 2).should == e3_minus_power_2
    e3_plus.send(@method, 0).should == 1
    e3_minus.send(@method, 1).should == e3_minus
    e2_plus.send(@method, -1).should == e5_minus
    e2_plus.send(@method, -1).should == e5_minus.power(1)
    (e2_plus.send(@method, -1) * e5_minus.send(@method, -1)).should == 1
    e.send(@method, 2).should == e * e
    e.send(@method, -1).should be_close(one.div(e, 120), tolerance)
    ten.send(@method, 10000).should == ten_powers
    pi.send(@method, 10).should be_close(Math::PI ** 10, TOLERANCE)
  end

  it "powers of 1 equal 1" do
    one = BigDecimal("1")
    one.send(@method, 0).should == 1
    one.send(@method, 1).should == 1
    one.send(@method, 10).should == 1
    one.send(@method, -10).should == 1
  end

  it "0 to power of 0 is 1" do
    zero = BigDecimal("0")
    zero.send(@method, 0).should == 1
  end

  it "0 to powers < 0 is Infinity" do
    zero = BigDecimal("0")
    infinity = BigDecimal("Infinity")
    zero.send(@method, -10).should == infinity
    zero.send(@method, -1).should == infinity
  end

  it "other powers of 0 are 0" do
    zero = BigDecimal("0")
    zero.send(@method, 1).should == 0
    zero.send(@method, 10).should == 0
  end

  it "returns NaN if self is NaN" do
    BigDecimal("NaN").send(@method, -5).nan?.should == true
    BigDecimal("NaN").send(@method, 5).nan?.should == true
  end

  it "returns 0.0 if self is infinite and argument is negative" do
    BigDecimal("Infinity").send(@method, -5).should == 0
    BigDecimal("-Infinity").send(@method, -5).should == 0
  end

  it "returns infinite if self is infinite and argument is positive" do
    infinity = BigDecimal("Infinity")
    BigDecimal("Infinity").send(@method, 4).should == infinity
    BigDecimal("-Infinity").send(@method, 4).should == infinity
    BigDecimal("Infinity").send(@method, 5).should == infinity
    BigDecimal("-Infinity").send(@method, 5).should == -infinity
  end
end