Mentions légales du service

Skip to content
Snippets Groups Projects

Few modifications done while rereading and running the hands ons

All threads resolved!
4 files
+ 25
20
Compare changes
  • Side-by-side
  • Inline
Files
4
%% Cell type:markdown id: tags:
# [Getting started in C++](./) - [Procedural programming](./0-main.ipynb) - [Hands-on 3](./7b-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="#EXERCICE-8:-Multiplication-by-an-integer" data-toc-modified-id="EXERCICE-8:-Multiplication-by-an-integer-1"><strong>EXERCICE 8: Multiplication by an integer</strong></a></span></li><li><span><a href="#EXERCICE-9:-display-sum-of-two-Multiply" data-toc-modified-id="EXERCICE-9:-display-sum-of-two-Multiply-2">EXERCICE 9: display sum of two <code>Multiply</code></a></span></li><li><span><a href="#EXERCICE-10:-print-error-in-DisplaySumOfMultiply()" data-toc-modified-id="EXERCICE-10:-print-error-in-DisplaySumOfMultiply()-3"><strong>EXERCICE 10: print error in <code>DisplaySumOfMultiply()</code></strong></a></span></li><li><span><a href="#[optional]-EXERCICE-11:-function-pointers" data-toc-modified-id="[optional]-EXERCICE-11:-function-pointers-4">[optional] EXERCICE 11: function pointers</a></span></li><li><span><a href="#[optional]-EXERCICE-12:-write-in-output-file" data-toc-modified-id="[optional]-EXERCICE-12:-write-in-output-file-5">[optional] EXERCICE 12: write in output file</a></span></li></ul></div>
<div class="toc"><ul class="toc-item"><li><span><a href="#EXERCICE-8:-Multiplication-by-an-integer" data-toc-modified-id="EXERCICE-8:-Multiplication-by-an-integer-1"><strong>EXERCICE 8: Multiplication by an integer</strong></a></span></li><li><span><a href="#EXERCICE-9:-display-sum-of-two-Multiply" data-toc-modified-id="EXERCICE-9:-display-sum-of-two-Multiply-2">EXERCICE 9: display sum of two Multiply</a></span></li><li><span><a href="#EXERCICE-10:-print-error-in-DisplaySumOfMultiply()" data-toc-modified-id="EXERCICE-10:-print-error-in-DisplaySumOfMultiply()-3"><strong>EXERCICE 10: print error in <code>DisplaySumOfMultiply()</code></strong></a></span></li><li><span><a href="#[optional]-EXERCICE-11:-function-pointers" data-toc-modified-id="[optional]-EXERCICE-11:-function-pointers-4">[optional] EXERCICE 11: function pointers</a></span></li><li><span><a href="#[optional]-EXERCICE-12:-write-in-output-file" data-toc-modified-id="[optional]-EXERCICE-12:-write-in-output-file-5">[optional] EXERCICE 12: write in output file</a></span></li></ul></div>
%% Cell type:markdown id: tags:
### __EXERCICE 8: Multiplication by an integer__
Write the `Multiply()` function that calculates the approximate product of a real by an integer coefficient and returns an integer.
This function must approximate the real using the `ComputePowerOf2Approx()` function above; the returned integer shall use `TimesPowerOf2()` function.
The arguments of `Multiply()` are the maximum number of bits for approximation, the real and the integer coefficient.
The new main will be:
%% 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
for (int nbits = 2; nbits <= 8; nbits += 2)
DisplayPowerOf2Approx(nbits, 0.65) ;
std::cout << std::endl;
for (int nbits = 2; nbits <= 8; nbits += 2)
DisplayPowerOf2Approx(nbits, 0.35) ;
std::cout << std::endl;
for (int nbits = 1; nbits <= 8; ++nbits)
{
double exact = 0.65 * 3515;
int rounded = RoundAsInt(exact);
int approx = Multiply(nbits, 0.65, 3515);
std::cout << "[With " << nbits << " bits]: 0.65 * 3515 = "
<< rounded << " ~ " << approx << std::endl;
}
return EXIT_SUCCESS;
}
```
%% Cell type:markdown id: tags:
_Expected result:_
````
[With 2 bits]: 0.65 ~ 0.75 (3 / 2^2) [error = 15/100]
[With 4 bits]: 0.65 ~ 0.625 (10 / 2^4) [error = 4/100]
[With 6 bits]: 0.65 ~ 0.65625 (42 / 2^6) [error = 1/100]
[With 8 bits]: 0.65 ~ 0.648438 (166 / 2^8) [error = 0/100]
[With 2 bits]: 0.35 ~ 0.375 (3 / 2^3) [error = 7/100]
[With 4 bits]: 0.35 ~ 0.34375 (11 / 2^5) [error = 2/100]
[With 6 bits]: 0.35 ~ 0.351562 (45 / 2^7) [error = 0/100]
[With 8 bits]: 0.35 ~ 0.349609 (179 / 2^9) [error = 0/100]
[With 1 bits]: 0.65 * 3515 = 2285 ~ 1757
[With 2 bits]: 0.65 * 3515 = 2285 ~ 2636
[With 3 bits]: 0.65 * 3515 = 2285 ~ 2196
[With 4 bits]: 0.65 * 3515 = 2285 ~ 2196
[With 5 bits]: 0.65 * 3515 = 2285 ~ 2306
[With 6 bits]: 0.65 * 3515 = 2285 ~ 2306
[With 7 bits]: 0.65 * 3515 = 2285 ~ 2279
[With 8 bits]: 0.65 * 3515 = 2285 ~ 2279
````
%% Cell type:markdown id: tags:
### EXERCICE 9: display sum of two `Multiply`
### EXERCICE 9: display sum of two Multiply
Write a `DisplaySumOfMultiply` function which will write the computation of the sum of two real numbers through their approximation.
This function will take 5 arguments:
* The number of bits to use.
* Two real values.
* Their associated coefficients.
New main will 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
for (int nbits = 2; nbits <= 8; nbits += 2)
DisplayPowerOf2Approx(0.65, nbits);
std::cout << std::endl;
for (int nbits = 2; nbits <= 8; nbits += 2)
DisplayPowerOf2Approx(0.35, nbits);
std::cout << std::endl;
for (int nbits = 1; nbits <= 8; ++nbits)
DisplaySumOfMultiply(nbits, 0.65, 3515, 0.35, 4832); // to compute 0.65 * 3515 + 0.35 * 4832
return EXIT_SUCCESS;
}
```
%% Cell type:markdown id: tags:
*Expected result*:
[With 2 bits]: 0.65 ~ 0.75 (3 / 2^2) [error = 15/100]
[With 4 bits]: 0.65 ~ 0.625 (10 / 2^4) [error = 4/100]
[With 6 bits]: 0.65 ~ 0.65625 (42 / 2^6) [error = 1/100]
[With 8 bits]: 0.65 ~ 0.648438 (166 / 2^8) [error = 0/100]
[With 2 bits]: 0.35 ~ 0.375 (3 / 2^3) [error = 7/100]
[With 4 bits]: 0.35 ~ 0.34375 (11 / 2^5) [error = 2/100]
[With 6 bits]: 0.35 ~ 0.351562 (45 / 2^7) [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
[With 2 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4448
[With 3 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4008
[With 4 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3857
[With 5 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3967
[With 6 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4004
[With 7 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3977
[With 8 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3968
%% Cell type:markdown id: tags:
### __EXERCICE 10: print error in `DisplaySumOfMultiply()`__
Modify slightly the function defined above to add display of the error; we will express it over 1000 (see exercice 7 which was roughly the same!)
*Expected result*:
````
[With 2 bits]: 0.65 ~ 0.75 (3/2^2) [error = 15/100]
[With 4 bits]: 0.65 ~ 0.625 (10/2^4) [error = 4/100]
[With 6 bits]: 0.65 ~ 0.65625 (42/2^6) [error = 1/100]
[With 8 bits]: 0.65 ~ 0.648438 (166/2^8) [error = 0/100]
[With 2 bits]: 0.35 ~ 0.375 (3/2^3) [error = 7/100]
[With 4 bits]: 0.35 ~ 0.34375 (11/2^5) [error = 2/100]
[With 6 bits]: 0.35 ~ 0.351562 (45/2^7) [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:
### [optional] EXERCICE 11: function pointers
Create a `Loop()` function that takes as an argument :
* An initial number of bits
* A final number of bits
* An increment to be applied to the number of bits
* A pointer to a function to be executed for each number of bits
You will need the following intermediate function to be able to use them in `Loop()` (as a specific signature is expected):
%% Cell type:code id: tags:
``` C++17
void Display065(int nbits)
void Display_065(int nbits)
{
DisplayPowerOf2Approx(nbits, 0.65);
}
void Display035(int nbits)
void Display_035(int nbits)
{
DisplayPowerOf2Approx(nbits, 0.35);
}
void Display065_3515_035_4832(int nbits)
void Display_065_3515_035_4832(int nbits)
{
DisplaySumOfMultiply(nbits, 0.65, 3515, 0.35, 4832);
}
int main()
{
Loop(2, 8, 2, Display065);
Loop(2, 8, 2, Display035);
Loop(1, 8, 1, Display065_3515_035_4832);
Loop(2, 8, 2, Display_065);
Loop(2, 8, 2, Display_035);
Loop(1, 8, 1, Display_065_3515_035_4832);
return EXIT_SUCCESS;
}
```
%% Cell type:markdown id: tags:
### [optional] EXERCICE 12: write in output file
Modify the program so that the `displayXXX` functions take an additional argument: the output stream to which the content should be written. `Loop()` will be modified as well.
Modify the program so that the `DisplayXXX` functions take an additional argument: the output stream to which the content should be written. `Loop()` will be modified as well.
The following `main()` which writes part of the outputs in a file should work:
%% Cell type:code id: tags:
``` C++17
#include <fstream>
int main(int argc, char** argv)
{
if (argc != 2)
{
std::cerr << "Two arguments are expected on command line: the name of the program followed by the "
"path of the file in which some of the outputs will be written." << std::endl;
exit(EXIT_FAILURE);
}
// Open the stream to the file.
std::ofstream output_file(argv[1]);
Loop(output_file, 2, 8, 2, Display065);
Loop(output_file, 2, 8, 2, Display035);
Loop(std::cout, 1, 8, 1, Display065_3515_035_4832);
Loop(output_file, 2, 8, 2, Display_065);
Loop(output_file, 2, 8, 2, Display_035);
Loop(std::cout, 1, 8, 1, Display_065_3515_035_4832);
return EXIT_SUCCESS;
}
```
%% Cell type:markdown id: tags:
© _CNRS 2016_ - _Inria 2018-2022_
_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)_
Loading