radix 2.0.0 → 2.0.1
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/.ruby +46 -0
- data/.yardopts +5 -0
- data/HISTORY.rdoc +14 -1
- data/NOTICE.rdoc +31 -0
- data/README.rdoc +24 -19
- data/lib/radix.rb +18 -1
- data/lib/radix.yml +46 -0
- data/lib/radix/rational.rb +2 -2
- metadata +52 -92
- data/LICENSE +0 -205
- data/doc/01_synopsis.rdoc +0 -60
- data/doc/02_integer.rdoc +0 -48
- data/doc/03_float.rdoc +0 -36
- data/doc/04_rational.rdoc +0 -27
- data/doc/05_base.rdoc +0 -82
- data/doc/applique/ae.rb +0 -1
- data/doc/applique/qed.rb +0 -1
- data/lib/radix/meta/data.rb +0 -29
- data/lib/radix/meta/package +0 -8
- data/lib/radix/meta/profile +0 -21
- data/meta/data.rb +0 -29
- data/meta/package +0 -8
- data/meta/profile +0 -21
- data/test/02_integer.rdoc +0 -256
- data/test/03_float.rdoc +0 -294
- data/test/04_rational.rdoc +0 -84
- data/test/05_base.rdoc +0 -78
- data/test/applique/ae.rb +0 -1
- data/test/applique/qed.rb +0 -1
data/doc/applique/ae.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'ae/should'
|
data/doc/applique/qed.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'qed/extensions/check'
|
data/lib/radix/meta/data.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
Object.__send__(:remove_const, :VERSION) if Object.const_defined?(:VERSION) # becuase Ruby 1.8~ gets in the way
|
2
|
-
|
3
|
-
module Radix
|
4
|
-
|
5
|
-
def self.__DIR__
|
6
|
-
File.dirname(__FILE__)
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.package
|
10
|
-
@package ||= (
|
11
|
-
require 'yaml'
|
12
|
-
YAML.load(File.new(__DIR__ + '/package'))
|
13
|
-
)
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.profile
|
17
|
-
@profile ||= (
|
18
|
-
require 'yaml'
|
19
|
-
YAML.load(File.new(__DIR__ + '/profile'))
|
20
|
-
)
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.const_missing(name)
|
24
|
-
key = name.to_s.downcase
|
25
|
-
package[key] || profile[key] || super(name)
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
|
data/lib/radix/meta/package
DELETED
data/lib/radix/meta/profile
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title : Radix
|
3
|
-
suite : rubyworks
|
4
|
-
summary: Convert to and from any base.
|
5
|
-
contact: trans <transfire@gmail.com>
|
6
|
-
license: Apache 2.0
|
7
|
-
authors: Thomas Sawyer
|
8
|
-
created: 2009-07-01
|
9
|
-
|
10
|
-
description:
|
11
|
-
Convert to and from any base.
|
12
|
-
|
13
|
-
resources:
|
14
|
-
home: http://rubyworks.github.com/radix
|
15
|
-
code: http://github.com/rubyworks/radix
|
16
|
-
mail: http://groups.google.com/group/rubyworks-mailinglist
|
17
|
-
repo: git://github.com/rubyworks/radix.git
|
18
|
-
|
19
|
-
copyright:
|
20
|
-
Copyright (c) 2009 Thomas Sawyer
|
21
|
-
|
data/meta/data.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
Object.__send__(:remove_const, :VERSION) if Object.const_defined?(:VERSION) # becuase Ruby 1.8~ gets in the way
|
2
|
-
|
3
|
-
module Radix
|
4
|
-
|
5
|
-
def self.__DIR__
|
6
|
-
File.dirname(__FILE__)
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.package
|
10
|
-
@package ||= (
|
11
|
-
require 'yaml'
|
12
|
-
YAML.load(File.new(__DIR__ + '/package'))
|
13
|
-
)
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.profile
|
17
|
-
@profile ||= (
|
18
|
-
require 'yaml'
|
19
|
-
YAML.load(File.new(__DIR__ + '/profile'))
|
20
|
-
)
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.const_missing(name)
|
24
|
-
key = name.to_s.downcase
|
25
|
-
package[key] || profile[key] || super(name)
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
|
data/meta/package
DELETED
data/meta/profile
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
---
|
2
|
-
title : Radix
|
3
|
-
suite : rubyworks
|
4
|
-
summary: Convert to and from any base.
|
5
|
-
contact: trans <transfire@gmail.com>
|
6
|
-
license: Apache 2.0
|
7
|
-
authors: Thomas Sawyer
|
8
|
-
created: 2009-07-01
|
9
|
-
|
10
|
-
description:
|
11
|
-
Convert to and from any base.
|
12
|
-
|
13
|
-
resources:
|
14
|
-
home: http://rubyworks.github.com/radix
|
15
|
-
code: http://github.com/rubyworks/radix
|
16
|
-
mail: http://groups.google.com/group/rubyworks-mailinglist
|
17
|
-
repo: git://github.com/rubyworks/radix.git
|
18
|
-
|
19
|
-
copyright:
|
20
|
-
Copyright (c) 2009 Thomas Sawyer
|
21
|
-
|
data/test/02_integer.rdoc
DELETED
@@ -1,256 +0,0 @@
|
|
1
|
-
= Radix Integer
|
2
|
-
|
3
|
-
Radix provides an Integer class for working with integers in various bases.
|
4
|
-
|
5
|
-
require 'radix'
|
6
|
-
|
7
|
-
== Initialization
|
8
|
-
|
9
|
-
Radix::Integer's initializer can accept either an Integer, String or
|
10
|
-
Array as a value and an integer base.
|
11
|
-
|
12
|
-
Give an integer value, it will automatically be converted to the base
|
13
|
-
specified.
|
14
|
-
|
15
|
-
check do |integer, base, digits|
|
16
|
-
r = Radix::Integer.new(integer, base)
|
17
|
-
r.digits.assert == digits
|
18
|
-
end
|
19
|
-
|
20
|
-
ok 8, 2, [1,0,0,0]
|
21
|
-
ok 4, 2, [1,0,0]
|
22
|
-
ok 8, 10, [8]
|
23
|
-
ok 10, 10, [1, 0]
|
24
|
-
ok 8, 16, [8]
|
25
|
-
ok 16, 16, [1, 0]
|
26
|
-
|
27
|
-
Where as a String value is taken to already be in the base given.
|
28
|
-
|
29
|
-
ok "1000", 2, [1,0,0,0]
|
30
|
-
ok "100", 2, [1,0,0]
|
31
|
-
|
32
|
-
ok "8", 10, [8]
|
33
|
-
ok "10", 10, [1, 0]
|
34
|
-
ok "8", 16, [8]
|
35
|
-
ok "10", 16, [1, 0]
|
36
|
-
|
37
|
-
And an Array is also taken to be in the base given.
|
38
|
-
|
39
|
-
ok %w[1 0 0 0], 2, [1,0,0,0]
|
40
|
-
ok %w[ 1 0 0], 2, [1,0,0]
|
41
|
-
|
42
|
-
ok %w[ 8], 10, [8]
|
43
|
-
ok %w[1 0], 10, [1, 0]
|
44
|
-
ok %w[ 8], 16, [8]
|
45
|
-
ok %w[1 0], 16, [1, 0]
|
46
|
-
|
47
|
-
Integers can also be negative, rather than positive. In each case
|
48
|
-
just prepend the value with a minus sign.
|
49
|
-
|
50
|
-
check do |integer, base, digits|
|
51
|
-
r = Radix::Integer.new(integer, base)
|
52
|
-
r.digits.assert == digits
|
53
|
-
r.assert.negative?
|
54
|
-
end
|
55
|
-
|
56
|
-
ok -8, 2, ['-',1,0,0,0]
|
57
|
-
ok "-1000", 2, ['-',1,0,0,0]
|
58
|
-
ok %w[- 1 0 0 0], 2, ['-',1,0,0,0]
|
59
|
-
|
60
|
-
If a value has a digit outside of the range of the base an ArgumentError
|
61
|
-
will be raised.
|
62
|
-
|
63
|
-
expect ArgumentError do
|
64
|
-
Radix::Integer.new('9', 2)
|
65
|
-
end
|
66
|
-
|
67
|
-
Radix provides a convenience extension method to Integer, String and Array
|
68
|
-
called #b, to more easily initialize a Radix numeric object. The method simply
|
69
|
-
passes the receiver on to `Radix::Integer#new`.
|
70
|
-
|
71
|
-
check do |integer, base, digits|
|
72
|
-
r = integer.b(base)
|
73
|
-
r.assert.is_a?(Radix::Integer)
|
74
|
-
r.digits.assert == digits
|
75
|
-
end
|
76
|
-
|
77
|
-
ok 8, 2, [1,0,0,0]
|
78
|
-
ok 4, 2, [1,0,0]
|
79
|
-
|
80
|
-
ok "1000", 2, [1,0,0,0]
|
81
|
-
ok "100", 2, [1,0,0]
|
82
|
-
|
83
|
-
ok %w"1 0 0 0", 2, [1,0,0,0]
|
84
|
-
ok %w"1 0 0", 2, [1,0,0]
|
85
|
-
|
86
|
-
== Conversion
|
87
|
-
|
88
|
-
Radix integers can ve converted to other bases with the #convert method.
|
89
|
-
|
90
|
-
b = "1000".b(2)
|
91
|
-
d = b.convert(10)
|
92
|
-
d.digits.assert == [8]
|
93
|
-
|
94
|
-
We can convert a Radix::Integer to a regular base-10 Integer with the #to_i
|
95
|
-
method.
|
96
|
-
|
97
|
-
b = "1000".b(2)
|
98
|
-
d = b.to_i
|
99
|
-
d.assert == 8
|
100
|
-
|
101
|
-
== Equality
|
102
|
-
|
103
|
-
Radix extend the Integer, String and Array classes with the #b method
|
104
|
-
which simplifies the creation of Radix::Integer instances. The following
|
105
|
-
return the equivalent instance of Radix::Integer.
|
106
|
-
|
107
|
-
a = 8.b(2)
|
108
|
-
b = "1000".b(2)
|
109
|
-
c = [1, 0, 0, 0].b(2)
|
110
|
-
|
111
|
-
a.assert == b
|
112
|
-
b.assert == c
|
113
|
-
c.assert == a
|
114
|
-
|
115
|
-
a.assert == 8
|
116
|
-
b.assert == 8
|
117
|
-
c.assert == 8
|
118
|
-
|
119
|
-
More stringent equality can be had from #eql?, in which the other integer
|
120
|
-
must be a Radix::Integer too.
|
121
|
-
|
122
|
-
a.assert.eql?(b)
|
123
|
-
a.refute.eql?(8)
|
124
|
-
|
125
|
-
=== Operations
|
126
|
-
|
127
|
-
Radix::Integer supports all the usual mathematical operators.
|
128
|
-
|
129
|
-
=== Addition
|
130
|
-
|
131
|
-
check do |a, b, x|
|
132
|
-
(a + b).assert == x
|
133
|
-
end
|
134
|
-
|
135
|
-
ok "1000".b(2), "0010".b(2), "1010".b(2)
|
136
|
-
ok "1000".b(2), "2".b(8), "1010".b(2)
|
137
|
-
ok "1000".b(2), "2".b(8), "10".b(10)
|
138
|
-
|
139
|
-
A more complex example.
|
140
|
-
|
141
|
-
x = "AZ42".b(62) + "54".b(10)
|
142
|
-
x.assert == "2518124".b(10)
|
143
|
-
x.assert == 2518124
|
144
|
-
|
145
|
-
Adding negative integers will, of course, be akin to subtraction.
|
146
|
-
|
147
|
-
ok "1000".b(2), "-0010".b(2), "110".b(2)
|
148
|
-
ok "1000".b(2), "-2".b(8), "110".b(2)
|
149
|
-
ok "1000".b(2), "-2".b(8), "6".b(10)
|
150
|
-
|
151
|
-
ok "-1000".b(2), "0010".b(2), "-110".b(2)
|
152
|
-
ok "-1000".b(2), "2".b(8), "-110".b(2)
|
153
|
-
ok "-1000".b(2), "2".b(8), "-6".b(10)
|
154
|
-
|
155
|
-
ok "-1000".b(2), "-0010".b(2), "-1010".b(2)
|
156
|
-
ok "-1000".b(2), "-2".b(8), "-1010".b(2)
|
157
|
-
ok "-1000".b(2), "-2".b(8), "-10".b(10)
|
158
|
-
|
159
|
-
=== Subtraction
|
160
|
-
|
161
|
-
check do |a, b, x|
|
162
|
-
(a - b).assert == x
|
163
|
-
end
|
164
|
-
|
165
|
-
ok "1000".b(2), "10".b(2), "0110".b(2)
|
166
|
-
ok "1000".b(2), "2".b(8), "0110".b(2)
|
167
|
-
ok "1000".b(2), "2".b(8), "6".b(8)
|
168
|
-
ok "1000".b(2), "2".b(8), "6".b(10)
|
169
|
-
|
170
|
-
A more complex example.
|
171
|
-
|
172
|
-
x = "AZ42".b(62) - "54".b(10)
|
173
|
-
x.assert == "2518016".b(10)
|
174
|
-
x.assert == 2518016
|
175
|
-
|
176
|
-
=== Multiplication
|
177
|
-
|
178
|
-
check do |a, b, x|
|
179
|
-
(a * b).assert == x
|
180
|
-
end
|
181
|
-
|
182
|
-
ok "1000".b(2), "10".b(2), "10000".b(2)
|
183
|
-
ok "1000".b(2), "2".b(8), "10000".b(2)
|
184
|
-
ok "1000".b(2), "2".b(8), "20".b(8)
|
185
|
-
ok "1000".b(2), "2".b(8), "16".b(10)
|
186
|
-
|
187
|
-
A more complex example.
|
188
|
-
|
189
|
-
x = "Z42".b(62) * "4".b(10)
|
190
|
-
x.assert == "539160".b(10)
|
191
|
-
x.assert == 539160
|
192
|
-
|
193
|
-
=== Division
|
194
|
-
|
195
|
-
check do |a, b, x|
|
196
|
-
(a / b).assert == x
|
197
|
-
end
|
198
|
-
|
199
|
-
ok "1000".b(2), "10".b(2), "100".b(2)
|
200
|
-
ok "1000".b(2), "2".b(8), "100".b(2)
|
201
|
-
ok "1000".b(2), "2".b(8), "4".b(8)
|
202
|
-
ok "1000".b(2), "2".b(8), "4".b(10)
|
203
|
-
|
204
|
-
A more complex example.
|
205
|
-
|
206
|
-
x = "AZ42".b(62) / "54".b(10)
|
207
|
-
x.assert == "46630".b(10)
|
208
|
-
x.assert == 46630
|
209
|
-
|
210
|
-
=== Power
|
211
|
-
|
212
|
-
check do |a, b, x|
|
213
|
-
(a ** b).assert == x
|
214
|
-
end
|
215
|
-
|
216
|
-
ok "1000".b(2), "10".b(2), 64
|
217
|
-
|
218
|
-
=== Modulo
|
219
|
-
|
220
|
-
check do |a, b, x|
|
221
|
-
(a % b).assert == x
|
222
|
-
end
|
223
|
-
|
224
|
-
ok "1000".b(2), "10".b(2), 0
|
225
|
-
ok "1000".b(2), "11".b(2), 2
|
226
|
-
|
227
|
-
=== Bitwise Shift
|
228
|
-
|
229
|
-
check do |a, b, x|
|
230
|
-
(a << b).assert == x
|
231
|
-
end
|
232
|
-
|
233
|
-
ok "10".b(2), "10".b(2), "1000".b(2)
|
234
|
-
ok "10".b(2), 2, "1000".b(2)
|
235
|
-
ok "10".b(2), 2, 8
|
236
|
-
|
237
|
-
=== Bitwise AND
|
238
|
-
|
239
|
-
check do |a, b, x|
|
240
|
-
(a & b).assert == x
|
241
|
-
end
|
242
|
-
|
243
|
-
ok "1010".b(2), "10".b(2), "10".b(2)
|
244
|
-
ok "1010".b(2), "2".b(8), "10".b(2)
|
245
|
-
|
246
|
-
== Coerce
|
247
|
-
|
248
|
-
When a Radix::Integer is the operand in an operation against a regular
|
249
|
-
Ruby Integer, the calculation should still work via #coerce.
|
250
|
-
|
251
|
-
check do |a, b, x|
|
252
|
-
(a + b).assert == x
|
253
|
-
end
|
254
|
-
|
255
|
-
ok 10, "10".b(2), "12".b(10)
|
256
|
-
|
data/test/03_float.rdoc
DELETED
@@ -1,294 +0,0 @@
|
|
1
|
-
= Radix Float
|
2
|
-
|
3
|
-
Radix provides a Float class for working with rational numbers in various bases.
|
4
|
-
Actually Radix's implementation of Float is a <i>fixed point</i>, not a
|
5
|
-
<i>floating point</i>.
|
6
|
-
|
7
|
-
require 'radix'
|
8
|
-
|
9
|
-
D = Radix::DOT
|
10
|
-
|
11
|
-
== Initialization
|
12
|
-
|
13
|
-
Radix::Float's initializer can accept either an Integer, Float, String or
|
14
|
-
Array as a value and an integer base.
|
15
|
-
|
16
|
-
Give a float value, it will automatically be converted to the base
|
17
|
-
specified.
|
18
|
-
|
19
|
-
check do |float, base, digits|
|
20
|
-
r = Radix::Float.new(float, base)
|
21
|
-
r.digits.assert == digits
|
22
|
-
end
|
23
|
-
|
24
|
-
ok 8.5, 2, [1,0,0,0,D,1]
|
25
|
-
ok 4.5, 2, [ 1,0,0,D,1]
|
26
|
-
|
27
|
-
ok 8.1, 10, [ 8,D,1]
|
28
|
-
ok 10.2, 10, [1,0,D,2]
|
29
|
-
#ok 8.1, 16, [ 8,D,1]
|
30
|
-
#ok 16.1, 16, [1,0,D,1]
|
31
|
-
|
32
|
-
Give an integer value, it will automatically be converted to the base
|
33
|
-
specified and given a fraction part set to zero.
|
34
|
-
|
35
|
-
check do |float, base, digits|
|
36
|
-
r = Radix::Float.new(float, base)
|
37
|
-
r.digits.assert == digits
|
38
|
-
end
|
39
|
-
|
40
|
-
ok 8, 2, [1,0,0,0,D,0]
|
41
|
-
ok 4, 2, [ 1,0,0,D,0]
|
42
|
-
|
43
|
-
ok 8, 10, [ 8,D,0]
|
44
|
-
ok 10, 10, [1,0,D,0]
|
45
|
-
ok 8, 16, [ 8,D,0]
|
46
|
-
ok 16, 16, [1,0,D,0]
|
47
|
-
|
48
|
-
Given a float, the same will occur.
|
49
|
-
|
50
|
-
ok 8.0, 2, [1,0,0,0,D,0]
|
51
|
-
ok 4.0, 2, [ 1,0,0,D,0]
|
52
|
-
|
53
|
-
ok 8.0, 10, [ 8,D,0]
|
54
|
-
ok 10.0, 10, [1,0,D,0]
|
55
|
-
ok 8.0, 16, [ 8,D,0]
|
56
|
-
ok 16.0, 16, [1,0,D,0]
|
57
|
-
|
58
|
-
Where as a String value is taken to already be in the base given.
|
59
|
-
|
60
|
-
ok "1000", 2, [1,0,0,0,D,0]
|
61
|
-
ok "100", 2, [ 1,0,0,D,0]
|
62
|
-
|
63
|
-
ok "8", 10, [ 8,D,0]
|
64
|
-
ok "10", 10, [1,0,D,0]
|
65
|
-
ok "8", 16, [ 8,D,0]
|
66
|
-
ok "10", 16, [1,0,D,0]
|
67
|
-
|
68
|
-
ok "1000.0", 2, [1,0,0,0,D,0]
|
69
|
-
ok "100.0", 2, [ 1,0,0,D,0]
|
70
|
-
|
71
|
-
ok "8.0", 10, [ 8,D,0]
|
72
|
-
ok "10.0", 10, [1,0,D,0]
|
73
|
-
ok "8.0", 16, [ 8,D,0]
|
74
|
-
ok "10.0", 16, [1,0,D,0]
|
75
|
-
|
76
|
-
And an Array is also taken to be in the base given.
|
77
|
-
|
78
|
-
ok %w[1 0 0 0], 2, [1,0,0,0,D,0]
|
79
|
-
ok %w[ 1 0 0], 2, [ 1,0,0,D,0]
|
80
|
-
|
81
|
-
ok %w[ 8], 10, [ 8,D,0]
|
82
|
-
ok %w[1 0], 10, [1,0,D,0]
|
83
|
-
ok %w[ 8], 16, [ 8,D,0]
|
84
|
-
ok %w[1 0], 16, [1,0,D,0]
|
85
|
-
|
86
|
-
Passing in an Array with a fraction part, either the DOT constant can be used,
|
87
|
-
which is simply the symbol :'.', or the string '.' can be used.
|
88
|
-
|
89
|
-
ok %w[1 0 0 0 . 0], 2, [1,0,0,0,D,0]
|
90
|
-
ok %w[ 1 0 0 . 0], 2, [ 1,0,0,D,0]
|
91
|
-
|
92
|
-
ok %w[ 8 . 0], 10, [ 8,D,0]
|
93
|
-
ok %w[1 0 . 0], 10, [1,0,D,0]
|
94
|
-
ok %w[ 8 . 0], 16, [ 8,D,0]
|
95
|
-
ok %w[1 0 . 0], 16, [1,0,D,0]
|
96
|
-
|
97
|
-
Integers can also be negative, rather than positive. In each case
|
98
|
-
just prepend the value with a minus sign.
|
99
|
-
|
100
|
-
check do |float, base, digits|
|
101
|
-
r = Radix::Float.new(float, base)
|
102
|
-
r.digits.assert = digits
|
103
|
-
r.assert.negative?
|
104
|
-
end
|
105
|
-
|
106
|
-
ok( -8, 2, ['-',1,0,0,0,D,0])
|
107
|
-
ok( "-1000", 2, ['-',1,0,0,0,D,0])
|
108
|
-
ok( %w[- 1 0 0 0], 2, ['-',1,0,0,0,D,0])
|
109
|
-
|
110
|
-
If a value has a digit outside of the range of the base an ArgumentError
|
111
|
-
will be raised.
|
112
|
-
|
113
|
-
expect ArgumentError do
|
114
|
-
Radix::Float.new('9', 2)
|
115
|
-
end
|
116
|
-
|
117
|
-
Radix provides a convenience extension method to Integer, String and Array
|
118
|
-
called #b, to more easily initialize a Radix numeric object. The method simply
|
119
|
-
passes the receiver on to `Radix::Integer#new`.
|
120
|
-
|
121
|
-
check do |float, base, digits|
|
122
|
-
r = float.b(base)
|
123
|
-
r.assert.is_a?(Radix::Float)
|
124
|
-
r.digits.assert = digits
|
125
|
-
end
|
126
|
-
|
127
|
-
ok 8.0, 2, [1,0,0,0,D,0]
|
128
|
-
ok 4.0, 2, [ 1,0,0,D,0]
|
129
|
-
|
130
|
-
ok "1000.0", 2, [1,0,0,0,D,0]
|
131
|
-
ok "100.0", 2, [ 1,0,0,D,0]
|
132
|
-
|
133
|
-
ok %w"1 0 0 0 . 0", 2, [1,0,0,0,D,0]
|
134
|
-
ok %w"1 0 0 . 0", 2, [ 1,0,0,D,0]
|
135
|
-
|
136
|
-
== Conversion
|
137
|
-
|
138
|
-
Radix integers can ve converted to other bases with the #convert method.
|
139
|
-
|
140
|
-
b = "1000.0".b(2)
|
141
|
-
d = b.convert(10)
|
142
|
-
d.digits.assert == [8,D,0]
|
143
|
-
|
144
|
-
We can convert a Radix::Float to a regular base-10 Float with the #to_f
|
145
|
-
method.
|
146
|
-
|
147
|
-
b = "1000.0".b(2)
|
148
|
-
d = b.to_f
|
149
|
-
d.assert == 8.0
|
150
|
-
|
151
|
-
We can convert a Radix::Float to a regular base-10 Integer with the #to_i
|
152
|
-
method.
|
153
|
-
|
154
|
-
b = "1000.0".b(2)
|
155
|
-
d = b.to_i
|
156
|
-
d.assert == 8
|
157
|
-
|
158
|
-
=== Equality
|
159
|
-
|
160
|
-
Radix extend the Integer, String and Array classes with the #b method
|
161
|
-
which simplifies the creation of Radix::Float instances. The following
|
162
|
-
return the equivalent instance of Radix::Float.
|
163
|
-
|
164
|
-
a = 8.0.b(2)
|
165
|
-
b = "1000.0".b(2)
|
166
|
-
c = [1,0,0,0,'.',0].b(2)
|
167
|
-
|
168
|
-
a.assert = b
|
169
|
-
b.assert = c
|
170
|
-
c.assert = a
|
171
|
-
|
172
|
-
a.assert = 8.0
|
173
|
-
b.assert = 8.0
|
174
|
-
c.assert = 8.0
|
175
|
-
|
176
|
-
More stringent equality can be had from #eql?, in which the other integer
|
177
|
-
must be a Radix::Integer too.
|
178
|
-
|
179
|
-
a.assert.eql?(b)
|
180
|
-
a.refute.eql?(8.0)
|
181
|
-
|
182
|
-
== Operations
|
183
|
-
|
184
|
-
Radix::Float supports all the usual mathematical operators.
|
185
|
-
|
186
|
-
=== Addition
|
187
|
-
|
188
|
-
check do |a, b, x|
|
189
|
-
(a + b).assert = x
|
190
|
-
end
|
191
|
-
|
192
|
-
ok "1000.0".b(2), "0010.0".b(2), "1010.0".b(2)
|
193
|
-
ok "1000.0".b(2), "2.0".b(8), "1010.0".b(2)
|
194
|
-
ok "1000.0".b(2), "2.0".b(8), "10.0".b(10)
|
195
|
-
|
196
|
-
A more complex example.
|
197
|
-
|
198
|
-
x = "AZ42.0".b(62) + "54.0".b(10)
|
199
|
-
x.assert == "2518124.0".b(10)
|
200
|
-
x.assert == 2518124.0
|
201
|
-
|
202
|
-
Adding negative integers will, of course, be akin to subtraction.
|
203
|
-
|
204
|
-
ok "1000.0".b(2), "-0010".b(2), "110.0".b(2)
|
205
|
-
ok "1000.0".b(2), "-2".b(8), "110.0".b(2)
|
206
|
-
ok "1000.0".b(2), "-2".b(8), "6.0".b(10)
|
207
|
-
|
208
|
-
ok "-1000.0".b(2), "0010".b(2), "-110.0".b(2)
|
209
|
-
ok "-1000.0".b(2), "2".b(8), "-110.0".b(2)
|
210
|
-
ok "-1000.0".b(2), "2".b(8), "-6.0".b(10)
|
211
|
-
|
212
|
-
ok "-1000.0".b(2), "-0010".b(2), "-1010.0".b(2)
|
213
|
-
ok "-1000.0".b(2), "-2".b(8), "-1010.0".b(2)
|
214
|
-
ok "-1000.0".b(2), "-2".b(8), "-10.0".b(10)
|
215
|
-
|
216
|
-
=== Subtraction
|
217
|
-
|
218
|
-
check do |a, b, x|
|
219
|
-
(a - b).assert == x
|
220
|
-
end
|
221
|
-
|
222
|
-
ok "1000.0".b(2), "10".b(2), "110.0".b(2)
|
223
|
-
ok "1000.0".b(2), "2".b(8), "110.0".b(2)
|
224
|
-
ok "1000.0".b(2), "2".b(8), "6.0".b(8)
|
225
|
-
ok "1000.0".b(2), "2".b(8), "6.0".b(10)
|
226
|
-
|
227
|
-
A more complex example.
|
228
|
-
|
229
|
-
x = "AZ42.0".b(62) - "54".b(10)
|
230
|
-
x.assert == "2518016.0".b(10)
|
231
|
-
x.assert == 2518016.0
|
232
|
-
|
233
|
-
=== Multiplication
|
234
|
-
|
235
|
-
check do |a, b, x|
|
236
|
-
(a * b).assert = x
|
237
|
-
end
|
238
|
-
|
239
|
-
ok "1000.0".b(2), "10".b(2), "10000.0".b(2)
|
240
|
-
ok "1000.0".b(2), "2".b(8), "10000.0".b(2)
|
241
|
-
ok "1000.0".b(2), "2".b(8), "20.0".b(8)
|
242
|
-
ok "1000.0".b(2), "2".b(8), "16.0".b(10)
|
243
|
-
|
244
|
-
A more complex example.
|
245
|
-
|
246
|
-
x = "Z42.0".b(62) * "4.0".b(10)
|
247
|
-
x.assert == "539160.0".b(10)
|
248
|
-
x.assert == 539160.0
|
249
|
-
|
250
|
-
=== Division
|
251
|
-
|
252
|
-
check do |a, b, x|
|
253
|
-
(a / b).assert = x
|
254
|
-
end
|
255
|
-
|
256
|
-
ok "1000.0".b(2), "10".b(2), "100.0".b(2)
|
257
|
-
ok "1000.0".b(2), "2".b(8), "100.0".b(2)
|
258
|
-
ok "1000.0".b(2), "2".b(8), "4.0".b(8)
|
259
|
-
ok "1000.0".b(2), "2".b(8), "4.0".b(10)
|
260
|
-
|
261
|
-
A more complex example.
|
262
|
-
|
263
|
-
x = "AZ40.0".b(62) / "62.0".b(10)
|
264
|
-
x.assert == "40614.0".b(10)
|
265
|
-
x.assert == 40614.0
|
266
|
-
|
267
|
-
=== Power
|
268
|
-
|
269
|
-
check do |a, b, x|
|
270
|
-
(a ** b).assert == x
|
271
|
-
end
|
272
|
-
|
273
|
-
ok "1000.0".b(2), "10.0".b(2), 64.0
|
274
|
-
|
275
|
-
=== Modulo
|
276
|
-
|
277
|
-
check do |a, b, x|
|
278
|
-
(a % b).assert == x
|
279
|
-
end
|
280
|
-
|
281
|
-
ok "1000.0".b(2), "10".b(2), 0
|
282
|
-
ok "1000.0".b(2), "11".b(2), 2
|
283
|
-
|
284
|
-
== Coerce
|
285
|
-
|
286
|
-
When a Radix::Integer is the operand in an operation against a regular
|
287
|
-
Ruby Integer, the calculation should still work via #coerce.
|
288
|
-
|
289
|
-
check do |a, b, x|
|
290
|
-
(a + b).assert == x
|
291
|
-
end
|
292
|
-
|
293
|
-
ok 10.0, "10".b(2), "12".b(10)
|
294
|
-
|