Class: Radix::Rational

Inherits:
Numeric
  • Object
show all
Defined in:
lib/radix/rational.rb

Overview

Represents rational numbers.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Numeric

#*, #+, #-, #/, #base_decode, #base_encode, #decimal, #parse_array, #parse_base, #parse_numeric, #parse_string

Constructor Details

#initialize(numerator, denominator = nil, base = 10) ⇒ void (private)

Create a new Radix::Rational instance.

Examples:

Rational.new(<Integer>, <Integer>, <Integer>)
Rational.new(<Rational>, <Integer>)

Parameters:

  • numerator (Radix::Rational, ::Rational, Fixnum)

    A rational number or a fixnum for the numerator of a new Rational.

  • denominator (Fixnum) (defaults to: nil)

    Denominator for new Rational.

  • base (Fixnum) (defaults to: 10)

    Base level for new Rational.



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/radix/rational.rb', line 55

def initialize(numerator, denominator=nil, base=10)
  case numerator
  when ::Rational, Rational
    ratn = numerator
    base = denominator
    @value = Rational(ratn.numerator, ratn.denominator)
  else
    n = parse_value(numerator, base)
    d = parse_value(denominator, base)
    @value = Rational(n, d)
  end
  @base, @code = parse_base(base)
end

Instance Attribute Details

#baseFixnum (readonly)

Base of the number.

Returns:

  • (Fixnum)

    The base level of Rational instance.



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
# File 'lib/radix/rational.rb', line 15

class Rational < Numeric

  ##
  # Alternative to #new.
  #
  # @return [Radix::Rational]
  def self.[](n,d=nil,b=10)
    new(n,d,b)
  end

  ##
  # Stores the Rational value in Base-10.
  #
  # @return [Rational] 
  attr :value

  ##
  # Base of the number.
  #
  # @return [Fixnum] The base level of Rational instance.
  attr :base

  ##
  # Base encoding table.
  #
  # @return [Array<String>, nil] Substitution chars or nil if default.
  attr :code

private

  ##
  # Create a new Radix::Rational instance.
  # @example
  #   Rational.new(<Integer>, <Integer>, <Integer>)
  #   Rational.new(<Rational>, <Integer>)
  # @param [Radix::Rational, ::Rational, Fixnum] numerator A rational number
  #   or a fixnum for the numerator of a new Rational.
  # @param [Fixnum] denominator Denominator for new Rational.
  # @param [Fixnum] base Base level for new Rational.
  # @return [void]
  def initialize(numerator, denominator=nil, base=10)
    case numerator
    when ::Rational, Rational
      ratn = numerator
      base = denominator
      @value = Rational(ratn.numerator, ratn.denominator)
    else
      n = parse_value(numerator, base)
      d = parse_value(denominator, base)
      @value = Rational(n, d)
    end
    @base, @code = parse_base(base)
  end

  ##
  # Parses String, Array, Radix::Float, Radix::Integer or Ruby numerics and
  # returns the decimal value from base context for storage in @value.
  # 
  # @param [Fixnum] base 
  def parse_value(value, base)
    case value
    when Float, Integer # Radix
      parse_numeric(value.to_i, base)
    when ::Array
      parse_array(value, base)
    when ::String
      parse_string(value, base)
    when ::Numeric
      parse_numeric(value.to_i, base)
    end
  end

  public

  ##
  # The numerator.
  #
  # @return [Fixnum] The numerator of Radix::Rational
  def numerator
    @value.numerator
  end

  ##
  # The denominator.
  #
  # @return [Fixnum] The denominator of Radix::Rational
  def denominator
    @value.denominator
  end

  ##
  # Is the value negative?
  #
  # @return [Boolean] Returns true if value < 0.
  def negative?
    value < 0
  end

  ##
  # Convert rational to new base.
  #
  # @param [Fixnum] base Desired base.
  # @return [Radix::Rational] Returns new Radix::Rational in passed base.
  def convert(base)
    self.class.new(numerator, denominator, base)
  end

  ##
  # Convert to rational.
  #
  # @return [Rational] Returns the value.
  def to_r
    value
  end

  ##
  # Convert to Float by dividing the numerator by the denominator.
  #
  # Returns the converted value. [Float]
  def to_f
    numerator.to_f / denominator.to_f
  end

  ##
  # Convert to Integer by converting to Float first then
  # appling #to_i to the float.
  #
  # Returns the converted value. [Integer]
  def to_i
    to_f.to_i
  end

  ##
  # Translate value into an array of places. Uses current base unless
  # specified.
  # 
  # @param [Fixnum] base Desired base.
  # @return [Array<Fixnum, String>] Array of place values.
  def to_a(base=nil)
    if base
      convert(base).digits_encoded
    else
      digits_encoded
    end     
  end

  ##
  # Convert the value into a string representation of the given base.
  #
  # @param [Fixnum] base The base to convert.
  # @param [String] divider The string char(s) to divided with.
  # @return [String] Translated value.
  def to_s(base=nil, divider=nil)
    divider = divider.to_s if divider
    if base
      convert(base).to_s(nil, divider)
    else
      if code
        digits_encoded.join(divider)
      else
        if @base > 10
          digits.join(divider || DIVIDER)
        else
          digits.join(divider)
        end
      end
    end
  end

  ##
  # Simple equality requires equal values only.
  #
  # @todo This may need improvement to be more percise.
  # @param [#to_f] other The value to compare to.
  # @return [Boolean] True if equal values.
  def ==(other)
    a, b = self.to_f, other.to_f
    a == b
  end

  ##
  # Returns an irreducible version of self in current base.
  # 
  # @todo Is this method neccesary since @value is a Ruby Rational and
  #   therefore already irreducible?
  # @return [Radix::Rational]
  def reduce
    self.class.new(Rational(numerator, denominator), base)
  end

  ##
  # Returns an array representation of the numerator and denominator with
  # each column's value.
  #
  # @return [Array<String, Fixnum>] Values per column of @base as array. 
  #   Prepended with "-" if negative. 
  def digits
    n = base_conversion(numerator, base)
    d = base_conversion(denominator, base)
    i = n + ['/'] + d
    i.unshift('-') if negative? 
    i
  end

  ##
  # Returns digits, or coded version of digits if @code.
  #
  # @return [Array<String, Fixnum>] Values per column of @base as array. 
  #   Prepended with "-" if negative. Or encoded version if @code is
  #   defined.
  def digits_encoded
    base_encode(digits)
  end

  ##
  # Creates a string representation of self.
  #
  # @return [String] String rep of self.digits and @base.
  def inspect
    "#{digits.join(' ')} (#{base})"
  end

  ##
  # Returns new Radix::Rational of passed value in base-10 and self as an
  # Array.
  # 
  # @return [Array<(Radix::Rational, Radix::Rational)>]
  def coerce(value)
    [Radix::Rational.new(value), self]  
  end

  private

  ##
  # Perform passed arithmetic operation.
  #
  # @param [#to_r] other  
  # @return [Radix::Rational] Returns the result of the operation in @base.
  def operation(op, other)
    x = value.__send__(op, other.to_r)
    self.class.new(x, base)
  end

  ##
  # Perform base conversion.
  #
  # @return [Array<Fixnum, String>] Array of places.
  def base_conversion(value, base)
    #if value < 0
    #  @negative, value = true, value.abs
    #end
    i = value.abs

    a = []
    while i > 0
      i, r = i.divmod(base)
      a << r
    end

    a << 0 if a.empty?

    a.reverse
  end

end

#codeArray<String>? (readonly)

Base encoding table.

Returns:

  • (Array<String>, nil)

    Substitution chars or nil if default.



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
# File 'lib/radix/rational.rb', line 15

class Rational < Numeric

  ##
  # Alternative to #new.
  #
  # @return [Radix::Rational]
  def self.[](n,d=nil,b=10)
    new(n,d,b)
  end

  ##
  # Stores the Rational value in Base-10.
  #
  # @return [Rational] 
  attr :value

  ##
  # Base of the number.
  #
  # @return [Fixnum] The base level of Rational instance.
  attr :base

  ##
  # Base encoding table.
  #
  # @return [Array<String>, nil] Substitution chars or nil if default.
  attr :code

private

  ##
  # Create a new Radix::Rational instance.
  # @example
  #   Rational.new(<Integer>, <Integer>, <Integer>)
  #   Rational.new(<Rational>, <Integer>)
  # @param [Radix::Rational, ::Rational, Fixnum] numerator A rational number
  #   or a fixnum for the numerator of a new Rational.
  # @param [Fixnum] denominator Denominator for new Rational.
  # @param [Fixnum] base Base level for new Rational.
  # @return [void]
  def initialize(numerator, denominator=nil, base=10)
    case numerator
    when ::Rational, Rational
      ratn = numerator
      base = denominator
      @value = Rational(ratn.numerator, ratn.denominator)
    else
      n = parse_value(numerator, base)
      d = parse_value(denominator, base)
      @value = Rational(n, d)
    end
    @base, @code = parse_base(base)
  end

  ##
  # Parses String, Array, Radix::Float, Radix::Integer or Ruby numerics and
  # returns the decimal value from base context for storage in @value.
  # 
  # @param [Fixnum] base 
  def parse_value(value, base)
    case value
    when Float, Integer # Radix
      parse_numeric(value.to_i, base)
    when ::Array
      parse_array(value, base)
    when ::String
      parse_string(value, base)
    when ::Numeric
      parse_numeric(value.to_i, base)
    end
  end

  public

  ##
  # The numerator.
  #
  # @return [Fixnum] The numerator of Radix::Rational
  def numerator
    @value.numerator
  end

  ##
  # The denominator.
  #
  # @return [Fixnum] The denominator of Radix::Rational
  def denominator
    @value.denominator
  end

  ##
  # Is the value negative?
  #
  # @return [Boolean] Returns true if value < 0.
  def negative?
    value < 0
  end

  ##
  # Convert rational to new base.
  #
  # @param [Fixnum] base Desired base.
  # @return [Radix::Rational] Returns new Radix::Rational in passed base.
  def convert(base)
    self.class.new(numerator, denominator, base)
  end

  ##
  # Convert to rational.
  #
  # @return [Rational] Returns the value.
  def to_r
    value
  end

  ##
  # Convert to Float by dividing the numerator by the denominator.
  #
  # Returns the converted value. [Float]
  def to_f
    numerator.to_f / denominator.to_f
  end

  ##
  # Convert to Integer by converting to Float first then
  # appling #to_i to the float.
  #
  # Returns the converted value. [Integer]
  def to_i
    to_f.to_i
  end

  ##
  # Translate value into an array of places. Uses current base unless
  # specified.
  # 
  # @param [Fixnum] base Desired base.
  # @return [Array<Fixnum, String>] Array of place values.
  def to_a(base=nil)
    if base
      convert(base).digits_encoded
    else
      digits_encoded
    end     
  end

  ##
  # Convert the value into a string representation of the given base.
  #
  # @param [Fixnum] base The base to convert.
  # @param [String] divider The string char(s) to divided with.
  # @return [String] Translated value.
  def to_s(base=nil, divider=nil)
    divider = divider.to_s if divider
    if base
      convert(base).to_s(nil, divider)
    else
      if code
        digits_encoded.join(divider)
      else
        if @base > 10
          digits.join(divider || DIVIDER)
        else
          digits.join(divider)
        end
      end
    end
  end

  ##
  # Simple equality requires equal values only.
  #
  # @todo This may need improvement to be more percise.
  # @param [#to_f] other The value to compare to.
  # @return [Boolean] True if equal values.
  def ==(other)
    a, b = self.to_f, other.to_f
    a == b
  end

  ##
  # Returns an irreducible version of self in current base.
  # 
  # @todo Is this method neccesary since @value is a Ruby Rational and
  #   therefore already irreducible?
  # @return [Radix::Rational]
  def reduce
    self.class.new(Rational(numerator, denominator), base)
  end

  ##
  # Returns an array representation of the numerator and denominator with
  # each column's value.
  #
  # @return [Array<String, Fixnum>] Values per column of @base as array. 
  #   Prepended with "-" if negative. 
  def digits
    n = base_conversion(numerator, base)
    d = base_conversion(denominator, base)
    i = n + ['/'] + d
    i.unshift('-') if negative? 
    i
  end

  ##
  # Returns digits, or coded version of digits if @code.
  #
  # @return [Array<String, Fixnum>] Values per column of @base as array. 
  #   Prepended with "-" if negative. Or encoded version if @code is
  #   defined.
  def digits_encoded
    base_encode(digits)
  end

  ##
  # Creates a string representation of self.
  #
  # @return [String] String rep of self.digits and @base.
  def inspect
    "#{digits.join(' ')} (#{base})"
  end

  ##
  # Returns new Radix::Rational of passed value in base-10 and self as an
  # Array.
  # 
  # @return [Array<(Radix::Rational, Radix::Rational)>]
  def coerce(value)
    [Radix::Rational.new(value), self]  
  end

  private

  ##
  # Perform passed arithmetic operation.
  #
  # @param [#to_r] other  
  # @return [Radix::Rational] Returns the result of the operation in @base.
  def operation(op, other)
    x = value.__send__(op, other.to_r)
    self.class.new(x, base)
  end

  ##
  # Perform base conversion.
  #
  # @return [Array<Fixnum, String>] Array of places.
  def base_conversion(value, base)
    #if value < 0
    #  @negative, value = true, value.abs
    #end
    i = value.abs

    a = []
    while i > 0
      i, r = i.divmod(base)
      a << r
    end

    a << 0 if a.empty?

    a.reverse
  end

end

#valueRational (readonly)

Stores the Rational value in Base-10.

Returns:



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
# File 'lib/radix/rational.rb', line 15

class Rational < Numeric

  ##
  # Alternative to #new.
  #
  # @return [Radix::Rational]
  def self.[](n,d=nil,b=10)
    new(n,d,b)
  end

  ##
  # Stores the Rational value in Base-10.
  #
  # @return [Rational] 
  attr :value

  ##
  # Base of the number.
  #
  # @return [Fixnum] The base level of Rational instance.
  attr :base

  ##
  # Base encoding table.
  #
  # @return [Array<String>, nil] Substitution chars or nil if default.
  attr :code

private

  ##
  # Create a new Radix::Rational instance.
  # @example
  #   Rational.new(<Integer>, <Integer>, <Integer>)
  #   Rational.new(<Rational>, <Integer>)
  # @param [Radix::Rational, ::Rational, Fixnum] numerator A rational number
  #   or a fixnum for the numerator of a new Rational.
  # @param [Fixnum] denominator Denominator for new Rational.
  # @param [Fixnum] base Base level for new Rational.
  # @return [void]
  def initialize(numerator, denominator=nil, base=10)
    case numerator
    when ::Rational, Rational
      ratn = numerator
      base = denominator
      @value = Rational(ratn.numerator, ratn.denominator)
    else
      n = parse_value(numerator, base)
      d = parse_value(denominator, base)
      @value = Rational(n, d)
    end
    @base, @code = parse_base(base)
  end

  ##
  # Parses String, Array, Radix::Float, Radix::Integer or Ruby numerics and
  # returns the decimal value from base context for storage in @value.
  # 
  # @param [Fixnum] base 
  def parse_value(value, base)
    case value
    when Float, Integer # Radix
      parse_numeric(value.to_i, base)
    when ::Array
      parse_array(value, base)
    when ::String
      parse_string(value, base)
    when ::Numeric
      parse_numeric(value.to_i, base)
    end
  end

  public

  ##
  # The numerator.
  #
  # @return [Fixnum] The numerator of Radix::Rational
  def numerator
    @value.numerator
  end

  ##
  # The denominator.
  #
  # @return [Fixnum] The denominator of Radix::Rational
  def denominator
    @value.denominator
  end

  ##
  # Is the value negative?
  #
  # @return [Boolean] Returns true if value < 0.
  def negative?
    value < 0
  end

  ##
  # Convert rational to new base.
  #
  # @param [Fixnum] base Desired base.
  # @return [Radix::Rational] Returns new Radix::Rational in passed base.
  def convert(base)
    self.class.new(numerator, denominator, base)
  end

  ##
  # Convert to rational.
  #
  # @return [Rational] Returns the value.
  def to_r
    value
  end

  ##
  # Convert to Float by dividing the numerator by the denominator.
  #
  # Returns the converted value. [Float]
  def to_f
    numerator.to_f / denominator.to_f
  end

  ##
  # Convert to Integer by converting to Float first then
  # appling #to_i to the float.
  #
  # Returns the converted value. [Integer]
  def to_i
    to_f.to_i
  end

  ##
  # Translate value into an array of places. Uses current base unless
  # specified.
  # 
  # @param [Fixnum] base Desired base.
  # @return [Array<Fixnum, String>] Array of place values.
  def to_a(base=nil)
    if base
      convert(base).digits_encoded
    else
      digits_encoded
    end     
  end

  ##
  # Convert the value into a string representation of the given base.
  #
  # @param [Fixnum] base The base to convert.
  # @param [String] divider The string char(s) to divided with.
  # @return [String] Translated value.
  def to_s(base=nil, divider=nil)
    divider = divider.to_s if divider
    if base
      convert(base).to_s(nil, divider)
    else
      if code
        digits_encoded.join(divider)
      else
        if @base > 10
          digits.join(divider || DIVIDER)
        else
          digits.join(divider)
        end
      end
    end
  end

  ##
  # Simple equality requires equal values only.
  #
  # @todo This may need improvement to be more percise.
  # @param [#to_f] other The value to compare to.
  # @return [Boolean] True if equal values.
  def ==(other)
    a, b = self.to_f, other.to_f
    a == b
  end

  ##
  # Returns an irreducible version of self in current base.
  # 
  # @todo Is this method neccesary since @value is a Ruby Rational and
  #   therefore already irreducible?
  # @return [Radix::Rational]
  def reduce
    self.class.new(Rational(numerator, denominator), base)
  end

  ##
  # Returns an array representation of the numerator and denominator with
  # each column's value.
  #
  # @return [Array<String, Fixnum>] Values per column of @base as array. 
  #   Prepended with "-" if negative. 
  def digits
    n = base_conversion(numerator, base)
    d = base_conversion(denominator, base)
    i = n + ['/'] + d
    i.unshift('-') if negative? 
    i
  end

  ##
  # Returns digits, or coded version of digits if @code.
  #
  # @return [Array<String, Fixnum>] Values per column of @base as array. 
  #   Prepended with "-" if negative. Or encoded version if @code is
  #   defined.
  def digits_encoded
    base_encode(digits)
  end

  ##
  # Creates a string representation of self.
  #
  # @return [String] String rep of self.digits and @base.
  def inspect
    "#{digits.join(' ')} (#{base})"
  end

  ##
  # Returns new Radix::Rational of passed value in base-10 and self as an
  # Array.
  # 
  # @return [Array<(Radix::Rational, Radix::Rational)>]
  def coerce(value)
    [Radix::Rational.new(value), self]  
  end

  private

  ##
  # Perform passed arithmetic operation.
  #
  # @param [#to_r] other  
  # @return [Radix::Rational] Returns the result of the operation in @base.
  def operation(op, other)
    x = value.__send__(op, other.to_r)
    self.class.new(x, base)
  end

  ##
  # Perform base conversion.
  #
  # @return [Array<Fixnum, String>] Array of places.
  def base_conversion(value, base)
    #if value < 0
    #  @negative, value = true, value.abs
    #end
    i = value.abs

    a = []
    while i > 0
      i, r = i.divmod(base)
      a << r
    end

    a << 0 if a.empty?

    a.reverse
  end

end

Class Method Details

.[](n, d = nil, b = 10) ⇒ Radix::Rational

Alternative to #new.

Returns:



21
22
23
# File 'lib/radix/rational.rb', line 21

def self.[](n,d=nil,b=10)
  new(n,d,b)
end

Instance Method Details

#==(other) ⇒ Boolean

TODO:

This may need improvement to be more percise.

Simple equality requires equal values only.

Parameters:

  • other (#to_f)

    The value to compare to.

Returns:

  • (Boolean)

    True if equal values.



190
191
192
193
# File 'lib/radix/rational.rb', line 190

def ==(other)
  a, b = self.to_f, other.to_f
  a == b
end

#base_conversion(value, base) ⇒ Array<Fixnum, String> (private)

Perform base conversion.

Returns:



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/radix/rational.rb', line 262

def base_conversion(value, base)
  #if value < 0
  #  @negative, value = true, value.abs
  #end
  i = value.abs

  a = []
  while i > 0
    i, r = i.divmod(base)
    a << r
  end

  a << 0 if a.empty?

  a.reverse
end

#coerce(value) ⇒ Array<(Radix::Rational, Radix::Rational)>

Returns new Radix::Rational of passed value in base-10 and self as an Array.



242
243
244
# File 'lib/radix/rational.rb', line 242

def coerce(value)
  [Radix::Rational.new(value), self]  
end

#convert(base) ⇒ Radix::Rational

Convert rational to new base.

Parameters:

  • base (Fixnum)

    Desired base.

Returns:



118
119
120
# File 'lib/radix/rational.rb', line 118

def convert(base)
  self.class.new(numerator, denominator, base)
end

#denominatorFixnum

The denominator.

Returns:

  • (Fixnum)

    The denominator of Radix::Rational



101
102
103
# File 'lib/radix/rational.rb', line 101

def denominator
  @value.denominator
end

#digitsArray<String, Fixnum>

Returns an array representation of the numerator and denominator with each column’s value.

Returns:

  • (Array<String, Fixnum>)

    Values per column of @base as array. Prepended with “-” if negative.



211
212
213
214
215
216
217
# File 'lib/radix/rational.rb', line 211

def digits
  n = base_conversion(numerator, base)
  d = base_conversion(denominator, base)
  i = n + ['/'] + d
  i.unshift('-') if negative? 
  i
end

#digits_encodedArray<String, Fixnum>

Returns digits, or coded version of digits if @code.

Returns:

  • (Array<String, Fixnum>)

    Values per column of @base as array. Prepended with “-” if negative. Or encoded version if @code is defined.



225
226
227
# File 'lib/radix/rational.rb', line 225

def digits_encoded
  base_encode(digits)
end

#inspectString

Creates a string representation of self.

Returns:

  • (String)

    String rep of self.digits and @base.



233
234
235
# File 'lib/radix/rational.rb', line 233

def inspect
  "#{digits.join(' ')} (#{base})"
end

#negative?Boolean

Is the value negative?

Returns:

  • (Boolean)

    Returns true if value < 0.



109
110
111
# File 'lib/radix/rational.rb', line 109

def negative?
  value < 0
end

#numeratorFixnum

The numerator.

Returns:

  • (Fixnum)

    The numerator of Radix::Rational



93
94
95
# File 'lib/radix/rational.rb', line 93

def numerator
  @value.numerator
end

#operation(op, other) ⇒ Radix::Rational (private)

Perform passed arithmetic operation.

Parameters:

Returns:



253
254
255
256
# File 'lib/radix/rational.rb', line 253

def operation(op, other)
  x = value.__send__(op, other.to_r)
  self.class.new(x, base)
end

#parse_value(value, base) ⇒ Object (private)

Parses String, Array, Radix::Float, Radix::Integer or Ruby numerics and returns the decimal value from base context for storage in @value.

Parameters:

  • base (Fixnum)


74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/radix/rational.rb', line 74

def parse_value(value, base)
  case value
  when Float, Integer # Radix
    parse_numeric(value.to_i, base)
  when ::Array
    parse_array(value, base)
  when ::String
    parse_string(value, base)
  when ::Numeric
    parse_numeric(value.to_i, base)
  end
end

#reduceRadix::Rational

TODO:

Is this method neccesary since @value is a Ruby Rational and therefore already irreducible?

Returns an irreducible version of self in current base.

Returns:



201
202
203
# File 'lib/radix/rational.rb', line 201

def reduce
  self.class.new(Rational(numerator, denominator), base)
end

#to_a(base = nil) ⇒ Array<Fixnum, String>

Translate value into an array of places. Uses current base unless specified.

Parameters:

  • base (Fixnum) (defaults to: nil)

    Desired base.

Returns:



153
154
155
156
157
158
159
# File 'lib/radix/rational.rb', line 153

def to_a(base=nil)
  if base
    convert(base).digits_encoded
  else
    digits_encoded
  end     
end

#to_fObject

Convert to Float by dividing the numerator by the denominator.

Returns the converted value. [Float]



134
135
136
# File 'lib/radix/rational.rb', line 134

def to_f
  numerator.to_f / denominator.to_f
end

#to_iObject

Convert to Integer by converting to Float first then appling #to_i to the float.

Returns the converted value. [Integer]



143
144
145
# File 'lib/radix/rational.rb', line 143

def to_i
  to_f.to_i
end

#to_rRational

Convert to rational.

Returns:



126
127
128
# File 'lib/radix/rational.rb', line 126

def to_r
  value
end

#to_s(base = nil, divider = nil) ⇒ String

Convert the value into a string representation of the given base.

Parameters:

  • base (Fixnum) (defaults to: nil)

    The base to convert.

  • divider (String) (defaults to: nil)

    The string char(s) to divided with.

Returns:

  • (String)

    Translated value.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/radix/rational.rb', line 167

def to_s(base=nil, divider=nil)
  divider = divider.to_s if divider
  if base
    convert(base).to_s(nil, divider)
  else
    if code
      digits_encoded.join(divider)
    else
      if @base > 10
        digits.join(divider || DIVIDER)
      else
        digits.join(divider)
      end
    end
  end
end