radix 2.1.1 → 2.2.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.
- data/.index +7 -3
- data/.yardopts +6 -3
- data/HISTORY.md +21 -5
- data/README.md +16 -7
- data/demo/issues/004_zero_empty_string.md +18 -0
- data/lib/radix.rb +13 -40
- data/lib/radix.yml +7 -3
- data/lib/radix/base.rb +137 -20
- data/lib/radix/float.rb +206 -14
- data/lib/radix/integer.rb +221 -21
- data/lib/radix/numeric.rb +99 -26
- data/lib/radix/operator.rb +82 -0
- data/lib/radix/rational.rb +100 -27
- metadata +20 -3
data/lib/radix/float.rb
CHANGED
@@ -2,30 +2,68 @@ require 'radix/numeric'
|
|
2
2
|
|
3
3
|
module Radix
|
4
4
|
|
5
|
-
|
5
|
+
##
|
6
|
+
# Advanced float class for Radix conversions and mathematical operations
|
7
|
+
# with other bases.
|
6
8
|
#
|
7
|
-
#
|
8
|
-
#
|
9
|
+
# @todo Make fully immutable. After that we can catch @digits and
|
10
|
+
# the library should be a good bit faster.
|
11
|
+
#
|
12
|
+
# @!attribute [r] value
|
13
|
+
# @return [Float] Float's decimal value.
|
14
|
+
# @!attribute [r] base
|
15
|
+
# @return [Fixnum] The base level of Float instance.
|
16
|
+
# @!attribute [r] code
|
17
|
+
# @return [Array<String>, nil] Substitution chars or nil if default.
|
9
18
|
class Float < Numeric
|
10
19
|
|
20
|
+
##
|
11
21
|
# Internal floating point value.
|
22
|
+
#
|
23
|
+
# @return [Float] Float's decimal value.
|
12
24
|
attr :value
|
13
25
|
|
26
|
+
##
|
14
27
|
# Base of the number.
|
28
|
+
#
|
29
|
+
# @return [Fixnum] The base level of Float instance.
|
15
30
|
attr :base
|
16
31
|
|
32
|
+
##
|
17
33
|
# Base encoding table.
|
34
|
+
#
|
35
|
+
# @return [Array<String>, nil] Substitution chars or nil if default.
|
18
36
|
attr :code
|
19
37
|
|
20
38
|
private
|
21
39
|
|
40
|
+
##
|
41
|
+
# Starts a new instance of the Radix::Float class.
|
22
42
|
#
|
43
|
+
# @param [Radix::Numeric, Numeric, Array, String] value
|
44
|
+
# The value of the new integer in context of base.
|
45
|
+
#
|
46
|
+
# @param [Fixnum, Array<String>] base
|
47
|
+
# The base context in which value is determined. Can be an array
|
48
|
+
# of characters to use in place of default.
|
49
|
+
#
|
50
|
+
# @return [void]
|
23
51
|
def initialize(value, base=10)
|
24
52
|
@value = parse_value(value, base)
|
25
53
|
@base, @code = parse_base(base)
|
26
54
|
end
|
27
55
|
|
56
|
+
##
|
57
|
+
# Takes a Radix::Numeric, String or array and returns the decimal float
|
58
|
+
# value for storage in @value.
|
59
|
+
#
|
60
|
+
# @param [Radix::Numeric, Numeric, String, Array<Numeric, String>] value
|
61
|
+
# The value of the integer in base context.
|
62
|
+
#
|
63
|
+
# @param [Fixnum, Array<String>] base
|
64
|
+
# The context base of value.
|
28
65
|
#
|
66
|
+
# @return [Float] Float value of Integer.
|
29
67
|
def parse_value(value, base)
|
30
68
|
case value
|
31
69
|
when Float, Integer # Radix
|
@@ -41,20 +79,32 @@ module Radix
|
|
41
79
|
|
42
80
|
public
|
43
81
|
|
44
|
-
|
82
|
+
##
|
83
|
+
# Makes this Radix::Float a ruby Integer.
|
84
|
+
#
|
85
|
+
# @return [Integer] Base(10) value as Integer.
|
45
86
|
def to_i
|
46
87
|
to_f.to_i
|
47
88
|
end
|
48
89
|
|
49
|
-
#
|
50
90
|
alias_method :to_int, :to_i
|
51
91
|
|
52
|
-
|
92
|
+
##
|
93
|
+
# Makes this Radix::Float a ruby float.
|
94
|
+
#
|
95
|
+
# @return [Float] Base(10) value as Float.
|
53
96
|
def to_f
|
54
97
|
value.to_f
|
55
98
|
end
|
56
99
|
|
100
|
+
##
|
101
|
+
# Makes this Radix::Float an array using code if defined. Returns an
|
102
|
+
# array using default chars otherwise.
|
103
|
+
#
|
104
|
+
# @param [Fixnum] base
|
105
|
+
# Desired base.
|
57
106
|
#
|
107
|
+
# @return [Array<Fixnum, String>] Current base encoded array.
|
58
108
|
def to_a(base=nil)
|
59
109
|
if base
|
60
110
|
convert(base).digits_encoded
|
@@ -63,7 +113,18 @@ module Radix
|
|
63
113
|
end
|
64
114
|
end
|
65
115
|
|
116
|
+
##
|
117
|
+
# Creates an encoded string in passed base, with passed digit divider.
|
66
118
|
#
|
119
|
+
# @note For base 10 or less does not use a divider unless specified.
|
120
|
+
#
|
121
|
+
# @param [Fixnum, Array<String>] base
|
122
|
+
# Desired base.
|
123
|
+
#
|
124
|
+
# @param [String] divider
|
125
|
+
# Desired divider character(s).
|
126
|
+
#
|
127
|
+
# @return [String] Encoded string with specified divider.
|
67
128
|
def to_s(base=nil, divider=nil)
|
68
129
|
divider = divider.to_s if divider
|
69
130
|
if base
|
@@ -81,12 +142,19 @@ module Radix
|
|
81
142
|
end
|
82
143
|
end
|
83
144
|
|
145
|
+
##
|
146
|
+
# Creates a string representation of self.
|
84
147
|
#
|
148
|
+
# @return [String] String rep of self.digits and @base.
|
85
149
|
def inspect
|
86
150
|
"#{digits.join(' ')} (#{base})"
|
87
151
|
end
|
88
152
|
|
153
|
+
##
|
154
|
+
# Returns an array representation of each column's value in decimal chars.
|
89
155
|
#
|
156
|
+
# @return [Array<String, Fixnum>]
|
157
|
+
# Values per column of @base as array. Prepended with "-" if negative.
|
90
158
|
def digits
|
91
159
|
i, f = base_conversion(value, base)
|
92
160
|
if negative?
|
@@ -96,63 +164,129 @@ module Radix
|
|
96
164
|
end
|
97
165
|
end
|
98
166
|
|
167
|
+
##
|
168
|
+
# Returns digits, or coded version of digits if @code.
|
99
169
|
#
|
170
|
+
# @return [Array<String, Fixnum>]
|
171
|
+
# Values per column of @base as array. Prepended with "-" if negative.
|
172
|
+
# Or encoded version if @code is defined.
|
100
173
|
def digits_encoded
|
101
174
|
base_encode(digits)
|
102
175
|
end
|
103
176
|
|
104
|
-
|
177
|
+
##
|
178
|
+
# Returns true if the number is negative?
|
179
|
+
#
|
180
|
+
# @return [Boolean] True if float value is < 0.
|
105
181
|
def negative?
|
106
182
|
value < 0
|
107
183
|
end
|
108
184
|
|
185
|
+
##
|
186
|
+
# Creates a new Radix::Float of same value in different base.
|
109
187
|
#
|
188
|
+
# @return [Radix::Float] New float of same value in different base.
|
110
189
|
def convert(new_base)
|
111
190
|
self.class.new(value, new_base)
|
112
191
|
end
|
113
192
|
|
114
|
-
|
193
|
+
##
|
194
|
+
# Power exponentional operation.
|
195
|
+
#
|
196
|
+
# @param [#to_f] other
|
197
|
+
# The exponent by which to raise Float.
|
198
|
+
#
|
199
|
+
# @return [Radix::Float] Result of exponential operation.
|
115
200
|
def **(other)
|
116
201
|
operation(:**, other)
|
117
202
|
end
|
118
203
|
|
119
|
-
|
204
|
+
##
|
205
|
+
# Modulo binary operation.
|
206
|
+
#
|
207
|
+
# @param [#to_f] other
|
208
|
+
#
|
209
|
+
# @return [Radix::Float] Modulo result of division operation.
|
120
210
|
def %(other)
|
121
211
|
operation(:%, other)
|
122
212
|
end
|
123
213
|
|
124
|
-
#
|
125
214
|
alias_method :modulo, :%
|
126
215
|
|
216
|
+
##
|
217
|
+
# Returns the absolute value of self in @base.
|
127
218
|
#
|
219
|
+
# @return [Radix::Float] Absolute of @value.
|
128
220
|
def abs
|
129
221
|
self.class.new(value.abs, base)
|
130
222
|
end
|
131
223
|
|
224
|
+
##
|
225
|
+
# Returns the largest integer greater than or equal to self as a
|
226
|
+
# Radix::Float.
|
132
227
|
#
|
228
|
+
# @return [Radix::Float]
|
133
229
|
def ceil
|
134
230
|
self.class.new(value.ceil, base)
|
135
231
|
end
|
136
232
|
|
233
|
+
##
|
234
|
+
# Returns the smallest integer less than or equal to self as a
|
235
|
+
# Radix::Float.
|
137
236
|
#
|
237
|
+
# @return [Radix::Float]
|
138
238
|
def floor
|
139
239
|
self.class.new(value.floor, base)
|
140
240
|
end
|
141
241
|
|
242
|
+
##
|
243
|
+
# Returns a new Radix::Float instance of same base, rounded to the nearest
|
244
|
+
# whole integer.
|
245
|
+
#
|
246
|
+
# @example Rounding Radix Float
|
247
|
+
# > round_test = Radix::Float.new(123.03, 16)
|
248
|
+
# 7 11 . 0 7 10 14 1 4 7 10 14 1 (16)
|
249
|
+
# > round_test.value
|
250
|
+
# 123.03
|
251
|
+
# > round_test.round
|
252
|
+
# 7 11 . 0 (16)
|
253
|
+
# > round_test.round.value
|
254
|
+
# 123.0
|
255
|
+
# > round_test += 0.5
|
256
|
+
# 7 11 . 8 7 10 14 1 4 7 10 14 1 (16)
|
257
|
+
# > round_test.value
|
258
|
+
# 123.53
|
259
|
+
# > round_test.round
|
260
|
+
# 7 12 . 0 (16)
|
261
|
+
# > round_test.round.value
|
262
|
+
# 124.0
|
142
263
|
#
|
264
|
+
# @return [Radix::Float] New Instance
|
143
265
|
def round
|
144
266
|
return self.class.new((value + 0.5).floor, base) if self > 0.0
|
145
267
|
return self.class.new((value - 0.5).ceil, base) if self < 0.0
|
146
268
|
return self.class.new(0, base)
|
147
269
|
end
|
148
270
|
|
271
|
+
##
|
149
272
|
# Strict equality requires same class as well as value.
|
273
|
+
#
|
274
|
+
# @param [Object] num
|
275
|
+
# Object to compare.
|
276
|
+
#
|
277
|
+
# @return [Boolean] True if class and value are equal.
|
150
278
|
def eql?(num)
|
151
279
|
self.class.equal?(num.class) && self == num
|
152
280
|
end
|
153
281
|
|
282
|
+
##
|
154
283
|
# Simple equality requires equal values only.
|
155
|
-
|
284
|
+
#
|
285
|
+
# @param [Numeric] other
|
286
|
+
# Any Numeric instance.
|
287
|
+
#
|
288
|
+
# @return [Boolean] True if values are equal.
|
289
|
+
def ==(other)
|
156
290
|
case other
|
157
291
|
when Float, Integer # Radix
|
158
292
|
value == other.value
|
@@ -161,7 +295,27 @@ module Radix
|
|
161
295
|
end
|
162
296
|
end
|
163
297
|
|
298
|
+
##
|
299
|
+
# Comparitive binary operation. Very useful for sorting methods.
|
300
|
+
#
|
301
|
+
# @param [#to_f] other
|
302
|
+
# The object to compare value against.
|
303
|
+
#
|
304
|
+
# @example Comparison testing
|
305
|
+
# > lower = Radix::Float.new(123.00,10)
|
306
|
+
# 1 2 3 . 0 (10)
|
307
|
+
# > higher = Radix::Float.new(456.00,16)
|
308
|
+
# 1 12 8 . 0 (16)
|
309
|
+
# > lower <=> higher
|
310
|
+
# -1
|
311
|
+
# > lower <=> 123
|
312
|
+
# 0
|
313
|
+
# > lower <=> "123"
|
314
|
+
# 0
|
315
|
+
# > higher <=> lower
|
316
|
+
# 1
|
164
317
|
#
|
318
|
+
# @return [Fixnum] Returns -1 for less than, 0 for equal or 1 for more than.
|
165
319
|
def <=>(other)
|
166
320
|
to_f <=> other.to_f
|
167
321
|
end
|
@@ -181,22 +335,42 @@ module Radix
|
|
181
335
|
# digits[-2,2] == [DOT, 0]
|
182
336
|
#end
|
183
337
|
|
338
|
+
##
|
339
|
+
# Create a new Radix::Float from value in Base-10.
|
340
|
+
#
|
341
|
+
# @param [Numeric, Array, String] other
|
342
|
+
# The value of the new integer in base-10.
|
184
343
|
#
|
185
|
-
|
186
|
-
|
344
|
+
# @return [Array<Radix::Float>] An array of the new Float object and self.
|
345
|
+
def coerce(other)
|
346
|
+
[Radix::Float.new(other), self]
|
187
347
|
end
|
188
348
|
|
189
349
|
private
|
190
350
|
|
191
|
-
|
351
|
+
##
|
352
|
+
# Perform passed arithmetic operation.
|
353
|
+
#
|
354
|
+
# @param [#to_f] other
|
355
|
+
#
|
356
|
+
# @return [Radix::Float] Result of binary operation in @base.
|
192
357
|
def operation(op, other)
|
193
358
|
a = self.to_f
|
194
359
|
b = other.to_f
|
195
360
|
x = a.__send__(op, b)
|
196
361
|
Radix::Float.new(x, base)
|
362
|
+
|
197
363
|
end
|
198
364
|
|
365
|
+
##
|
366
|
+
# Returns two arrays. The integer part and the fractional part of the Float
|
367
|
+
# value in param base.
|
368
|
+
#
|
369
|
+
# @param [Float] value Float's decimal value.
|
370
|
+
# @param [Fixnum] base The base level of Float instance.
|
371
|
+
# @param [Fixnum] prec The # of places to extend F-part.
|
199
372
|
#
|
373
|
+
# @return [Array<(Array[Fixnum], Array[Fixnum])>]
|
200
374
|
def base_conversion(value, base, prec=10)
|
201
375
|
#if value < 0
|
202
376
|
# @negative, value = true, value.abs
|
@@ -228,7 +402,14 @@ module Radix
|
|
228
402
|
[a.reverse, b]
|
229
403
|
end
|
230
404
|
|
405
|
+
##
|
406
|
+
# Convert array of values of a different base to decimal as called by
|
407
|
+
# parse_array.
|
231
408
|
#
|
409
|
+
# @param [Array<Numeric, String>] digits Representation of Base values.
|
410
|
+
# @param [Fixnum, Array<String>] base The base to convert from.
|
411
|
+
#
|
412
|
+
# @return [Float] The digits of base converted to decimal.
|
232
413
|
def decimal(digits, base)
|
233
414
|
i, f = split_digits(digits)
|
234
415
|
e = i.size - 1
|
@@ -240,7 +421,13 @@ module Radix
|
|
240
421
|
v
|
241
422
|
end
|
242
423
|
|
424
|
+
##
|
425
|
+
# Returns the I-Part and F-Part of the passed value as arrays of fixnums.
|
426
|
+
#
|
427
|
+
# @param [Array<Numeric, String>] value
|
428
|
+
# The array of decimal values per column of @base.
|
243
429
|
#
|
430
|
+
# @return [Array<(Array<Fixnum>, Array<Fixnum>)>]
|
244
431
|
def split_digits(value)
|
245
432
|
if d = value.index(DOT) || value.index('.')
|
246
433
|
i, f = value[0...d], value[d+1..-1]
|
@@ -252,7 +439,12 @@ module Radix
|
|
252
439
|
return i, f
|
253
440
|
end
|
254
441
|
|
442
|
+
##
|
443
|
+
# Returns an array of Integer and Float portions of the Radix::Float
|
444
|
+
#
|
445
|
+
# @param [Radix::Float] float Float value to split
|
255
446
|
#
|
447
|
+
# @return [Array<(Integer, Float)>]
|
256
448
|
def split_float(float)
|
257
449
|
i, f = float.to_s.split('.')
|
258
450
|
return i.to_i, ('0.'+f).to_f
|
data/lib/radix/integer.rb
CHANGED
@@ -2,27 +2,64 @@ require 'radix/numeric'
|
|
2
2
|
|
3
3
|
module Radix
|
4
4
|
|
5
|
+
##
|
6
|
+
# Advanced integer class for Radix conversions and mathematical operations
|
7
|
+
# with other bases.
|
5
8
|
#
|
9
|
+
# @!attribute [r] value
|
10
|
+
# @return [Fixnum] Integer's decimal value.
|
11
|
+
# @!attribute [r] base
|
12
|
+
# @return [Fixnum] The base level of Integer instance.
|
13
|
+
# @!attribute [r] code
|
14
|
+
# @return [Array<String>, nil] Substitution chars or nil if default.
|
6
15
|
class Integer < Numeric
|
7
16
|
|
8
|
-
|
17
|
+
##
|
18
|
+
# Stores the numeric value as normal number.
|
19
|
+
#
|
20
|
+
# @return [Fixnum] Integer's decimal value.
|
9
21
|
attr :value
|
10
22
|
|
23
|
+
##
|
11
24
|
# Base of the number.
|
25
|
+
#
|
26
|
+
# @return [Fixnum] The base level of Integer instance.
|
12
27
|
attr :base
|
13
28
|
|
29
|
+
##
|
14
30
|
# Base encoding table.
|
31
|
+
#
|
32
|
+
# @return [Array<String>, nil] Substitution chars or nil if default.
|
15
33
|
attr :code
|
16
34
|
|
17
35
|
private
|
18
36
|
|
37
|
+
##
|
38
|
+
# Starts a new instance of the Radix::Integer class
|
19
39
|
#
|
40
|
+
# @param [Radix::Numeric, Numeric, Array, String] value
|
41
|
+
# The value of the new integer in context of base.
|
42
|
+
#
|
43
|
+
# @param [Fixnum, Array<String>] base The base context in which value is
|
44
|
+
# determined. Can be an array of characters to use in place of default.
|
45
|
+
#
|
46
|
+
# @return [void]
|
20
47
|
def initialize(value, base=10)
|
21
48
|
@value = parse_value(value, base)
|
22
49
|
@base, @code = parse_base(base)
|
23
50
|
end
|
24
51
|
|
52
|
+
##
|
53
|
+
# Takes a Radix::Numeric, String or array and returns the decimal value for
|
54
|
+
# storage in @value.
|
55
|
+
#
|
56
|
+
# @param [Radix::Numeric, Numeric, String, Array<Numeric, String>] value
|
57
|
+
# The value of the integer in base context.
|
25
58
|
#
|
59
|
+
# @param [Fixnum, Array<String>] base
|
60
|
+
# The context base of value.
|
61
|
+
#
|
62
|
+
# @return [Fixnum] Decimal value of Integer.
|
26
63
|
def parse_value(value, base)
|
27
64
|
case value
|
28
65
|
when Integer, Float # Radix
|
@@ -36,11 +73,18 @@ module Radix
|
|
36
73
|
end
|
37
74
|
end
|
38
75
|
|
76
|
+
##
|
39
77
|
# Take an Array in the form of [..., d2, d1, d0] and convert it to
|
40
78
|
# base ten, and store in @value.
|
41
79
|
#
|
42
|
-
# If a float style array is passed in for +value+, e.g. [9, '.', 5],
|
43
|
-
#
|
80
|
+
# @note If a float style array is passed in for +value+, e.g. [9, '.', 5],
|
81
|
+
# the fractional part will simply be truncated.
|
82
|
+
#
|
83
|
+
# @param [Array<String, Numeric>] value Given value.
|
84
|
+
#
|
85
|
+
# @param [Fixnum, Array<String>] base Desired base.
|
86
|
+
#
|
87
|
+
# @return [Fixnum] Decimal version of array value in base context.
|
44
88
|
def parse_array(value, base)
|
45
89
|
if i = value.index(DOT)
|
46
90
|
value = [0...i]
|
@@ -52,20 +96,32 @@ module Radix
|
|
52
96
|
|
53
97
|
public
|
54
98
|
|
55
|
-
|
99
|
+
|
100
|
+
##
|
101
|
+
# Makes this Radix::Integer a ruby integer.
|
102
|
+
#
|
103
|
+
# @return [Fixnum] Base(10) value.
|
56
104
|
def to_i
|
57
105
|
value.to_i #(sign + convert(10).digits.join('')).to_i
|
58
106
|
end
|
59
107
|
|
60
|
-
|
61
|
-
alias_method :to_int, :to_i
|
108
|
+
alias :to_int :to_i
|
62
109
|
|
63
|
-
|
110
|
+
##
|
111
|
+
# Makes this Radix::Integer a ruby float.
|
112
|
+
#
|
113
|
+
# @return [Float] Base(10) value as float.
|
64
114
|
def to_f
|
65
115
|
value.to_f #(sign + convert(10).digits.join('')).to_f
|
66
116
|
end
|
67
117
|
|
118
|
+
##
|
119
|
+
# Makes this Radix::Integer an array using code if defined. Returns an
|
120
|
+
# array using default chars otherwise.
|
121
|
+
#
|
122
|
+
# @param [Fixnum] base Desired base.
|
68
123
|
#
|
124
|
+
# @return [Array<Fixnum, String>] Current base encoded array.
|
69
125
|
def to_a(base=nil)
|
70
126
|
if base
|
71
127
|
convert(base).digits_encoded
|
@@ -74,7 +130,18 @@ module Radix
|
|
74
130
|
end
|
75
131
|
end
|
76
132
|
|
133
|
+
##
|
134
|
+
# Creates an encoded string in desired base, with desired digit divider.
|
135
|
+
#
|
136
|
+
# @note For base 10 or less does not use a divider unless specified.
|
137
|
+
#
|
138
|
+
# @param [Fixnum, Array<String>] base
|
139
|
+
# Desired base.
|
140
|
+
#
|
141
|
+
# @param [String] divider
|
142
|
+
# Desired divider character(s).
|
77
143
|
#
|
144
|
+
# @return [String] Encoded string with specified divider.
|
78
145
|
def to_s(base=nil, divider=nil)
|
79
146
|
divider = divider.to_s if divider
|
80
147
|
if base
|
@@ -92,87 +159,177 @@ module Radix
|
|
92
159
|
end
|
93
160
|
end
|
94
161
|
|
162
|
+
##
|
163
|
+
# Creates a string representation of self.
|
95
164
|
#
|
165
|
+
# @return [String] String rep of self.digits and @base.
|
96
166
|
def inspect
|
97
167
|
"#{digits.join(' ')} (#{base})"
|
98
168
|
end
|
99
169
|
|
170
|
+
##
|
171
|
+
# Returns an array representation of each column's value in decimal chars.
|
100
172
|
#
|
173
|
+
# @return [Array<String, Fixnum>] Values per column of @base as array.
|
174
|
+
# Prepended with "-" if negative.
|
101
175
|
def digits
|
102
176
|
i = base_conversion(value, base)
|
103
177
|
i.unshift('-') if negative?
|
104
178
|
i
|
105
179
|
end
|
106
180
|
|
181
|
+
##
|
182
|
+
# Returns the encoded version of digits.
|
107
183
|
#
|
184
|
+
# @return [Array<String>] The encoded digits. Or digits if @code exists.
|
108
185
|
def digits_encoded
|
109
186
|
base_encode(digits)
|
110
187
|
end
|
111
188
|
|
189
|
+
##
|
112
190
|
# Returns true if the number is negative.
|
191
|
+
#
|
192
|
+
# @return [Boolean] True if negative.
|
113
193
|
def negative?
|
114
194
|
value < 0
|
115
195
|
end
|
116
196
|
|
197
|
+
##
|
198
|
+
# Converts Integer to a new base.
|
199
|
+
#
|
200
|
+
# @param [Fixnum, Array<String>] base
|
201
|
+
# The base context in which value is determined. Can be an array
|
202
|
+
# of characters to use in place of default.
|
117
203
|
#
|
204
|
+
# @return [Radix:Integer] New Integer of same value, different base.
|
118
205
|
def convert(base)
|
119
206
|
self.class.new(value, base)
|
120
207
|
#new_digits = Radix::Base.convert_base(digits, base, new_base)
|
121
208
|
#self.class.new(new_digits, new_base)
|
122
209
|
end
|
123
210
|
|
124
|
-
|
211
|
+
##
|
212
|
+
# Addition binary operation.
|
213
|
+
#
|
214
|
+
# @param [#to_i] other
|
215
|
+
#
|
216
|
+
# @example Which operand determines the base?
|
217
|
+
# > i = Radix::Integer.new(123,16)
|
218
|
+
# 7 11 (16)
|
219
|
+
# > i2 = Radix::Integer.new(456,10)
|
220
|
+
# 4 5 6 (10)
|
221
|
+
# > i + i2 # i is base 16 and is first operand
|
222
|
+
# 2 4 3 (16) # so base of return is 16
|
223
|
+
# > i2 + i # i2 is base 10 and is first operand
|
224
|
+
# 5 7 9 (10) # so base of return is 10
|
225
|
+
#
|
226
|
+
# @return [Radix::Integer] Result of arithmetic operation.
|
125
227
|
def +(other)
|
126
228
|
operation(:+, other)
|
127
229
|
end
|
128
230
|
|
129
|
-
|
231
|
+
##
|
232
|
+
# Subtraction binary operation.
|
233
|
+
#
|
234
|
+
# @param [#to_i] other
|
235
|
+
#
|
236
|
+
# @return [Radix::Integer] Result of arithmetic operation.
|
130
237
|
def -(other)
|
131
238
|
operation(:-, other)
|
132
239
|
end
|
133
240
|
|
134
|
-
|
241
|
+
##
|
242
|
+
# Multiplication binary operation.
|
243
|
+
#
|
244
|
+
# @param [#to_i] other
|
245
|
+
#
|
246
|
+
# @return [Radix::Integer] Result of arithmetic operation.
|
135
247
|
def *(other)
|
136
248
|
operation(:*, other)
|
137
249
|
end
|
138
250
|
|
139
|
-
|
251
|
+
##
|
252
|
+
# Division binary operation.
|
253
|
+
#
|
254
|
+
# @param [#to_i] other
|
255
|
+
#
|
256
|
+
# @return [Radix::Integer] Result of arithmetic operation.
|
140
257
|
def /(other)
|
141
258
|
operation(:/, other)
|
142
259
|
end
|
143
260
|
|
144
|
-
|
261
|
+
##
|
262
|
+
# Power, exponentional operation.
|
263
|
+
#
|
264
|
+
# @param [#to_i] other
|
265
|
+
# The exponent by which to raise Integer.
|
266
|
+
#
|
267
|
+
# @return [Radix::Integer] Result of exponential operation.
|
145
268
|
def **(other)
|
146
269
|
operation(:**, other)
|
147
270
|
end
|
148
271
|
|
149
|
-
|
272
|
+
##
|
273
|
+
# Modulo binary operation.
|
274
|
+
#
|
275
|
+
# @param [#to_i] other
|
276
|
+
#
|
277
|
+
# @return [Radix::Integer] Modulo result of division operation.
|
150
278
|
def %(other)
|
151
279
|
operation(:%, other)
|
152
280
|
end
|
153
281
|
|
154
|
-
|
155
|
-
|
156
|
-
|
282
|
+
##
|
283
|
+
# Leftwise bit shift operator.
|
284
|
+
#
|
285
|
+
# @note Negative numbers will shift rightward. This will truncate bytes
|
286
|
+
# that get carried past zero.
|
287
|
+
#
|
288
|
+
# @param [#to_int] integer
|
289
|
+
# The number of places to shift the bits of self.
|
290
|
+
#
|
291
|
+
# @return [Radix::Integer] The new Radix::Integer with shifted bits.
|
292
|
+
def <<(integer)
|
293
|
+
Radix::Integer.new(to_int << integer.to_int, base)
|
157
294
|
end
|
158
295
|
|
159
|
-
|
160
|
-
|
161
|
-
|
296
|
+
##
|
297
|
+
# AND bitwise operator
|
298
|
+
#
|
299
|
+
# @param [#to_int] integer
|
300
|
+
#
|
301
|
+
# @return [Radix::Integer] The logical AND.
|
302
|
+
def &(integer)
|
303
|
+
Radix::Integer.new(to_int & integer.to_int, base)
|
162
304
|
end
|
163
305
|
|
306
|
+
##
|
307
|
+
# Returns the absolute value of self in @base.
|
164
308
|
#
|
309
|
+
# @return [Radix::Integer] Absolute of @value.
|
165
310
|
def abs
|
166
311
|
self.class.new(value.abs, base)
|
167
312
|
end
|
168
313
|
|
314
|
+
##
|
169
315
|
# Strict equality requires same class as well as value.
|
316
|
+
#
|
317
|
+
# @param [Object] num
|
318
|
+
# Object to compare.
|
319
|
+
#
|
320
|
+
# @return [Boolean] True if class and value are equal.
|
170
321
|
def eql?(num)
|
171
322
|
self.class.equal?(num.class) && self == num
|
172
323
|
end
|
173
324
|
|
325
|
+
##
|
174
326
|
# Simple equality requires equal values only.
|
175
|
-
#
|
327
|
+
# @todo Handle Float and Radix::Float.
|
328
|
+
#
|
329
|
+
# @param [#value] other
|
330
|
+
# Any object that responds to value.
|
331
|
+
#
|
332
|
+
# @return [Boolean] True if values are equal.
|
176
333
|
def ==(other)
|
177
334
|
case other
|
178
335
|
when Float, Integer # Radix
|
@@ -182,19 +339,55 @@ module Radix
|
|
182
339
|
end
|
183
340
|
end
|
184
341
|
|
342
|
+
##
|
343
|
+
# Comparitive binary operation. Very useful for sorting methods.
|
344
|
+
#
|
345
|
+
# @param [#to_f] other The object to compare value against.
|
346
|
+
#
|
347
|
+
# @example Comparison testing
|
348
|
+
# > lower = Radix::Integer.new(123,10)
|
349
|
+
# 1 2 3 (10)
|
350
|
+
# > higher = Radix::Integer.new(456, 16)
|
351
|
+
# 1 12 8 (16)
|
352
|
+
# > lower <=> higher
|
353
|
+
# -1
|
354
|
+
# > lower <=> 123
|
355
|
+
# 0
|
356
|
+
# > higher <=> lower
|
357
|
+
# 1
|
185
358
|
#
|
359
|
+
# @return [Fixnum] Returns -1 for less than, 0 for equal or 1 for more than.
|
186
360
|
def <=>(other)
|
187
361
|
value <=> other.to_f # to_num
|
188
362
|
end
|
189
363
|
|
364
|
+
##
|
365
|
+
# Create a new Radix::Integer from value in Base-10
|
190
366
|
#
|
367
|
+
# @return [Array<Radix::Integer>] An array of the new Integer object and
|
368
|
+
# self.
|
191
369
|
def coerce(value)
|
192
370
|
[Radix::Integer.new(value), self]
|
193
371
|
end
|
194
372
|
|
195
373
|
private
|
196
374
|
|
197
|
-
|
375
|
+
##
|
376
|
+
# Perform passed arithmetic operation.
|
377
|
+
#
|
378
|
+
# @param [#to_i] other
|
379
|
+
#
|
380
|
+
# @example Which operand determines the base?
|
381
|
+
# > i = Radix::Integer.new(123,16)
|
382
|
+
# 7 11 (16)
|
383
|
+
# > i2 = Radix::Integer.new(456,10)
|
384
|
+
# 4 5 6 (10)
|
385
|
+
# > i + i2 # i is base 16 and is first operand
|
386
|
+
# 2 4 3 (16) # so base of return is 16
|
387
|
+
# > i2 + i # i2 is base 10 and is first operand
|
388
|
+
# 5 7 9 (10) # so base of return is 10
|
389
|
+
#
|
390
|
+
# @return [Radix::Integer] Result of binary operation in @base.
|
198
391
|
def operation(op, other)
|
199
392
|
a = self.to_i
|
200
393
|
b = other.to_i
|
@@ -202,7 +395,14 @@ module Radix
|
|
202
395
|
self.class.new(x, base)
|
203
396
|
end
|
204
397
|
|
398
|
+
##
|
399
|
+
# Returns the value as an array of decimal values where each column is a
|
400
|
+
# place of @base.
|
401
|
+
#
|
402
|
+
# @param (see #Radix::Integer.value)
|
403
|
+
# @param (see #Radix::Integer.base)
|
205
404
|
#
|
405
|
+
# @return [Array<Fixnum>]
|
206
406
|
def base_conversion(value, base)
|
207
407
|
#if value < 0
|
208
408
|
# @negative, value = true, value.abs
|