Class: DPT9_Float

Inherits:
BinData::Primitive
  • Object
show all
Defined in:
lib/ansible/knx/dpt/tests/test_dpt9.rb

Instance Method Summary collapse

Instance Method Details

#getObject



10
11
12
13
14
# File 'lib/ansible/knx/dpt/tests/test_dpt9.rb', line 10

def get
    puts "get, sign=#{sign} exp=#{exp} mant=#{mant}"
    mantissa = (self.sign==1) ? ~(self.mant^2047) : self.mant
    return Math.ldexp((0.01*mantissa), self.exp)
end

#set(v) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ansible/knx/dpt/tests/test_dpt9.rb', line 16

def set(v)
    mantissa, exponent = Math.frexp(v)
    puts "#{self}.set(#{v}) with initial mantissa=#{mantissa}, exponent=#{exponent}"
    # find the minimum exponent that will upsize the normalized mantissa (0,5 to 1 range)
    # in order to fit in 11 bits (-2048..2047)
    max_mantissa = 0
    minimum_exp = exponent.downto(-15).find{ | e |
        max_mantissa = Math.ldexp(100*mantissa, e).to_i
        max_mantissa.between?(-2048, 2047)
    } 
    self.sign = (mantissa < 0) ?  1 :  0 
    self.mant  = (mantissa < 0) ?  ~(max_mantissa^2047) : max_mantissa 
    self.exp = exponent - minimum_exp  
    puts "... set(#{v}) finished: sign=#{sign}, mantissa=#{mant}, exponent=#{exp}"
end