You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: text/main/basics/simpleDataTypesAndOperations/int/int.tex
+2-2
Original file line number
Diff line number
Diff line change
@@ -140,9 +140,9 @@
140
140
141
141
Finally, we can also shift bit strings to the left or right by $i$~places.
142
142
The former corresponds to multiplying with~$2^i$, the latter is the same as an integer division by~$2^i$.
143
-
Shifting \pythonil{22} by one bit position to the \emph{left} -- which is done by entering \pythonil{22 << 1}\pythonIdx{<<} -- therefore results in~\pythonil{44}.
143
+
Shifting \pythonil{22} by one bit position to the \emph{left} -- which is done by entering \pythonil{22 << 1}\pythonIdx{<\strut<} -- therefore results in~\pythonil{44}.
144
144
We already know that \pythonil{bin(22)} is \pythonil{'0b10110'} and so it comes at no surprise that \pythonil{bin(44)} is \pythonil{'0b101100'} (notice the additional \pythonil{0} that appeared on the right hand side).
145
-
Shifting \pythonil{22} by two bit positions to the \emph{right} -- which is done by entering \pythonil{22 >> 2}\pythonIdx{>>} -- results in \pythonil{5}.
145
+
Shifting \pythonil{22} by two bit positions to the \emph{right} -- which is done by entering \pythonil{22 >> 2}\pythonIdx{>\strut>} -- results in \pythonil{5}.
146
146
The \pythonil{10} on the right hand side of the binary representation disappeared, as \pythonil{bin(5)} is \pythonil{'0b101'}.
147
147
148
148
Besides the binary representation of integer numbers, which is to the basis~2, there also exists the hexadecimal representation (base~16) and the octal representation (base~7).
Copy file name to clipboardExpand all lines: text/main/controlFlow/conditionals/conditionals.tex
+12-1
Original file line number
Diff line number
Diff line change
@@ -262,7 +262,18 @@
262
262
Both programs are equivalent, but the second one only has~13 instead of 22~lines of code.
263
263
Notice again how a \pgls{linter} can help us to refine and compactify our code.%
264
264
%
265
-
\FloatBarrier%
266
265
\endhsection%
266
+
%
267
+
\hsection{Summary}%
268
+
With the statements we discussed in this section, you are now able to create a program that makes decisions based on data.
269
+
Before this, we could only perform straightforward computations and calculate the results of simple functions.
270
+
Now our variables can receive the result of a function~$A$ if the input meets a condition~$B$ and otherwise the result of a function~$C$.
271
+
This is already quite nice.
272
+
For example, we can now implement and hard-code decision trees~\cite{RN2022AIAMA,SSBD2014UMLFTTA} and \cref{lst:conditionals:if_elif} is basically an example for that.
273
+
Still, the instructions in our programs are still executed in the sequence in which we wrote them down.
274
+
While our control flow can now branch, it cannot perform anything more fancy and advanced {\dots} like looping back upon itself\dots%
Copy file name to clipboardExpand all lines: text/main/controlFlow/loops/loops.tex
+65
Original file line number
Diff line number
Diff line change
@@ -346,6 +346,71 @@
346
346
Maybe their future versions will.
347
347
Regardless, it becomes clear that using more static analysis tools is always a good idea, as stated back in \cref{bp:manyCodeAnalysisTools}.%
348
348
%
349
+
\FloatBarrier%
350
+
\endhsection%
351
+
%
352
+
\hsection{The \texttt{while} Loop}%
353
+
%
354
+
Old clay tablets show that the Babylonians were able to approximate~$\sqrt{2}$ maybe as far back as 4000~years ago~\cite{FR1998SRAIOBMY7IC,S2011NA2}.
355
+
The mathematician Hero(n) of Alexandria, who lived in the first century~CE, specified an abstract algorithm for computing the square root of numbers which, today, is known as Heron's Method~\cite{S2011NA2,K2009BMOCTSRJBOFTAOCC}.
356
+
357
+
Given a number~$a$ and aiming to find the square root~$\sqrt{a}$ of~$a$, this algorithm starts with a guess~$x_0$, let's say~$x_0=1$.
358
+
In each iteration~$i$, it will compute a new guess as follows~\cite{S2011NA2,K2009BMOCTSRJBOFTAOCC}:%
We compute the square root of a number using Heron's Method~\cite{S2011NA2,K2009BMOCTSRJBOFTAOCC} implemented as py \pythonilIdx{while} loop.}%
392
+
%
393
+
In the \pythonilIdx{while} loop, the body is executed as long as a Boolean expression in the head of the loop evaluates to~\pythonilIdx{True}.
394
+
We now use this new construct to implement Heron's Method in \cref{lst:loops:while_loop_sqrt} and use it to compute the square roots of~0.5, 2, and~3.
395
+
396
+
We begin the program with an outer \pythonil{for} loop that iterates a variable~\pythonil{number} over the \pythonil{float} values \pythonil{0.5}, \pythonil{2.0}, and~\pythonil{3.0}.
397
+
We want to apply the algorithm to each of these values.
398
+
We use two variables~\pythonil{guess} be the current guess of what $\sqrt{\pythonil{number}}$ could be and \pythonil{old_guess} be the previous guess.%
399
+
%
400
+
\begin{sloppypar}%
401
+
We initialize \pythonil{guess} with \pythonil{1.0}.
402
+
Our \pythonilIdx{while} loop should keep iterating as long as \pythonil{guess != old_guess}.
403
+
This only works because the \pythonil{float} datatype has limited precision which we will eventually exhaust, see \cref{sec:howFloatingPointNumbersWork}.
404
+
Anyway, the loop condition necessitates us to store some value different from~\pythonil{1.0} in \pythonil{old_guess} initially (and we pick~\pythonil{0.0}).
405
+
In the loop, first the current guess becomes the old guess via \pythonil{old_guess = guess}.
406
+
Then we update the guess as in \cref{eq:heronGuessUpdate}, by setting \pythonil{guess = 0.5 * (guess + number / guess)}.%
407
+
\end{sloppypar}%
408
+
%
409
+
Finally, we print the result of the computation, and for the sake of comparison, we also print the output of the \pythonilIdx{sqrt} function of the \pythonilIdx{math} module.
410
+
As you can see, our algorithm delivers exactly the same result.
411
+
Also, notice how we used the Unicode escape method from \cref{sec:unicodeChars} to represent the characters~$\sqrt{\cdot}$ and~$\approx$ as \textil{\\u221A} and \textil{\\u2248} to get them printed on the console.%
0 commit comments