Module: BinData::FloatingPoint
- Defined in:
- lib/bindata/float.rb
Overview
Defines a number of classes that contain a floating point number. The float is defined by precision and endian.
Class Method Summary collapse
- .create_num_bytes_code(precision) ⇒ Object
- .create_read_code(precision, endian) ⇒ Object
- .create_to_binary_s_code(precision, endian) ⇒ Object
- .define_methods(float_class, precision, endian) ⇒ Object
Class Method Details
.create_num_bytes_code(precision) ⇒ Object
32 33 34 |
# File 'lib/bindata/float.rb', line 32 def create_num_bytes_code(precision) (precision == :single) ? 4 : 8 end |
.create_read_code(precision, endian) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/bindata/float.rb', line 36 def create_read_code(precision, endian) if precision == :single unpack = (endian == :little) ? 'e' : 'g' nbytes = 4 else # double_precision unpack = (endian == :little) ? 'E' : 'G' nbytes = 8 end "io.readbytes(#{nbytes}).unpack('#{unpack}').at(0)" end |
.create_to_binary_s_code(precision, endian) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/bindata/float.rb', line 48 def create_to_binary_s_code(precision, endian) if precision == :single pack = (endian == :little) ? 'e' : 'g' else # double_precision pack = (endian == :little) ? 'E' : 'G' end "[val].pack('#{pack}')" end |
.define_methods(float_class, precision, endian) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/bindata/float.rb', line 9 def define_methods(float_class, precision, endian) float_class.module_eval <<-END def _do_num_bytes #{create_num_bytes_code(precision)} end #--------------- private def sensible_default 0.0 end def value_to_binary_string(val) #{create_to_binary_s_code(precision, endian)} end def read_and_return_value(io) #{create_read_code(precision, endian)} end END end |