fxpmath
fxpmath copied to clipboard
bin() 2's complement or 1's complement reseprentation
I've been trying to export my weights from a neural network to binary using bin function but i noticed that the representation is in ones complement. Although, in the definition of bin function np.binary_repr( ) is used which returns 2's complement binary number.
print(x0[0][0][0][0].bin(frac_dot=True))
#rval = utils.binary_repr(int(self.val), n_word=self.n_word, n_frac=n_frac_dot)
val = np.binary_repr(int(x0[0][0][0][0]), width=16)
print(val)
and the results are
111110.1101000000
1111111111111110
np.binary_repr
of int(Fxp)
will return a binary representation of integer part of fixed point number. This function completes with sign bit up to 16 bits, but those are not the 16 bits of whole fractional fixed point value.
The Fxp.bin()
function returns a binary representation of integer and fractional part, that is the same that binary representation of raw value. This function does not return one's complement.
Note:
x = Fxp() # some Fxp object
# Fxp to int converstion
x_int_a = int(x) # get the integer part of fractional fixed point value x
x_int_b = x.astype(int) # equivalent to x_int_a
x_int_c = int(x()) # reconstructed float value converted to int. You can do also int(x.astype(float)) or int(x.get_val())