radix 2.0.1 → 2.1.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 → .index} +36 -27
- data/DEMO.md +763 -0
- data/{HISTORY.rdoc → HISTORY.md} +31 -5
- data/LICENSE.txt +23 -0
- data/{README.rdoc → README.md} +27 -34
- data/demo/01_synopsis.md +46 -0
- data/demo/02_integer.md +256 -0
- data/demo/03_float.md +294 -0
- data/demo/04_rational.md +84 -0
- data/demo/05_base.md +78 -0
- data/demo/applique/ae.rb +3 -0
- data/demo/applique/check.rb +7 -0
- data/demo/applique/radix.rb +1 -0
- data/lib/radix.yml +36 -27
- data/lib/radix/float.rb +1 -0
- data/lib/radix/integer.rb +3 -0
- data/lib/radix/rational.rb +43 -8
- metadata +37 -15
- data/NOTICE.rdoc +0 -31
data/{HISTORY.rdoc → HISTORY.md}
RENAMED
@@ -1,6 +1,32 @@
|
|
1
|
-
|
1
|
+
# RELEASE HISTORY
|
2
2
|
|
3
|
-
|
3
|
+
## 2.1.1 / 2013-02-06
|
4
|
+
|
5
|
+
Minor release to fix gemspec. Which, alas, Bunder cannot do without.
|
6
|
+
|
7
|
+
Changes:
|
8
|
+
|
9
|
+
* Update .gemspec file for lastest indexer.
|
10
|
+
|
11
|
+
|
12
|
+
## 2.1.0 / 2013-01-31
|
13
|
+
|
14
|
+
This release fixes two bugs. The first caused base conversions of `0`
|
15
|
+
to return an empty string. Second, the `Float#to_r` extension was
|
16
|
+
using multiplication when it should have been using power.
|
17
|
+
Ruby 1.9 includes `Float#to_r` though, so the use of Radix's *fixed*
|
18
|
+
core extension is conditioned on Ruby 1.8 now. This release also
|
19
|
+
updates the build a bit, primarily converting RDoc documents to
|
20
|
+
Markdown.
|
21
|
+
|
22
|
+
Changes:
|
23
|
+
|
24
|
+
* Fix issue #5, Float#to_r using times instead of power. (simonratner)
|
25
|
+
* Fix issue #4, zero converts to empty string. (bbasata)
|
26
|
+
* Convert RDoc documents to Markdown.
|
27
|
+
|
28
|
+
|
29
|
+
## 2.0.1 / 2011-10-23
|
4
30
|
|
5
31
|
This release is simply an adminstrative release to update the project
|
6
32
|
build configuration. The functionality of the library itself has not
|
@@ -13,7 +39,7 @@ Changes:
|
|
13
39
|
* Switch to BSD-2-Clause license.
|
14
40
|
|
15
41
|
|
16
|
-
|
42
|
+
## 2.0.0 / 2010-10-31
|
17
43
|
|
18
44
|
Radix::Number has been deprecate in favor of three separate classes,
|
19
45
|
Radix::Integer, Radix::Float and Radix::Rational. In addition
|
@@ -27,7 +53,7 @@ Changes:
|
|
27
53
|
* Add Radix::Rational
|
28
54
|
|
29
55
|
|
30
|
-
|
56
|
+
## 1.1.0 / 2010-09-03
|
31
57
|
|
32
58
|
Radix now provides an actual Numeric subclass, Radix::Number, that stores
|
33
59
|
the base and can be used like any other Numeric object. This makes it very
|
@@ -42,7 +68,7 @@ Changes:
|
|
42
68
|
* Rename Radix class to Radix::Base.
|
43
69
|
|
44
70
|
|
45
|
-
|
71
|
+
## 1.0.0 / 2009-07-01
|
46
72
|
|
47
73
|
This is the initial stand-alone release of Radix,
|
48
74
|
ported from the basex.rb library of Ruby Facets.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
BSD-2-Clause License
|
2
|
+
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
4
|
+
modification, are permitted provided that the following conditions are met:
|
5
|
+
|
6
|
+
1. Redistributions of source code must retain the above copyright notice,
|
7
|
+
this list of conditions and the following disclaimer.
|
8
|
+
|
9
|
+
2. Redistributions in binary form must reproduce the above copyright
|
10
|
+
notice, this list of conditions and the following disclaimer in the
|
11
|
+
documentation and/or other materials provided with the distribution.
|
12
|
+
|
13
|
+
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
14
|
+
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
15
|
+
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
16
|
+
COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
17
|
+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
18
|
+
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
19
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
20
|
+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
21
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
22
|
+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
23
|
+
|
data/{README.rdoc → README.md}
RENAMED
@@ -1,19 +1,17 @@
|
|
1
|
-
|
1
|
+
# Radix
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
== DESCRIPTION
|
3
|
+
[Website](http://rubyworks.github.com/radix) /
|
4
|
+
[Report Issue](http://github.com/rubyworks/radix/issues) /
|
5
|
+
[Source Code](http://github.com/rubyworks/radix) /
|
6
|
+
[](http://travis-ci.org/rubyworks/radix) /
|
7
|
+
[](http://badge.fury.io/rb/radix)
|
10
8
|
|
11
9
|
Radix is a very easy to use Ruby library for converting numbers to and from
|
12
10
|
any base. It supports both Integer, Float and Rational numbers, as well as
|
13
11
|
representational string-notations that need not be in ASCII order.
|
14
12
|
|
15
13
|
|
16
|
-
|
14
|
+
## Features
|
17
15
|
|
18
16
|
* Convert to and from any base.
|
19
17
|
* Convert Integers, Floats and Rational numbers.
|
@@ -22,65 +20,60 @@ representational string-notations that need not be in ASCII order.
|
|
22
20
|
* Very intuitive API.
|
23
21
|
|
24
22
|
|
25
|
-
|
26
|
-
|
27
|
-
* {Website}[http://rubyworks.github.com/radix]
|
28
|
-
* {Source Code}[http://github.com/rubyworks/radix]
|
29
|
-
* {Mailing List}[http://groups.google/group/rubyworks-mailinglist]
|
30
|
-
* {Issue Tracker}[http://github.com/rubyworks/radix/issues]
|
31
|
-
|
32
|
-
|
33
|
-
== SYNOPSIS
|
23
|
+
## Usage
|
34
24
|
|
35
25
|
Base conversions with ASCII ordered notations are easy in Ruby.
|
36
26
|
|
37
|
-
|
27
|
+
255.to_s(16) #=> "FF"
|
38
28
|
|
39
|
-
|
29
|
+
"FF".to_i(16) #=> 255
|
40
30
|
|
41
31
|
But Ruby reaches it's limit at base 36.
|
42
32
|
|
43
|
-
|
33
|
+
255.to_s(37) #=> Error
|
44
34
|
|
45
35
|
Radix provides the means of converting to and from any base.
|
46
36
|
|
47
37
|
For example, a number in base 256 can be represented by the array [100, 10]
|
48
38
|
(ie. 100**256 + 10**1) and can be convert to base 10.
|
49
39
|
|
50
|
-
|
40
|
+
[100,10].b(256).to_a(10) #=> [2,5,6,1,0]
|
51
41
|
|
52
42
|
Or, to get a string representation for any base up to 62.
|
53
43
|
|
54
|
-
|
44
|
+
[100,10].b(256).to_s(10) #=> "25610"
|
55
45
|
|
56
46
|
A string representation of a number can be converted too, again,
|
57
47
|
up to base 62.
|
58
48
|
|
59
|
-
|
49
|
+
"10".b(62).to_s(10) #=> "62"
|
60
50
|
|
61
51
|
To use a custom character set, use an array of characters as the base
|
62
52
|
rather than an integer. For example we can convert a base 10 number
|
63
53
|
to another base 10 number using a different encoding.
|
64
54
|
|
65
|
-
|
55
|
+
base = [:Q, :W, :E, :R, :T, :Y, :U, :I, :O, :U]
|
66
56
|
|
67
|
-
|
57
|
+
"10".b(10).to_a(base) #=> [:W, :Q]
|
68
58
|
|
69
|
-
To learn more have a look at the
|
59
|
+
To learn more have a look at the [QED Demo](http://rubydoc.info/gems/radix/file/DEMO.md).
|
70
60
|
|
71
|
-
|
61
|
+
|
62
|
+
## Installing
|
72
63
|
|
73
64
|
To install with RubyGems simply open a console and type:
|
74
65
|
|
75
|
-
|
66
|
+
$ gem install radix
|
67
|
+
|
68
|
+
Radix follows [Ruby Setup](http://rubyworks.github.com/setup) package standard
|
69
|
+
so it can also be installed in an FHS compliant manner using setup.rb.
|
76
70
|
|
77
|
-
Radix follows {Ruby Setup}[http://rubyworks.github.com/setup] package standard.
|
78
71
|
|
72
|
+
## Copyrights
|
79
73
|
|
80
|
-
|
74
|
+
Copyright (c) 2009 Rubyworks
|
81
75
|
|
82
|
-
|
76
|
+
This program is distributable in accordance with the *BSD-2-Clause* license.
|
83
77
|
|
84
|
-
|
78
|
+
See LICENSE.txt for details.
|
85
79
|
|
86
|
-
See NOTICE.rdoc for details.
|
data/demo/01_synopsis.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# Synopsis
|
2
|
+
|
3
|
+
Radix provides the means of converting to and from any base.
|
4
|
+
For example, a number in base 256 can be represented by the array [100, 10]
|
5
|
+
(ie. 100**256 + 10**1) and easily converted to base 10.
|
6
|
+
|
7
|
+
[100,10].b(256).to_i #=> 25610
|
8
|
+
|
9
|
+
We can get an Array representation as well.
|
10
|
+
|
11
|
+
[100,10].b(256).to_a(10) #=> [2,5,6,1,0]
|
12
|
+
[100,10].b(256).to_a(62) #=> [6,41,4]
|
13
|
+
[100,10].b(256).to_a(64) #=> [6,16,10]
|
14
|
+
|
15
|
+
To get a String representation for any base use #to_s.
|
16
|
+
|
17
|
+
[100,10].b(256).to_s(10) #=> "25610"
|
18
|
+
[100,10].b(256).to_s(62) #=> "6 41 4"
|
19
|
+
[100,10].b(256).to_s(64) #=> "6 16 10"
|
20
|
+
|
21
|
+
Notice that anything above base 10 is seperated by a space divider. The divider
|
22
|
+
can be changed by providing a second argument.
|
23
|
+
|
24
|
+
[100,10].b(256).to_s(64, ':') #=> "6:16:10"
|
25
|
+
|
26
|
+
A string representation of a number can be converted upto base 62 (B62).
|
27
|
+
|
28
|
+
"10".b(62).to_s(10) #=> "62"
|
29
|
+
"zz".b(62).to_s(10) #=> "3843"
|
30
|
+
|
31
|
+
To encode a number with a base greater than 10, use an Array base. Radix
|
32
|
+
provides a built-in set of these, such as `BASE::B62`.
|
33
|
+
|
34
|
+
[100,10].b(256).to_s(Radix::BASE::B62) #=> "6f4"
|
35
|
+
|
36
|
+
To use a custom character set, use an array of characters as the base
|
37
|
+
rather than an integer. For example we can convert a base 10 number
|
38
|
+
to another base 10 number but useing a different encoding.
|
39
|
+
|
40
|
+
base = %w[Q W E R T Y U I O U]
|
41
|
+
|
42
|
+
"10".b(10).to_a(base) #=> ["W", "Q"]
|
43
|
+
|
44
|
+
"10".b(10).to_s(base) #=> "WQ"
|
45
|
+
|
46
|
+
All of the above holds equally for floating point numbers.
|
data/demo/02_integer.md
ADDED
@@ -0,0 +1,256 @@
|
|
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
|
+
|