Commit 401dc4cf authored by ROUVREAU Vincent's avatar ROUVREAU Vincent
Browse files

fix typo

parent 6a5296f9
%% Cell type:markdown id: tags:
# [Getting started in C++](/) - [Object programming](/notebooks/2-ObjectProgramming/0-main.ipynb) - [TP 7](/notebooks/2-ObjectProgramming/4b-TP.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="#EXERCICE-16:-transform-struct-PowerOfTwoApprox-into-a-class" data-toc-modified-id="EXERCICE-16:-transform-struct-PowerOfTwoApprox-into-a-class-1">EXERCICE 16: transform struct <code>PowerOfTwoApprox</code> into a class</a></span></li><li><span><a href="#EXERCICE-17:-transform-multiply()-into-a-method-Multiply()-of-PowerOfTwoApprox" data-toc-modified-id="EXERCICE-17:-transform-multiply()-into-a-method-Multiply()-of-PowerOfTwoApprox-2">EXERCICE 17: transform <code>multiply()</code> into a method <code>Multiply()</code> of <code>PowerOfTwoApprox</code></a></span></li><li><span><a href="#EXERCICE-18:-transform-display_power_of_2_approx()-into-a-class" data-toc-modified-id="EXERCICE-18:-transform-display_power_of_2_approx()-into-a-class-3">EXERCICE 18: transform <code>display_power_of_2_approx()</code> into a class</a></span></li><li><span><a href="#EXERCICE-19:-transform-display_multiply()-into-a-class" data-toc-modified-id="EXERCICE-19:-transform-display_multiply()-into-a-class-4">EXERCICE 19: transform <code>display_multiply()</code> into a class</a></span></li><li><span><a href="#EXERCICE-20:-introduce-common-print_line()-function-for-outputs" data-toc-modified-id="EXERCICE-20:-introduce-common-print_line()-function-for-outputs-5">EXERCICE 20: introduce common <code>print_line()</code> function for outputs</a></span></li></ul></div>
%% Cell type:markdown id: tags:
### EXERCICE 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:
### EXERCICE 17: transform `multiply()` into a method `Multiply()` of `PowerOfTwoApprox`
The method will take as argument only the integer coefficient.
`display_multiply()` will of course need also some light rewriting to accomodate that change.
Expected output is the same as previously.
%% Cell type:markdown id: tags:
### EXERCICE 18: transform `display_power_of_2_approx()` 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)
display_multiply(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:
### EXERCICE 19: transform `display_multiply()` into a class
Likewise, create a class `TestDisplayMultiply` 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;
TestDisplayMultiply test_display_multiply;
for (int Nbits = 1; Nbits <= 8; ++Nbits)
test_display_multiply.Do(Nbits);
return EXIT_SUCCESS;
}
```
%% Cell type:markdown id: tags:
### EXERCICE 20: introduce common `print_line()` 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 `print_line()` 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 (humour 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).
* 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
[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](/notebooks/1-ProceduralProgramming/6-Streams.ipynb#Conversion)).
Last subtlety: for the `DisplayMultiply` 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 `print_line()` should be:
````
void print_line(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:
© _CNRS 2016_ - _Inria 2018-2019_
_This notebook is an adaptation of a lecture prepared by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_
_The present version has been written by Sébastien Gilles and Vincent Rouvreau (Inria)_
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment