Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
refactor: ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
boostvolt authored Jan 13, 2024
1 parent b2c47c6 commit d07f454
Showing 1 changed file with 42 additions and 26 deletions.
68 changes: 42 additions & 26 deletions Scripts/Kapitel_2/maschinenzahl.py
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])

0 comments on commit d07f454

Please sign in to comment.