Skip to content

Commit 2f53651

Browse files
committed
section on integers completed
1 parent 43e3004 commit 2f53651

File tree

13 files changed

+187
-26
lines changed

13 files changed

+187
-26
lines changed

bibliography/bibliography.bib

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ @string { a_davis_andy
4949
@string { a_dean_jeffrey = "Jeffrey Dean" }
5050
@string { a_da_veiga_leprevost_felipe = "Felipe {da Veiga Leprevost}" }
5151
@string { a_devin_metthieu = "Matthieu Devin" }
52+
@string { a_dimitrovic_slobodan = "Slobodan Dmitrovi{\'c}" }
5253
@string { a_dubourg_vincent = "Vincent Dubourg" }
5354
@string { a_duchesnay_edouard = "Edouard Duchesnay" }
5455
@string { a_eglen_stephen_j = "Stephen J.\ Eglen" }
@@ -84,7 +85,9 @@ @string { a_kudlur_manjunath
8485
@string { a_larson_eric = "Eric Larson" }
8586
@string { a_laxalde_denis = "Denis Laxalde" }
8687
@string { a_lee_kent_d = "Kent D.\ Lee" }
88+
@string { a_leuck_daniel = "Daniel Leuck" }
8789
@string { a_levenberg_josh = "Josh Levenberg" }
90+
@string { a_loy_marc = "Marc Loy" }
8891
@string { a_mann_samuel = "Samuel Mann" }
8992
@string { a_mayorov_nikolay = "Nikolay Mayorov" }
9093
@string { a_michel_vincent = "Vincent Michel" }
@@ -95,6 +98,7 @@ @string { a_moore_sherry
9598
@string { a_murray_derek_gordon = "Derek Gordon Murray" }
9699
@string { a_negus_christopher = "Christopher Negus" }
97100
@string { a_nelson_andrew_r_j = "Andrew R.\ J.\ Nelson" }
101+
@string { a_niemeyer_patrick = "Patrick Niemeyer" }
98102
@string { a_nguyen_quan = "Quan Nguyen" }
99103
@string { a_norvig_peter = "Peter Norvig" }
100104
@string { a_oliphant_travis_e = "Travis E.\ Oliphant" }
@@ -411,6 +415,15 @@ @book{CN2020ULB
411415
xdata = {ser_bs},
412416
}
413417

418+
@book{D2024MCFABAFITTCPL,
419+
author = a_dimitrovic_slobodan,
420+
title = {Modern~\pgls{C} for Absolute Beginners: A Friendly Introduction to the \pgls{C}~Programming Language},
421+
date = {2024-03},
422+
publisher = p_apress,
423+
address = pa_apress,
424+
isbn = {9798868802249},
425+
}
426+
414427
@book{G2019DSFSFPWP,
415428
author = a_grus_joel,
416429
title = {Data Science from Scratch: First Principles with Python},
@@ -461,7 +474,7 @@ @article{HMvdWGVCWTBSKPHvKBHFdRWPGMSRWAGO2020APWN
461474
}
462475

463476
@techreport{ISOIEC207PLCWDOS,
464-
title = {Programming Languages -- \softwareStyle{C}, Working Document of~{SC22/WG14}},
477+
title = {Programming Languages -- \pgls{C}, Working Document of~{SC22/WG14}},
465478
number = {{ISO/\linebreak{3}IEC9899:2017} {C17~ballot} {N2176}},
466479
xdata = {rep_iso_iec_standard},
467480
date = {2017-11},
@@ -478,6 +491,16 @@ @book{LH2015DSAAWP
478491
isbn = {978-3-319-13071-2},
479492
}
480493

494+
@book{LNL2020LJ,
495+
author = a_loy_marc # and # a_niemeyer_patrick # and # a_leuck_daniel,
496+
title = {Learning \pgls{Java}},
497+
edition = {5},
498+
date = {2020-03},
499+
publisher = p_oreilly,
500+
address = pa_oreilly,
501+
isbn = {9781492056270},
502+
}
503+
481504
@article{PRGWSUdVLFTEKPKFBV2016TSRFTAOGAG,
482505
author = a_perez_riverol_yasset # and # a_gatto_laurent # and # a_wang_rui # and # a_sachsenberg_timo # and # a_uszkoreit_julian # and # a_da_veiga_leprevost_felipe # and # a_fufezan_christian # and # a_ternent_tobias # and # a_eglen_stephen_j # and # a_katz_daniel_s # and # a_pollard_tom_j # and # a_konovalov_alexander # and # a_flight_robert_m # and # a_blin_kai # and # a_vizcaino_juan_antonio,
483506
title = {Ten Simple Rules for Taking Advantage of \git\ and \github},

notation/math.sty

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
\newSymbol{integerNumbers}{\ensuremath{\mathSpace{Z}}}{Z}{the set of the integers numbers including positive and negative numbers and~0, i.e., {\dots}, -3, -2, -1, 0, 1, 2, 3, {\dots}, and so on}%
88
%
99
\protected\gdef\intRange#1#2{\ensuremath{#1..#2}}%
10+
\protected\gdef\decSep{\hspace*{0.22em}}%
1011
%

notation/terms.sty

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22
%% Access to Source Codes
33
%%
44
%
5+
%
6+
\newglossaryentry{C}{%
7+
name={\softwareStyle{C}},
8+
sort={C},
9+
description={%
10+
is another programming language, which is very successful in system programming situations~\cite{ISOIEC207PLCWDOS,D2024MCFABAFITTCPL}.%
11+
}%
12+
}%
13+
%
14+
%
15+
\newglossaryentry{Java}{%
16+
name={\softwareStyle{Java}},
17+
sort={Java},
18+
description={%
19+
is another very success programming language, with roots in the \pgls{C}~family of languages~\cite{LNL2020LJ}.%
20+
}%
21+
}%
22+
%
523
\newglossaryentry{git}{%
624
name={Git},
725
description={%

styles/keys.sty

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
%%
44
%
55
\RequirePackage[os=win]{menukeys}%
6-
\renewmenumacro{\keys}[+]{shadowedroundedkeys}%
6+
\renewmenumacro{\keys}[+]{roundedkeys}%
77
\renewmenumacro{\directory}[/]{hyphenatepaths}%
88
%
99
%% see https://tex.stackexchange.com/questions/387952

text/main/basics/gettingStarted/pythonInTheTerminal/pythonInTheTerminal.tex

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,19 @@
131131
A \python\ program is just a normal text file, after all. %
132132
We can execute such a text file by entering its directory and typing \bashil{python3 programName} (where \bashil{programName} is \bashil{very_first_program.py}, in our case) and hitting \keys{\enter}. %
133133
Then the program is executed directly in the \pgls{terminal}. %
134-
This process is shown in \cref{fig:terminalPython}.%
134+
This process is shown in \cref{sec:terminalPython,fig:terminalPython}.%
135135
%
136136
\item Alternatively, we could open the \python\ interpreter console in \pycharm\ and enter and execute our code line-by-line. %
137-
This is sketched in \cref{fig:pycharmConsoleA}.%
137+
This is sketched in \cref{sec:pycharmConsole,fig:pycharmConsoleA}.%
138138
%
139139
\item Besides using the \python\ console inside \pycharm, we can also open it inside a \pgls{terminal}. %
140140
We can then enter separate \python\ instructions and run them there. %
141-
This fourth option is outlined in \cref{fig:terminalConsoleA}.%
141+
This fourth option is outlined in \cref{sec:terminalConsolem,fig:terminalConsoleA}.%
142142
%
143143
\end{enumerate}%
144144
%
145+
\hsection{Executing a \python\ Program in a Terminal}%
146+
\label{sec:terminalPython}%
145147
In order to directly execute a \python\ program in a \pgls{terminal}, we first need to open one.
146148
Under \ubuntu\ \linux, we simply press \ubuntuTerminal.
147149
Under \windows, we have to \windowsTerminal.
@@ -151,8 +153,11 @@
151153
We provide a screenshot for that, taken under \ubuntu\ \linux, in \cref{fig:terminalPython1cd}.
152154
Now we simply call the \python\ interpreter by writing \bashil{python3} followed by the file name of our program, which is \bashil{very_first_program.py} in our case.
153155
In \cref{fig:terminalPython2python} we do this and hit \keys{\enter}, which causes the \python\ interpreter to execute our program.
154-
The output \inQuotes{Hello World!} is then printed into the \pgls{terminal} in \cref{fig:terminalPython3result}.
155-
156+
The output \inQuotes{Hello World!} is then printed into the \pgls{terminal} in \cref{fig:terminalPython3result}.%
157+
\endhsection%
158+
%
159+
\hsection{Entering Commands in the \python\ Console inside \pycharm}%
160+
\label{sec:pycharmConsole}%
156161
Besides writing programs in files and executing them, we can also directly enter them into the \python\ console and execute them step-by-step.
157162
This does not make sense if we want to reuse our programs later.
158163
But it does make a lot of sense when we just want to test some commands or functions or quickly test some idea.
@@ -161,14 +166,21 @@
161166
To explore entering \python\ code in the \python\ console inside \pycharm, we continue where we left of in \cref{sec:ourFirstProgram}.
162167
In \pycharm, we first click the \menu{\pycharmConsole} on the vertical icon bar on the left side of the \pycharm\ window, as shown in \cref{fig:pycharmConsole1consoleButton}.
163168
This directly brings us to the \python\ console~(\cref{fig:pycharmConsole2consoleOpen}).
164-
We can enter the one-line-program from \cref{lst:very_first_program} and press \keys{\enter}, as illustrated in \cref{fig:pycharmConsole3writingCode}, which leads to the expected output shown in \cref{fig:pycharmConsole4codeOutput}.
165-
169+
We can enter the one-line-program from \cref{lst:very_first_program}, as illustrated in \cref{fig:pycharmConsole3writingCode}.
170+
Notice that the input prompt of the console is marked by the three greater characters \bashil{>>>} after which we enter our text.
171+
Pressing \keys{\enter} after writing the code leads to the expected output shown in \cref{fig:pycharmConsole4codeOutput}.
172+
This output directly appears in the console and is not preceded by any other text, in particular not by \bashil{>>>}, which makes it easy to visually distinguish what the input and output in a \python\ console are.%
173+
\endhsection%
174+
%
175+
\hsection{Entering Commands in the \python\ Console in a Terminal}%
176+
\label{sec:terminalConsolem}%
166177
Let us now open a \python\ console from the \pgls{terminal} instead of using the one in \pycharm.
167178
We therefore first need to open a normal terminal.
168179
Under \ubuntu\ \linux, we simply press \ubuntuTerminal.
169180
Under \windows, we have to \windowsTerminal.
170181
Either way, the terminal opens and we can enter \bashil{python3} and press \keys{\enter}, as shown in \cref{fig:terminalConsole1python}.
171182
Now the \python\ interpreter starts right inside the \pgls{terminal}~(\cref{fig:terminalConsole2pythonRunning}).
183+
The prompt, i.e., the place where we can write our code, again is preceded by the \bashil{>>>} characters.
172184
As illustrated in \cref{fig:terminalConsole3writingCode}, we copy the single line of code, \pythonil{print("Hello World!")} from \cref{lst:very_first_program} and press \keys{\enter}.
173185
The output \inQuotes{Hello World!} is printed as expected in \cref{fig:terminalConsole4codeOutput}.
174186
However, we now are still in the \python\ interpreter.
@@ -177,6 +189,6 @@
177189
We are now back in the basic terminal, as shown in \cref{fig:terminalConsole6left}.
178190
In these figures, I was using \ubuntu\ \linux.
179191
On \windows\ or other \linux\ variants, the process would have looked quite similar.%
180-
%
192+
\endhsection%
181193
\endhsection%
182194
%
Binary file not shown.

text/main/basics/simpleDataTypesAndOperations/int/binaryMath.svg

Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
\hsection{Integers}%
2+
\label{sec:int}%
3+
%
4+
Integer arithmetic is the very first thing that you learn in mathematics in primary school.
5+
Integer arithmetic is also the very first thing you learn here.
6+
\emph{Integer} is a Latin word that means \inQuotes{whole} or \inQuotes{intact.}
7+
The integers include all whole numbers and negative numbers and zero, without fractions and decimals.
8+
9+
In many programming languages, there are different integer datatypes with different ranges.
10+
In \pgls{Java}, a \pythonil{byte} is an integer datatype with range~\intRange{-2^7}{2^7-1}, a \pythonil{short} has range~\intRange{-2^{15}}{2^{17}-1}, an \pythonil{int} has range~\intRange{-2^{31}}{2^{31}-1}, and \pythonil{long} has range~\intRange{-2^{63}}{2^{63}-1}, for example.
11+
The draft for the \softwareStyle{C17} standard for the \pgls{C}~programming language lists five signed and five unsigned integer types, plus several ways to extend them~\cite{ISOIEC207PLCWDOS}.
12+
The different integer types of both languages have different ranges and sizes, and the programmer must carefully choose which she needs to use in which situation.
13+
14+
\python\ only has one integer type, called \pythonil{int}.
15+
This type has basically an unbounded range.
16+
The \python\ interpreter will allocate as much memory as is needed to store the number you want.
17+
(Ok, so the range is not actually \emph{unbounded}, it is bounded by the amount of memory available on your computer{\dots} {\dots}but for all intents and purposes within this book, we can assume that~$\pythonil{int}\equiv\integerNumbers$.)
18+
19+
Now, what can we do with integer numbers?
20+
We can add, subtract, multiply, divide, modulo divide, and raise them to powers.
21+
22+
\begin{figure}%
23+
\centering%
24+
\includegraphics[width=0.8\linewidth]{\currentDir/pythonIntMathInConsoleA}%
25+
\caption{Examples for \python\ integer math in the console, part~1 (see \cref{fig:pythonIntMathInConsoleB} for part~2).}%
26+
\label{fig:pythonIntMathInConsoleA}%
27+
\end{figure}%
28+
29+
In \cref{fig:pythonIntMathInConsoleA}, you can find some examples for this.
30+
Like back in \cref{sec:terminalConsolem}, press \ubuntuTerminal\ under \ubuntu\ \linux\ or \windowsTerminal\ under \windows\ to open a \pgls{terminal}.
31+
After entering \bashil{python3} and hitting \keys{\enter}, we can begin experimenting with integer maths.
32+
The lines with \python\ commands in the console begin with \pythonil{>>>}, whereas the result is directly output below them without prefix string.
33+
34+
In the very first line of \cref{fig:pythonIntMathInConsoleA}, we enter \pythonil{4 + 3} and hit \keys{\enter}.
35+
The result is displayed on the next line and, as expected, is \pythonil{7}.
36+
We then attempt to multiply the two integers \pythonil{7} and \pythonil{5} by typing \pythonil{7 * 5} and hitting \keys{\enter}.
37+
The result is \pythonil{35}.
38+
39+
\python\ does not just support normal arithmetics as you have learned it in school, it also follows the operator precedence rules.
40+
If we type in \pythonil{4 + 3 * 5}, it will compute $4+(3*5)=4+15=19$ and, hence, print \pythonil{19}.
41+
We can also use parentheses and type in \pythonil{(4 + 3) * 5}, which will be evaluated as, well $(4+3)*5=7*5=35$, and we get \pythonil{35}.
42+
Integers can be signed, so typing \pythonil{4 - -12} yields \python{16}.
43+
Parentheses can be arbitrarily nested, so we can also compute \pythonil{((4 + 3) * (4 - -12) - 5) * 3}, which evaluates to $((7 * 16) - 5) * 3 = (112-5)*3=107*3=321$.
44+
45+
Division is a bit tricky in programming in general and in \python\ as well.
46+
There are \emph{two kinds} of division in \python: Integer division, denoted by \pythonil{//} and fractional division, denoted as \pythonil{/}.
47+
48+
\pythonil{32 // 4} yields \pythonil{8}, because \pythonil{4} fits \pythonil{8} times into \pythonil{32}.
49+
\pythonil{33 // 4}, \pythonil{34 // 4}, and \pythonil{35 // 4} all still yield \pythonil{8}, as \pythonil{4} \emph{completely} fits \pythonil{8} times into these numbers (leavin some remainder left over).
50+
\pythonil{36 // 4} then finally yields \pythonil{9}.
51+
The results of the integer division operator \pythonil{//} are always also \pythonil{int}s.
52+
53+
Fractional division, however, returns \pythonil{float} values, which we will explore in the next section in detail.
54+
For now, let's just say that they can represent fractional parts (to a limited precision), which is denoted by having a \python{.} in the text output of the numbers.
55+
Computing \pythonil{32 / 4} thus yields \pythonil{8.0}, {33 / 4} gives us \pythonil{8.25}, \pythonil{34 / 4} yields \pythonil{8.5}, \pythonil{35 / 4} results in \pythonil{8.75}, and, finally, \pythonil{36 / 4} returns \pythonil{9.0}.
56+
Notice that the result of this division operator is always a floating point number, even if the number itself is an integer.
57+
58+
Now above we have said that \pythonil{33 // 4} yields the integer \pythonil{8}.
59+
The remainder of this operation can be computed using the modulo operator \pythonil{\%}, i.e., by typing \pythonil{33 \% 4}, which yields \pythonil{1}.
60+
We also find that \pythonil{34 \% 4} yields \pythonil{2}, \pythonil{35 \% 4} gives us \pythonil{3}, and \pythonil{36 \% 4} is \pythonil{0}.
61+
62+
\begin{figure}%
63+
\centering%
64+
\includegraphics[width=0.8\linewidth]{\currentDir/pythonIntMathInConsoleB}%
65+
\caption{Examples for \python\ integer math in the console, part~2 (see \cref{fig:pythonIntMathInConsoleA} for part~1).}%
66+
\label{fig:pythonIntMathInConsoleB}%
67+
\end{figure}%
68+
69+
As you will find in \cref{fig:pythonIntMathInConsoleB}, integers can also be raised to a power.
70+
For example, $2^7$~is expressed as \pythonil{2 ** 7} in \python\ (and yields~\pythonil{128}).
71+
\pythonil{7 ** 11}, i.e., $7^{11}$ gives us~1\decSep977\decSep326\decSep743 and shows as \pythonil{1977326743} in the output.
72+
73+
In most programming languages such as \pgls{Java} and \pgls{C}, the largest integer type available off the shelf is 64~bits wide.
74+
If it is signed (can have negative values) like \pgls{Java}'s \pythonil{long}, it has range~\intRange{-2^{63}}{2^{63}-1}.
75+
An unsinged 64~bit integer type, such as \pythonil{unsigned long long} in \pgls{C}, would have range~\intRange{0}{2^{64}-1}.
76+
In \python\, we can compute $2^{63}$~(\pythonil{2 ** 63}), namely 9\decSep223\decSep372\decSep036\decSep854\decSep775\decSep808, and
77+
$2^{64}$~(\pythonil{2 ** 64}), which is~18\decSep446\decSep744\decSep073\decSep709\decSep551\decSep616.
78+
These are very large numbers and the latter one would overflow the range of the standard integer types of \pgls{Java} and \pgls{C}.
79+
However, we can also keep going and compute \pythonil{2 ** 1024}, which is such a huge number that it wraps four times in the output of our \python\ console in \cref{fig:pythonIntMathInConsoleB}!
80+
\python\ integers are basically unbounded.
81+
However, the larger they get, the more memory they need and the longer it will take to compute with them.
82+
83+
\begin{figure}%
84+
\centering%
85+
\includegraphics[width=0.8\linewidth]{\currentDir/binaryMath}%
86+
\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}).}%
87+
\label{fig:binaryMath}%
88+
\end{figure}
89+
90+
\emph{The following paragraphs is just for the sake of completeness. %
91+
You do not necessarily need to understand it when reading this book for the first time.}
92+
As a small excursion for the binary mathematics aficionados:
93+
All integer numbers can be represented as bit strings.
94+
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.
95+
Then, $\dots b_4 b_3 b_2 b_1 b_0$ is a bit string.
96+
If we represent integers with such strings of five bits, then the number~1 would have representation~\texttt{00001}, because it is equivalent to~$2^0$.
97+
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$.
98+
In \python, we can compute the bitwise (i.e., binary) \emph{or}, \emph{and}, as well as the and \emph{exclusive~or} of this binary representation of integers using the \pythonil{|}, \pythonil{&}, and \pythonil{^} operators, respectively.
99+
100+
Binary \emph{or} returns an integer in which all bits are set to~1 which were~1 in either of its two operands.
101+
\pythonil{22 | 1} yields \pythonil{23}, because the bit with value~1 is not set in~22 and the binary~\emph{or} sets it (effectively adding~1 to~22).
102+
The slightly more comprehensive example \pythonil{22 | 15} sketched in \cref{fig:binaryMath} gives us \pythonil{31}, because $22=2^4+2^2+2^1$ and $15=2^3+2^2+2^1+2^0$, which \pythonil{|} combines to $31=2^4+2^3+2^2+2^1+2^0$, i.e., each power of~2 that occurred in either of the two operands is present in the result.
103+
104+
Binary \emph{and} returns the integer where only the bits remain~1 that were~1 in \emph{both} operands.
105+
Applying binary \emph{and} instead of \emph{or}, i.e., doing \pythonil{22 & 1} results in \pythonil{0}, because, as said before, the bit $2^0$ is not set in~22.
106+
\pythonil{22 & 15}, yields~\pythonil{6}, because only $2^2$ and $2^1$ appear both in~22 and~15.
107+
108+
The \emph{exclusive~or}, which is often called \emph{xor}, will set a bit to~1 if it is~1 in exactly one of the two operands.
109+
Therefore, \pythonil{22 ^ 1} gives~\pythonil{23}, since only the bit with value $2^0$ is set in~1 and the other bits that are~1 in~22 are not.
110+
\pythonil{22 ^ 15} yields \pythonil{25}, because $2^4$, $2^3$, and $2^0$ occur only once in the two operators (whereas $2^2$ and $2^1$ occured in both of them).
111+
With this, our excursion into binary maths ends and we now welcome back all first-time readers.
112+
113+
In conclusion, the integer type \pythonil{int} represents whole numbers.
114+
Integers can be positive or negative or zero.
115+
All the primitive mathematical operations like addition, subtraction, multiplication, and division that you learned in school can be applied to integers.
116+
The normal arithmetic precedence rules (that you also have learned in school) apply.
117+
Parentheses can be used to group operations.
118+
Finally, \python\ also provides the same binary logic operators, working on the bit string representation of integers, that you may or or may not know from other programming languages as well.
119+
%
120+
\endhsection%
121+
%
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)