File: exponent.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (281 lines) | stat: -rw-r--r-- 12,452 bytes parent folder | download
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
require File.expand_path('../../../spec_helper', __FILE__)
require 'rational'

describe :rational_exponent, :shared => true do
  describe "when passed Rational" do
    conflicts_with :Prime do
      ruby_version_is ""..."1.9" do
        it "converts self to a Float and returns it raised to the passed argument" do
          (Rational(3, 4) ** Rational(4, 3)).should be_close(0.681420222312052, TOLERANCE)
          (Rational(3, 4) ** Rational(-4, 3)).should be_close(1.46752322173095, TOLERANCE)
          (Rational(3, 4) ** Rational(4, -3)).should be_close(1.46752322173095, TOLERANCE)

          (Rational(3, 4) ** Rational(0, 3)).should eql(1.0)
          (Rational(-3, 4) ** Rational(0, 3)).should eql(1.0)
          (Rational(3, -4) ** Rational(0, 3)).should eql(1.0)
          (Rational(3, 4) ** Rational(0, -3)).should eql(1.0)

          (Rational(bignum_value, 4) ** Rational(0, 3)).should eql(1.0)
          (Rational(3, -bignum_value) ** Rational(0, 3)).should eql(1.0)
          (Rational(3, 4) ** Rational(0, bignum_value)).should eql(1.0)
          (Rational(3, 4) ** Rational(0, -bignum_value)).should eql(1.0)
        end
      end

      ruby_version_is "1.9" do
        it "returns Rational(1) if the exponent is Rational(0)" do
          (Rational(0) ** Rational(0)).should eql(Rational(1))
          (Rational(1) ** Rational(0)).should eql(Rational(1))
          (Rational(3, 4) ** Rational(0)).should eql(Rational(1))
          (Rational(-1) ** Rational(0)).should eql(Rational(1))
          (Rational(-3, 4) ** Rational(0)).should eql(Rational(1))
          (Rational(bignum_value) ** Rational(0)).should eql(Rational(1))
          (Rational(-bignum_value) ** Rational(0)).should eql(Rational(1))
        end

        it "returns self raised to the argument as a Rational if the exponent's denominator is 1" do
          (Rational(3, 4) ** Rational(1, 1)).should eql(Rational(3, 4))
          (Rational(3, 4) ** Rational(2, 1)).should eql(Rational(9, 16))
          (Rational(3, 4) ** Rational(-1, 1)).should eql(Rational(4, 3))
          (Rational(3, 4) ** Rational(-2, 1)).should eql(Rational(16, 9))
        end

        it "returns self raised to the argument as a Float if the exponent's denominator is not 1" do
          (Rational(3, 4) ** Rational(4, 3)).should be_close(0.681420222312052, TOLERANCE)
          (Rational(3, 4) ** Rational(-4, 3)).should be_close(1.46752322173095, TOLERANCE)
          (Rational(3, 4) ** Rational(4, -3)).should be_close(1.46752322173095, TOLERANCE)
        end

        it "returns a complex number when self is negative and the passed argument is not 0" do
          (Rational(-3, 4) ** Rational(-4, 3)).should == Complex(
            -0.7337616108654732, 1.2709123906625817)
        end
      end

      ruby_version_is ""..."1.9" do
        it "returns NaN when self is negative and the passed argument is not 0" do
          (Rational(-3, 4) ** Rational(-4, 3)).nan?.should be_true
        end
      end
    end
  end

  describe "when passed Integer" do
    it "returns the Rational value of self raised to the passed argument" do
      (Rational(3, 4) ** 4).should == Rational(81, 256)
      (Rational(3, 4) ** -4).should == Rational(256, 81)
      (Rational(-3, 4) ** -4).should == Rational(256, 81)
      (Rational(3, -4) ** -4).should == Rational(256, 81)

      (Rational(bignum_value, 4) ** 4).should == Rational(28269553036454149273332760011886696253239742350009903329945699220681916416, 1)
      (Rational(3, bignum_value) ** -4).should == Rational(7237005577332262213973186563042994240829374041602535252466099000494570602496, 81)
      (Rational(-bignum_value, 4) ** -4).should == Rational(1, 28269553036454149273332760011886696253239742350009903329945699220681916416)
      (Rational(3, -bignum_value) ** -4).should == Rational(7237005577332262213973186563042994240829374041602535252466099000494570602496, 81)
    end

    conflicts_with :Prime do
      it "returns Rational(1, 1) when the passed argument is 0" do
        (Rational(3, 4) ** 0).should eql(Rational(1, 1))
        (Rational(-3, 4) ** 0).should eql(Rational(1, 1))
        (Rational(3, -4) ** 0).should eql(Rational(1, 1))

        (Rational(bignum_value, 4) ** 0).should eql(Rational(1, 1))
        (Rational(3, -bignum_value) ** 0).should eql(Rational(1, 1))
      end
    end
  end

  describe "when passed Bignum" do
    ruby_version_is ""..."1.9" do
      it "returns Rational(0) when self is Rational(0) and the exponent is positive" do
        (Rational(0) ** bignum_value).should eql(Rational(0))
      end

      it "returns Rational(1, 0) when self is Rational(0) and the exponent is negative" do
        result = (Rational(0) ** -bignum_value)
        result.numerator.should eql(1)
        result.denominator.should eql(0)
      end

      it "returns Rational(1) when self is Rational(1)" do
        (Rational(1) ** bignum_value).should eql(Rational(1))
      end

      it "returns Rational(1) when self is Rational(-1) and the exponent is even" do
        (Rational(-1) ** bignum_value(0)).should eql(Rational(1))
      end

      it "returns Rational(-1) when self is Rational(-1) and the exponent is odd" do
        (Rational(-1) ** bignum_value(1)).should eql(Rational(-1))
      end

      it "raises FloatDomainError when self is > 1 or < -1" do
        lambda { Rational(2) ** bignum_value           }.should raise_error(FloatDomainError)
        lambda { Rational(-2) ** bignum_value          }.should raise_error(FloatDomainError)
        lambda { Rational(fixnum_max) ** bignum_value  }.should raise_error(FloatDomainError)
        lambda { Rational(fixnum_min) ** bignum_value  }.should raise_error(FloatDomainError)

        lambda { Rational(2) ** -bignum_value          }.should raise_error(FloatDomainError)
        lambda { Rational(-2) ** -bignum_value         }.should raise_error(FloatDomainError)
        lambda { Rational(fixnum_max) ** -bignum_value }.should raise_error(FloatDomainError)
        lambda { Rational(fixnum_min) ** -bignum_value }.should raise_error(FloatDomainError)
      end
    end

    ruby_version_is "1.9" do
      ruby_bug "#5713", "2.0" do
        it "returns Rational(0) when self is Rational(0) and the exponent is positive" do
          (Rational(0) ** bignum_value).should eql(Rational(0))
        end

        it "raises ZeroDivisionError when self is Rational(0) and the exponent is negative" do
          lambda { Rational(0) ** -bignum_value }.should raise_error(ZeroDivisionError)
        end

        it "returns Rational(1) when self is Rational(1)" do
          (Rational(1) **  bignum_value).should eql(Rational(1))
          (Rational(1) ** -bignum_value).should eql(Rational(1))
        end

        it "returns Rational(1) when self is Rational(-1) and the exponent is positive and even" do
          (Rational(-1) ** bignum_value(0)).should eql(Rational(1))
          (Rational(-1) ** bignum_value(2)).should eql(Rational(1))
        end

        it "returns Rational(-1) when self is Rational(-1) and the exponent is positive and odd" do
          (Rational(-1) ** bignum_value(1)).should eql(Rational(-1))
          (Rational(-1) ** bignum_value(3)).should eql(Rational(-1))
        end
      end

      it "returns positive Infinity when self is > 1" do
        (Rational(2) ** bignum_value).infinite?.should == 1
        (Rational(fixnum_max) ** bignum_value).infinite?.should == 1
      end

      it "returns 0.0 when self is > 1 and the exponent is negative" do
        (Rational(2) ** -bignum_value).should eql(0.0)
        (Rational(fixnum_max) ** -bignum_value).should eql(0.0)
      end

      # Fails on linux due to pow() bugs in glibc: http://sources.redhat.com/bugzilla/show_bug.cgi?id=3866
      platform_is_not :linux do
        it "returns positive Infinity when self < -1" do
          (Rational(-2) ** bignum_value).infinite?.should == 1
          (Rational(-2) ** (bignum_value + 1)).infinite?.should == 1
          (Rational(fixnum_min) ** bignum_value).infinite?.should == 1
        end

        it "returns 0.0 when self is < -1 and the exponent is negative" do
          (Rational(-2) ** -bignum_value).should eql(0.0)
          (Rational(fixnum_min) ** -bignum_value).should eql(0.0)
        end
      end
    end
  end

  describe "when passed Float" do
    it "returns self converted to Float and raised to the passed argument" do
      (Rational(3, 1) ** 3.0).should eql(27.0)
      (Rational(3, 1) ** 1.5).should be_close(5.19615242270663, TOLERANCE)
      (Rational(3, 1) ** -1.5).should be_close(0.192450089729875, TOLERANCE)
    end

    ruby_version_is ""..."1.9" do
      it "returns NaN if self is negative and the passed argument is not 0" do
        (Rational(-3, 2) ** 1.5).nan?.should be_true
        (Rational(3, -2) ** 1.5).nan?.should be_true
        (Rational(3, -2) ** -1.5).nan?.should be_true
      end

      it "returns 1.0 when the passed argument is 0.0" do
        (Rational(3, 4) ** 0.0).should eql(1.0)
        (Rational(-3, 4) ** 0.0).should eql(1.0)
        (Rational(-3, 4) ** 0.0).should eql(1.0)
      end
    end

    ruby_version_is "1.9" do
      it "returns a complex number if self is negative and the passed argument is not 0" do
        (Rational(-3, 2) ** 1.5).should be_close(                                                Complex(
                                                                                                   -3.374618290464398e-16, -1.8371173070873836), TOLERANCE)
        (Rational(3, -2) ** 1.5).should be_close(                                                Complex(
                                                                                                   -3.374618290464398e-16, -1.8371173070873836), TOLERANCE)
        (Rational(3, -2) ** -1.5).should be_close(                                               Complex(
                                                                                                   -9.998869008783402e-17, 0.5443310539518174), TOLERANCE)
      end

      it "returns Complex(1.0) when the passed argument is 0.0" do
        (Rational(3, 4) ** 0.0).should == Complex(1.0)
        (Rational(-3, 4) ** 0.0).should == Complex(1.0)
        (Rational(-3, 4) ** 0.0).should == Complex(1.0)
      end
    end
  end

  it "calls #coerce on the passed argument with self" do
    rational = Rational(3, 4)
    obj      = mock("Object")
    obj.should_receive(:coerce).with(rational).and_return([1, 2])

    rational ** obj
  end

  it "calls #** on the coerced Rational with the coerced Object" do
    rational = Rational(3, 4)

    coerced_rational = mock("Coerced Rational")
    coerced_rational.should_receive(:**).and_return(:result)

    coerced_obj = mock("Coerced Object")

    obj = mock("Object")
    obj.should_receive(:coerce).and_return([coerced_rational, coerced_obj])

    (rational ** obj).should == :result
  end

  ruby_version_is ""..."1.9" do
    it "returns Rational(1, 0) for Rational(0, 1) passed a negative Integer" do
      [-1, -4, -9999].each do |exponent|
        result = (Rational(0, 1) ** exponent)
        result.numerator.should eql(1)
        result.denominator.should eql(0)
      end
    end

    conflicts_with :Prime do
      it "returns Infinity for Rational(0, 1) passed a negative Rational" do
        [Rational(-1, 1), Rational(-3, 1), Rational(-3, 2)].each do |exponent|
          (Rational(0, 1) ** exponent).infinite?.should == 1
        end
      end
    end
  end

  ruby_version_is "1.9" do
    it "raises ZeroDivisionError for Rational(0, 1) passed a negative Integer" do
      [-1, -4, -9999].each do |exponent|
        lambda { Rational(0, 1) ** exponent }.should raise_error(ZeroDivisionError, "divided by 0")
      end
    end

    it "raises ZeroDivisionError for Rational(0, 1) passed a negative Rational with denominator 1" do
      [Rational(-1, 1), Rational(-3, 1)].each do |exponent|
        lambda { Rational(0, 1) ** exponent }.should raise_error(ZeroDivisionError, "divided by 0")
      end
    end

    ruby_bug "#7513", "2.0.0" do
      it "raises ZeroDivisionError for Rational(0, 1) passed a negative Rational" do
        lambda { Rational(0, 1) ** Rational(-3, 2) }.should raise_error(ZeroDivisionError, "divided by 0")
      end
    end
  end

  it "returns Infinity for Rational(0, 1) passed a negative Float" do
    [-1.0, -3.0, -3.14].each do |exponent|
      (Rational(0, 1) ** exponent).infinite?.should == 1
    end
  end
end