Method: Float#to_struct

Defined in:
lib/struct_float.rb

#to_structObject

Split a float number into a Struct::Float fields: sign, biased exponent, fraction. It is the inverse of Struct::Float#to_f.

(2.0).to_struct          # => Struct::Float[1, 1024, 0]


41
42
43
44
# File 'lib/struct_float.rb', line 41

def to_struct
  x, = [self].pack("G").unpack("B*")
  Struct::Float[x[0] == ?1 ? -1 : 1, x[1,11].to_i(2), x[12,52].to_i(2)]
end