I would like to be able to extract the significand and exponent of floating-point numbers in NumPy. Getting the exponent as an integer is fine and ok for the significand. Getting the significand as a bitfield would be even more convienient.
I am aware that Python floats have a hex method; however, I wish to use numpy.float32, numpy arrays, and ufuncs. I am also aware of the numpy view method that allows me to see the float as an integer and thus as a binary string:
>>> import numpy as np
>>> b = bin(np.float32(1.23456789).view(np.int32))
'0b111111100111100000011001010010'
>>> b[-23:] # extract last 23 bits of IEEE 754 binary32 float, is significand
'00111100000011001010010'
Extracting the exponent and sign in this way is not convenient, as leading 0s are dropped by bin. (I could left-pad to 32 bits with 0s though…)
In any case, because bin is not a ufunc, this is not convenient and I would have to iterate over the array.
Isn't there any more convenient approach to doing what I want?
numpy.frexpnumpy.frexpgives a decomposition into exponent and mantissa, not the decomposition into exponent and significand encoded in an IEEE 754 float. (The exponent given byfrexpdiffers by one from the IEEE 754 float exponent.)frexpresult to the IEEE 754 decomposition. As you say, the exponent differs by one, so subtract one from the exponent and double the significand.