Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Integers or Floats conversion to Binary (signed or unsigned)

$
0
0

Any tips to correct this?Example;Enter a float number: -34.5Binary: 1101 1110.1 #-33.5The output is incorrect,Because:1 1 0 1 1 1 1 0 1-128+64+0+16+8+4+2+0+0.5= -33.5

The output should be:1101 1101.1 #-34.5

Because:1 1 0 1 1 1 0 1.1-128+64+0+16+8+4+0+1+.5 = -34.5

def twos_complement(binary):    complement = ''.join('1' if bit == '0' else '0' if bit == '1' else bit for bit in binary)    carry = 1    result = ''    for bit in complement[::-1]:              if bit == '1' and carry == 1:            result += '0'        elif bit == '0' and carry == 1:            result += '1'            carry = 0        else:            result += bit    return result[::-1]def integer_to_binary(integer):    num_bits = integer.bit_length()    num_bits = max(num_bits, 8)    if integer >= 0:        binary_value = bin(integer)[2:].zfill(num_bits)        binary_value = ('0' * 4) + binary_value        binary_value = ''.join(binary_value[max(0, i - 4):i][::-1] for i in range(len(binary_value), 0, -4))        return binary_value[::-1]    elif 0 > integer >= -128:        binary_value = bin((1 << num_bits) + integer & ((1 << num_bits) - 1))[2:].zfill(num_bits)        binary_value = ('1' * 4) + binary_value        binary_value = ''.join(binary_value[max(0, i - 4):i][::-1] for i in range(len(binary_value), 0, -4))        return binary_value[::-1]    elif integer <= -129:        binary_value = bin(integer)[3:].zfill(num_bits)        binary_value = ('1' * 4) + twos_complement(binary_value)        binary_value = ''.join(binary_value[max(0, i - 4):i][::-1] for i in range(len(binary_value), 0, -4))        return binary_value[::-1]def float_to_binary(mixed):    integer_part, decimal_part = str(mixed).split('.')    binary_integer_part = integer_to_binary(int(integer_part))    binary_decimal_part = ''    if decimal_part != '0':        decimal_part = '0.'+ decimal_part        decimal = float(decimal_part)        while decimal != 0:            decimal *= 2            binary_decimal_part += str(int(decimal))            decimal -= int(decimal)    result = binary_integer_part +'.'+ binary_decimal_part    return resultinput = float(input("Enter a float number: "))result = float_to_binary(input)print("Binary:", result)

I split the integer and decimals, then the integer part will be converted to binary by using it as an argument to the integer_to_binary function.But the decimal part is converted to binary by repeatedly multiplying it by 2 and taking the integer part of the result.

My code doesn't have incorrect problem, if the expected output is integer(negative or positive) and float(positive only).The problem only arise if the expected output is float and also negative.

What i want is, if the input is negative float. The output should be correct.


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>