Class: Ansible::KNX::DPT14::DPT14_Float

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

Overview

special Bindata::Primitive class required for non-standard 32-bit floats used by DPT14

Instance Method Summary collapse

Instance Method Details

#getObject



43
44
45
46
47
# File 'lib/ansible/knx/dpt/dpt14.rb', line 43

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

#set(v) ⇒ Object

DPT9_Range = -671088.64..670760.96



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ansible/knx/dpt/dpt14.rb', line 51

def set(v)
    #raise "Value (#{v}) out of range" unless DPT9_Range === 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(-127).find{ | e |
        max_mantissa = Math.ldexp(100*mantissa, e).to_i
        max_mantissa.between?(-8388608, 8388607)
    } 
    self.sign = (mantissa < 0) ?  1 :  0 
    self.mant  = (mantissa < 0) ?  ~(max_mantissa^8388607) : max_mantissa 
    self.exp = exponent - minimum_exp  
end