Class: FixedPoint::Number
- Inherits:
-
Object
- Object
- FixedPoint::Number
- Defined in:
- lib/fixed_point/number.rb
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#source ⇒ Object
readonly
Input value.
Instance Method Summary collapse
-
#bin ⇒ Object
Methods to return number formats.
-
#binary ⇒ Object
Methods to calculate formats.
-
#binary=(text) ⇒ Object
Methods to set value from fixed point format.
-
#frac ⇒ Object
Method returns fractional section of fixed point type not a to_ method as it is not representative of the whole number.
- #fraction ⇒ Object
- #hex ⇒ Object
- #hexadecimal ⇒ Object
-
#initialize(number, input_format = Format.new(1,12,20), decimal_mark = ".") ⇒ Number
constructor
Init.
-
#overflow? ⇒ Boolean
Number overflow/underflow flags.
-
#to_b ⇒ Object
To Binary form.
-
#to_f ⇒ Object
To Floating Point form, quantised version of source.
-
#to_h ⇒ Object
To Hexadecimal form.
-
#to_i ⇒ Object
To Integer, limited integer part of source.
- #underflow? ⇒ Boolean
-
#warnings(val = true) ⇒ Object
TODO def log def log2.
Constructor Details
#initialize(number, input_format = Format.new(1,12,20), decimal_mark = ".") ⇒ Number
Init
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/fixed_point/number.rb', line 19 def initialize(number, input_format=Format.new(1,12,20), decimal_mark=".") @source = number @format = input_format @decimal_mark = decimal_mark @warnings = false #Now construct values based on config data @quantised = quantise_value( source ) end |
Instance Attribute Details
#format ⇒ Object (readonly)
Returns the value of attribute format.
5 6 7 |
# File 'lib/fixed_point/number.rb', line 5 def format @format end |
#source ⇒ Object (readonly)
Input value
4 5 6 |
# File 'lib/fixed_point/number.rb', line 4 def source @source end |
Instance Method Details
#bin ⇒ Object
Methods to return number formats
44 45 46 |
# File 'lib/fixed_point/number.rb', line 44 def bin binary end |
#binary ⇒ Object
Methods to calculate formats
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/fixed_point/number.rb', line 85 def binary #Take signed quantised value and create binary string if (@quantised < 0) and fraction.nonzero? # Fractional numbers not negative # So the integer part is 1 less than other wise would be and add 1+frac ret_bin_int = (@format.max_int_unsigned + to_i ) frac_value = 1 + fraction end if (@quantised < 0) and fraction.zero? ret_bin_int = (@format.max_int_unsigned + to_i + 1 ) frac_value = fraction end if @quantised >= 0 ret_bin_int = self.to_i frac_value = fraction end ## Convert to binary String and extend to correct length ret_bin_int = ret_bin_int.to_s(2).rjust(@format.int_bits, '0') ## Normalise Fractional (fractional bits shifted to appear as integer) ret_bin_frac = Integer(frac_value * 2**@format.frac_bits) ret_bin_frac = ret_bin_frac.to_s(2).rjust(@format.frac_bits, '0' ) #Decide if we need to add Decimal( Binary ) Point if @format.frac_bits > 0 binary_string = ret_bin_int + @decimal_mark + ret_bin_frac else binary_string = ret_bin_int end return binary_string end |
#binary=(text) ⇒ Object
Methods to set value from fixed point format
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/fixed_point/number.rb', line 137 def binary=(text) if text.match(/([01]*)(.?)([01]*)/ ) set_int = $1 int_bits = $1.size @decimal_mark = $2 set_frac = $3 frac_bits = $3.size #TODO Warn if the number of bits supplied does not match @format ## This should now create a new format type # Do not change the Signed format as can not detect that from bit pattern @format = Format.new(@format.signed, int_bits, frac_bits) ########################### ### Routine to generate source from binary ########################### @source = 0.0 index = 0 set_int.reverse.each_char do |x| if x == "1" #If input is signed then MSB is negative if ((index + 1) == @format.int_bits) and (@format.signed?) @source = @source + -2**index else @source = @source + 2**index end end index = index + 1 end index = 1 set_frac.each_char do |x| if x == "1" @source = @source + 2**-index end index = index + 1 end ################################ ## Set the Quantised value @quantised = @source return binary else puts "ERROR invalid input binary\(#{text}\)" return nil end end |
#frac ⇒ Object
Method returns fractional section of fixed point type not a to_ method as it is not representative of the whole number
74 75 76 |
# File 'lib/fixed_point/number.rb', line 74 def frac (@quantised - to_i) end |
#fraction ⇒ Object
78 79 80 |
# File 'lib/fixed_point/number.rb', line 78 def fraction frac end |
#hex ⇒ Object
48 49 50 |
# File 'lib/fixed_point/number.rb', line 48 def hex hexadecimal end |
#hexadecimal ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/fixed_point/number.rb', line 121 def hexadecimal #Clean Binary code (remove _ - . etc) clean_binary = to_b.scan(/[01]/).join('') #Convert to unsigned int then to hex hex = clean_binary.to_i(2).to_s(16) hex_chars = (@format.width/4.0).ceil ## Extend to the correct length ## Negative numbers will already have MSBs this if for small +VE return hex.rjust(hex_chars, '0') end |
#overflow? ⇒ Boolean
Number overflow/underflow flags
33 34 35 |
# File 'lib/fixed_point/number.rb', line 33 def overflow? @overflow end |
#to_b ⇒ Object
To Binary form
53 54 55 |
# File 'lib/fixed_point/number.rb', line 53 def to_b binary end |
#to_f ⇒ Object
To Floating Point form, quantised version of source
63 64 65 |
# File 'lib/fixed_point/number.rb', line 63 def to_f @quantised end |
#to_h ⇒ Object
To Hexadecimal form
58 59 60 |
# File 'lib/fixed_point/number.rb', line 58 def to_h hexadecimal end |
#to_i ⇒ Object
To Integer, limited integer part of source
68 69 70 |
# File 'lib/fixed_point/number.rb', line 68 def to_i @quantised.to_i end |
#underflow? ⇒ Boolean
37 38 39 |
# File 'lib/fixed_point/number.rb', line 37 def underflow? @underflow end |
#warnings(val = true) ⇒ Object
TODO def log def log2
196 197 198 |
# File 'lib/fixed_point/number.rb', line 196 def warnings( val=true ) @warnings = val end |