Experimental Float Reader

Programming related discussions related to game research
El Duderino
Posts: 14
Joined: Fri Aug 08, 2014 12:30 am

Experimental Float Reader

Post by El Duderino »

I was bored and looked at the Wikipedia article on floating point numbers.
Actually the math was the only part I could understand and said "To hell with the actual article!" :lol:
Anyway, I've only tested this on FP32 and FP16s because that's all I have.....plus I was feeling fruity and "added a few". :roll:
Also, it only takes 2 lines of code (1 if you disregard the elif line) to add a new format - pretty simple.

EDIT: Needs the bitreader: http://opensourcehacker.com/2010/09/08/bitreader-python-module-for-reading-bits-from-bytes/

Code: Select all

def mb(n):
   l = []
   k = '{:08b}'.format(n)
   for x in k: l.append(int(x))
   return l



def rdf(data,bits,order):
   e,sign,exponent,significand,bias = -1,0,0,0,0
   if bits == 512:     # satire
      sign,exponent,significand,bias = 1,25,487,16777215
   elif bits == 256:   # scientific applications only please
      sign,exponent,significand,bias = 1,20,236,524287
   elif bits == 128:
      sign,exponent,significand,bias = 1,15,112,16383
   elif bits == 80:
      sign,exponent,significand,bias = 1,15,64,16383
   elif bits == 64:
      sign,exponent,significand,bias = 1,11,52,1023
   elif bits == 32:
      sign,exponent,significand,bias = 1,8,23,127
   elif bits == 16:
      sign,exponent,significand,bias = 1,5,10,15
   elif bits == 14:
      exponent,significand,bias = 5,9,15
   elif bits == 11:
      exponent,significand,bias = 5,6,15
   elif bits == 10:
      exponent,significand,bias = 5,5,15
   elif bits == 8:    # more satire
      exponent,significand,bias = 3,5,3
   elif bits == 5:    # a moose once bit my sister
      exponent,significand,bias = 2,3,1
   else:
      print '\n\n\n\n\n\n\n\n\n\n\n\n'
      print 'Congradulations! You\'re an idiot!'
      print '\n\n\n\n'
   if sign != 0:   
      spec = ['sign',sign, 'exponent',exponent, 'significand',significand]
   else:
      spec = ['exponent',exponent, 'significand',significand]
   if order[:3].lower() == 'big':
      reader = BitReader(spec, BitReader.BIG_ENDIAN)
   elif order[:6].lower() == 'little':
      reader = BitReader(spec, BitReader.LITTLE_ENDIAN)
   else:
      print '\n\n'
      print 'No such byte order as:  %s'%order
      print '\n\n'
   vBits = reader.read(data)
   ex = vBits.exponent - bias
   fr = mb(vBits.significand)
   whatsYourSign = vBits.sign
   while len(fr) < significand:
      fr.insert(0,0)
   temp=[]
   for j in fr:
      if j == 1:
         temp.append(2**e)
      e-=1
   val = (sum(temp)+1) * 2**ex
   if whatsYourSign != 0:
      val *= -1
   return val





#I'm so sick of all that "foo"/"bar"/"baz" garbage!
cougarMom = []

for boyToy in range(100):
   px = struct.unpack("2B", fh.read(2))
   x = rdf(px,16,'bIG')
   py = struct.unpack("2B", fh.read(2))
   y = rdf(py,16,'biG')
   pz = struct.unpack("2B", fh.read(2))
   z = rdf(pz,16,'*SPAM*')
   struct.unpack("26B",fh.read(26))[0]
   cougarMom.append((x,y,z))



fh.close()
aluigi
Site Admin
Posts: 12984
Joined: Wed Jul 30, 2014 9:32 pm

Re: Experimental Float Reader

Post by aluigi »

that cougarMom array made my day :)