fixed_point 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.md +5 -0
- data/README.md +30 -0
- data/examples/table_generation.rb +46 -0
- data/lib/fixed_point.rb +1 -1
- data/lib/fixed_point/number.rb +25 -22
- metadata +5 -4
data/HISTORY.md
CHANGED
data/README.md
CHANGED
@@ -25,6 +25,36 @@ Checkout the examples folder, but here are a few:
|
|
25
25
|
puts fix_num.to_h # Hexadecimal
|
26
26
|
puts fix_num.to_b # Binary
|
27
27
|
|
28
|
+
Building tables:
|
29
|
+
|
30
|
+
require 'fixed_point'
|
31
|
+
|
32
|
+
#Signed 8 bit, (4 bit Integer, 4 bit fractional)
|
33
|
+
format = FixedPoint::Format.new(1,4,4)
|
34
|
+
|
35
|
+
table_for_conversion = [
|
36
|
+
2.7505, 2.5, 2.25, 2, 0.5, 0, -0.5, -4.5
|
37
|
+
]
|
38
|
+
|
39
|
+
table_for_conversion.each do |num|
|
40
|
+
fixt = FixedPoint::Number.new(num, format, '_')
|
41
|
+
puts "#{format.width}'b#{fixt.to_b} // Quantised %7.4f Source %7.4f" % [ fixt.to_f, fixt.source]
|
42
|
+
end
|
43
|
+
|
44
|
+
Returns:
|
45
|
+
|
46
|
+
8'b0010_1100 // Quantised 2.7500 Source 2.7505
|
47
|
+
8'b0010_1000 // Quantised 2.5000 Source 2.5000
|
48
|
+
8'b0010_0100 // Quantised 2.2500 Source 2.2500
|
49
|
+
8'b0010_0000 // Quantised 2.0000 Source 2.0000
|
50
|
+
8'b0000_1000 // Quantised 0.5000 Source 0.5000
|
51
|
+
8'b0000_0000 // Quantised 0.0000 Source 0.0000
|
52
|
+
8'b1111_1000 // Quantised -0.5000 Source -0.5000
|
53
|
+
8'b1011_1000 // Quantised -4.5000 Source -4.5000
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
28
58
|
TODO
|
29
59
|
----
|
30
60
|
|
@@ -0,0 +1,46 @@
|
|
1
|
+
|
2
|
+
begin
|
3
|
+
require 'rubygems'
|
4
|
+
require 'fixed_point'
|
5
|
+
rescue
|
6
|
+
require_relative '../lib/fixed_point'
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
puts
|
11
|
+
puts 'Unsigned 4 bit Integer'
|
12
|
+
|
13
|
+
#Unsigned, 4 bit integer
|
14
|
+
format = FixedPoint::Format.new(0,4,0)
|
15
|
+
|
16
|
+
(0...16).each do |num|
|
17
|
+
fixt = FixedPoint::Number.new(num, format)
|
18
|
+
puts "#{format.width}'b#{fixt.to_b} // #{fixt.to_f} "
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
puts
|
24
|
+
puts 'Signed 8 bit (6 bit int, 2 bit frac)'
|
25
|
+
|
26
|
+
format = FixedPoint::Format.new(1,4,4)
|
27
|
+
|
28
|
+
table_for_conversion = [
|
29
|
+
2.7505, 2.5, 2.25, 2, 0.5, 0, -0.5, -4.5
|
30
|
+
]
|
31
|
+
|
32
|
+
table_for_conversion.each do |num|
|
33
|
+
fixt = FixedPoint::Number.new(num, format, '_')
|
34
|
+
puts "#{format.width}'b#{fixt.to_b} // Quantised #{fixt.to_f} Source #{fixt.source}"
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
puts
|
39
|
+
puts 'As above, in hex and better string formatting in comments'
|
40
|
+
|
41
|
+
## NB "%min_length.frac_length" % float_number, min_length wil lpad with saces as required
|
42
|
+
table_for_conversion.each do |num|
|
43
|
+
fixt = FixedPoint::Number.new(num, format, '_')
|
44
|
+
puts "#{format.width}'h#{fixt.to_h} // Quantised %7.4f Source %7.4f" % [ fixt.to_f, fixt.source]
|
45
|
+
end
|
46
|
+
|
data/lib/fixed_point.rb
CHANGED
data/lib/fixed_point/number.rb
CHANGED
@@ -13,9 +13,6 @@ module FixedPoint
|
|
13
13
|
############################################
|
14
14
|
|
15
15
|
|
16
|
-
####################################
|
17
|
-
### Init
|
18
|
-
####################################
|
19
16
|
def initialize(number, input_format=Format.new(1,12,20), decimal_mark=".")
|
20
17
|
@source = number
|
21
18
|
@format = input_format
|
@@ -28,12 +25,15 @@ module FixedPoint
|
|
28
25
|
end
|
29
26
|
|
30
27
|
####################################
|
31
|
-
###
|
28
|
+
### Numerical overflow flag
|
32
29
|
####################################
|
33
30
|
def overflow?
|
34
31
|
@overflow
|
35
32
|
end
|
36
33
|
|
34
|
+
####################################
|
35
|
+
### Numerical underflow flag
|
36
|
+
####################################
|
37
37
|
def underflow?
|
38
38
|
@underflow
|
39
39
|
end
|
@@ -41,64 +41,62 @@ module FixedPoint
|
|
41
41
|
####################################
|
42
42
|
### Methods to return number formats
|
43
43
|
####################################
|
44
|
+
|
45
|
+
# Alias for binary.
|
44
46
|
def bin
|
45
47
|
binary
|
46
48
|
end
|
47
49
|
|
50
|
+
# Alias for hexadecimal.
|
48
51
|
def hex
|
49
52
|
hexadecimal
|
50
53
|
end
|
51
54
|
|
52
|
-
#
|
55
|
+
# Alias for binary.
|
53
56
|
def to_b
|
54
57
|
binary
|
55
58
|
end
|
56
59
|
|
57
|
-
#
|
60
|
+
# Alias for hexadecimal.
|
58
61
|
def to_h
|
59
62
|
hexadecimal
|
60
63
|
end
|
61
64
|
|
62
|
-
#
|
65
|
+
#Floating Point form of the quantised value.
|
63
66
|
def to_f
|
64
67
|
@quantised
|
65
68
|
end
|
66
69
|
|
67
|
-
#
|
70
|
+
#Integer, integer part of quantised value.
|
68
71
|
def to_i
|
69
72
|
@quantised.to_i
|
70
73
|
end
|
71
74
|
|
72
|
-
#
|
73
|
-
# not a to_ method as it is not representative of the whole number
|
75
|
+
# Fractional section of quantised value.
|
74
76
|
def frac
|
75
77
|
(@quantised - to_i)
|
76
78
|
end
|
77
79
|
|
78
|
-
def fraction
|
79
|
-
frac
|
80
|
-
end
|
81
|
-
|
82
80
|
####################################
|
83
|
-
###
|
81
|
+
### Calculate Binary format
|
84
82
|
####################################
|
85
83
|
def binary
|
86
84
|
#Take signed quantised value and create binary string
|
87
|
-
if (@quantised < 0) and
|
85
|
+
if (@quantised < 0) and frac.nonzero?
|
88
86
|
# Fractional numbers not negative
|
89
87
|
# So the integer part is 1 less than other wise would be and add 1+frac
|
90
88
|
ret_bin_int = (@format.max_int_unsigned + to_i )
|
91
|
-
frac_value = 1 +
|
89
|
+
frac_value = 1 + frac
|
92
90
|
end
|
93
91
|
|
94
|
-
if (@quantised < 0) and
|
92
|
+
if (@quantised < 0) and frac.zero?
|
95
93
|
ret_bin_int = (@format.max_int_unsigned + to_i + 1 )
|
96
|
-
frac_value =
|
94
|
+
frac_value = frac
|
97
95
|
end
|
98
96
|
|
99
97
|
if @quantised >= 0
|
100
98
|
ret_bin_int = self.to_i
|
101
|
-
frac_value =
|
99
|
+
frac_value = frac
|
102
100
|
end
|
103
101
|
|
104
102
|
## Convert to binary String and extend to correct length
|
@@ -118,6 +116,9 @@ module FixedPoint
|
|
118
116
|
return binary_string
|
119
117
|
end
|
120
118
|
|
119
|
+
####################################
|
120
|
+
### Calculate Hexadecimal format
|
121
|
+
####################################
|
121
122
|
def hexadecimal
|
122
123
|
#Clean Binary code (remove _ - . etc)
|
123
124
|
clean_binary = to_b.scan(/[01]/).join('')
|
@@ -185,8 +186,10 @@ module FixedPoint
|
|
185
186
|
end
|
186
187
|
end
|
187
188
|
|
188
|
-
|
189
|
-
|
189
|
+
def hex=( text )
|
190
|
+
puts "hex = Mehtod not completed"
|
191
|
+
## Strip leading 0x if present
|
192
|
+
end
|
190
193
|
|
191
194
|
|
192
195
|
#TODO
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fixed_point
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Morgan Prior
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-01-
|
18
|
+
date: 2012-01-22 00:00:00 +00:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -32,6 +32,7 @@ files:
|
|
32
32
|
- README.md
|
33
33
|
- HISTORY.md
|
34
34
|
- Rakefile
|
35
|
+
- examples/table_generation.rb
|
35
36
|
- lib/fixed_point/fixdt.rb
|
36
37
|
- lib/fixed_point/format.rb
|
37
38
|
- lib/fixed_point/number.rb
|