Class: Struct::Float

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

Overview

Struct class for the fields that form a Float according to the IEEE-754 standard: sign, biased exponent, fraction.

Instance Method Summary collapse

Instance Method Details

#inspectObject

Overrides Object#inspect.



28
29
30
# File 'lib/struct_float.rb', line 28

def inspect
  self.class.name + to_a.inspect
end

#to_fObject

Assemble a float number from a Float::Struct. It is the inverse of Float#to_struct.

Struct::Float[1,1024,0].to_f
                         # => 2.0


23
24
25
# File 'lib/struct_float.rb', line 23

def to_f
  ["%b%011b%052b" % [sign < 0 ? 1 : 0, biased_exp , fraction]].pack("B*").unpack("G").first
end