def getf(self):
"""convert the stored floating-point number into a python native float"""
exponentbias = (2**self.components[1])/2 - 1
res = bitmap.new( self.__getvalue__(), sum(self.components) )
# extract components
res,sign = bitmap.shift(res, self.components[0])
res,exponent = bitmap.shift(res, self.components[1])
res,mantissa = bitmap.shift(res, self.components[2])
if exponent > 0 and exponent < (2**self.components[2]-1):
# convert to float
s = -1 if sign else +1
e = exponent - exponentbias
m = 1.0 + (float(mantissa) / 2**self.components[2])
# done
return math.ldexp( math.copysign(m,s), e)
# FIXME: this should return NaN or something
Log.warn('float_t.getf : {:s} : Invalid exponent value : {:d}'.format(self.instance(), exponent))
return 0.0
评论列表
文章目录