Class: FixedPoint::Format
- Inherits:
-
Object
- Object
- FixedPoint::Format
- Defined in:
- lib/fixed_point/format.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#frac_bits ⇒ Object
readonly
Returns the value of attribute frac_bits.
-
#int_bits ⇒ Object
readonly
Returns the value of attribute int_bits.
-
#max_frac ⇒ Object
readonly
Returns the value of attribute max_frac.
-
#max_int_signed ⇒ Object
readonly
Returns the value of attribute max_int_signed.
-
#max_int_unsigned ⇒ Object
readonly
Returns the value of attribute max_int_unsigned.
-
#max_signed ⇒ Object
readonly
Returns the value of attribute max_signed.
-
#max_unsigned ⇒ Object
readonly
Returns the value of attribute max_unsigned.
-
#max_value ⇒ Object
readonly
Returns the value of attribute max_value.
-
#min_value ⇒ Object
readonly
Returns the value of attribute min_value.
-
#resolution ⇒ Object
readonly
Returns the value of attribute resolution.
-
#signed ⇒ Object
readonly
Returns the value of attribute signed.
-
#width ⇒ Object
readonly
Calculated attributes.
Instance Method Summary collapse
-
#calculate_attributes(signed, int_bits, frac_bits) ⇒ Object
Format Should hold the maxim possible values not part of number (Value).
-
#initialize(signed, int_bits, frac_bits) ⇒ Format
constructor
A new instance of Format.
- #signed? ⇒ Boolean
Constructor Details
#initialize(signed, int_bits, frac_bits) ⇒ Format
Returns a new instance of Format.
14 15 16 17 18 19 20 |
# File 'lib/fixed_point/format.rb', line 14 def initialize(signed, int_bits, frac_bits) @signed = signed @int_bits = int_bits @frac_bits = frac_bits calculate_attributes(signed, int_bits, frac_bits) end |
Instance Attribute Details
#frac_bits ⇒ Object (readonly)
Returns the value of attribute frac_bits.
5 6 7 |
# File 'lib/fixed_point/format.rb', line 5 def frac_bits @frac_bits end |
#int_bits ⇒ Object (readonly)
Returns the value of attribute int_bits.
5 6 7 |
# File 'lib/fixed_point/format.rb', line 5 def int_bits @int_bits end |
#max_frac ⇒ Object (readonly)
Returns the value of attribute max_frac.
11 12 13 |
# File 'lib/fixed_point/format.rb', line 11 def max_frac @max_frac end |
#max_int_signed ⇒ Object (readonly)
Returns the value of attribute max_int_signed.
10 11 12 |
# File 'lib/fixed_point/format.rb', line 10 def max_int_signed @max_int_signed end |
#max_int_unsigned ⇒ Object (readonly)
Returns the value of attribute max_int_unsigned.
10 11 12 |
# File 'lib/fixed_point/format.rb', line 10 def max_int_unsigned @max_int_unsigned end |
#max_signed ⇒ Object (readonly)
Returns the value of attribute max_signed.
12 13 14 |
# File 'lib/fixed_point/format.rb', line 12 def max_signed @max_signed end |
#max_unsigned ⇒ Object (readonly)
Returns the value of attribute max_unsigned.
12 13 14 |
# File 'lib/fixed_point/format.rb', line 12 def max_unsigned @max_unsigned end |
#max_value ⇒ Object (readonly)
Returns the value of attribute max_value.
9 10 11 |
# File 'lib/fixed_point/format.rb', line 9 def max_value @max_value end |
#min_value ⇒ Object (readonly)
Returns the value of attribute min_value.
9 10 11 |
# File 'lib/fixed_point/format.rb', line 9 def min_value @min_value end |
#resolution ⇒ Object (readonly)
Returns the value of attribute resolution.
9 10 11 |
# File 'lib/fixed_point/format.rb', line 9 def resolution @resolution end |
#signed ⇒ Object (readonly)
Returns the value of attribute signed.
5 6 7 |
# File 'lib/fixed_point/format.rb', line 5 def signed @signed end |
#width ⇒ Object (readonly)
Calculated attributes
8 9 10 |
# File 'lib/fixed_point/format.rb', line 8 def width @width end |
Instance Method Details
#calculate_attributes(signed, int_bits, frac_bits) ⇒ Object
Format Should hold the maxim possible values not part of number (Value)
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/fixed_point/format.rb', line 28 def calculate_attributes(signed, int_bits, frac_bits) @width = int_bits + frac_bits #Calculate Number ranges @resolution = 2**(-@frac_bits) @max_frac = 1 - 2**(-@frac_bits) @max_int_signed = ( 2**(@int_bits - 1) - 1) @max_int_unsigned = ( 2**@int_bits - 1) @max_signed = @max_int_signed + @max_frac @max_unsigned = @max_int_unsigned + @max_frac @min_signed = (-2**(@int_bits-1)) @min_unsigned = 0 #Set Max/Min values if signed? @max_value = @max_signed @min_value = @min_signed else @max_value = @max_unsigned @min_value = @min_unsigned end end |
#signed? ⇒ Boolean
22 23 24 |
# File 'lib/fixed_point/format.rb', line 22 def signed? (@signed == 1) end |