Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
AKIS Python Notebooks
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christof Kaufmann
AKIS Python Notebooks
Commits
95fce00c
Commit
95fce00c
authored
9 months ago
by
Christof Kaufmann
Browse files
Options
Downloads
Patches
Plain Diff
Notebooks from ckaufmann/akis-python-framework@2372db24
parent
85016c66
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
06-gleitkommazahlen/07-speziellewerte-sol.ipynb
+8
-8
8 additions, 8 deletions
06-gleitkommazahlen/07-speziellewerte-sol.ipynb
with
8 additions
and
8 deletions
06-gleitkommazahlen/07-speziellewerte-sol.ipynb
+
8
−
8
View file @
95fce00c
...
@@ -88,10 +88,10 @@
...
@@ -88,10 +88,10 @@
"\n",
"\n",
" ``` python\n",
" ``` python\n",
" import numpy as np\n",
" import numpy as np\n",
"
-1 / np.array(0
)\n",
"
bool(np.isinf(-1 / np.float64(0))
)\n",
" ```\n",
" ```\n",
"\n",
"\n",
"
-inf
\n",
"
True
\n",
"\n",
"\n",
" Um den Wert absichtlich zu erzeugen, benutzt man\n",
" Um den Wert absichtlich zu erzeugen, benutzt man\n",
"\n",
"\n",
...
@@ -112,32 +112,32 @@
...
@@ -112,32 +112,32 @@
" Dabei wird das Vorzeichen ignoriert. Beispiele:\n",
" Dabei wird das Vorzeichen ignoriert. Beispiele:\n",
"\n",
"\n",
" ``` python\n",
" ``` python\n",
"
0 / np.array(0
)\n",
"
float('inf') - float('inf'
)\n",
" ```\n",
" ```\n",
"\n",
"\n",
" nan\n",
" nan\n",
"\n",
"\n",
" ``` python\n",
" ``` python\n",
" float('
inf') - float('inf
')\n",
" float('
nan
')\n",
" ```\n",
" ```\n",
"\n",
"\n",
" nan\n",
" nan\n",
"\n",
"\n",
" ``` python\n",
" ``` python\n",
"
float('
nan
')
\n",
"
np.
nan\n",
" ```\n",
" ```\n",
"\n",
"\n",
" nan\n",
" nan\n",
"\n",
"\n",
" ``` python\n",
" ``` python\n",
" np.nan\n",
"
bool(
np.
is
nan
(0 / np.float64(0)))
\n",
" ```\n",
" ```\n",
"\n",
"\n",
"
nan
\n",
"
True
\n",
"\n",
"\n",
"Nun zu den Vergleichsoperatoren:"
"Nun zu den Vergleichsoperatoren:"
],
],
"id": "0007-
cc3c3675cbeeb66bc009f1d2a57f89996381f4f12a69d33b9f81171b9f3
"
"id": "0007-
f83f25dd7c695716547cb8f92751e0fb198c620e0ff2d69a46d19d5b246
"
},
},
{
{
"cell_type": "code",
"cell_type": "code",
...
...
%% Cell type:markdown id:0002-b450325f90fad5a8e24c374cc1fe9c7a909e809a750ad15a9c514b51f97 tags:
%% Cell type:markdown id:0002-b450325f90fad5a8e24c374cc1fe9c7a909e809a750ad15a9c514b51f97 tags:
# Spezielle Werte: 0, -inf, inf und NaN
# Spezielle Werte: 0, -inf, inf und NaN
Wir betrachten die speziellen Gleitkommawerte 0, -inf, inf und NaN im
Wir betrachten die speziellen Gleitkommawerte 0, -inf, inf und NaN im
Kontext von Vergleichen.
Kontext von Vergleichen.
Probieren folgenden Code aus:
Probieren folgenden Code aus:
%% Cell type:code id:0003-3f4b66c977275c535935c7da45f249a443fcff3498de887e9a3b5d1d11f tags:
%% 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 < -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('0 > nan:', 0 > float('nan'))
print('0 > nan:', 0 > float('nan'))
print('-inf < inf:', float('-inf') < float('inf'))
print('-inf < inf:', float('-inf') < float('inf'))
print('-inf < nan:', float('-inf') < float('nan'))
print('-inf < nan:', float('-inf') < float('nan'))
print('nan < inf:', float('nan') < float('inf'))
print('nan < inf:', float('nan') < float('inf'))
print('nan == nan:', float('nan') == float('nan'))
print('nan == nan:', float('nan') == float('nan'))
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'))
print('inf != inf:', float('inf') != float('inf'))
print('inf != inf:', float('inf') != float('inf'))
```
```
%% Cell type:markdown id:0007-
cc3c3675cbeeb66bc009f1d2a57f89996381f4f12a69d33b9f81171b9f3
tags:
%% Cell type:markdown id:0007-
f83f25dd7c695716547cb8f92751e0fb198c620e0ff2d69a46d19d5b246
tags:
Was fällt Ihnen auf?
Was fällt Ihnen auf?
## Lösung
## Lösung
-
`0`
: Positive und negative Null sind Spezialfälle im
-
`0`
: Positive und negative Null sind Spezialfälle im
IEEE-754-Standard und repräsentieren den Wert null. Dabei ist der
IEEE-754-Standard und repräsentieren den Wert null. Dabei ist der
Exponent 0, die Mantisse 0 und v beliebig. Negative und positive 0
Exponent 0, die Mantisse 0 und v beliebig. Negative und positive 0
sind gleich:
sind gleich:
``` python
``` python
-0.0 == 0.00
-0.0 == 0.00
```
```
True
True
-
`-inf`
(negatives Unendlich): Eine Gleitkommazahl kann den Wert -inf
-
`-inf`
(negatives Unendlich): Eine Gleitkommazahl kann den Wert -inf
annehmen, wenn sie einen negativen Überlauf hat, d. h., dass man
annehmen, wenn sie einen negativen Überlauf hat, d. h., dass man
eine Zahl als Ergebnis erhält, die kleiner als die kleinstmögliche
eine Zahl als Ergebnis erhält, die kleiner als die kleinstmögliche
Zahl ist. Beispiel:
Zahl ist. Beispiel:
``` python
``` python
-1e308
-1e308
```
```
-1e+308
-1e+308
``` python
``` python
-1e309
-1e309
```
```
-inf
-inf
Das passiert häuftig bei einer Division durch 0. In reinem Python
Das passiert häuftig bei einer Division durch 0. In reinem Python
gibt das einen Fehler:
gibt das einen Fehler:
``` python
``` python
-1 / 0
-1 / 0
```
```
Traceback (most recent call last):
Traceback (most recent call last):
ZeroDivisionError: division by zero
ZeroDivisionError: division by zero
Aber mit NumPy gibt es – je nach Einstellung – nur eine Warnung und
Aber mit NumPy gibt es – je nach Einstellung – nur eine Warnung und
das Ergebnis ist `-inf`:
das Ergebnis ist `-inf`:
``` python
``` python
import numpy as np
import numpy as np
-1 / np.
array
(0)
bool(np.isinf(
-1 / np.
float64
(0)
))
```
```
-inf
True
Um den Wert absichtlich zu erzeugen, benutzt man
Um den Wert absichtlich zu erzeugen, benutzt man
``` python
``` python
float('-inf')
float('-inf')
```
```
-inf
-inf
-
`+inf`
(positives Unendlich): Analog zu -inf. Generell gilt für die
-
`+inf`
(positives Unendlich): Analog zu -inf. Generell gilt für die
Bitmuster von +inf bzw. -inf, dass der Exponent 255 bzw. 2047, die
Bitmuster von +inf bzw. -inf, dass der Exponent 255 bzw. 2047, die
Mantisse 0 und das Vorzeichen entsprechend sind.
Mantisse 0 und das Vorzeichen entsprechend sind.
-
NaN (Not a Number): NaN repräsentiert einen undefinierten Wert, der
-
NaN (Not a Number): NaN repräsentiert einen undefinierten Wert, der
oft durch mathematische Operationen entsteht, die nicht definiert
oft durch mathematische Operationen entsteht, die nicht definiert
sind, wie z. B. die Division von 0 durch 0. Für den Wert NaN muss
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.
der Exponent wieder 255 bzw. 2047, aber die Mantisse größer 0 sein.
Dabei wird das Vorzeichen ignoriert. Beispiele:
Dabei wird das Vorzeichen ignoriert. Beispiele:
``` python
``` python
0 / np.array(0
)
float('inf') - float('inf'
)
```
```
nan
nan
``` python
``` python
float('
inf') - float('inf
')
float('
nan
')
```
```
nan
nan
``` python
``` python
float('
nan
')
np.
nan
```
```
nan
nan
``` python
``` python
np.nan
bool(
np.
is
nan
(0 / np.float64(0)))
```
```
nan
True
Nun zu den Vergleichsoperatoren:
Nun zu den Vergleichsoperatoren:
%% Cell type:code id:0008-ae7997c347611f4c5dd9e4b123a17e78bdc9ee0476bb67d2e0cb467d875 tags:
%% Cell type:code id:0008-ae7997c347611f4c5dd9e4b123a17e78bdc9ee0476bb67d2e0cb467d875 tags:
```
```
print('0 < inf:', 0 < float('inf'))
print('0 < inf:', 0 < float('inf'))
```
```
%% Output
%% Output
0 < inf: True
0 < inf: True
%% Cell type:code id:0009-afc859d9041222728ab14f0eb19725cc29e2b4a81fdacf5f02855593466 tags:
%% Cell type:code id:0009-afc859d9041222728ab14f0eb19725cc29e2b4a81fdacf5f02855593466 tags:
```
```
print('0 > inf:', 0 > float('inf'))
print('0 > inf:', 0 > float('inf'))
```
```
%% Output
%% Output
0 > inf: False
0 > inf: False
%% Cell type:code id:0010-85958bbadad1d60958bd59696145c8419ed48110033c3caba4829c98cb6 tags:
%% Cell type:code id:0010-85958bbadad1d60958bd59696145c8419ed48110033c3caba4829c98cb6 tags:
```
```
print('0 < -inf:', 0 < float('-inf'))
print('0 < -inf:', 0 < float('-inf'))
```
```
%% Output
%% Output
0 < -inf: False
0 < -inf: False
%% Cell type:code id:0011-f92e0df77c0eaeccdfc5103c42b11289e7266a4cd19f565de01a44b219f tags:
%% Cell type:code id:0011-f92e0df77c0eaeccdfc5103c42b11289e7266a4cd19f565de01a44b219f tags:
```
```
print('0 > -inf:', 0 > float('-inf'))
print('0 > -inf:', 0 > float('-inf'))
```
```
%% Output
%% Output
0 > -inf: True
0 > -inf: True
%% Cell type:code id:0012-ec279eac1fe0ace1b1f98c0b16eec27e56d8f4b7add03d8e423512e764f tags:
%% Cell type:code id:0012-ec279eac1fe0ace1b1f98c0b16eec27e56d8f4b7add03d8e423512e764f tags:
```
```
print('0 < nan:', 0 < float('nan'))
print('0 < nan:', 0 < float('nan'))
```
```
%% Output
%% Output
0 < nan: False
0 < nan: False
%% Cell type:code id:0013-8872960f9cd31bd389b31cbfc93e70fc2e6047666bd71f52c3986553b1c tags:
%% Cell type:code id:0013-8872960f9cd31bd389b31cbfc93e70fc2e6047666bd71f52c3986553b1c tags:
```
```
print('0 > nan:', 0 > float('nan'))
print('0 > nan:', 0 > float('nan'))
```
```
%% Output
%% Output
0 > nan: False
0 > nan: False
%% Cell type:code id:0014-3f3541bc436a313d67ff9e953ccbb068d57292849e317e1275d891fa2e4 tags:
%% Cell type:code id:0014-3f3541bc436a313d67ff9e953ccbb068d57292849e317e1275d891fa2e4 tags:
```
```
print('-inf < inf:', float('-inf') < float('inf'))
print('-inf < inf:', float('-inf') < float('inf'))
```
```
%% Output
%% Output
-inf < inf: True
-inf < inf: True
%% Cell type:code id:0015-9a8131968f725946ad4cbeb53a9a0610bde47a39f94a9f91d1b127f1eca tags:
%% Cell type:code id:0015-9a8131968f725946ad4cbeb53a9a0610bde47a39f94a9f91d1b127f1eca tags:
```
```
print('-inf < nan:', float('-inf') < float('nan'))
print('-inf < nan:', float('-inf') < float('nan'))
```
```
%% Output
%% Output
-inf < nan: False
-inf < nan: False
%% Cell type:code id:0016-a11b01379d4cbac9623375507d43e0be3e9893790fc47f5e32f9715eeac tags:
%% Cell type:code id:0016-a11b01379d4cbac9623375507d43e0be3e9893790fc47f5e32f9715eeac tags:
```
```
print('nan < inf:', float('nan') < float('inf'))
print('nan < inf:', float('nan') < float('inf'))
```
```
%% Output
%% Output
nan < inf: False
nan < inf: False
%% Cell type:code id:0017-2767755cb767d6b05cf504e8fc1b9f3f7c26bd6ee6aecc74d1fcd321398 tags:
%% Cell type:code id:0017-2767755cb767d6b05cf504e8fc1b9f3f7c26bd6ee6aecc74d1fcd321398 tags:
```
```
print('nan == nan:', float('nan') == float('nan'))
print('nan == nan:', float('nan') == float('nan'))
```
```
%% Output
%% Output
nan == nan: False
nan == nan: False
%% Cell type:code id:0018-466de212a6706fc48100fe6059e1adcadb4e752e65cb0ebfb7e6c1daa0c tags:
%% Cell type:code id:0018-466de212a6706fc48100fe6059e1adcadb4e752e65cb0ebfb7e6c1daa0c tags:
```
```
print('nan != nan:', float('nan') != float('nan'))
print('nan != nan:', float('nan') != float('nan'))
```
```
%% Output
%% Output
nan != nan: True
nan != nan: True
%% Cell type:code id:0019-c5edcc702cf64f698d0efaa8e62916e28c4ed0fe1dfaaa84aa7c2a236ae tags:
%% Cell type:code id:0019-c5edcc702cf64f698d0efaa8e62916e28c4ed0fe1dfaaa84aa7c2a236ae tags:
```
```
print('inf == inf:', float('inf') == float('inf'))
print('inf == inf:', float('inf') == float('inf'))
```
```
%% Output
%% Output
inf == inf: True
inf == inf: True
%% Cell type:code id:0020-05b6542122f334e342d4b54a77f6b69700fcb2c9edf8d7b43908712c636 tags:
%% Cell type:code id:0020-05b6542122f334e342d4b54a77f6b69700fcb2c9edf8d7b43908712c636 tags:
```
```
print('inf != inf:', float('inf') != float('inf'))
print('inf != inf:', float('inf') != float('inf'))
```
```
%% Output
%% Output
inf != inf: False
inf != inf: False
%% Cell type:markdown id:0021-4e2f423a1535affce5ed34c188db639cde6ed128649a18821817cefcb91 tags:
%% Cell type:markdown id:0021-4e2f423a1535affce5ed34c188db639cde6ed128649a18821817cefcb91 tags:
Es fällt auf, dass
`nan != nan`
gilt. NaN ist der einzige Wert, für den
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
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`
.
zu können, ob eine Variable kein NaN enthält:
`x == x`
.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment