|
409 | 409 | %
|
410 | 410 | \endhsection%
|
411 | 411 | %
|
| 412 | +\hsection{Summary}% |
| 413 | +% |
| 414 | +In many situations, integer numbers are the way to go, for example when we count things or add discrete quantities. |
| 415 | +However, fractional numbers are also very common and we encounter them all the time in our daily life. |
| 416 | +Fractional numbers can be represented by the \pythonilIdx{float} datatype in \python. |
| 417 | + |
| 418 | +\python\ offers us the very cool feature that basically arbitrary integer numbers can be represented by the \pythonilIdx{int} datatype. |
| 419 | +We can store very large numbers and are only limited by the memory available on our computer. |
| 420 | +Very large integer numbers are, however, something of a corner case {\dots} they do not happen often. |
| 421 | + |
| 422 | +Real numbers in~\realNumbers\ are a whole different beast. |
| 423 | +They include irrational numbers like~$\sqrt{2}$ and transcendental numbers like~$\pi$. |
| 424 | +These numbers are needed \emph{often} and they have infinitely many fractional digits. |
| 425 | +Thus, there is no way to exactly represent them in computer memory exactly. |
| 426 | +Another problem is that we may need both very large numbers like~$10^{300}$ and very small numbery like~$10^{-300}$. |
| 427 | + |
| 428 | +Floating point numbers, provided as the \python\ datatype \pythonilIdx{float}, solve all of these problems (to some degree). |
| 429 | +They offer a precision of about 15~digits for a wide range of large and small numbers. |
| 430 | +15~digits are more than enough for most applications. |
| 431 | +Many functions for floating point numbers, like logarithms and trigonometric functions, are offered by the \pythonilIdx{math} module. |
| 432 | + |
| 433 | +Floating point numbers can be converted back to integers via rounding or truncating. |
| 434 | +Since writing out numbers such as $5.235^{212}$ in their full length would waste a lot of space and also make no sense, since only the highest 15~digits are actually stored and the rest would be random nonsense, the scientific notation was developed. |
| 435 | +It would represent this number as \pythonil{5.235e+212}, which is quite readable if you know that the~\pythonil{e} in \pythonil{eXXX} here just means~$10^{\mathtt{XXX}}$. |
| 436 | + |
| 437 | +However, when one deals with floating point numbers, a set of nasty problems can occur. |
| 438 | +For example, it could happen that we want to represent a number which is just \emph{too big} of the available range. |
| 439 | +This number would be rendered as \pythonilIdx{inf}, which stands for both mathematical infinity~$\infty$ and, well, \inQuotes{too big to be represented}. |
| 440 | +In some cases, computations that would exceed the available range may also raise an \pythonilIdx{OverflowError}, which simply terminates them (we learn about this later). |
| 441 | +Numbers which are too tiny, on the other hand, simply become~\pythonilIdx{0}. |
| 442 | +Finally, there are also situations where the result of a computation is neither too big nor too small and, yet, not an actual number. |
| 443 | +For example, a way to make mathematicians cry would be to try to compute \pythonil{inf - inf} or \pythonil{inf / inf}. |
| 444 | +The result is then \pythonilIdx{nan}, i.e., \inQuotes{Not a Number}\pythonIdx{Not a Number}. |
| 445 | +The value \pythonilIdx{nan} has the interesting property that it is (to my knowledge) the only native \python\ value which is \emph{different from itself}, i.e., \pythonil{nan != nan}. |
| 446 | +The \pythonilIdx{math} module offers a set of functions to check whether a value is finite~(\pythonilIdx{isfinite}), infinite~(\pythonilIdx{isinf}), or \pythonil{nan}~(\pythonilIdx{isnan}). |
| 447 | + |
| 448 | +When doing floating point computations, these are the issues that you should aware about. |
| 449 | +The precision and range is limited and strange things happen at its limits.% |
| 450 | +% |
| 451 | +\endhsection% |
| 452 | +% |
412 | 453 | \endhsection%
|
413 | 454 | %
|
0 commit comments