Last active
September 18, 2018 06:43
-
-
Save jcchurch/53882dfbc00f62aba26d023a8d3dd901 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This program demonstrates how to convert | |
floating-point numbers to IEEE 754 standard. | |
Set the "data_type" variable at the beginning | |
to either "double" or "float" to test how the | |
numbers are arranged differently under each type. | |
""" | |
data_type = "double" | |
exponent_length = 0 | |
significand_lenth = 0 | |
if data_type == "float": | |
exponent_length = 8 | |
significand_lenth = 23 | |
if data_type == "double": | |
exponent_length = 11 | |
significand_lenth = 52 | |
exponent_bais = 2**(exponent_length - 1) - 1 | |
value = float(input("Enter a value: ")) | |
""" | |
------------------------------------------------ | |
Part 1: Determine the sign. | |
------------------------------------------------ | |
""" | |
print() | |
print("Determine the sign bit.") | |
sign_bit = "0" | |
if value < 0: | |
sign_bit = "1" | |
value = value * -1 | |
print("Sign:", sign_bit) | |
""" | |
------------------------------------------------ | |
Part 2: Convert the number to binary. | |
------------------------------------------------ | |
""" | |
print() | |
print("Convert the absolute value of our number to binary.") | |
whole_part = int(value) | |
fractional_part = value - whole_part | |
whole_str = "" | |
while whole_part > 0: | |
whole_str = str(whole_part % 2) + whole_str | |
whole_part = int(whole_part / 2) | |
if whole_str == "": | |
whole_str = "0" | |
fractional_str = "" | |
while fractional_part > 0: | |
fractional_str = fractional_str + str(int(fractional_part * 2)) | |
fractional_part *= 2 | |
fractional_part -= int(fractional_part) | |
if fractional_str == "": | |
fractional_str = "0" | |
print("Binary: ", whole_str, ".", fractional_str) | |
""" | |
------------------------------------------------ | |
Part 3: Normalize. | |
------------------------------------------------ | |
""" | |
print() | |
print("Convert the binary to a number which is 1.xxx * 2 ^ N:") | |
value_exponent = 0 | |
while len(whole_str) > 1: | |
fractional_str = whole_str[-1] + fractional_str | |
whole_str = whole_str[:-1] | |
value_exponent += 1 | |
whole = int(whole_str) | |
while whole == 0 and int(fractional_str) > 0: | |
whole = int(fractional_str[0]) | |
fractional_str = fractional_str[1:] | |
value_exponent -= 1 | |
if fractional_str == "": | |
fractional_str = "0" | |
print("New Binary: ", whole, ".", fractional_str, "* 2 ^", value_exponent) | |
print() | |
print("----") | |
print("At this point, the whole part of the binary should be 1. It is no longer used.") | |
print("----") | |
""" | |
------------------------------------------------ | |
Part 4: Add the exponent bias. | |
------------------------------------------------ | |
""" | |
print() | |
print("Add the exponent_bais (", exponent_bais, ") to the exponent.") | |
value_exponent += exponent_bais | |
print("New exponent: ", value_exponent) | |
""" | |
------------------------------------------------ | |
Part 5: Build the exponent string. | |
------------------------------------------------ | |
""" | |
print() | |
print("Build exponent string.") | |
exponent_str = "" | |
for i in range(exponent_length): | |
exponent_str = str(value_exponent % 2) + exponent_str | |
value_exponent = int(value_exponent / 2) | |
print("Exponent string: ", exponent_str) | |
""" | |
------------------------------------------------ | |
Part 6: Build the significand. | |
------------------------------------------------ | |
""" | |
print() | |
print("Build significand string.") | |
significand_str = fractional_str[:significand_lenth] + "0" * (significand_lenth - len(fractional_str[:significand_lenth])) | |
print("Significand:", significand_str) | |
""" | |
------------------------------------------------ | |
Part 7: Put all of the parts together. | |
------------------------------------------------ | |
""" | |
print() | |
print("Finally, build the binary value.") | |
binary = sign_bit + exponent_str + significand_str | |
print("Binary:", binary) | |
print("Binary Length:", len(binary), "bits") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment