Class: FixedPoint::Format

Inherits:
Object
  • Object
show all
Defined in:
lib/fixed_point/format.rb

Direct Known Subclasses

Fixdt

Instance Attribute Summary collapse

Instance Method Summary collapse

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_bitsObject (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_bitsObject (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_fracObject (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_signedObject (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_unsignedObject (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_signedObject (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_unsignedObject (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_valueObject (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_valueObject (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

#resolutionObject (readonly)

Returns the value of attribute resolution.



9
10
11
# File 'lib/fixed_point/format.rb', line 9

def resolution
  @resolution
end

#signedObject (readonly)

Returns the value of attribute signed.



5
6
7
# File 'lib/fixed_point/format.rb', line 5

def signed
  @signed
end

#widthObject (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

Returns:

  • (Boolean)


22
23
24
# File 'lib/fixed_point/format.rb', line 22

def signed?
  (@signed == 1)
end