"Any real number can be approximated by the ratio between two integers. \n",
"\n",
"We will choose here to approximate any real `r` by an expression `numerator / 2^exponent`, where both `numerator` and `exponent` are integers.\n",
"$$\n",
"\\forall r \\, \\in \\mathbb{R}, \\quad r \\simeq \\frac{a}{2^b} \\quad \\text{with} \\quad [a, \\, b] \\in \\mathbb{Z}^2.\n",
"$$\n",
"\n",
"In practice, if we fix the value for the exponent $b$ we can compute the numerator $a$ through $a = r \\cdot 2^b$, which gives an approximation of $r$ as $r_{\\text{approx}} = a \\cdot 2^{-b}$.\n",
"\n",
"The higher the numerator and exponent values, the more accurate the approximation can be. For example, 0.65 can be approximated successively by: \n",
"* 1 / 2<sup>1</sup> = 0.5 \n",
...
...
%% Cell type:markdown id: tags:
# [Getting started in C++](./) - [Procedural programming](./0-main.ipynb) - [Hands-on 1](./4b-hands-on.ipynb)
%% Cell type:markdown id: tags:
## Introduction
### How to do the hands-on?
This [notebook](../HandsOn/HowTo.ipynb) explains very briefly your options to run the hands-ons.
### The problem we'll deal with throughout the hands-ons
Any real number can be approximated by the ratio between two integers.
We will choose here to approximate any real `r` by an expression `numerator / 2^exponent`, where both `numerator` and `exponent` are integers.
$$
\forall r \,\in \mathbb{R}, \quad r \simeq \frac{a}{2^b} \quad \text{with} \quad [a, \, b] \in \mathbb{Z}^2.
$$
In practice, if we fix the value for the exponent $b$ we can compute the numerator $a$ through $a = r \cdot 2^b$, which gives an approximation of $r$ as $r_{\text{approx}} = a \cdot 2^{-b}$.
The higher the numerator and exponent values, the more accurate the approximation can be. For example, 0.65 can be approximated successively by:
* 1 / 2<sup>1</sup> = 0.5
* 3 / 2<sup>2</sup> = 0.75
* 5 / 2<sup>3</sup> = 0.625
* ...
The highest possible numbers will therefore be chosen, within the limits set by the system, i.e. by the number of bits available to encode these numbers.
As part of the hands-on, the number of bits allowed to store the numerator will be arbitrarily fixed, and the effect on the accuracy of the approximation will be calculated. Note that if you have N bits to store an integer, the largest possible integer is 2<sup>N</sup> - 1.
The file you need to start is [provided](../HandsOn/1-ProceduralProgramming/initial_file.cpp) in the _HandsOn_ folder of ProceduralProgramming lectures.
*Note*: you may found more about the context of the formation exercises in those articles: