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/demo/applique/ae.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'radix'
|
data/lib/radix.yml
CHANGED
@@ -1,46 +1,55 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
|
2
|
+
revision: 2013
|
3
|
+
type: ruby
|
4
|
+
sources:
|
5
|
+
- var
|
4
6
|
authors:
|
5
7
|
- name: Thomas Sawyer
|
6
8
|
email: transfire@gmail.com
|
7
|
-
|
8
|
-
- holder: ''
|
9
|
-
year: '2009'
|
10
|
-
license: BSD-2-Clause
|
11
|
-
replacements: []
|
12
|
-
alternatives: []
|
9
|
+
organizations: []
|
13
10
|
requirements:
|
14
|
-
-
|
15
|
-
groups:
|
11
|
+
- groups:
|
16
12
|
- build
|
17
13
|
development: true
|
18
|
-
|
19
|
-
|
14
|
+
name: detroit
|
15
|
+
- groups:
|
20
16
|
- test
|
21
17
|
development: true
|
22
|
-
|
18
|
+
name: qed
|
23
19
|
conflicts: []
|
20
|
+
alternatives: []
|
21
|
+
resources:
|
22
|
+
- type: home
|
23
|
+
uri: http://rubyworks.github.com/radix
|
24
|
+
label: Homepage
|
25
|
+
- type: code
|
26
|
+
uri: http://github.com/rubyworks/radix
|
27
|
+
label: Source Code
|
28
|
+
- type: mail
|
29
|
+
uri: http://groups.google.com/groups/rubyworks-mailinglist
|
30
|
+
label: Mailing List
|
31
|
+
- type: bugs
|
32
|
+
uri: http://github.com/rubyworks/radix/issues
|
33
|
+
label: Issue Tracker
|
24
34
|
repositories:
|
25
|
-
-
|
35
|
+
- name: upstream
|
26
36
|
scm: git
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
+
uri: git://github.com/rubyworks/radix.git
|
38
|
+
categories: []
|
39
|
+
copyrights:
|
40
|
+
- holder: ''
|
41
|
+
year: '2009'
|
42
|
+
license: BSD-2-Clause
|
43
|
+
customs: []
|
44
|
+
paths:
|
45
|
+
lib:
|
46
|
+
- lib
|
37
47
|
created: '2009-07-01'
|
38
48
|
summary: Convert to and from any base.
|
39
49
|
title: Radix
|
40
|
-
version: 2.
|
50
|
+
version: 2.1.1
|
41
51
|
name: radix
|
42
52
|
description: ! "Radix is a very easy to use Ruby library for converting numbers to
|
43
53
|
and from\nany base. It supports both Integer, Float and Rational numbers, as well
|
44
54
|
as \nrepresentational string-notations that need not be in ASCII order."
|
45
|
-
|
46
|
-
date: '2011-10-23'
|
55
|
+
date: '2013-02-06'
|
data/lib/radix/float.rb
CHANGED
data/lib/radix/integer.rb
CHANGED
data/lib/radix/rational.rb
CHANGED
@@ -3,6 +3,7 @@ require 'radix/numeric'
|
|
3
3
|
|
4
4
|
module Radix
|
5
5
|
|
6
|
+
# Represents rational numbers.
|
6
7
|
#
|
7
8
|
class Rational < Numeric
|
8
9
|
|
@@ -57,42 +58,56 @@ module Radix
|
|
57
58
|
|
58
59
|
public
|
59
60
|
|
61
|
+
# The numerator.
|
60
62
|
#
|
61
63
|
def numerator
|
62
64
|
@value.numerator
|
63
65
|
end
|
64
66
|
|
67
|
+
# The denominator.
|
65
68
|
#
|
66
69
|
def denominator
|
67
70
|
@value.denominator
|
68
71
|
end
|
69
72
|
|
73
|
+
# Is the value negative?
|
70
74
|
#
|
71
75
|
def negative?
|
72
76
|
value < 0
|
73
77
|
end
|
74
78
|
|
79
|
+
# Convert rational to new base.
|
75
80
|
#
|
81
|
+
# Returns new rational. [Radix::Rational]
|
76
82
|
def convert(base)
|
77
83
|
self.class.new(numerator, denominator, base)
|
78
84
|
end
|
79
85
|
|
86
|
+
# Convert to rational.
|
80
87
|
#
|
88
|
+
# Returns the internal value. [Rational]
|
81
89
|
def to_r
|
82
90
|
value
|
83
91
|
end
|
84
92
|
|
93
|
+
# Convert to Float by dividing the numerator by the denominator.
|
85
94
|
#
|
95
|
+
# Returns the converted value. [Float]
|
86
96
|
def to_f
|
87
97
|
numerator.to_f / denominator.to_f
|
88
98
|
end
|
89
99
|
|
100
|
+
# Convert to Integer by converting to Float first then
|
101
|
+
# appling #to_i to the float.
|
90
102
|
#
|
103
|
+
# Returns the converted value. [Integer]
|
91
104
|
def to_i
|
92
105
|
to_f.to_i
|
93
106
|
end
|
94
107
|
|
108
|
+
# Translate value into an array of places.
|
95
109
|
#
|
110
|
+
# Returns array of places. [Array]
|
96
111
|
def to_a(base=nil)
|
97
112
|
if base
|
98
113
|
convert(base).digits_encoded
|
@@ -101,7 +116,12 @@ module Radix
|
|
101
116
|
end
|
102
117
|
end
|
103
118
|
|
119
|
+
# Convert the value into a string representation of the given base.
|
104
120
|
#
|
121
|
+
# base - the base to convert
|
122
|
+
# divider - symbol to used ot divide numbers
|
123
|
+
#
|
124
|
+
# Returns translated value. [String]
|
105
125
|
def to_s(base=nil, divider=nil)
|
106
126
|
divider = divider.to_s if divider
|
107
127
|
if base
|
@@ -119,7 +139,11 @@ module Radix
|
|
119
139
|
end
|
120
140
|
end
|
121
141
|
|
142
|
+
# Are two rationals equal?
|
143
|
+
#
|
144
|
+
# TODO: this may need improvement to be more percise.
|
122
145
|
#
|
146
|
+
# Returns true if equal, otherwise false. [Boolean]
|
123
147
|
def ==(other)
|
124
148
|
a, b = self.to_f, other.to_f
|
125
149
|
a == b
|
@@ -144,7 +168,7 @@ module Radix
|
|
144
168
|
base_encode(digits)
|
145
169
|
end
|
146
170
|
|
147
|
-
#
|
171
|
+
# Returns [String]
|
148
172
|
def inspect
|
149
173
|
"#{digits.join(' ')} (#{base})"
|
150
174
|
end
|
@@ -156,13 +180,17 @@ module Radix
|
|
156
180
|
|
157
181
|
private
|
158
182
|
|
183
|
+
# Perform operation.
|
159
184
|
#
|
185
|
+
# Returns new rational. [Radix::Rational]
|
160
186
|
def operation(op, other)
|
161
187
|
x = value.__send__(op, other.to_r)
|
162
188
|
self.class.new(x, base)
|
163
189
|
end
|
164
190
|
|
191
|
+
# Perform base conversion.
|
165
192
|
#
|
193
|
+
# Returns array of places. [Array]
|
166
194
|
def base_conversion(value, base)
|
167
195
|
#if value < 0
|
168
196
|
# @negative, value = true, value.abs
|
@@ -175,6 +203,8 @@ module Radix
|
|
175
203
|
a << r
|
176
204
|
end
|
177
205
|
|
206
|
+
a << 0 if a.empty?
|
207
|
+
|
178
208
|
a.reverse
|
179
209
|
end
|
180
210
|
|
@@ -184,7 +214,10 @@ end
|
|
184
214
|
|
185
215
|
class ::Array
|
186
216
|
# Convenience method for creating a Radix::Rational.
|
217
|
+
#
|
187
218
|
# TODO: Keep #br? Or find another way?
|
219
|
+
#
|
220
|
+
# Returns [Radix::Rational]
|
188
221
|
def br(base=nil)
|
189
222
|
args = dup
|
190
223
|
args << base if base
|
@@ -192,12 +225,14 @@ class ::Array
|
|
192
225
|
end
|
193
226
|
end
|
194
227
|
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
228
|
+
if RUBY_VERSION < '1.9'
|
229
|
+
class ::Float
|
230
|
+
def to_r
|
231
|
+
n, f = to_s.split('.')
|
232
|
+
d = (10 ** f.size).to_i
|
233
|
+
n = (n.to_i * d) + f.to_i
|
234
|
+
Rational(n, d)
|
235
|
+
end
|
202
236
|
end
|
203
237
|
end
|
238
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: radix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-02-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: detroit
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,15 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: qed
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ! '>='
|
@@ -32,7 +37,12 @@ dependencies:
|
|
32
37
|
version: '0'
|
33
38
|
type: :development
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
36
46
|
description: ! "Radix is a very easy to use Ruby library for converting numbers to
|
37
47
|
and from\nany base. It supports both Integer, Float and Rational numbers, as well
|
38
48
|
as \nrepresentational string-notations that need not be in ASCII order."
|
@@ -41,12 +51,21 @@ email:
|
|
41
51
|
executables: []
|
42
52
|
extensions: []
|
43
53
|
extra_rdoc_files:
|
44
|
-
-
|
45
|
-
-
|
46
|
-
-
|
54
|
+
- LICENSE.txt
|
55
|
+
- HISTORY.md
|
56
|
+
- README.md
|
57
|
+
- DEMO.md
|
47
58
|
files:
|
48
|
-
- .
|
59
|
+
- .index
|
49
60
|
- .yardopts
|
61
|
+
- demo/01_synopsis.md
|
62
|
+
- demo/02_integer.md
|
63
|
+
- demo/03_float.md
|
64
|
+
- demo/04_rational.md
|
65
|
+
- demo/05_base.md
|
66
|
+
- demo/applique/ae.rb
|
67
|
+
- demo/applique/check.rb
|
68
|
+
- demo/applique/radix.rb
|
50
69
|
- lib/radix/base.rb
|
51
70
|
- lib/radix/float.rb
|
52
71
|
- lib/radix/integer.rb
|
@@ -55,11 +74,13 @@ files:
|
|
55
74
|
- lib/radix/rational.rb
|
56
75
|
- lib/radix.rb
|
57
76
|
- lib/radix.yml
|
58
|
-
- HISTORY.
|
59
|
-
- README.
|
60
|
-
-
|
77
|
+
- HISTORY.md
|
78
|
+
- README.md
|
79
|
+
- DEMO.md
|
80
|
+
- LICENSE.txt
|
61
81
|
homepage: http://rubyworks.github.com/radix
|
62
|
-
licenses:
|
82
|
+
licenses:
|
83
|
+
- BSD-2-Clause
|
63
84
|
post_install_message:
|
64
85
|
rdoc_options: []
|
65
86
|
require_paths:
|
@@ -78,8 +99,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
99
|
version: '0'
|
79
100
|
requirements: []
|
80
101
|
rubyforge_project:
|
81
|
-
rubygems_version: 1.8.
|
102
|
+
rubygems_version: 1.8.24
|
82
103
|
signing_key:
|
83
104
|
specification_version: 3
|
84
105
|
summary: Convert to and from any base.
|
85
106
|
test_files: []
|
107
|
+
has_rdoc:
|
data/NOTICE.rdoc
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
= COPYRIGHT NOTICES
|
2
|
-
|
3
|
-
== Radix
|
4
|
-
|
5
|
-
Copyright:: (c) 2009 Thomas Sawyer, Rubyworks
|
6
|
-
License:: BSD-2-Clause
|
7
|
-
Website:: http://rubyworks.github.com/radix
|
8
|
-
|
9
|
-
Copyright 2011 Thomas Sawyer. All rights reserved.
|
10
|
-
|
11
|
-
Redistribution and use in source and binary forms, with or without
|
12
|
-
modification, are permitted provided that the following conditions are met:
|
13
|
-
|
14
|
-
1. Redistributions of source code must retain the above copyright notice,
|
15
|
-
this list of conditions and the following disclaimer.
|
16
|
-
|
17
|
-
2. Redistributions in binary form must reproduce the above copyright
|
18
|
-
notice, this list of conditions and the following disclaimer in the
|
19
|
-
documentation and/or other materials provided with the distribution.
|
20
|
-
|
21
|
-
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
22
|
-
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
23
|
-
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
24
|
-
COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
25
|
-
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
26
|
-
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
27
|
-
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
28
|
-
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
29
|
-
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
30
|
-
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31
|
-
|