fixed_point 0.2.0 → 0.2.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/HISTORY.md CHANGED
@@ -9,8 +9,7 @@ HISTORY/Changelog
9
9
  0.2.0
10
10
  -----
11
11
 
12
- Added to_s method on numbers. Output is now the same as calling .to_f.to_s
13
-
12
+ Added to_s method on numbers. Output is now the same as calling .to_f.to_s
14
13
 
15
14
  0.1.1
16
15
  -----
data/Rakefile CHANGED
@@ -24,11 +24,6 @@ namespace :deploy do
24
24
  line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
25
25
  end
26
26
 
27
- #Dir.chdir( './lib/')
28
- #puts require NAME
29
- #Dir.chdir( './../')
30
-
31
-
32
27
  desc 'Run Spec'
33
28
  #file_list = FileList['spec/*_spec.rb']
34
29
  html_path = "spec_results.html"
@@ -69,7 +64,7 @@ namespace :deploy do
69
64
 
70
65
  task :push_gem do
71
66
  puts "Pushing Gem #{name}-#{version}"
72
- #`gem push`
67
+ `gem push #{name}-#{version}.gem`
73
68
  unless $?.success?
74
69
  $stderr.puts "Gem Push Failed"
75
70
  exit -1
@@ -78,7 +73,7 @@ namespace :deploy do
78
73
 
79
74
  task :create_git_tag do
80
75
  puts "Tagging #{name}-#{version}"
81
- #`git tag -a #{version} -m 'Tagging gem release #{version}'`
76
+ `git tag -a #{version} -m 'Tagging gem release #{version}'`
82
77
  unless $?.success?
83
78
  $stderr.puts "Tagging Git Failed"
84
79
  exit -1
@@ -87,7 +82,7 @@ namespace :deploy do
87
82
 
88
83
  task :push_tag do
89
84
  puts "Push Tag #{version}"
90
- #`git push origin #{version}`
85
+ `git push origin #{version}`
91
86
  unless $?.success?
92
87
  $stderr.puts "Pushing Git Tag Failed"
93
88
  exit -1
@@ -1,5 +1,5 @@
1
1
  module FixedPoint
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
4
4
 
5
5
  begin
@@ -154,45 +154,59 @@ module FixedPoint
154
154
  # Do not change the Signed format as can not detect that from bit pattern
155
155
  @format = Format.new(@format.signed, int_bits, frac_bits)
156
156
 
157
- ###########################
158
- ### Routine to generate source from binary
159
- ###########################
160
- @source = 0.0
161
- index = 0
162
- set_int.reverse.each_char do |x|
163
- if x == "1"
164
- #If input is signed then MSB is negative
165
- if ((index + 1) == @format.int_bits) and (@format.signed?)
166
- @source = @source + -2**index
167
- else
168
- @source = @source + 2**index
169
- end
170
- end
171
- index = index + 1
172
- end
173
-
174
- index = 1
175
- set_frac.each_char do |x|
176
- if x == "1"
177
- @source = @source + 2**-index
178
- end
179
- index = index + 1
180
- end
181
- ################################
157
+ @source = binary_to_float( set_int, set_frac)
182
158
 
183
159
  ## Set the Quantised value
184
160
  @quantised = @source
185
161
 
186
162
  return binary
187
163
  else
188
- puts "ERROR invalid input binary\(#{text}\)"
164
+ $stderr.puts "ERROR invalid binary input \(#{text}\)"
189
165
  return nil
190
166
  end
191
167
  end
192
168
 
169
+ ## TODO this feature for version 0.2.1
193
170
  def hex=( text )
194
- puts "hex = Mehtod not completed"
195
- ## Strip leading 0x if present
171
+ ## Hex numbers do not get delimited
172
+ ## TODO Strip leading \d+'h (Verilog denominator)
173
+
174
+ if text.match(/(0x)?([0-9a-fA-F]*)/ )
175
+ # Convert Hex to integer
176
+ # integer to binary string
177
+ binary_bits = $2.to_i(16).to_s(2)
178
+
179
+ #Ensure min length (for reliability manipulating string)
180
+ binary_bits = binary_bits.rjust(text.size*4, '0')
181
+
182
+ if @format.frac_bits > 0
183
+ set_frac = binary_bits[-@format.frac_bits, @format.frac_bits]
184
+ else
185
+ set_frac = ""
186
+ end
187
+
188
+ set_int = binary_bits[-(@format.width)..-(@format.frac_bits+1)]
189
+
190
+ ## Was the input word length too Long
191
+ oversized_bits = binary_bits.size - @format.width
192
+ if oversized_bits > 0
193
+ # give a semantic name to the discarded bits
194
+ discarded_bits = binary_bits[0,oversized_bits]
195
+
196
+ #If data (mix of 1 and 0) contained in the discarded bits raise Error
197
+ unless all_bits_the_same discarded_bits
198
+ $stderr.puts %{Error using hex=#{text}, format is #{@format.width}, truncated data is #{discarded_bits}
199
+ The MSBs seem to contain data they are not just 0 or 1 padded}
200
+ end
201
+ end
202
+
203
+ @source = binary_to_float( set_int, set_frac )
204
+ @quantised = @source
205
+
206
+ else
207
+ $stderr.puts "ERROR invalid hex input \(#{text}\)"
208
+ return nil
209
+ end
196
210
  end
197
211
 
198
212
 
@@ -206,6 +220,14 @@ module FixedPoint
206
220
 
207
221
  private
208
222
 
223
+ def all_bits_the_same( bin_string )
224
+ word = bin_string.split('')
225
+ all_one = word.all?{|x| x == '1' }
226
+ all_zero = word.all?{|x| x == '0' }
227
+ return (all_one || all_zero)
228
+ end
229
+
230
+
209
231
  def check_for_overflow_underflow( source, format)
210
232
  overflow = false
211
233
  underflow = false
@@ -299,13 +321,39 @@ module FixedPoint
299
321
 
300
322
 
301
323
 
324
+ ##
325
+ def binary_to_float(int_part, frac_part )
326
+ index = 0
327
+ value = 0.0
328
+ int_part.reverse.each_char do |x|
329
+ if x == "1"
330
+ #If input is signed then MSB is negative
331
+ if ((index + 1) == @format.int_bits) and (@format.signed?)
332
+ value = value + -2**index
333
+ else
334
+ value = value + 2**index
335
+ end
336
+ end
337
+ index = index + 1
338
+ end
339
+
340
+ index = 1
341
+ frac_part.each_char do |x|
342
+ if x == "1"
343
+ value = value + 2**-index
344
+ end
345
+ index = index + 1
346
+ end
347
+
348
+ return value
349
+ end
302
350
 
303
351
 
304
- ## Taking methd out until covered by tests
352
+ ## Taking method out until covered by tests
305
353
  #def normalised
306
354
  # #This use to be only for positive numbers
307
355
  #
308
- # # This function shiftes the fixedpoint number
356
+ # # This function shifts the fixedpoint number
309
357
  # # so it can be represented as an integer.
310
358
  # return Integer((@quantised)*(2**@format.frac_bits))
311
359
  #end
@@ -0,0 +1,201 @@
1
+ require 'spec_helper'
2
+
3
+ describe FixedPoint do
4
+ it "Create 4 bit hex 7" do
5
+ format = FixedPoint::Fixdt.new(0, 4, 0)
6
+ fixt = FixedPoint::Number.new(0, format)
7
+
8
+ fixt.hex = "7"
9
+ fixt.source.should == 7.0
10
+ fixt.to_f.should == 7.0
11
+ fixt.to_s.should == "7.0"
12
+ fixt.to_i.should == 7
13
+ fixt.frac.should == 0.0
14
+ fixt.to_h.should == "7"
15
+ fixt.to_b.should == "0111"
16
+ fixt.overflow?.should == false
17
+ fixt.underflow?.should == false
18
+ end
19
+
20
+ it "Create 4 bit hex F" do
21
+ format = FixedPoint::Fixdt.new(0, 4, 0)
22
+ fixt = FixedPoint::Number.new(0, format)
23
+
24
+ fixt.hex = "F"
25
+ fixt.source.should == 15.0
26
+ fixt.to_f.should == 15.0
27
+ fixt.to_s.should == "15.0"
28
+ fixt.to_i.should == 15
29
+ fixt.frac.should == 0.0
30
+ fixt.to_h.should == "f"
31
+ fixt.to_b.should == "1111"
32
+ fixt.overflow?.should == false
33
+ fixt.underflow?.should == false
34
+ end
35
+
36
+ it "Create 4 bit hex F Signed" do
37
+ format = FixedPoint::Fixdt.new(1, 4, 0)
38
+ fixt = FixedPoint::Number.new(0, format)
39
+
40
+ fixt.hex = "F"
41
+ fixt.source.should == -1.0
42
+ fixt.to_f.should == -1.0
43
+ fixt.to_s.should == "-1.0"
44
+ fixt.to_i.should == -1
45
+ fixt.frac.should == 0.0
46
+ fixt.to_h.should == "f"
47
+ fixt.to_b.should == "1111"
48
+ fixt.overflow?.should == false
49
+ fixt.underflow?.should == false
50
+ end
51
+
52
+ it "Create 4 bit hex F Signed" do
53
+ format = FixedPoint::Fixdt.new(1, 4, 0)
54
+ fixt = FixedPoint::Number.new(0, format)
55
+
56
+ fixt.hex = "F"
57
+ fixt.source.should == -1.0
58
+ fixt.to_f.should == -1.0
59
+ fixt.to_s.should == "-1.0"
60
+ fixt.to_i.should == -1
61
+ fixt.frac.should == 0.0
62
+ fixt.to_h.should == "f"
63
+ fixt.to_b.should == "1111"
64
+ fixt.overflow?.should == false
65
+ fixt.underflow?.should == false
66
+ end
67
+
68
+ it "Create 8 bit Signed hex f0 [1,4,4] = -1.0" do
69
+ format = FixedPoint::Format.new(1, 4, 4)
70
+ fixt = FixedPoint::Number.new(0, format)
71
+
72
+ fixt.hex = "f0"
73
+ fixt.source.should == -1.0
74
+ fixt.to_f.should == -1.0
75
+ fixt.to_s.should == "-1.0"
76
+ fixt.to_i.should == -1
77
+ fixt.frac.should == 0.0
78
+ fixt.to_h.should == "f0"
79
+ fixt.to_b.should == "1111.0000"
80
+ fixt.overflow?.should == false
81
+ fixt.underflow?.should == false
82
+ end
83
+
84
+ it "hex= should ignore msb past word length" do
85
+ format = FixedPoint::Format.new(1, 4, 4)
86
+ fixt = FixedPoint::Number.new(0, format)
87
+
88
+ fixt.hex = "f80"
89
+ fixt.source.should == -8.0
90
+ fixt.to_f.should == -8.0
91
+ fixt.to_s.should == "-8.0"
92
+ fixt.to_i.should == -8
93
+ fixt.frac.should == 0.0
94
+ fixt.to_h.should == "80"
95
+ fixt.to_b.should == "1000.0000"
96
+ fixt.overflow?.should == false
97
+ fixt.underflow?.should == false
98
+ end
99
+
100
+ it "hex= should ignore truncated MSBs, 0 padded" do
101
+ format = FixedPoint::Format.new(0, 6, 0)
102
+ fixt = FixedPoint::Number.new(0, format)
103
+
104
+ fixt.hex = "00"
105
+ fixt.source.should == 0.0
106
+ fixt.to_f.should == 0.0
107
+ fixt.to_s.should == "0.0"
108
+ fixt.to_i.should == 0
109
+ fixt.frac.should == 0.0
110
+ fixt.to_h.should == "00"
111
+ fixt.to_b.should == "000000"
112
+ fixt.overflow?.should == false
113
+ fixt.underflow?.should == false
114
+ end
115
+ it "hex= should ignore truncated MSBs, 1 padded" do
116
+ format = FixedPoint::Format.new(0, 6, 0)
117
+ fixt = FixedPoint::Number.new(0, format)
118
+
119
+ fixt.hex = "C0"
120
+ fixt.source.should == 0.0
121
+ fixt.to_f.should == 0.0
122
+ fixt.to_s.should == "0.0"
123
+ fixt.to_i.should == 0
124
+ fixt.frac.should == 0.0
125
+ fixt.to_h.should == "00"
126
+ fixt.to_b.should == "000000"
127
+ fixt.overflow?.should == false
128
+ fixt.underflow?.should == false
129
+ end
130
+
131
+ it "hex= should catch data in truncated MSBs" do
132
+ format = FixedPoint::Format.new(0, 6, 0)
133
+ fixt = FixedPoint::Number.new(0, format)
134
+
135
+ error = %{Error using hex=40, format is 6, truncated data is 01\n The MSBs seem to contain data they are not just 0 or 1 padded}
136
+ $stderr.should_receive(:puts).with( error )
137
+ fixt.hex = "40"
138
+
139
+ fixt.source.should == 0.0
140
+ fixt.to_f.should == 0.0
141
+ fixt.to_s.should == "0.0"
142
+ fixt.to_i.should == 0
143
+ fixt.frac.should == 0.0
144
+ fixt.to_h.should == "00"
145
+ fixt.to_b.should == "000000"
146
+ fixt.overflow?.should == false
147
+ fixt.underflow?.should == false
148
+ end
149
+
150
+ #it "hex= should catch data in truncated MSBs" do
151
+ # format = FixedPoint::Format.new(0, 6, 0)
152
+ # fixt = FixedPoint::Number.new(0, format)
153
+ #
154
+ # error = %{Error using hex=80, format is 6, truncated data is 10\n The MSBs seem to contain data they are not just 0 or 1 padded}
155
+ # $stderr.should_receive(:puts).with( error )
156
+ # fixt.hex = "80"
157
+
158
+ # fixt.source.should == 0.0
159
+ # fixt.to_f.should == 0.0
160
+ # fixt.to_s.should == "0.0"
161
+ # fixt.to_i.should == 0
162
+ # fixt.frac.should == 0.0
163
+ # fixt.to_h.should == "00"
164
+ # fixt.to_b.should == "000000"
165
+ # fixt.overflow?.should == false
166
+ # fixt.underflow?.should == false
167
+ #end
168
+
169
+ it "Bin= check 0x removal " do
170
+ format = FixedPoint::Format.new(1, 4, 4)
171
+ fixt = FixedPoint::Number.new(0, format)
172
+
173
+ fixt.hex = "0xf0"
174
+ fixt.source.should == -1.0
175
+ fixt.to_f.should == -1.0
176
+ fixt.to_s.should == "-1.0"
177
+ fixt.to_i.should == -1
178
+ fixt.frac.should == 0.0
179
+ fixt.to_h.should == "f0"
180
+ fixt.to_b.should == "1111.0000"
181
+ fixt.overflow?.should == false
182
+ fixt.underflow?.should == false
183
+ end
184
+
185
+ it "Bin= check 0x removal " do
186
+ format = FixedPoint::Format.new(1, 4, 4)
187
+ fixt = FixedPoint::Number.new(0, format)
188
+
189
+ fixt.hex = "0xf1"
190
+ fixt.source.should == -0.9375
191
+ fixt.to_f.should == -0.9375
192
+ fixt.to_s.should == "-0.9375"
193
+ fixt.to_i.should == 0
194
+ fixt.frac.should == -0.9375
195
+ fixt.to_h.should == "f1"
196
+ fixt.to_b.should == "1111.0001"
197
+ fixt.overflow?.should == false
198
+ fixt.underflow?.should == false
199
+ end
200
+ end
201
+
@@ -21,83 +21,83 @@ describe FixedPoint do
21
21
  fixt.frac.should == 0.0
22
22
  fixt.to_h.should == "00000000"
23
23
  fixt.to_b.should == "000000000000.00000000000000000000"
24
- end
24
+ end
25
25
 
26
- it "Integers 0 " do
27
- format = FixedPoint::Format.new(1,8,0)
28
- fixt = FixedPoint::Number.new(7.0, format, "_")
29
-
30
- fixt.source.should == 7.0
31
- fixt.to_f.should == 7.0
32
- fixt.to_s.should == "7.0"
33
- fixt.to_i.should == 7
34
- fixt.frac.should == 0.0
35
- fixt.to_h.should == "07"
36
- fixt.to_b.should == "00000111"
37
- end
26
+ it "Integers 0 " do
27
+ format = FixedPoint::Format.new(1,8,0)
28
+ fixt = FixedPoint::Number.new(7.0, format, "_")
38
29
 
39
- it "Different Decimal Mark _ instead of ." do
40
- format = FixedPoint::Format.new(1,12,20)
41
- fixt = FixedPoint::Number.new(0, format, "_")
42
-
43
- fixt.source.should == 0.0
44
- fixt.to_f.should == 0.0
45
- fixt.to_s.should == "0.0"
46
- fixt.to_i.should == 0
47
- fixt.frac.should == 0.0
48
- fixt.to_h.should == "00000000"
49
- fixt.to_b.should == "000000000000_00000000000000000000"
50
- end
30
+ fixt.source.should == 7.0
31
+ fixt.to_f.should == 7.0
32
+ fixt.to_s.should == "7.0"
33
+ fixt.to_i.should == 7
34
+ fixt.frac.should == 0.0
35
+ fixt.to_h.should == "07"
36
+ fixt.to_b.should == "00000111"
37
+ end
51
38
 
52
- (1...20).to_a.reverse_each do |x|
53
- int_bits = 12
54
- it "Returns 0.0 for initalise of 0 with #{x} fractional bits" do
55
- format = FixedPoint::Format.new(1, int_bits, x)
56
- fixt = FixedPoint::Number.new(0, format)
57
- #fixt = FixedPoint::Number.new(0,1,int_bits,x)
39
+ it "Different Decimal Mark _ instead of ." do
40
+ format = FixedPoint::Format.new(1,12,20)
41
+ fixt = FixedPoint::Number.new(0, format, "_")
58
42
 
59
43
  fixt.source.should == 0.0
60
44
  fixt.to_f.should == 0.0
61
45
  fixt.to_s.should == "0.0"
62
46
  fixt.to_i.should == 0
63
47
  fixt.frac.should == 0.0
64
- #Calculate hex length and fill with 0's
65
- hex = ""
66
- hex_length = ((x.to_f+int_bits.to_f)/4).ceil
67
- hex_length.times { hex += "0" }
68
- fixt.to_h.should == hex
69
- #Calculate binary fractional length and fill with 0's
70
- lsbs = ""
71
- x.times { lsbs += "0" }
72
- fixt.to_b.should == "000000000000.#{lsbs}"
73
- end
74
- end
48
+ fixt.to_h.should == "00000000"
49
+ fixt.to_b.should == "000000000000_00000000000000000000"
50
+ end
75
51
 
52
+ (1...20).to_a.reverse_each do |x|
53
+ int_bits = 12
54
+ it "Returns 0.0 for initalise of 0 with #{x} fractional bits" do
55
+ format = FixedPoint::Format.new(1, int_bits, x)
56
+ fixt = FixedPoint::Number.new(0, format)
57
+ #fixt = FixedPoint::Number.new(0,1,int_bits,x)
58
+
59
+ fixt.source.should == 0.0
60
+ fixt.to_f.should == 0.0
61
+ fixt.to_s.should == "0.0"
62
+ fixt.to_i.should == 0
63
+ fixt.frac.should == 0.0
64
+ #Calculate hex length and fill with 0's
65
+ hex = ""
66
+ hex_length = ((x.to_f+int_bits.to_f)/4).ceil
67
+ hex_length.times { hex += "0" }
68
+ fixt.to_h.should == hex
69
+ #Calculate binary fractional length and fill with 0's
70
+ lsbs = ""
71
+ x.times { lsbs += "0" }
72
+ fixt.to_b.should == "000000000000.#{lsbs}"
73
+ end
74
+ end
76
75
 
77
- it "Zero fractional bits " do
78
- format = FixedPoint::Format.new(1,12,0)
79
- fixt = FixedPoint::Number.new(0, format, "_")
80
76
 
81
- fixt.source.should == 0.0
82
- fixt.to_f.should == 0.0
83
- fixt.to_s.should == "0.0"
84
- fixt.to_i.should == 0
85
- fixt.frac.should == 0.0
86
- fixt.to_h.should == "000"
87
- fixt.to_b.should == "000000000000"
88
- end
77
+ it "Zero fractional bits " do
78
+ format = FixedPoint::Format.new(1,12,0)
79
+ fixt = FixedPoint::Number.new(0, format, "_")
89
80
 
81
+ fixt.source.should == 0.0
82
+ fixt.to_f.should == 0.0
83
+ fixt.to_s.should == "0.0"
84
+ fixt.to_i.should == 0
85
+ fixt.frac.should == 0.0
86
+ fixt.to_h.should == "000"
87
+ fixt.to_b.should == "000000000000"
88
+ end
90
89
 
91
- it "returns 2.5 for initalise of 2.5" do
92
- fixt = FixedPoint::Number.new(2.5)
93
90
 
94
- fixt.source.should == 2.5
95
- fixt.to_f.should == 2.5
96
- fixt.to_s.should == "2.5"
97
- fixt.to_i.should == 2
98
- fixt.frac.should == 0.5
99
- fixt.to_h.should == "00280000"
100
- fixt.to_b.should == "000000000010.10000000000000000000"
91
+ it "returns 2.5 for initalise of 2.5" do
92
+ fixt = FixedPoint::Number.new(2.5)
93
+
94
+ fixt.source.should == 2.5
95
+ fixt.to_f.should == 2.5
96
+ fixt.to_s.should == "2.5"
97
+ fixt.to_i.should == 2
98
+ fixt.frac.should == 0.5
99
+ fixt.to_h.should == "00280000"
100
+ fixt.to_b.should == "000000000010.10000000000000000000"
101
101
  end
102
102
 
103
103
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fixed_point
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-10 00:00:00.000000000 Z
12
+ date: 2012-05-22 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Fixed Point numerical type for simulating fixed point calculations
15
15
  email: fixed_point_gem@amaras-tech.co.uk
@@ -26,6 +26,7 @@ files:
26
26
  - lib/fixed_point/format.rb
27
27
  - lib/fixed_point/number.rb
28
28
  - lib/fixed_point.rb
29
+ - spec/fixed_point_hex_spec.rb
29
30
  - spec/fixed_point_negative_spec.rb
30
31
  - spec/fixed_point_spec.rb
31
32
  - spec/spec_helper.rb