continued_fractions 1.5.2 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG +12 -2
- data/Rakefile +1 -1
- data/continued_fractions.gemspec +3 -3
- data/lib/continued_fractions.rb +8 -6
- data/spec/continued_fractions/continued_fraction_spec.rb +45 -32
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 833e0f015fc2902302f744f7c1c4901b8b77e51f
|
4
|
+
data.tar.gz: 65ca91e63bfa5adac66a77529de915d9d3bb6145
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9267372f711ade8ce6da446a79bd8e3e869361a96f47cd16286ae5a3a22e2be848979da6e0188b959dd8dc1aaaf9de5d0b2ab539fdf65cf0a281e8a35fe73944
|
7
|
+
data.tar.gz: f332b7f14cef53dc9e84f1eef4eacd5bb62aac97f06e9e62ea39a3b274e48bf66e1c67d5ae853b6af2bd5102e8de45f9c91761d98ca3985e046fca8f50054946
|
data/CHANGELOG
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
-
|
1
|
+
v0.1.0 Initial release
|
2
2
|
|
3
|
-
v1.
|
3
|
+
v1.1.0 Change public class representation of convergents from an array of Rationals to an array of arrays.
|
4
|
+
|
5
|
+
v1.2.0 Change parameter signature of ContinuedFraction#convergents_as_rationals
|
6
|
+
|
7
|
+
v1.3.0 Add binary operators: + - * /
|
8
|
+
|
9
|
+
v1.4.0 Eliminate need to invoke "include ContinuedFractions"
|
10
|
+
|
11
|
+
v1.5.0 Remove ContinuedFractions module; Move its methods into ContinuedFraction class
|
12
|
+
|
13
|
+
v1.6.0 Handle rational numbers, finite continued fractions
|
data/Rakefile
CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
require 'echoe'
|
4
4
|
|
5
|
-
Echoe.new('continued_fractions', '1.
|
5
|
+
Echoe.new('continued_fractions', '1.6.0') do |config|
|
6
6
|
config.summary = 'Generate continued fractions'
|
7
7
|
config.description = 'Class for working with continued fractions'
|
8
8
|
|
data/continued_fractions.gemspec
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
# stub: continued_fractions 1.
|
2
|
+
# stub: continued_fractions 1.6.0 ruby lib
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "continued_fractions"
|
6
|
-
s.version = "1.
|
6
|
+
s.version = "1.6.0"
|
7
7
|
|
8
8
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
9
9
|
s.require_paths = ["lib"]
|
10
10
|
s.authors = ["Jose Hales-Garcia"]
|
11
|
-
s.date = "2015-05-
|
11
|
+
s.date = "2015-05-10"
|
12
12
|
s.description = "Class for working with continued fractions"
|
13
13
|
s.email = "jolohaga@me.com"
|
14
14
|
s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README.md", "lib/continued_fractions.rb"]
|
data/lib/continued_fractions.rb
CHANGED
@@ -124,13 +124,15 @@ class ContinuedFraction
|
|
124
124
|
end
|
125
125
|
|
126
126
|
def calculate_quotients #:nodoc:
|
127
|
-
qs = Array.new(limit)
|
128
127
|
n = number
|
129
|
-
limit.
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
128
|
+
Array.new(limit).tap do |qs|
|
129
|
+
limit.times do |i|
|
130
|
+
qs[i] = n.to_i
|
131
|
+
break if ((divisor = n-qs[i]) == 0.0)
|
132
|
+
n = 1.0/divisor
|
133
|
+
end
|
134
|
+
self.limit = qs.compact.length
|
135
|
+
end.compact
|
134
136
|
end
|
135
137
|
|
136
138
|
def calculate_convergents #:nodoc:
|
@@ -4,74 +4,87 @@ describe ContinuedFraction do
|
|
4
4
|
let(:cf) { described_class.new(number,10) }
|
5
5
|
let(:number) { rand }
|
6
6
|
|
7
|
-
it "
|
8
|
-
# First 10 convergents of PI are...
|
9
|
-
convs = ['3/1', '22/7', '333/106', '355/113', '103993/33102', '104348/33215', '208341/66317', '312689/99532', '833719/265381', '1146408/364913'].map{|c| Rational(c)}
|
10
|
-
cf = described_class.new(Math::PI,10)
|
11
|
-
expect((cf.convergents_as_rationals - convs)).to be_empty
|
12
|
-
end
|
13
|
-
|
14
|
-
it "should return array for convergents method" do
|
7
|
+
it "returns an array of convergents" do
|
15
8
|
expect(cf.convergents).to be_kind_of(Array)
|
16
9
|
end
|
17
10
|
|
18
|
-
it "
|
11
|
+
it "preserves the number input" do
|
19
12
|
expect(cf.number).to eq number
|
20
13
|
end
|
21
14
|
|
22
|
-
it "
|
23
|
-
0.upto(cf.convergents.length-2) do |i|
|
24
|
-
convergent_rational_i_plus1, convergent_rational_i = cf.convergent_to_rational(cf.convergents[i+1]), cf.convergent_to_rational(cf.convergents[i])
|
25
|
-
expect(((convergent_rational_i_plus1 - cf.number).abs <= (convergent_rational_i - cf.number).abs)).to be true
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
it "should contain convergents which are expressed in lowest terms" do
|
30
|
-
1.upto(cf.convergents.length-1) do |i|
|
31
|
-
convergent_rational_i, convergent_rational_i_plus1 = cf.convergent_to_rational(cf.convergent(i)), cf.convergent_to_rational(cf.convergent(i+1))
|
32
|
-
expect((convergent_rational_i.numerator*convergent_rational_i_plus1.denominator - convergent_rational_i_plus1.numerator*convergent_rational_i.denominator)).to eq (-1)**(i)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
it "should add a number (on the right-hand-side) with a continued fraction and return a continued fraction" do
|
15
|
+
it "adds a number on the right-hand-side with a continued fraction and returns a continued fraction" do
|
37
16
|
expect((cf + 3)).to be_kind_of(ContinuedFraction)
|
38
17
|
end
|
39
18
|
|
40
|
-
it "
|
19
|
+
it "subtracts a number on the right-hand-side from a continued fraction and returns a continued fraction" do
|
41
20
|
expect((cf - 3)).to be_kind_of(ContinuedFraction)
|
42
21
|
end
|
43
22
|
|
44
|
-
it "
|
23
|
+
it "multiplies a number on the right-hand-side with a continued fraction and returns a continued fraction" do
|
45
24
|
expect((cf * 3)).to be_kind_of(ContinuedFraction)
|
46
25
|
end
|
47
26
|
|
48
|
-
it "
|
27
|
+
it "divides a number on the right-hand-side with a continued fraction and returns a continued fraction" do
|
49
28
|
expect((cf / 3)).to be_kind_of(ContinuedFraction)
|
50
29
|
end
|
51
30
|
|
52
|
-
it "
|
31
|
+
it "adds a continued fraction with another continued fraction and returns a continued fraction" do
|
53
32
|
c = described_class.new(rand,20)
|
54
33
|
expect((cf + c)).to be_kind_of(ContinuedFraction)
|
55
34
|
end
|
56
35
|
|
57
|
-
it "
|
36
|
+
it "subtracts a continued fraction with another continued fraction and returns a continued fraction" do
|
58
37
|
c = described_class.new(rand,20)
|
59
38
|
expect((cf - c)).to be_kind_of(ContinuedFraction)
|
60
39
|
end
|
61
40
|
|
62
|
-
it "
|
41
|
+
it "multiplies a continued fraction with another continued fraction and returns a continued fraction" do
|
63
42
|
c = described_class.new(rand,20)
|
64
43
|
expect((cf * c)).to be_kind_of(ContinuedFraction)
|
65
44
|
end
|
66
45
|
|
67
|
-
it "
|
46
|
+
it "divides a continued fraction with another continued fraction and returns a continued fraction" do
|
68
47
|
c = described_class.new(rand,20)
|
69
48
|
expect((cf / c)).to be_kind_of(ContinuedFraction)
|
70
49
|
end
|
71
50
|
|
72
|
-
it "
|
51
|
+
it "assigns the resulting continued fraction of a binary operation the max limit of the two operands" do
|
73
52
|
c = described_class.new(rand,20)
|
74
53
|
result = cf + c
|
75
54
|
expect(result.limit).to eq [cf.limit,c.limit].max
|
76
55
|
end
|
56
|
+
|
57
|
+
context 'with irrational numbers' do
|
58
|
+
it "accurately calculates the convergents" do
|
59
|
+
# First 10 convergents of PI are...
|
60
|
+
convs = ['3/1', '22/7', '333/106', '355/113', '103993/33102', '104348/33215', '208341/66317', '312689/99532', '833719/265381', '1146408/364913'].map{|c| Rational(c)}
|
61
|
+
cf = described_class.new(Math::PI,10)
|
62
|
+
expect((cf.convergents_as_rationals - convs)).to be_empty
|
63
|
+
end
|
64
|
+
|
65
|
+
it "contains convergents approaching the number" do
|
66
|
+
0.upto(cf.convergents.length-2) do |i|
|
67
|
+
convergent_rational_i_plus1, convergent_rational_i = cf.convergent_to_rational(cf.convergents[i+1]), cf.convergent_to_rational(cf.convergents[i])
|
68
|
+
expect(((convergent_rational_i_plus1 - cf.number).abs <= (convergent_rational_i - cf.number).abs)).to be true
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
it "contains convergents which are expressed in lowest terms" do
|
73
|
+
1.upto(cf.convergents.length-1) do |i|
|
74
|
+
convergent_rational_i, convergent_rational_i_plus1 = cf.convergent_to_rational(cf.convergent(i)), cf.convergent_to_rational(cf.convergent(i+1))
|
75
|
+
expect((convergent_rational_i.numerator*convergent_rational_i_plus1.denominator - convergent_rational_i_plus1.numerator*convergent_rational_i.denominator)).to eq (-1)**(i)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'with rational numbers' do
|
81
|
+
it 'changes the limit to the number of convergents calculated' do
|
82
|
+
expect(described_class.new(1.5, 10).limit).to be 2
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'calculates the convergents' do
|
86
|
+
convs = [ '1/1', '3/2' ].map{|c| Rational(c)}
|
87
|
+
expect(described_class.new(1.5, 10).convergents_as_rationals - convs).to be_empty
|
88
|
+
end
|
89
|
+
end
|
77
90
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: continued_fractions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jose Hales-Garcia
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05-
|
11
|
+
date: 2015-05-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|