Skip to content

Commit 74ee518

Browse files
committed
more screenshots replaced with listings
1 parent 401eda0 commit 74ee518

File tree

9 files changed

+36
-60
lines changed

9 files changed

+36
-60
lines changed

text/main/basics/simpleDataTypesAndOperations/bool/bool.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
When talking about comparisons, there is one important, counter-intuitive exception to recall:
5050
The \pythonilIdx{nan}\pythonIdx{Not a Number} floating point value\pythonIdx{float} from \cref{sec:float:special}.
51-
In \cref{fig:floatMathInConsoleNaN}, we learned that \pythonil{nan == nan}\pythonIdx{==} is \pythonilIdx{False} and \pythonil{nan != nan}\pythonIdx{"!=} is \pythonilIdx{True}.
51+
In \cref{exec:float_nan}, we learned that \pythonil{nan == nan}\pythonIdx{==} is \pythonilIdx{False} and \pythonil{nan != nan}\pythonIdx{"!=} is \pythonilIdx{True}.
5252
This is the only primitive value (to my knowledge) which is not equal to itself.%
5353
\endhsection%
5454
%

text/main/basics/simpleDataTypesAndOperations/float/float.tex

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@
7373
\hsection{Floating Point Arithmetic}%
7474
\label{sec:floatarith}%
7575
%
76-
\gitEvalPython{float_arith}{}{simple_datatypes/float_arith.py}%
77-
\listingBox{exec:float_arith}{Basic arithmetics with floating point numbers in \python.}{,style=python_console_style}%
76+
\gitEvalPython{float_arithmetics}{}{simple_datatypes/float_arithmetics.py}%
77+
\listingBox{exec:float_arithmetics}{Basic arithmetics with floating point numbers in \python.}{,style=python_console_style}%
7878
%
7979
Floating point numbers in \python\ can be distinguished from \pythonils{int}\pythonIdx{int} by having a decimal dot in their text representation, i.e., \pythonil{5.0} is a \pythonilIdx{float} and \pythonil{5} is an \pythonilIdx{int}.
80-
Let us now look at some examples for the basic arithmetic operations available for \pythonils{float}\pythonIdx{float} in \cref{exec:float_arith}.
80+
Let us now look at some examples for the basic arithmetic operations available for \pythonils{float}\pythonIdx{float} in \cref{exec:float_arithmetics}.
8181

8282
We already learned that the division operator~\pythonilIdx{/} always yields a \pythonilIdx{float} result.
8383
Therefore \pythonil{6 / 3} yields \pythonil{2.0} instead of \pythonil{2}.
@@ -162,12 +162,8 @@
162162
%
163163
\hsection{Back to Integers: Rounding}%
164164
%
165-
\begin{figure}%
166-
\centering%
167-
\includegraphics[width=0.8\linewidth]{\currentDir/floatMathInConsoleRound}%
168-
\caption{Rounding \pythonilIdx{float} values to \pythonilIdx{int} values.}%
169-
\label{fig:floatMathInConsoleRound}%
170-
\end{figure}%
165+
\gitEvalPython{float_rounding}{}{simple_datatypes/float_rounding.py}%
166+
\listingBox{exec:float_rounding}{Rounding \pythonilIdx{float} values to \pythonilIdx{int} values.}{,style=python_console_style}%
171167
%
172168
We already learned that a single \pythonilIdx{float} value inside a computation that otherwise uses \pythonils{int}\pythonIdx{int} basically forces the whole result to become a \pythonilIdx{float} as well.
173169
But maybe sometimes we want to have an \pythonilIdx{int} as result.
@@ -179,7 +175,7 @@
179175
If two integer numbers are equally close, it rounds it to the one that is even.
180176
This can be very surprising, as we learned in school that $x.5$ should be rounded to $x+1$.
181177
This will only happen with \pythonilIdx{round} if $x+1$~is even.
182-
We find examples for the behavior of \pythonilIdx{round} in \cref{fig:floatMathInConsoleRound}.
178+
We find examples for the behavior of \pythonilIdx{round} in \cref{exec:float_rounding}.
183179
\pythonil{round(0.4)} yields the integer~\pythonil{0}, as expected.
184180
\pythonil{round(0.5)} returns~\pythonil{0} as well, which one may not expect -- but \pythonil{0} is even and \pythonil{1} would be odd.
185181
\pythonil{round(0.6)} gives us the integer~\pythonil{1}.
@@ -233,13 +229,8 @@
233229
\label{fig:scientificNotation}%
234230
\end{figure}%
235231
%
236-
\begin{figure}%
237-
\centering%
238-
%
239-
\includegraphics[width=0.8\linewidth]{\currentDir/floatMathInConsoleSciNot}%
240-
\caption{Examples for the scientific notation in \python.}%
241-
\label{fig:floatMathInConsoleSciNot}%
242-
\end{figure}%
232+
\gitEvalPython{float_scientific_notation}{}{simple_datatypes/float_scientific_notation.py}%
233+
\listingBox{exec:float_scientific_notation}{Examples of the scientific notation in \python.}{,style=python_console_style}%
243234
%
244235
Earlier on, we wrote that \pythonils{float}\pythonIdx{float} can represent numbers as large as~$10^{300}$ or as small as~$10^{-300}$.
245236
This leads to the question how it would print such values on the console and how we can read them.
@@ -256,7 +247,7 @@
256247
This notation is illustrated in \cref{fig:scientificNotation}.
257248
\emph{This notation only applies to \pythonils{float}\pythonIdx{float}, \textbf{not} \pythonils{int}\pythonIdx{int}.}
258249

259-
In \cref{fig:floatMathInConsoleSciNot} we provide some examples for this notation.
250+
In \cref{exec:float_scientific_notation} we provide some examples for this notation.
260251
When we write \pythonil{0.001} or \pythonil{0.0001} in a \python\ console, the output is still this same number.
261252
However, \pythonil{0.00009} is presented as \pythonil{9e-05}, which stands for~$9*10^{-5}$.%
262253
%
@@ -301,12 +292,9 @@
301292
\hsection{Limits and Special Floating Point Values: Infinity and \inQuotes{Not a Number}}%
302293
\label{sec:float:special}%
303294
%
304-
\begin{figure}%
305-
\centering%
306-
\includegraphics[width=0.7\linewidth]{\currentDir/floatMathInConsoleZero}%
307-
\caption{What happens with very small floating point numbers in \python?\pythonIdx{0.0}}%
308-
\label{fig:floatMathInConsoleZero}%
309-
\end{figure}%
295+
%
296+
\gitEvalPython{float_very_small}{}{simple_datatypes/float_very_small.py}%
297+
\listingBox{exec:float_very_small}{What happens with very small floating point numbers in \python?\pythonIdx{0.0}.}{,style=python_console_style}%
310298
%
311299
We already learned that the floating point type \pythonilIdx{float} can represent both very small and very large numbers.
312300
But we also know that it is internally stored as chunk of 64~bits.
@@ -321,7 +309,7 @@
321309
In its documentation~\cite{O2024JPSEJDKV2AS:CD}, we find that the minimum value is~$2^{-1074}$, which is approximately~$4.940\decSep656\decSep458\decSep412\decSep465\decSep44*10^{-324}$.
322310
So we would expect the smallest possible floating point value in \python\ to also be in this range.
323311

324-
In \cref{fig:floatMathInConsoleInf}, we take a look what happens if we approach this number.
312+
In \cref{exec:float_very_large}, we take a look what happens if we approach this number.
325313
We use the scientific notation and begin to print the number~\pythonil{1e-323} (which is~$10^{-323}$).
326314
This number is correctly represented as \pythonilIdx{float} and shows up in the console exactly as we wrote it.
327315
However, if we go a bit smaller and enter \pythonil{9e-324}, which is~$9*10^{-324}=0.9*10^{-323}$, we find that it again shows up in the console as \pythonil{1e-323}.
@@ -343,16 +331,12 @@
343331
So we learned what happens if we try to define very small floating point numbers:
344332
They become zero.
345333

346-
\begin{figure}%
347-
\centering%
348-
\includegraphics[width=0.7\linewidth]{\currentDir/floatMathInConsoleInf}%
349-
\caption{What happens with very large floating point numbers in \python?\pythonIdx{inf}}%
350-
\label{fig:floatMathInConsoleInf}%
351-
\end{figure}%
334+
\gitEvalPython{float_very_large}{}{simple_datatypes/float_very_large.py}%
335+
\listingBox{exec:float_very_large}{What happens with very large floating point numbers in \python?\pythonIdx{inf}.}{,style=python_console_style,literate={0}{0\-}1 {1}{1\-}1 {2}{2\-}1 {3}{3\-}1 {4}{4\-}1 {5}{5\-}1 {6}{6\-}1 {7}{7\-}1 {8}{8\-}1 {9}{9\-}1,breakatwhitespace=false,breaklines=true}
352336

353337
But what happens to numbers that are too big for the available range?
354338
Again, according to the nice documentation of \pgls{Java}, the maximum 64~bit double precision floating point number value is~$(2-2^{-52})*2^{1023}\approx1.797\decSep693\decSep134\decSep862\decSep315\decSep708\dots*10^{308}$.
355-
We can enter this value as \pythonilIdx{1.7976931348623157e+308}\pythonIdx{float!largest} and it indeed prints correctly in \cref{fig:floatMathInConsoleInf}.
339+
We can enter this value as \pythonilIdx{1.7976931348623157e+308}\pythonIdx{float!largest} and it indeed prints correctly in \cref{exec:float_very_large}.
356340
If we step it up a little bit and enter \pythonil{1.7976931348623158e+308}, due to the limited precision, we again get \pythonilIdx{1.7976931348623157e+308}\pythonIdx{float!largest}.
357341
However, if we try entering \pythonil{1.7976931348623159e+308} into the \python\ console, something strange happens:
358342
The output is \pythonilIdx{inf}.
@@ -391,15 +375,11 @@
391375
We can import it via \pythonil{from math import inf}\pythonIdx{inf}.
392376
And indeed, since the text \pythonil{1.7976931348623159e+308} is parsed to a \pythonilIdx{float} value of \pythonilIdx{inf}, we find that \pythonil{1.7976931348623159e+308 == inf} yields~\pythonilIdx{True}.
393377

394-
\begin{figure}%
395-
\centering%
396-
\includegraphics[width=0.8\linewidth]{\currentDir/floatMathInConsoleNaN}%
397-
\caption{Not a Number\pythonIdx{Not a Number}, i.e., \pythonilIdx{nan}.}%
398-
\label{fig:floatMathInConsoleNaN}%
399-
\end{figure}%
400-
list
378+
\gitEvalPython{float_nan}{}{simple_datatypes/float_nan.py}%
379+
\listingBox{exec:float_nan}{Not a Number\pythonIdx{Not a Number}, i.e., \pythonilIdx{nan}.}{,style=python_console_style}%
380+
401381
Now, \pythonilIdx{inf} not actually being~$\infty$ is a little bit weird.
402-
But it can get even stranger, as you will see in \cref{fig:floatMathInConsoleNaN}.
382+
But it can get even stranger, as you will see in \cref{exec:float_nan}.
403383
\pythonilIdx{inf} is a number which is too big to be represented as \pythonilIdx{float} \emph{or}~$\infty$.
404384
So it is natural to assume that \pythonil{inf - 1} remains \pythonilIdx{inf} and that even \pythonil{inf - 1e300} remains \pythonilIdx{inf} as well.
405385
However, what is \pythonil{inf - inf}?
@@ -421,16 +401,12 @@
421401
However, \pythonilIdx{nan} is \emph{really} different from really \emph{anything}.
422402
Therefore, \pythonil{nan == nan}\pythonIdx{==} is \pythonilIdx{False} and \pythonil{nan != nan}\pythonIdx{"!=} is \pythonilIdx{True}!
423403

424-
\begin{figure}%
425-
\centering%
426-
\includegraphics[width=0.8\linewidth]{\currentDir/floatMathInConsoleCheckFinite}%
427-
\caption{Checking for \pythonilIdx{nan} and \pythonilIdx{inf} via \pythonilIdx{isfinite}, \pythonilIdx{isinf}, and \pythonilIdx{isnan}.}%
428-
\label{fig:floatMathInConsoleCheckFinite}%
429-
\end{figure}%
404+
\gitEvalPython{float_check_finite}{}{simple_datatypes/float_check_finite.py}%
405+
\listingBox{exec:float_check_finite}{Checking for \pythonilIdx{nan} and \pythonilIdx{inf} via \pythonilIdx{isfinite}, \pythonilIdx{isinf}, and \pythonilIdx{isnan}.}{,style=python_console_style}%
430406

431407
Either way, the possible occurrence \pythonilIdx{inf} and \pythonilIdx{nan} in floating point computations is a cumbersome issue.
432408
If we want to further process results of a computation, we often want to make sure that it is neither \pythonilIdx{inf} nor \pythonilIdx{-inf} nor \pythonilIdx{nan}.
433-
This can be done via the function \pythonilIdx{isfinite}, which we can import from the \pythonilIdx{math} module\pythonIdx{import}\pythonIdx{from}, as you can see in \cref{fig:floatMathInConsoleCheckFinite}.
409+
This can be done via the function \pythonilIdx{isfinite}, which we can import from the \pythonilIdx{math} module\pythonIdx{import}\pythonIdx{from}, as you can see in \cref{exec:float_check_finite}.
434410
\pythonil{1e34} is a large number, but \pythonil{isfinite(1e34)} is certainly \pythonilIdx{True}.
435411
\pythonil{isfinite(inf)} and \pythonil{isfinite(nan)} are both \pythonilIdx{False}.
436412
The function \pythonilIdx{isinf}, again from the \pythonilIdx{math} module, checks if a \pythonilIdx{float} is infinite.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

text/main/basics/simpleDataTypesAndOperations/int/int.tex

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@
2424
\begin{figure}%
2525
\centering%
2626
\includegraphics[width=0.8\linewidth]{\currentDir/pythonIntMathInConsoleArith}%
27-
\caption{Examples of \python\ integer math in the console, part~1 (see \cref{exec:int_math_arith} for part~2).}%
27+
\caption{Examples of \python\ integer math in the console, part~1 (see \cref{exec:int_arithmetics} for part~2).}%
2828
\label{fig:pythonIntMathInConsoleArith}%
2929
\end{figure}%
3030
%
31-
\gitEvalPython{int_math_arith}{}{simple_datatypes/int_math_arith.py}%
32-
\listingBox{exec:int_math_arith}{The same examples of \python\ integer math in the \python\ console as given in \cref{fig:pythonIntMathInConsoleArith}, just as listing instead of screenshot.}{,style=python_console_style}%
31+
\gitEvalPython{int_arithmetics}{}{simple_datatypes/int_arithmetics.py}%
32+
\listingBox{exec:int_arithmetics}{The same examples of \python\ integer math in the \python\ console as given in \cref{fig:pythonIntMathInConsoleArith}, just as listing instead of screenshot.}{,style=python_console_style}%
3333

3434
In \cref{fig:pythonIntMathInConsoleArith}, you can find some examples of this.
35-
(The same example is given in \cref{exec:int_math_arith}, just as listing instead of screenshot.
35+
(The same example is given in \cref{exec:int_arithmetics}, just as listing instead of screenshot.
3636
We will use such listings from now on, as they convey the exactly same information, but are easier to read and I can more conveniently include comments.)
3737
Like back in \cref{sec:terminalConsolem}, press \ubuntuTerminal\ under \ubuntu\ \linux\ or \windowsTerminal\ under \microsoftWindows\ to open a \pgls{terminal}.
3838
After entering \bashil{python3} and hitting \keys{\enter}, we can begin experimenting with integer maths.
3939
The lines with \python\ commands in the console begin with \pythonil{>>>}, whereas the result is directly output below them without prefix string.
4040

41-
In the very first line of \cref{fig:pythonIntMathInConsoleArith,exec:int_math_arith}, we enter \pythonil{4 + 3}\pythonIdx{+} and hit \keys{\enter}.
41+
In the very first line of \cref{fig:pythonIntMathInConsoleArith,exec:int_arithmetics}, we enter \pythonil{4 + 3}\pythonIdx{+} and hit \keys{\enter}.
4242
The result is displayed on the next line and, as expected, is \pythonil{7}.
4343
We then attempt to multiply the two integers \pythonil{7} and \pythonil{5} by typing \pythonil{7 * 5}\pythonIdx{*!multiplication} and hitting \keys{\enter}.
4444
The result is \pythonil{35}.
@@ -70,10 +70,10 @@
7070
The remainder of this operation can be computed using the \pgls{modulodiv} operator \expandafter\pythonilIdx{\%}, i.e., by typing \pythonil{33 \% 4}, which yields~\pythonil{1}.
7171
We also find that \expandafter\pythonil{34 \% 4} yields~\pythonil{2}, \expandafter\pythonil{35 \% 4} gives us~\pythonil{3}, and \expandafter\pythonil{36 \% 4} is~\pythonil{0}.
7272

73-
\gitEvalPython{int_math_power}{}{simple_datatypes/int_math_power.py}%
74-
\listingBox{exec:int_math_power}{Examples of \python\ integer math (powers) in the \python\ console, part~2 (see \cref{exec:int_math_arith} for part~1).}{,style=python_console_style,literate={0}{0\-}1 {1}{1\-}1 {2}{2\-}1 {3}{3\-}1 {4}{4\-}1 {5}{5\-}1 {6}{6\-}1 {7}{7\-}1 {8}{8\-}1 {9}{9\-}1,breakatwhitespace=false,breaklines=true}
73+
\gitEvalPython{int_powers}{}{simple_datatypes/int_powers.py}%
74+
\listingBox{exec:int_powers}{Examples of \python\ integer math (powers) in the \python\ console, part~2 (see \cref{exec:int_arithmetics} for part~1).}{,style=python_console_style,literate={0}{0\-}1 {1}{1\-}1 {2}{2\-}1 {3}{3\-}1 {4}{4\-}1 {5}{5\-}1 {6}{6\-}1 {7}{7\-}1 {8}{8\-}1 {9}{9\-}1,breakatwhitespace=false,breaklines=true}
7575

76-
As you will find in \cref{exec:int_math_arith}, integers can also be raised to a power.
76+
As you will find in \cref{exec:int_arithmetics}, integers can also be raised to a power.
7777
For example, $2^7$~is expressed as \pythonil{2 ** 7}\pythonIdx{**!power} in \python\ (and yields~\pythonil{128}).
7878
\pythonil{7 ** 11}, i.e., $7^{11}$ gives us~1\decSep977\decSep326\decSep743 and shows as \pythonil{1977326743} in the output.
7979

@@ -83,7 +83,7 @@
8383
In \python\, we can compute $2^{63}$~(\pythonil{2 ** 63}), namely 9\decSep223\decSep372\decSep036\decSep854\decSep775\decSep808, and
8484
$2^{64}$~(\pythonil{2 ** 64}), which is~18\decSep446\decSep744\decSep073\decSep709\decSep551\decSep616.
8585
These are very large numbers and the latter one would overflow the range of the standard integer types of \pgls{Java} and \pgls{C}.
86-
However, we can also keep going and compute \pythonil{2 ** 1024}, which is such a huge number that it wraps several times in the output of our \python\ console in \cref{exec:int_math_arith}!
86+
However, we can also keep going and compute \pythonil{2 ** 1024}, which is such a huge number that it wraps several times in the output of our \python\ console in \cref{exec:int_arithmetics}!
8787
\python\ integers are basically unbounded~(or bounded only by the memory size of our computer).
8888
However, the larger they get, the more memory they need and the longer it will take to compute with them.
8989
\endhsection%
@@ -103,8 +103,8 @@
103103
\caption{Examples for how integer numbers are represented as bit strings in the computer (upper part) and for the binary (bitwise) operations \emph{and}, \emph{or}, and \emph{exclusive or} (often called~\emph{xor}).}%
104104
\label{fig:binaryMath}%
105105
\end{figure}
106-
\gitEvalPython{int_math_bin}{}{simple_datatypes/int_math_bin.py}%
107-
\listingBox{exec:int_math_bin}{Examples of the binary representation of integers and the operations that apply to it.}{,style=python_console_style}
106+
\gitEvalPython{int_binary}{}{simple_datatypes/int_binary.py}%
107+
\listingBox{exec:int_binary}{Examples of the binary representation of integers and the operations that apply to it.}{,style=python_console_style}
108108

109109
All integer numbers can be represented as bit strings.
110110
In other words, a number $z\in\integerNumbers$ can be expressed as $b_0 2^0 + b_1 2^1 + b_2 2^2 + b_3 2^3 + b_4 2^4\dots$, where the $b_i$-values each are either~0 or~1.
@@ -113,7 +113,7 @@
113113
In in \cref{fig:binaryMath}, we illustrate that the number~22 would be \texttt{10110} because $22=2^4+2^2+2^1$ and the number~15 would correspond to \texttt{01111}, as~$15=2^3+2^2+2^1+2^0$.
114114

115115
We can obtain the binary representation of integer numbers as text using the \pythonilIdx{bin} function in \python.
116-
As shown in \cref{exec:int_math_bin}, \pythonil{bin(22)} yields \pythonil{'0b10110'}.
116+
As shown in \cref{exec:int_binary}, \pythonil{bin(22)} yields \pythonil{'0b10110'}.
117117
Here, the \pythonilIdx{0b} prefix means that the following number is in binary representation.
118118
We can also enter numbers in binary representation in the console.
119119
Typing \pythonil{0b10110} corresponds to the number~\pythonil{22}.

0 commit comments

Comments
 (0)