Class: ContinuedFraction
- Inherits:
-
Object
- Object
- ContinuedFraction
- Includes:
- Comparable
- Defined in:
- lib/continued_fractions.rb
Overview
ContinuedFraction
Generates quotients and convergents for a given number
Constant Summary collapse
- DEFAULT_LIMIT =
5
Instance Attribute Summary collapse
-
#convergents ⇒ Object
readonly
Returns the value of attribute convergents.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#number ⇒ Object
readonly
Returns the value of attribute number.
-
#quotients ⇒ Object
readonly
Returns the value of attribute quotients.
Class Method Summary collapse
-
.from_quotients(*quotients) ⇒ ContinuedFraction
The result from the provided array of quotients.
Instance Method Summary collapse
-
#*(other) ⇒ ContinuedFraction
The CF product of self and another CF.
-
#+(other) ⇒ ContinuedFraction
The CF sum of self and another CF.
-
#-(other) ⇒ ContinuedFraction
The CF difference of self and another CF.
-
#/(other) ⇒ ContinuedFraction
The CF quotient of self and another CF.
- #<=>(other) ⇒ Object
-
#initialize(number, limit = DEFAULT_LIMIT) ⇒ ContinuedFraction
constructor
The quotients and convergents of a continued fraction for a given number.
- #inspect ⇒ Object
Constructor Details
#initialize(number, limit = DEFAULT_LIMIT) ⇒ ContinuedFraction
Returns the quotients and convergents of a continued fraction for a given number.
23 24 25 26 27 |
# File 'lib/continued_fractions.rb', line 23 def initialize(number, limit=DEFAULT_LIMIT) @number = number @ratioed_number = number.to_r @quotients, @convergents, @limit = calculate_quotients_and_convergents(limit) end |
Instance Attribute Details
#convergents ⇒ Object (readonly)
Returns the value of attribute convergents.
12 13 14 |
# File 'lib/continued_fractions.rb', line 12 def convergents @convergents end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
12 13 14 |
# File 'lib/continued_fractions.rb', line 12 def limit @limit end |
#number ⇒ Object (readonly)
Returns the value of attribute number.
12 13 14 |
# File 'lib/continued_fractions.rb', line 12 def number @number end |
#quotients ⇒ Object (readonly)
Returns the value of attribute quotients.
12 13 14 |
# File 'lib/continued_fractions.rb', line 12 def quotients @quotients end |
Class Method Details
.from_quotients(*quotients) ⇒ ContinuedFraction
Returns the result from the provided array of quotients.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/continued_fractions.rb', line 71 def self.from_quotients(*quotients) quotients = quotients.flatten return quotients.first if quotients.size == 1 p_minus_2, p_minus_1 = 0, 1 q_minus_2, q_minus_1 = 1, 0 quotients.each do |quotient| p = quotient * p_minus_1 + p_minus_2 q = quotient * q_minus_1 + q_minus_2 p_minus_2, p_minus_1 = p_minus_1, p q_minus_2, q_minus_1 = q_minus_1, q end ContinuedFraction.new((p_minus_1.to_r / q_minus_1).to_f, quotients.length) end |
Instance Method Details
#*(other) ⇒ ContinuedFraction
Returns the CF product of self and another CF.
59 60 61 62 63 |
# File 'lib/continued_fractions.rb', line 59 def *(other) number_of(other) do |num,prec| evaluate("#{number} * #{num}",prec) end end |
#+(other) ⇒ ContinuedFraction
Returns the CF sum of self and another CF.
32 33 34 35 36 |
# File 'lib/continued_fractions.rb', line 32 def +(other) number_of(other) do |num,prec| evaluate("#{number} + #{num}",prec) end end |
#-(other) ⇒ ContinuedFraction
Returns the CF difference of self and another CF.
41 42 43 44 45 |
# File 'lib/continued_fractions.rb', line 41 def -(other) number_of(other) do |num,prec| evaluate("#{number} - #{num}",prec) end end |
#/(other) ⇒ ContinuedFraction
Returns the CF quotient of self and another CF.
50 51 52 53 54 |
# File 'lib/continued_fractions.rb', line 50 def /(other) number_of(other) do |num,prec| evaluate("#{number} / #{num}",prec) end end |
#<=>(other) ⇒ Object
89 90 91 |
# File 'lib/continued_fractions.rb', line 89 def <=>(other) number <=> other.number end |
#inspect ⇒ Object
93 94 95 |
# File 'lib/continued_fractions.rb', line 93 def inspect "#{number}, quotients: #{quotients}, convergents: #{convergents[0..-1]}" end |