radix 2.0.1 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ require 'ae'
2
+ require 'ae/should'
3
+
@@ -0,0 +1,7 @@
1
+ def check(&block)
2
+ @check = block
3
+ end
4
+
5
+ def ok(*args)
6
+ @check.call(*args)
7
+ end
@@ -0,0 +1 @@
1
+ require 'radix'
data/lib/radix.yml CHANGED
@@ -1,46 +1,55 @@
1
1
  ---
2
- source:
3
- - meta/
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
- copyrights:
8
- - holder: ''
9
- year: '2009'
10
- license: BSD-2-Clause
11
- replacements: []
12
- alternatives: []
9
+ organizations: []
13
10
  requirements:
14
- - name: detroit
15
- groups:
11
+ - groups:
16
12
  - build
17
13
  development: true
18
- - name: qed
19
- groups:
14
+ name: detroit
15
+ - groups:
20
16
  - test
21
17
  development: true
22
- dependencies: []
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
- - uri: git://github.com/rubyworks/radix.git
35
+ - name: upstream
26
36
  scm: git
27
- name: upstream
28
- resources:
29
- home: http://rubyworks.github.com/radix
30
- code: http://github.com/rubyworks/radix
31
- mail: http://groups.google.com/groups/rubyworks-mailinglist
32
- bugs: http://github.com/rubyworks/radix/issues
33
- extra: {}
34
- load_path:
35
- - lib
36
- revision: 0
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.0.1
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
- organization: Rubyworks
46
- date: '2011-10-23'
55
+ date: '2013-02-06'
data/lib/radix/float.rb CHANGED
@@ -222,6 +222,7 @@ module Radix
222
222
  b << r
223
223
  end
224
224
 
225
+ a << 0 if a.empty?
225
226
  b << 0 if b.empty?
226
227
 
227
228
  [a.reverse, b]
data/lib/radix/integer.rb CHANGED
@@ -215,6 +215,9 @@ module Radix
215
215
  a << r
216
216
  end
217
217
 
218
+ # if nothing add zero
219
+ a << 0 if a.empty?
220
+
218
221
  a.reverse
219
222
  end
220
223
 
@@ -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
- class ::Float
196
- #
197
- def to_r
198
- n, f = to_s.split('.')
199
- d = (10 * f.size).to_i
200
- n = (n.to_i * d) + f.to_i
201
- Rational(n, d)
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.0.1
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: 2011-10-24 00:00:00.000000000 Z
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: &13289720 !ruby/object:Gem::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: *13289720
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: &13289040 !ruby/object:Gem::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: *13289040
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
- - HISTORY.rdoc
45
- - README.rdoc
46
- - NOTICE.rdoc
54
+ - LICENSE.txt
55
+ - HISTORY.md
56
+ - README.md
57
+ - DEMO.md
47
58
  files:
48
- - .ruby
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.rdoc
59
- - README.rdoc
60
- - NOTICE.rdoc
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.10
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
-