|
512 | 512 | \FloatBarrier% |
513 | 513 | \endhsection% |
514 | 514 | % |
515 | | -\hsection{Debugging}% |
516 | | -% |
517 | | -\gitPython{\programmingWithPythonCodeRepo}{09_dunder/fraction_decimal_str_err.py}{--args format --labels part_6}{dunder:fraction_decimal_str_err:part_6}{% |
518 | | -Part~6 of the \pythonil{Fraction} class: Adding a \pythonil{decimal_str} conversation method.}% |
| 515 | +\hsection{Interlude:~Debugging}% |
| 516 | +\label{sec:dunde:debugging}% |
519 | 517 | % |
520 | 518 | We now want to use mathematics based on our class \pythonil{Fraction} for some \inQuotes{real} computation. |
521 | 519 | Remember back in \cref{sec:whileLoop}, we implemented the algorithm of Heron to compute the square root using a \pythonil{while}~loop. |
|
533 | 531 | Since some fractions, like~$\frac{1}{3}$ and~$\frac{1}{7}$ have never-ending decimal representations, this function needs a parameter \pythonil{max_frac} specifying the maximum number of fractional digits to generate. |
534 | 532 | We will set it to 100 by default. |
535 | 533 |
|
| 534 | +\gitPython{\programmingWithPythonCodeRepo}{09_dunder/fraction_decimal_str_err.py}{--args format --labels part_6}{dunder:fraction_decimal_str_err:part_6}{% |
| 535 | +Part~6 of the \pythonil{Fraction} class: Adding a \pythonil{decimal_str} conversation method.}% |
| 536 | + |
536 | 537 | In \cref{lst:dunder:fraction_decimal_str_err:part_6} we present the part of our class \pythonil{Fraction} that contains the code converting the fraction to a decimal string. |
537 | 538 | Like all of our code in \pythonil{Fraction}, it takes the straightforward and probably inefficient route. |
538 | 539 | The idea is simply to first cut-off the integer part of the fraction and then to produce the fractional digits one-by-one. |
|
571 | 572 | As last digit, we therefore add \pythonil{80 // 16}, which is~\pythonil{5}. |
572 | 573 | This is the last digit, because \pythonil{80 \% 16} is~0. |
573 | 574 | Therefore, \pythonil{a == 0} holds after the fifth iteration. |
574 | | -This makes the first part of the the loop condition \pythonil{a != 0} become \pythonil{False} and the loop terminates. |
| 575 | +This makes the first part of the the loop condition~(\pythonil{a != 0}) become \pythonil{False} and the loop terminates. |
575 | 576 |
|
576 | 577 | At this point, \pythonil{digits == [11, 1, 8, 7, 5]}. |
577 | 578 | This is also right, because $\frac{179}{16}=11.1875$. |
|
596 | 597 | If we only have a single digit, then our fraction is an integer number and we can return it as such. |
597 | 598 | Thus, \pythonil{if len(digits) <= 1}, we convert the single digit to a string (after re-inserting the sign). |
598 | 599 |
|
599 | | -Otherwise, we need to have \inQuotes{.} after the first number in \pythonil{digits}. |
| 600 | +Otherwise, we need to have a~\inQuotes{.} after the first number in \pythonil{digits}. |
600 | 601 | We can use \pythonil{digits.insert(1, ".")} to place it there\pythonIdx{list!insert}. |
601 | 602 | In our original example of~$\frac{-179}{16}$, we first had \pythonil{digits == [11, 1, 8, 7, 5]}. |
602 | 603 | After this step, we get \pythonil{digits == [11, ".", 1, 8, 7, 5]}. |
|
684 | 685 | \end{figure}% |
685 | 686 |
|
686 | 687 | We want to investigate this very strange error. |
687 | | -First, let us repeat the unit tests by also executing them inside \pycharm\ in \cref{fig:dunder:doctests}. |
| 688 | +First, let us repeat the \pglspl{doctest} by also executing them inside \pycharm\ in \cref{fig:dunder:doctests}. |
688 | 689 | We open our source file and scroll to our function \pythonil{decimal_str}. |
689 | 690 | With a right mouse click, a context menu is opened. |
690 | 691 | Here, we then left-click on \menu{Run \inSQuotes{Doctest decimal\_str}}~(\cref{fig:dunder:doctests1}). |
|
0 commit comments