Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.
Beide Seiten, vorherige Überarbeitung Vorherige Überarbeitung Nächste Überarbeitung | Vorherige Überarbeitung | ||
ef_informatik:zahlensysteme [2024-08-27 11:21] – [Dezimalzahlen mit Nachkommastellen] sca | ef_informatik:zahlensysteme [2024-08-27 12:40] (aktuell) – [Weitere Aufgaben] sps | ||
---|---|---|---|
Zeile 18: | Zeile 18: | ||
++++ | ++++ | ||
+ | |||
+ | ++++Praktische Python Befehle| | ||
+ | |||
+ | <code python> | ||
+ | # Length | ||
+ | len(" | ||
+ | |||
+ | # Data types | ||
+ | str(42) | ||
+ | int(" | ||
+ | float(13) | ||
+ | type(42) | ||
+ | bin(132) | ||
+ | |||
+ | # Strings | ||
+ | s = "ich bin ein String" | ||
+ | s[2:] | ||
+ | s[4:7] | ||
+ | s[:5] | ||
+ | s.replace(' | ||
+ | li = s.split(' | ||
+ | s.strip(" | ||
+ | s.lstrip(" | ||
+ | s.rstrip(" | ||
+ | s = s.upper() | ||
+ | s = s.lower() | ||
+ | s.find(" | ||
+ | many_zeros_string = " | ||
+ | |||
+ | ## TIPP: In diesem Thema bietet es sich meist an, mit Strings anstelle Listen zu arbeiten. Sollte man aber das Bedürfnis nach Listen haben, hier die wichtigsten Befehle: | ||
+ | li = [' | ||
+ | len(li) | ||
+ | li.append(' | ||
+ | li.pop(2) | ||
+ | li.remove(' | ||
+ | print(li) | ||
+ | </ | ||
+ | |||
+ | ++++ | ||
+ | |||
===== - Voraussetzungen ===== | ===== - Voraussetzungen ===== | ||
Zeile 300: | Zeile 340: | ||
<nodisp 1> | <nodisp 1> | ||
++++Lösung| | ++++Lösung| | ||
+ | |||
+ | <code python> | ||
+ | def binary_string_to_float(bin_str, | ||
+ | # SIGN | ||
+ | sign = ' | ||
+ | if bin_str[0] == ' | ||
+ | |||
+ | # SPECIAL CASE: ZERO | ||
+ | if ' | ||
+ | |||
+ | # TODO: other special cases (too small, too big, nan) | ||
+ | |||
+ | # if int convert into float | ||
+ | if not ' | ||
+ | |||
+ | # EXPONENT, MANTISSA | ||
+ | bin_str = bin_str.lstrip(' | ||
+ | bias = (2**len_exp - 2)//2 | ||
+ | mantissa = bin_str | ||
+ | |||
+ | # determine exponent | ||
+ | i_one = bin_str.find(' | ||
+ | i_point = bin_str.find(' | ||
+ | exponent = i_point - i_one | ||
+ | if exponent > 0: exponent -= 1 | ||
+ | |||
+ | # remove point from mantissa | ||
+ | if ' | ||
+ | |||
+ | # add bias to exponent | ||
+ | exp_w_bias = bin(exponent+bias)[2: | ||
+ | exp_w_bias = ' | ||
+ | if len(exp_w_bias) > len_exp: | ||
+ | return [sign,' | ||
+ | | ||
+ | # normalize mantissa | ||
+ | mantissa = mantissa.lstrip(' | ||
+ | mantissa_normalized = mantissa[1:: | ||
+ | # ensure mantissa has correct length | ||
+ | if len(mantissa_normalized) > len_mant: | ||
+ | mantissa_normalized = mantissa_normalized[0: | ||
+ | else: | ||
+ | mantissa_normalized += ' | ||
+ | |||
+ | # TODO: round mantissa correctly | ||
+ | | ||
+ | return [sign, | ||
+ | </ | ||
+ | |||
++++ | ++++ | ||
</ | </ | ||
Zeile 375: | Zeile 464: | ||
++++Lösung| | ++++Lösung| | ||
- | |||
- | <nodisp 1> | ||
- | |||
- | <code python> | ||
- | def binary_string_to_float(bin_str, | ||
- | # SIGN | ||
- | sign = ' | ||
- | if bin_str[0] == ' | ||
- | |||
- | # SPECIAL CASE: ZERO | ||
- | if ' | ||
- | |||
- | # TODO: other special cases (too small, too big, nan) | ||
- | |||
- | # if int convert into float | ||
- | if not ' | ||
- | |||
- | # EXPONENT, MANTISSA | ||
- | bin_str = bin_str.lstrip(' | ||
- | bias = (2**len_exp - 2)//2 | ||
- | mantissa = bin_str | ||
- | |||
- | # determine exponent | ||
- | i_one = bin_str.find(' | ||
- | i_point = bin_str.find(' | ||
- | exponent = i_point - i_one | ||
- | if exponent > 0: exponent -= 1 | ||
- | |||
- | # remove point from mantissa | ||
- | if ' | ||
- | |||
- | # add bias to exponent | ||
- | exp_w_bias = bin(exponent+bias)[2: | ||
- | exp_w_bias = ' | ||
- | if len(exp_w_bias) > len_exp: | ||
- | return [sign,' | ||
- | | ||
- | # normalize mantissa | ||
- | mantissa = mantissa.lstrip(' | ||
- | mantissa_normalized = mantissa[1:: | ||
- | # ensure mantissa has correct length | ||
- | if len(mantissa_normalized) > len_mant: | ||
- | mantissa_normalized = mantissa_normalized[0: | ||
- | else: | ||
- | mantissa_normalized += ' | ||
- | |||
- | # TODO: round mantissa correctly | ||
- | | ||
- | return [sign, | ||
- | </ | ||
- | </ | ||
Zeile 532: | Zeile 570: | ||
* Exponenten ermitteln | * Exponenten ermitteln | ||
* Mantissen ermitteln | * Mantissen ermitteln | ||
- | * Exponenten angleichen, Komma der Mantissen entsprechend verschieben. Bias beibehalten | + | * Exponenten angleichen, Komma der Mantissen entsprechend verschieben. Bias beibehalten. |
* Addition durchführen | * Addition durchführen | ||
* Komma verschieben, | * Komma verschieben, |