Feb-16-2020, 01:49 PM
(Feb-11-2020, 07:49 PM)new_to_python Wrote:(Feb-11-2020, 02:54 PM)DeaD_EyE Wrote:import struct def double_to_bin(value): """ Convert an float to double (64 bit) binary representation Please check here: http://www.binaryconvert.com/result_double.html """ value_sum = sum( value << (shift * 8) for shift, value in enumerate(struct.pack('<d', value)) ) bin_str = f'{value_sum:064b}' return { 'dec': value, 'hex': f'{value_sum:08x}', 'bin': bin_str, 'sign': bin_str[0] == '1', 'exponent': bin_str[1:12], 'mantissa': bin_str[12:], }Better you check it online if the result is right.
I checked it with 1.0 and 0.3.
Thanks. In this case, 999 and 999.0 produces the same binary representation which is:
'0100000010001111001110000000000000000000000000000000000000000000'
So python treats them as the same?
Hi, anybody else knows the answer?