Skip to content
Snippets Groups Projects
Commit 3a6460fc authored by Christof Kaufmann's avatar Christof Kaufmann
Browse files

Notebooks from ckaufmann/akis-python-framework@a0af1fea

parent 95fce00c
No related branches found
No related tags found
No related merge requests found
Showing
with 5 additions and 5 deletions
%% Cell type:markdown id:0002-b450325f90fad5a8e24c374cc1fe9c7a909e809a750ad15a9c514b51f97 tags:
# Spezielle Werte: 0, -inf, inf und NaN
Wir betrachten die speziellen Gleitkommawerte 0, -inf, inf und NaN im
Kontext von Vergleichen.
Probieren folgenden Code aus:
%% Cell type:code id:0003-3f4b66c977275c535935c7da45f249a443fcff3498de887e9a3b5d1d11f tags:
```
print('0 < inf:', 0 < float('inf'))
print('0 > inf:', 0 > float('inf'))
print('0 < -inf:', 0 < float('-inf'))
print('0 > -inf:', 0 > float('-inf'))
print('0 < nan:', 0 < float('nan'))
print('0 > nan:', 0 > float('nan'))
print('-inf < inf:', float('-inf') < float('inf'))
print('-inf < nan:', float('-inf') < float('nan'))
print('nan < inf:', float('nan') < float('inf'))
print('nan == nan:', float('nan') == float('nan'))
print('nan != nan:', float('nan') != float('nan'))
print('inf == inf:', float('inf') == float('inf'))
print('inf != inf:', float('inf') != float('inf'))
```
%% Cell type:markdown id:0007-f83f25dd7c695716547cb8f92751e0fb198c620e0ff2d69a46d19d5b246 tags:
%% Cell type:markdown id:0007-a107b4793cb5e5a0b071e1c89e288666b3b4973168040cc6894f5909fa6 tags:
Was fällt Ihnen auf?
## Lösung
- `0`: Positive und negative Null sind Spezialfälle im
IEEE-754-Standard und repräsentieren den Wert null. Dabei ist der
Exponent 0, die Mantisse 0 und v beliebig. Negative und positive 0
sind gleich:
``` python
-0.0 == 0.00
```
True
- `-inf` (negatives Unendlich): Eine Gleitkommazahl kann den Wert -inf
annehmen, wenn sie einen negativen Überlauf hat, d. h., dass man
eine Zahl als Ergebnis erhält, die kleiner als die kleinstmögliche
Zahl ist. Beispiel:
``` python
-1e308
```
-1e+308
``` python
-1e309
```
-inf
Das passiert häuftig bei einer Division durch 0. In reinem Python
gibt das einen Fehler:
``` python
-1 / 0
```
Traceback (most recent call last):
ZeroDivisionError: division by zero
Aber mit NumPy gibt es – je nach Einstellung – nur eine Warnung und
das Ergebnis ist `-inf`:
``` python
import numpy as np
bool(np.isinf(-1 / np.float64(0)))
-1 / np.array(0)
```
True
-inf
Um den Wert absichtlich zu erzeugen, benutzt man
``` python
float('-inf')
```
-inf
- `+inf` (positives Unendlich): Analog zu -inf. Generell gilt für die
Bitmuster von +inf bzw. -inf, dass der Exponent 255 bzw. 2047, die
Mantisse 0 und das Vorzeichen entsprechend sind.
- NaN (Not a Number): NaN repräsentiert einen undefinierten Wert, der
oft durch mathematische Operationen entsteht, die nicht definiert
sind, wie z. B. die Division von 0 durch 0. Für den Wert NaN muss
der Exponent wieder 255 bzw. 2047, aber die Mantisse größer 0 sein.
Dabei wird das Vorzeichen ignoriert. Beispiele:
``` python
float('inf') - float('inf')
```
nan
``` python
float('nan')
```
nan
``` python
np.nan
```
nan
``` python
bool(np.isnan(0 / np.float64(0)))
0 / np.array(0)
```
True
nan
Nun zu den Vergleichsoperatoren:
%% Cell type:code id:0008-ae7997c347611f4c5dd9e4b123a17e78bdc9ee0476bb67d2e0cb467d875 tags:
```
print('0 < inf:', 0 < float('inf'))
```
%% Output
0 < inf: True
%% Cell type:code id:0009-afc859d9041222728ab14f0eb19725cc29e2b4a81fdacf5f02855593466 tags:
```
print('0 > inf:', 0 > float('inf'))
```
%% Output
0 > inf: False
%% Cell type:code id:0010-85958bbadad1d60958bd59696145c8419ed48110033c3caba4829c98cb6 tags:
```
print('0 < -inf:', 0 < float('-inf'))
```
%% Output
0 < -inf: False
%% Cell type:code id:0011-f92e0df77c0eaeccdfc5103c42b11289e7266a4cd19f565de01a44b219f tags:
```
print('0 > -inf:', 0 > float('-inf'))
```
%% Output
0 > -inf: True
%% Cell type:code id:0012-ec279eac1fe0ace1b1f98c0b16eec27e56d8f4b7add03d8e423512e764f tags:
```
print('0 < nan:', 0 < float('nan'))
```
%% Output
0 < nan: False
%% Cell type:code id:0013-8872960f9cd31bd389b31cbfc93e70fc2e6047666bd71f52c3986553b1c tags:
```
print('0 > nan:', 0 > float('nan'))
```
%% Output
0 > nan: False
%% Cell type:code id:0014-3f3541bc436a313d67ff9e953ccbb068d57292849e317e1275d891fa2e4 tags:
```
print('-inf < inf:', float('-inf') < float('inf'))
```
%% Output
-inf < inf: True
%% Cell type:code id:0015-9a8131968f725946ad4cbeb53a9a0610bde47a39f94a9f91d1b127f1eca tags:
```
print('-inf < nan:', float('-inf') < float('nan'))
```
%% Output
-inf < nan: False
%% Cell type:code id:0016-a11b01379d4cbac9623375507d43e0be3e9893790fc47f5e32f9715eeac tags:
```
print('nan < inf:', float('nan') < float('inf'))
```
%% Output
nan < inf: False
%% Cell type:code id:0017-2767755cb767d6b05cf504e8fc1b9f3f7c26bd6ee6aecc74d1fcd321398 tags:
```
print('nan == nan:', float('nan') == float('nan'))
```
%% Output
nan == nan: False
%% Cell type:code id:0018-466de212a6706fc48100fe6059e1adcadb4e752e65cb0ebfb7e6c1daa0c tags:
```
print('nan != nan:', float('nan') != float('nan'))
```
%% Output
nan != nan: True
%% Cell type:code id:0019-c5edcc702cf64f698d0efaa8e62916e28c4ed0fe1dfaaa84aa7c2a236ae tags:
```
print('inf == inf:', float('inf') == float('inf'))
```
%% Output
inf == inf: True
%% Cell type:code id:0020-05b6542122f334e342d4b54a77f6b69700fcb2c9edf8d7b43908712c636 tags:
```
print('inf != inf:', float('inf') != float('inf'))
```
%% Output
inf != inf: False
%% Cell type:markdown id:0021-4e2f423a1535affce5ed34c188db639cde6ed128649a18821817cefcb91 tags:
Es fällt auf, dass `nan != nan` gilt. NaN ist der einzige Wert, für den
das gilt. Im IEEE-754 Standard wurde das so festgelegt um einfach prüfen
zu können, ob eine Variable kein NaN enthält: `x == x`.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment