File: exponent_spec.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 (71 lines) | stat: -rw-r--r-- 3,436 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
require File.expand_path('../../../../spec_helper', __FILE__)
require 'mathn'

ruby_version_is ''...'1.9' do
  describe "Rational#** when passed [Rational]" do
    it "returns Rational.new!(1, 1) when the passed argument is 0" do
      (Rational.new!(3, 4) ** Rational.new!(0, 3)).should == Rational.new!(1,1)
      (Rational.new!(-3, 4) ** Rational.new!(0, 3)).should == Rational.new!(1,1)
      (Rational.new!(3, -4) ** Rational.new!(0, 3)).should == Rational.new!(1,1)
      (Rational.new!(3, 4) ** Rational.new!(0, -3)).should == Rational.new!(1,1)
    end

    it "returns Rational.new!(1, 1) when self is 1" do
      (Rational.new!(1,1) ** Rational.new!(2, 3)).should == Rational.new!(1,1)
      (Rational.new!(1,1) ** Rational.new!(-2, 3)).should == Rational.new!(1,1)
      (Rational.new!(1,1) ** Rational.new!(2, -3)).should == Rational.new!(1,1)
      (Rational.new!(1,1) ** Rational.new!(-2, -3)).should == Rational.new!(1,1)
    end

    it "returns Rational.new!(0, 1) when self is 0" do
      (Rational.new!(0,1) ** Rational.new!(2, 3)).should == Rational.new!(0,1)
      (Rational.new!(0,1) ** Rational.new!(-2, 3)).should == Rational.new!(0,1)
      (Rational.new!(0,1) ** Rational.new!(2, -3)).should == Rational.new!(0,1)
      (Rational.new!(0,1) ** Rational.new!(-2, -3)).should == Rational.new!(0,1)
    end

    it "returns a Complex number when self is negative" do
      (Rational.new!(-1,2) ** Rational.new!(2, 3)).should be_close(Complex(-0.314980262473718, 0.545561817985861), TOLERANCE)
      (Rational.new!(-1,2) ** Rational.new!(-2, 3)).should be_close(Complex(-0.793700525984099, -1.3747296369986), TOLERANCE)
      (Rational.new!(-1,2) ** Rational.new!(2, -3)).should be_close(Complex(-0.793700525984099, -1.3747296369986), TOLERANCE)
    end
  end

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

    it "returns Rational.new!(1, 1) when the passed argument is 0" do
      (Rational.new!(3, 4) ** 0).should == Rational.new!(1, 1)
      (Rational.new!(-3, 4) ** 0).should == Rational.new!(1, 1)
      (Rational.new!(3, -4) ** 0).should == Rational.new!(1, 1)

      (Rational.new!(bignum_value, 4) ** 0).should == Rational.new!(1, 1)
      (Rational.new!(3, -bignum_value) ** 0).should == Rational.new!(1, 1)
    end
  end

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

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

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