Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 381bc11e authored by GILLES Sebastien's avatar GILLES Sebastien
Browse files

#64 Update toc.

parent 95633d7f
No related branches found
No related tags found
1 merge request!59#64 Fix naming not properly propagated in hands on notebooks.
%% Cell type:markdown id: tags:
# [Getting started in C++](./) - [Object programming](./0-main.ipynb) - [Hands-on 7](./4b-hands-on.ipynb)
%% Cell type:markdown id: tags:
<h1>Table of contents<span class="tocSkip"></span></h1>
<div class="toc"><ul class="toc-item"><li><span><a href="#EXERCISE-16:-transform-struct-PowerOfTwoApprox-into-a-class" data-toc-modified-id="EXERCISE-16:-transform-struct-PowerOfTwoApprox-into-a-class-1">EXERCISE 16: transform struct <code>PowerOfTwoApprox</code> into a class</a></span></li><li><span><a href="#EXERCISE-17:-transform-Multiply()-into-a-method-Multiply()-of-PowerOfTwoApprox" data-toc-modified-id="EXERCISE-17:-transform-Multiply()-into-a-method-Multiply()-of-PowerOfTwoApprox-2">EXERCISE 17: transform <code>Multiply()</code> into a method <code>Multiply()</code> of <code>PowerOfTwoApprox</code></a></span></li><li><span><a href="#EXERCISE-18:-transform-DisplayPowerOf2Approx()-into-a-class" data-toc-modified-id="EXERCISE-18:-transform-DisplayPowerOf2Approx()-into-a-class-3">EXERCISE 18: transform <code>DisplayPowerOf2Approx()</code> into a class</a></span></li><li><span><a href="#EXERCISE-19:-transform-DisplayMultiply()-into-a-class" data-toc-modified-id="EXERCISE-19:-transform-DisplayMultiply()-into-a-class-4">EXERCISE 19: transform <code>DisplaySumOfMultiply()</code> into a class</a></span></li><li><span><a href="#EXERCISE-20:-introduce-common-PrintLine()-function-for-outputs" data-toc-modified-id="EXERCISE-20:-introduce-common-PrintLine()-function-for-outputs-5">EXERCISE 20: introduce common <code>PrintLine()</code> function for outputs</a></span></li></ul></div>
<div class="toc"><ul class="toc-item"><li><span><a href="#EXERCISE-16:-transform-struct-PowerOfTwoApprox-into-a-class" data-toc-modified-id="EXERCISE-16:-transform-struct-PowerOfTwoApprox-into-a-class-1">EXERCISE 16: transform struct <code>PowerOfTwoApprox</code> into a class</a></span></li><li><span><a href="#EXERCISE-17:-transform-Multiply()-into-a-method-Multiply()-of-PowerOfTwoApprox" data-toc-modified-id="EXERCISE-17:-transform-Multiply()-into-a-method-Multiply()-of-PowerOfTwoApprox-2">EXERCISE 17: transform <code>Multiply()</code> into a method <code>Multiply()</code> of <code>PowerOfTwoApprox</code></a></span></li><li><span><a href="#EXERCISE-18:-transform-DisplayPowerOf2Approx()-into-a-class" data-toc-modified-id="EXERCISE-18:-transform-DisplayPowerOf2Approx()-into-a-class-3">EXERCISE 18: transform <code>DisplayPowerOf2Approx()</code> into a class</a></span></li><li><span><a href="#EXERCISE-19:-transform-DisplaySumOfMultiply()-into-a-class" data-toc-modified-id="EXERCISE-19:-transform-DisplaySumOfMultiply()-into-a-class-4">EXERCISE 19: transform <code>DisplaySumOfMultiply()</code> into a class</a></span></li><li><span><a href="#EXERCISE-20:-introduce-common-PrintLine()-function-for-outputs" data-toc-modified-id="EXERCISE-20:-introduce-common-PrintLine()-function-for-outputs-5">EXERCISE 20: introduce common <code>PrintLine()</code> function for outputs</a></span></li></ul></div>
%% Cell type:markdown id: tags:
### EXERCISE 16: transform struct `PowerOfTwoApprox` into a class
Make `PowerOfTwoApprox` into a class, with proper encapsulation:
* Both data attributes should be made private.
* Constant accessors will therefore be needed (non-constant ones should not be required here)
Expected output is the same as previously.
%% Cell type:markdown id: tags:
### EXERCISE 17: transform `Multiply()` into a method `Multiply()` of `PowerOfTwoApprox`
The method will take as argument only the integer coefficient.
`DisplaySumOfMultiply()` will of course need also some light rewriting to accommodate that change.
Expected output is the same as previously.
%% Cell type:markdown id: tags:
### EXERCISE 18: transform `DisplayPowerOf2Approx()` into a class
Create a class `TestDisplayPowerOfTwoApprox` which will be in charge of printing the display for the 0.65 and 0.35 values.
Use two methods in this class:
* A public method `Do()` which will call the test for 0.65 and 0.35; this method will take the number of bits as argument.
* A private method `Display()` which will provide the display for a given double value (and will therefore be called twice: once for 0.65 and once for 0.35).
New main should look like:
%% Cell type:code id: tags:
``` C++17
int main(int argc, char** argv)
{
static_cast<void>(argc); // to silence warning about unused argc - don't bother
static_cast<void>(argv); // to silence warning about unused argv - don't bother
TestDisplayPowerOfTwoApprox test_display_approx;
for (int nbits = 2; nbits <= 8; nbits += 2)
test_display_approx.Do(nbits);
std::cout << std::endl;
for (int nbits = 1; nbits <= 8; ++nbits)
DisplaySumOfMultiply(nbits, 0.65, 3515, 0.35, 4832);
return EXIT_SUCCESS;
}
```
%% Cell type:markdown id: tags:
Output will be ordered differently:
````
[With 2 bits]: 0.65 ~ 0.75 (3/2^2) [error = 15/100]
[With 2 bits]: 0.35 ~ 0.375 (3/2^3) [error = 7/100]
[With 4 bits]: 0.65 ~ 0.625 (10/2^4) [error = 4/100]
[With 4 bits]: 0.35 ~ 0.34375 (11/2^5) [error = 2/100]
[With 6 bits]: 0.65 ~ 0.65625 (42/2^6) [error = 1/100]
[With 6 bits]: 0.35 ~ 0.351562 (45/2^7) [error = 0/100]
[With 8 bits]: 0.65 ~ 0.648438 (166/2^8) [error = 0/100]
[With 8 bits]: 0.35 ~ 0.349609 (179/2^9) [error = 0/100]
[With 1 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 2965 [error = 254/1000]
[With 2 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4448 [error = 119/1000]
[With 3 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4008 [error = 8/1000]
[With 4 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3857 [error = 30/1000]
[With 5 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3967 [error = 2/1000]
[With 6 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4004 [error = 7/1000]
[With 7 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3977 [error = 0/1000]
[With 8 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3968 [error = 2/1000]
````
%% Cell type:markdown id: tags:
### EXERCISE 19: transform `DisplaySumOfMultiply()` into a class
Likewise, create a class `TestDisplaySumOfMultiply` which will be in charge of printing the display for 0.65 * 3515 + 0.35 * 4832 with public method `Do()` and private method `Display()` which will takes 5 arguments:
* Number of bits.
* The two floating point values.
* Their integer coefficient.
New `main()` is:
%% Cell type:code id: tags:
``` C++17
int main(int argc, char** argv)
{
static_cast<void>(argc); // to silence warning about unused argc - don't bother
static_cast<void>(argv); // to silence warning about unused argv - don't bother
TestDisplayPowerOfTwoApprox test_display_approx;
for (int nbits = 2; nbits <= 8; nbits += 2)
test_display_approx.Do(nbits);
std::cout << std::endl;
TestDisplaySumOfMultiply test_display_sum_of_multiply;
for (int nbits = 1; nbits <= 8; ++nbits)
test_display_sum_of_multiply.Do(nbits);
return EXIT_SUCCESS;
}
```
%% Cell type:markdown id: tags:
### EXERCISE 20: introduce common `PrintLine()` function for outputs
Both `TestDisplay` classes are rather similar in the line in charge of printing content on standard output - so we would like to uniformize the implementation.
We will therefore introduce a `PrintLine()` function which will be in charge of printing the line for a given number of bits.
At the moment, we will renege the sound principle of separating the functionalities (humor me for the moment...) and this function will:
* Take as arguments the number of bits, the exact actual number (obtained through usual floating-point arithmetic) and the approximation of this number through our representation.
* Compute the error with a resolution also given as argument ((i.e. the maximum index against which error is expressed - 100 and 1000 respectively up to now).
However there are subtleties in the way the lines are displayed: the parts in red is supplementary text that vary depending on the call site:
[With 2 bits]: 0.65 ~ 0.75 <font color="red">(3/2^2)</font> [error = 15/100]
[With 1 bits]: <font color="red">0.65 * 3515 + 0.35 * 4832 = </font> 3976 ~ 2965 [error = 254/1000]
So to do that you will need two strings arguments to provide the possibility to customize the line at the two locations pointed out in red (to achieve this you may need a reminder of [how to convert a number into a string](../1-ProceduralProgramming/6-Streams.ipynb#Conversion)).
Last subtlety: for the `DisplaySumOfMultiply` case we round the exact value to an integer, but that would break the output for the `TestDisplayPowerOfTwoApprox` cases... So we introduce an enum class which will act as a boolean:
````
enum class RoundToInteger { no, yes };
````
To sum up, the signature or `PrintLine()` should be:
````
void PrintLine(int nbits, double exact, double approx, int maximum_error_index,
std::string optional_string1, std::string optional_string2,
RoundToInteger round_to_integer);
````
%% Cell type:markdown id: tags:
[© Copyright](../COPYRIGHT.md)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment