[Numpy-discussion] IEEE floating point arithmetics

Travis Oliphant oliphant.travis at ieee.org
Sat May 26 14:41:38 EDT 2007


lorenzo bolla wrote:
> Hi all.
> I have to work with floating point arithmetics and I found a module 
> called "double module" 
> (http://symptotic.com/mj/double/public/double-module.html 
> <http://symptotic.com/mj/double/public/double-module.html>) that does 
> what I'd like. Basically, I would like to find the nearest smaller and 
> bigger floating point numbers, given a "real" real number (the 
> function |doubleToLongBits| 
> <http://symptotic.com/mj/double/public/double-module.html#doubleToLongBits> can 
> be used to do just that.).
> Is there something similar in numpy?
> In other words: is there a way, in numpy, to convert a floating point 
> number to its binary representation and back?

Sure:

floating-point ---> binary representation

a = numpy.pi
bitstr = numpy.binary_repr(numpy.float_(a).view('u8'))

print bitstr

'100000000001001001000011111101101010100010001000010110100011000'

Then:

binary representation --> floating point

number = numpy.uint64(int(bitstr,base=2)).view('f8')

-Travis






More information about the NumPy-Discussion mailing list