File: power.rb

package info (click to toggle)
ruby3.3 3.3.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,620 kB
  • sloc: ruby: 1,244,308; ansic: 836,474; yacc: 28,074; pascal: 6,748; sh: 3,913; python: 1,719; cpp: 1,158; makefile: 742; asm: 712; javascript: 394; lisp: 97; perl: 62; awk: 36; sed: 23; xml: 4
file content (72 lines) | stat: -rw-r--r-- 2,501 bytes parent folder | download | duplicates (6)
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).should.nan?
    BigDecimal("NaN").send(@method, 5).should.nan?
  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