Commit 6d26aae5 authored by GILLES Sebastien's avatar GILLES Sebastien
Browse files

New version for exercices 45 to 51.

parent 9da5bd90
......@@ -36,7 +36,7 @@
"\n",
"This exception class will:\n",
"\n",
"- Take as argument a `std::string` explaining the exception (to be translated into the `what()` virtual method.\n",
"- Take as argument a `std::string` explaining the exception (to be translated into the `what()` virtual method).\n",
"- Derives from `std::exception`.\n",
"\n",
"New `main()` function is:"
......@@ -98,7 +98,7 @@
"\n",
"The way overflows were handled without exception is shaky at best: interface is cumbersome and the `operator*` management is catastrophic.\n",
"\n",
"Introduce an `Overflow` exception that should replace it (the `overflow` arguments should all go away with this).\n",
"Introduce an `Overflow` exception that should replace it (the `overflow` arguments should all go away with this). We will accept here to lose conditions: we only want to know whether a line could be computed or failed due to an overflow.\n",
"\n",
"In the `Display` methods of `TestDisplayXXX` classes:\n",
"\n",
......
%% Cell type:markdown id: tags:
# [Getting started in C++](/) - [Useful concepts and STL](/notebooks/5-UsefulConceptsAndSTL/0-main.ipynb) - [TP 13](/notebooks/5-UsefulConceptsAndSTL/1b-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="#Introduction" data-toc-modified-id="Introduction-1">Introduction</a></span></li><li><span><a href="#EXERCICE-45:-replace-error-function-by-an-Error-exception" data-toc-modified-id="EXERCICE-45:-replace-error-function-by-an-Error-exception-2">EXERCICE 45: replace <code>error</code> function by an <code>Error</code> exception</a></span></li><li><span><a href="#EXERCICE-46:-better-error-handling-of-overflow" data-toc-modified-id="EXERCICE-46:-better-error-handling-of-overflow-3">EXERCICE 46: better error handling of overflow</a></span></li></ul></div>
%% Cell type:markdown id: tags:
### Introduction
[This notebook](/notebooks/TP/HowTo.ipynb) explains very briefly your options to run the TP.
We will start from the solution to exercice 36 of the procedural object TP. `initial_file.cpp` in the TP directory is a copy to the solution of this exercice (symbolic link wouldn't have worked with Docker).
%% Cell type:markdown id: tags:
### EXERCICE 45: replace `error` function by an `Error` exception
This exception class will:
- Take as argument a `std::string` explaining the exception (to be translated into the `what()` virtual method.
- Take as argument a `std::string` explaining the exception (to be translated into the `what()` virtual method).
- Derives from `std::exception`.
New `main()` function is:
%% Cell type:code id: tags:
``` C++17
// Not usable in Xeus-cling
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
try
{
{
TestDisplayContainer<4> container;
container.Register(new TestDisplayPowerOfTwoApprox065<int>(1000000));
container.Register(new TestDisplayPowerOfTwoApprox035<int>(1000000));
container.Register(new TestDisplaySum<int>(1000000));
container.Register(new TestDisplaySum<short>(1000000));
loop(4, 16, 4, container);
}
{
TestDisplayContainer<2> container;
container.Register(new TestDisplayPowerOfTwoApprox065<char>(1000));
container.Register(new TestDisplayPowerOfTwoApprox065<unsigned char>(1000));
loop(1, 8, 1, container);
}
}
catch(const std::exception& e)
{
std::cerr << "An error occurred in the program: " << e.what() << std::endl;
}
return EXIT_SUCCESS;
}
```
%% Cell type:markdown id: tags:
Check exceptions are working by reducing one of the integer template parameter.
%% Cell type:markdown id: tags:
### EXERCICE 46: better error handling of overflow
The way overflows were handled without exception is shaky at best: interface is cumbersome and the `operator*` management is catastrophic.
Introduce an `Overflow` exception that should replace it (the `overflow` arguments should all go away with this).
Introduce an `Overflow` exception that should replace it (the `overflow` arguments should all go away with this). We will accept here to lose conditions: we only want to know whether a line could be computed or failed due to an overflow.
In the `Display` methods of `TestDisplayXXX` classes:
* Catch the `Overflow` exceptions.
* Call a new protected method `PrintOverflow()` which prints the case of an overflow.
Expected output should be:
````
[With 4 bits]: 0.65 ~ 0.625 (10/2^4) [error = 38462/1000000]
[With 8 bits]: 0.65 ~ 0.648438 (166/2^8) [error = 2404/1000000]
[With 12 bits]: 0.65 ~ 0.649902 (2662/2^12) [error = 150/1000000]
[With 16 bits]: 0.65 ~ 0.649994 (42598/2^16) [error = 9/1000000]
[With 4 bits]: 0.35 ~ 0.34375 (11/2^5) [error = 17857/1000000]
[With 8 bits]: 0.35 ~ 0.349609 (179/2^9) [error = 1116/1000000]
[With 12 bits]: 0.35 ~ 0.349976 (2867/2^13) [error = 70/1000000]
[With 16 bits]: 0.35 ~ 0.349998 (45875/2^17) [error = 4/1000000]
[With 4 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3857 [error = 29930/1000000]
[With 8 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3968 [error = 2012/1000000]
[With 12 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3975 [error = 252/1000000]
[With 16 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3975 [error = 252/1000000]
[With 4 bits]: Overflow!
[With 8 bits]: Overflow!
[With 12 bits]: Overflow!
[With 16 bits]: Overflow!
[With 1 bits]: 0.65 ~ 0.5 (1/2^1) [error = 231/1000]
[With 2 bits]: 0.65 ~ 0.75 (3/2^2) [error = 154/1000]
[With 3 bits]: 0.65 ~ 0.625 (5/2^3) [error = 38/1000]
[With 4 bits]: 0.65 ~ 0.625 (10/2^4) [error = 38/1000]
[With 5 bits]: 0.65 ~ 0.65625 (21/2^5) [error = 10/1000]
[With 6 bits]: Overflow!
[With 7 bits]: Overflow!
[With 8 bits]: Overflow!
[With 1 bits]: 0.65 ~ 0.5 (1/2^1) [error = 231/1000]
[With 2 bits]: 0.65 ~ 0.75 (3/2^2) [error = 154/1000]
[With 3 bits]: 0.65 ~ 0.625 (5/2^3) [error = 38/1000]
[With 4 bits]: 0.65 ~ 0.625 (10/2^4) [error = 38/1000]
[With 5 bits]: 0.65 ~ 0.65625 (21/2^5) [error = 10/1000]
[With 6 bits]: 0.65 ~ 0.65625 (42/2^6) [error = 10/1000]
[With 7 bits]: Overflow!
[With 8 bits]: Overflow!
````
%% Cell type:markdown id: tags:
© _CNRS 2016_ - _Inria 2018-2019_
_This notebook is an adaptation of a lecture prepared and redacted 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 redacted by Sébastien Gilles and Vincent Rouvreau (Inria)_
......
......@@ -23,7 +23,7 @@
"source": [
"### EXERCICE 49: Use a shared pointer in the `TestDisplayContainer::test_display_list_` container\n",
"\n",
"`Register` will of course have to be adapted accordingly."
"`Register` and `main` function will of course have to be adapted accordingly."
]
},
{
......
%% Cell type:markdown id: tags:
# [Getting started in C++](/) - [Useful concepts and STL](/notebooks/5-UsefulConceptsAndSTL/0-main.ipynb) - [TP 16](/notebooks/5-UsefulConceptsAndSTL/6b-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-49:-Use-a-shared-pointer-in-the-TestDisplayContainer::test_display_list_-container" data-toc-modified-id="EXERCICE-49:-Use-a-shared-pointer-in-the-TestDisplayContainer::test_display_list_-container-1">EXERCICE 49: Use a shared pointer in the <code>TestDisplayContainer::test_display_list_</code> container</a></span></li><li><span><a href="#EXERCICE-50:-Replace-shared_ptr-by-unique_ptr-for--TestDisplayContainer::test_display_list_" data-toc-modified-id="EXERCICE-50:-Replace-shared_ptr-by-unique_ptr-for--TestDisplayContainer::test_display_list_-2">EXERCICE 50: Replace <code>shared_ptr</code> by <code>unique_ptr</code> for <code>TestDisplayContainer::test_display_list_</code></a></span></li><li><span><a href="#EXERCICE-51:-remove-TestDisplayContainer-class" data-toc-modified-id="EXERCICE-51:-remove-TestDisplayContainer-class-3">EXERCICE 51: remove <code>TestDisplayContainer</code> class</a></span></li></ul></div>
%% Cell type:markdown id: tags:
### EXERCICE 49: Use a shared pointer in the `TestDisplayContainer::test_display_list_` container
`Register` will of course have to be adapted accordingly.
`Register` and `main` function will of course have to be adapted accordingly.
%% Cell type:markdown id: tags:
### EXERCICE 50: Replace `shared_ptr` by `unique_ptr` for `TestDisplayContainer::test_display_list_`
`shared_ptr` was easier to introduce first due to its easier copy mechanism, but is actually unnecessary: `unique_ptr` may provide the same service with more efficiency.
Replace `shared_ptr` by `unique_ptr`; you will need to play a bit with move semantics for that.
%% Cell type:markdown id: tags:
### EXERCICE 51: remove `TestDisplayContainer` class
The class is now just a wrapper over the underlying `std::vector<std::unique_ptr<TestDisplay>>`, use it directly.
%% Cell type:markdown id: tags:
© _CNRS 2016_ - _Inria 2018-2019_
_This notebook is an adaptation of a lecture prepared and redacted 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 redacted by Sébastien Gilles and Vincent Rouvreau (Inria)_
......
Supports Markdown
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