This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
42 additions
and
26 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]) | ||
print(result_machine_number[3]) |