diff --git a/Scripts/Kapitel_2/maschinenzahl.py b/Scripts/Kapitel_2/maschinenzahl.py index 0a461b7..5534a3f 100644 --- a/Scripts/Kapitel_2/maschinenzahl.py +++ b/Scripts/Kapitel_2/maschinenzahl.py @@ -1,56 +1,72 @@ -def machine_number(value, exponent=1,mantissa=5): - #Vorzeichen berechnen +def machine_number(value, exponent=1, mantissa=5): + # Vorzeichen berechnen if value > 0: - sign = '0' + sign = "0" else: - sign = '1' + sign = "1" - #Bias berechnen - bias = 2**(exponent-1)-1 + # Bias berechnen + bias = 2 ** (exponent - 1) - 1 - #Vorkommastellen berechnen + # Vorkommastellen berechnen integerValue = abs(int(value)) binaryIntegerValue = bin(integerValue)[2:] - #Exponent berechnen - binaryIntegerValueExponent = len(binaryIntegerValue)-1 + # Exponent berechnen + binaryIntegerValueExponent = len(binaryIntegerValue) - 1 - #Nachkommastellen berechnen + # Nachkommastellen berechnen fractionalValue = abs(value) - abs(integerValue) binaryFractionalValue = convert_fraction_to_binary(fractionalValue) - #Mantisse berechnen + # Mantisse berechnen mantissaValue = binaryIntegerValue[1:] + binaryFractionalValue correctMantissaLength = mantissaValue[:mantissa] if len(correctMantissaLength) < mantissa: - correctMantissaLength = correctMantissaLength + '0'*(mantissa-len(correctMantissaLength)) + correctMantissaLength = correctMantissaLength + "0" * ( + mantissa - len(correctMantissaLength) + ) - #bias berechnen + # bias berechnen biasValue = bin(binaryIntegerValueExponent + bias)[2:] - #Ergebnis als String zusammenfassen - strResult = str("Vorzeichen (0 Positiv, 1 Negative): " + sign + "\n" - + "Exponent: " + biasValue + "\n" - + "Mantisse inklusive führende 1: 1." + correctMantissaLength + "\n" - + "|" + sign + "|" + biasValue + "|" + correctMantissaLength + "|") + # Ergebnis als String zusammenfassen + strResult = str( + "Vorzeichen (0 Positiv, 1 Negative): " + + sign + + "\n" + + "Exponent: " + + biasValue + + "\n" + + "Mantisse inklusive führende 1: 1." + + correctMantissaLength + + "\n" + + "|" + + sign + + "|" + + biasValue + + "|" + + correctMantissaLength + + "|" + ) - - #Ergebnis + # Ergebnis return sign, biasValue, correctMantissaLength, strResult + def convert_fraction_to_binary(fraction): - """ Convert the fractional part of a decimal number to binary. """ - binary = '' + """Convert the fractional part of a decimal number to binary.""" + binary = "" while fraction and len(binary) < 64: # Limit to 64 iterations fraction *= 2 if fraction >= 1: - binary += '1' + binary += "1" fraction -= 1 else: - binary += '0' + binary += "0" return binary -if __name__ == '__main__': +if __name__ == "__main__": result_machine_number = machine_number(value=1.7320508) - print(result_machine_number[3]) \ No newline at end of file + print(result_machine_number[3])