|
| 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 | +% |
0 commit comments