continued_fractions 1.2.2 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,19 @@
1
+ === 1.3.0 / 2011-06-10
2
+
3
+ * Added binary operators: + - * / between two ContinuedFractions and a ContinuedFraction and a number (on the right-hand-side). Returns a ContinuedFraction.
4
+ * Added tests for operators.
5
+
6
+ === 1.2.3 / 2011-06-09
7
+
8
+ * Fixed emplementation of #convergent to match its definition.
9
+ * Added exception on #convergent to be raised when index is less than 1.
10
+ * Added mention of ContinuedFractions#convergents_as_rationals in README
11
+ * Changed README example. Variable real changed to pi.
12
+
1
13
  === 1.2.2 / 2010-03-27
2
14
 
3
15
  * Moved repo to Git.
4
16
 
5
-
6
17
  === 1.2.1 / 2010-03-18
7
18
 
8
19
  * Updated README file to display change in return class of ContinuedFractions#convergents.
data/README.txt CHANGED
@@ -16,10 +16,10 @@ Generates quotients and convergents for a given number.
16
16
  require 'continued_fractions'
17
17
  include ContinuedFractions
18
18
 
19
- real = Math::PI
19
+ pi = Math::PI
20
20
  => 3.14159265358979
21
21
 
22
- cf = ContinuedFraction.new(real,10)
22
+ cf = ContinuedFraction.new(pi,10)
23
23
  => #<ContinuedFractions::ContinuedFraction:0x00000101101368 @number=3.14159265358979, @limit=10,
24
24
  @quotients=[3, 7, 15, 1, 292, 1, 1, 1, 2, 1],
25
25
  @convergents=[[0, 1], [1, 0], [3, 1], [22, 7], [333, 106],
@@ -27,12 +27,23 @@ Generates quotients and convergents for a given number.
27
27
 
28
28
  cf.convergents
29
29
  => [[3,1], [22,7], [333,106], [355,113], [103993,33102], [104348,33215], [208341,66317], [312689,99532], [833719,265381], [1146408,364913]]
30
+
31
+ # Convergents can be output as an array of Rationals too
32
+ cf.convergents_as_rationals
33
+ [(3/1), (22/7), (333/106), (355/113), (103993/33102), (104348/33215), (208341/66317), (312689/99532), (833719/265381), (1146408/364913)]
30
34
 
31
35
  cf.convergent(1)
32
36
  => [22,7]
33
37
 
34
38
  cf.quotients
35
39
  => [3, 7, 15, 1, 292, 1, 1, 1, 2, 1]
40
+
41
+ # Add, subtract, multiply and divide continued fractions. The limit of the result is the max limit of the operands.
42
+ cf2 = ContinuedFraction.new(rand,20)
43
+ => #<ContinuedFractions::ContinuedFraction:0x000001009dde50 @number=0.03834175394982653, @limit=20, @quotients=[0, 26, 12, 3, 4, 1, 2, 24, 5, 1, 1, 1, 2, 1, 2, 2, 1, 7, 1, 5], @convergents=[[0, 1], [1, 26], [12, 313], [37, 965], [160, 4173], [197, 5138], [554, 14449], [13493, 351914], [68019, 1774019], [81512, 2125933], [149531, 3899952], [231043, 6025885], [611617, 15951722], [842660, 21977607], [2296937, 59906936], [5436534, 141791479], [7733471, 201698415], [59570831, 1553680384], [67304302, 1755378799], [396092341, 10330574379]]>
44
+ cf + cf2
45
+ => #<ContinuedFractions::ContinuedFraction:0x000001009d2708 @number=3.1799344075396196, @limit=20, @quotients=[3, 5, 1, 1, 3, 1, 5, 3, 7, 25, 1, 2, 15, 6, 1, 1, 2, 3, 1, 2], @convergents=[[3, 1], [16, 5], [19, 6], [35, 11], [124, 39], [159, 50], [919, 289], [2916, 917], [21331, 6708], [536191, 168617], [557522, 175325], [1651235, 519267], [25326047, 7964330], [153607517, 48305247], [178933564, 56269577], [332541081, 104574824], [844015726, 265419225], [2864588259, 900832499], [3708603985, 1166251724], [10281796229, 3233335947]]>
46
+
36
47
 
37
48
  == Requirements:
38
49
 
data/Rakefile CHANGED
@@ -2,8 +2,9 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('continued_fractions', '1.2.2') do |config|
5
+ Echoe.new('continued_fractions', '1.3.1') do |config|
6
6
  config.summary = 'Generate continued fractions.'
7
+ config.description = 'Class for working with continued fractions.'
7
8
  config.author = 'Jose Hales-Garcia'
8
9
  config.url = 'http://github.com/jolohaga/continued_fractions'
9
10
  config.email = 'jolohaga@me.com'
@@ -11,4 +12,4 @@ Echoe.new('continued_fractions', '1.2.2') do |config|
11
12
  config.development_dependencies = ['rspec >=1.3.0','echoe >=4.3']
12
13
  end
13
14
 
14
- Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each{|ext| load ext}
15
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each{|ext| load ext}
@@ -2,12 +2,12 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{continued_fractions}
5
- s.version = "1.2.2"
5
+ s.version = "1.3.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Jose Hales-Garcia"]
9
- s.date = %q{2010-03-27}
10
- s.description = %q{Generate continued fractions.}
9
+ s.date = %q{2011-06-10}
10
+ s.description = %q{Class for working with continued fractions.}
11
11
  s.email = %q{jolohaga@me.com}
12
12
  s.extra_rdoc_files = ["README.txt", "lib/continued_fractions.rb"]
13
13
  s.files = ["History.txt", "Manifest", "README.txt", "Rakefile", "lib/continued_fractions.rb", "spec/continued_fractions/continued_fraction_spec.rb", "spec/spec_helper.rb", "continued_fractions.gemspec"]
@@ -15,14 +15,14 @@ Gem::Specification.new do |s|
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Continued_fractions", "--main", "README.txt"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubyforge_project = %q{continued_fractions}
18
- s.rubygems_version = %q{1.3.6}
18
+ s.rubygems_version = %q{1.3.7}
19
19
  s.summary = %q{Generate continued fractions.}
20
20
 
21
21
  if s.respond_to? :specification_version then
22
22
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
23
  s.specification_version = 3
24
24
 
25
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
25
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
26
  s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
27
27
  s.add_development_dependency(%q<echoe>, [">= 4.3"])
28
28
  else
@@ -39,7 +39,8 @@ module ContinuedFractions
39
39
  # => [355,113]
40
40
  #
41
41
  def convergent(nth)
42
- convergents[nth]
42
+ raise(IndexError, "Convergent index must be greater than zero.") unless nth > 0
43
+ convergents[nth-1]
43
44
  end
44
45
 
45
46
  # Return array of convergents of the continued fraction instance up to the nth convergent as an array
@@ -76,14 +77,56 @@ module ContinuedFractions
76
77
  def convergents_as_rationals(nth=nil)
77
78
  ContinuedFractions.convergents_array_to_rationals(convergents,nth)
78
79
  end
80
+
81
+ def +(other)
82
+ _number_of(other) do |num,prec|
83
+ _evaluate("#{@number} + #{num}",prec)
84
+ end
85
+ end
86
+
87
+ def -(other)
88
+ _number_of(other) do |num,prec|
89
+ _evaluate("#{@number} - #{num}",prec)
90
+ end
91
+ end
92
+
93
+ def /(other)
94
+ _number_of(other) do |num,prec|
95
+ _evaluate("#{@number} / #{num}",prec)
96
+ end
97
+ end
98
+
99
+ def *(other)
100
+ _number_of(other) do |num,prec|
101
+ _evaluate("#{@number} * #{num}",prec)
102
+ end
103
+ end
79
104
 
80
105
  private
81
- def _convergents(nth=nil)
106
+ def _evaluate(e,p) #:nodoc:
107
+ ContinuedFraction.new(eval(e), p)
108
+ end
109
+
110
+ def _number_of(n) #:nodoc:
111
+ num = nil
112
+ prec = nil
113
+ case n.class.name
114
+ when "Fixnum","Integer"
115
+ num = n
116
+ prec = @limit
117
+ when "ContinuedFractions::ContinuedFraction"
118
+ num = n.number
119
+ prec = [n.limit,@limit].max
120
+ end
121
+ yield(num,prec)
122
+ end
123
+
124
+ def _convergents(nth=nil) #:nodoc:
82
125
  nth ||= @convergents.length
83
126
  @convergents[0...nth]
84
127
  end
85
128
 
86
- def calculate_quotients
129
+ def calculate_quotients #:nodoc:
87
130
  qs = Array.new(@limit)
88
131
  n = @number
89
132
  @limit.times do |i|
@@ -93,7 +136,7 @@ module ContinuedFractions
93
136
  qs
94
137
  end
95
138
 
96
- def calculate_convergents
139
+ def calculate_convergents #:nodoc:
97
140
  ContinuedFractions.convergents(quotients)
98
141
  end
99
142
  end
@@ -31,9 +31,51 @@ module ContinuedFractions
31
31
 
32
32
  it "should contain convergents which are expressed in lowest terms" do
33
33
  1.upto(@cf.convergents.length-1) do |i|
34
- convergent_rational_i_minus1, convergent_rational_i = ContinuedFractions.convergent_to_rational(@cf.convergent(i-1)), ContinuedFractions.convergent_to_rational(@cf.convergent(i))
35
- (convergent_rational_i_minus1.numerator*convergent_rational_i.denominator - convergent_rational_i.numerator*convergent_rational_i_minus1.denominator).should == (-1)**(i)
34
+ convergent_rational_i, convergent_rational_i_plus1 = ContinuedFractions.convergent_to_rational(@cf.convergent(i)), ContinuedFractions.convergent_to_rational(@cf.convergent(i+1))
35
+ (convergent_rational_i.numerator*convergent_rational_i_plus1.denominator - convergent_rational_i_plus1.numerator*convergent_rational_i.denominator).should == (-1)**(i)
36
36
  end
37
37
  end
38
+
39
+ it "should add a number (on the right-hand-side) with a continued fraction and return a continued fraction" do
40
+ (@cf + 3).class.should == ContinuedFractions::ContinuedFraction
41
+ end
42
+
43
+ it "should subtract a number (on the right-hand-side) from a continued fraction and return a continued fraction" do
44
+ (@cf - 3).class.should == ContinuedFractions::ContinuedFraction
45
+ end
46
+
47
+ it "should multiply a number (on the right-hand-side) with a continued fraction and return a continued fraction" do
48
+ (@cf * 3).class.should == ContinuedFractions::ContinuedFraction
49
+ end
50
+
51
+ it "should divide a number (on the right-hand-side) with a continued fraction and return a continued fraction" do
52
+ (@cf / 3).class.should == ContinuedFractions::ContinuedFraction
53
+ end
54
+
55
+ it "should add a continued fraction with another continued fraction and return a continued fraction" do
56
+ c = ContinuedFraction.new(rand,20)
57
+ (@cf + c).class.should == ContinuedFractions::ContinuedFraction
58
+ end
59
+
60
+ it "should subtract a continued fraction with another continued fraction and return a continued fraction" do
61
+ c = ContinuedFraction.new(rand,20)
62
+ (@cf - c).class.should == ContinuedFractions::ContinuedFraction
63
+ end
64
+
65
+ it "should multiply a continued fraction with another continued fraction and return a continued fraction" do
66
+ c = ContinuedFraction.new(rand,20)
67
+ (@cf * c).class.should == ContinuedFractions::ContinuedFraction
68
+ end
69
+
70
+ it "should divide a continued fraction with another continued fraction and return a continued fraction" do
71
+ c = ContinuedFraction.new(rand,20)
72
+ (@cf / c).class.should == ContinuedFractions::ContinuedFraction
73
+ end
74
+
75
+ it "should assign the result continued fraction of a binary operation the max limit of the two operands" do
76
+ c = ContinuedFraction.new(rand,20)
77
+ result = @cf + c
78
+ result.limit.should == [@cf.limit,c.limit].max
79
+ end
38
80
  end
39
81
  end
@@ -1,4 +1,5 @@
1
1
  $LOAD_PATH << File.join(File.dirname(__FILE__), "/../lib")
2
2
  require 'rubygems'
3
- require 'spec'
4
- require 'continued_fractions'
3
+ require 'rspec'
4
+ require 'continued_fractions'
5
+ include ContinuedFractions
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 1
7
- - 2
8
- - 2
9
- version: 1.2.2
7
+ - 3
8
+ - 1
9
+ version: 1.3.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jose Hales-Garcia
@@ -14,13 +14,14 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-27 00:00:00 -07:00
17
+ date: 2011-06-10 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rspec
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
24
25
  requirements:
25
26
  - - ">="
26
27
  - !ruby/object:Gem::Version
@@ -35,6 +36,7 @@ dependencies:
35
36
  name: echoe
36
37
  prerelease: false
37
38
  requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
38
40
  requirements:
39
41
  - - ">="
40
42
  - !ruby/object:Gem::Version
@@ -44,7 +46,7 @@ dependencies:
44
46
  version: "4.3"
45
47
  type: :development
46
48
  version_requirements: *id002
47
- description: Generate continued fractions.
49
+ description: Class for working with continued fractions.
48
50
  email: jolohaga@me.com
49
51
  executables: []
50
52
 
@@ -77,6 +79,7 @@ rdoc_options:
77
79
  require_paths:
78
80
  - lib
79
81
  required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
80
83
  requirements:
81
84
  - - ">="
82
85
  - !ruby/object:Gem::Version
@@ -84,6 +87,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
84
87
  - 0
85
88
  version: "0"
86
89
  required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
87
91
  requirements:
88
92
  - - ">="
89
93
  - !ruby/object:Gem::Version
@@ -94,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
98
  requirements: []
95
99
 
96
100
  rubyforge_project: continued_fractions
97
- rubygems_version: 1.3.6
101
+ rubygems_version: 1.3.7
98
102
  signing_key:
99
103
  specification_version: 3
100
104
  summary: Generate continued fractions.