Commit 4c4ae336 by GILLES Sebastien

### Modify display in all exercices to be consistent in exercice 21. Also reindex...

`Modify display in all exercices to be consistent in exercice 21. Also reindex the exercices: keep the same indexes throughout all the lectures.`
parent 334028a5
 ... ... @@ -14,7 +14,7 @@ }, "source": [ "

\n", "" "" ] }, { ... ... @@ -123,25 +123,27 @@ "\n", "Add in the `display_power_of_2_approx()` function the display of the approximate value, which is calculated by dividing each numerator by the power of two associated. Be careful, dividing an integer by an integer returns an integer: for instance 3 / 4 is 0. A division returns a real if one of the terms is real: 3. / 4 or `static_cast`(3) / 4 is 0.75.\n", "\n", "The expected display is as follows: \n", "\n", "0.65 ~ 1 / 2^1 (0.5) \n", "0.65 ~ 3 / 2^2 (0.75) \n", "0.65 ~ 5 / 2^3 (0.625) \n", "0.65 ~ 10 / 2^4 (0.625) \n", "0.65 ~ 21 / 2^5 (0.65625) \n", "0.65 ~ 42 / 2^6 (0.65625) \n", "0.65 ~ 83 / 2^7 (0.648438) \n", "0.65 ~ 166 / 2^8 (0.648438) \n", "\n", "0.35 ~ 1 / 2^1 (0.5) \n", "0.35 ~ 1 / 2^2 (0.25) \n", "0.35 ~ 3 / 2^3 (0.375) \n", "0.35 ~ 6 / 2^4 (0.375) \n", "0.35 ~ 11 / 2^5 (0.34375) \n", "0.35 ~ 22 / 2^6 (0.34375) \n", "0.35 ~ 45 / 2^7 (0.351562) \n", "0.35 ~ 90 / 2^8 (0.351562) " "We will modify the display slightly so that the output looks like:\n", "\n", "````\n", "0.65 ~ 0.5 (1 / 2^1)\n", "0.65 ~ 0.75 (3 / 2^2)\n", "0.65 ~ 0.625 (5 / 2^3)\n", "0.65 ~ 0.625 (10 / 2^4)\n", "0.65 ~ 0.65625 (21 / 2^5)\n", "0.65 ~ 0.65625 (42 / 2^6)\n", "0.65 ~ 0.648438 (83 / 2^7)\n", "0.65 ~ 0.648438 (166 / 2^8)\n", "\n", "0.35 ~ 0.5 (1 / 2^1)\n", "0.35 ~ 0.25 (1 / 2^2)\n", "0.35 ~ 0.375 (3 / 2^3)\n", "0.35 ~ 0.375 (6 / 2^4)\n", "0.35 ~ 0.34375 (11 / 2^5)\n", "0.35 ~ 0.34375 (22 / 2^6)\n", "0.35 ~ 0.351562 (45 / 2^7)\n", "0.35 ~ 0.351562 (90 / 2^8) \n", "````\n" ] }, { ... ... @@ -181,13 +183,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ "_Expected result_ is something like:\n", "Expected result is something like:\n", "\n", "````\n", "[With numerator < 15]: 0.65 ~ 10 / 2^4 (0.625) \n", "[With numerator < 255]: 0.65 ~ 166 / 2^8 (0.648438) \n", "[With numerator < 15]: 0.35 ~ 11 / 2^5 (0.34375) \n", "[With numerator < 255]: 0.35 ~ 179 / 2^9 (0.349609) \n", "[With numerator < 15]: 0.65 ~ 0.625 (10/2^4)\n", "[With numerator < 255]: 0.65 ~ 0.648438 (166/2^8)\n", "[With numerator < 15]: 0.35 ~ 0.34375 (11/2^5)\n", "[With numerator < 255]: 0.35 ~ 0.349609 (179/2^9)\n", "````" ] }, ... ... @@ -226,15 +228,15 @@ "_Expected result_:\n", "\n", "````\n", "[With 2 bits]: 0.65 ~ 3 / 2^2 (0.75) \n", "[With 4 bits]: 0.65 ~ 10 / 2^4 (0.625) \n", "[With 6 bits]: 0.65 ~ 42 / 2^6 (0.65625) \n", "[With 8 bits]: 0.65 ~ 166 / 2^8 (0.648438) \n", " \n", "[With 2 bits]: 0.35 ~ 3 / 2^3 (0.375) \n", "[With 4 bits]: 0.35 ~ 11 / 2^5 (0.34375) \n", "[With 6 bits]: 0.35 ~ 45 / 2^7 (0.351562) \n", "[With 8 bits]: 0.35 ~ 179 / 2^9 (0.349609) \n", "[With 2 bits]: 0.65 ~ 0.75 (3/2^2)\n", "[With 4 bits]: 0.65 ~ 0.625 (10/2^4)\n", "[With 6 bits]: 0.65 ~ 0.65625 (42/2^6)\n", "[With 8 bits]: 0.65 ~ 0.648438 (166/2^8)\n", "\n", "[With 2 bits]: 0.35 ~ 0.375 (3/2^3)\n", "[With 4 bits]: 0.35 ~ 0.34375 (11/2^5)\n", "[With 6 bits]: 0.35 ~ 0.351562 (45/2^7)\n", "[With 8 bits]: 0.35 ~ 0.349609 (179/2^9)\n", "````" ] }, ... ... @@ -264,11 +266,6 @@ "\n", "Add in `display_power_of_2_approx()` the relative error of the approximation, computed by\n", "\n", "\n", "\n", "\n", "\n", "\n", "````\n", "(number - approx) / number\n", "````\n", ... ... @@ -278,15 +275,15 @@ "_Expected result_:\n", "\n", "````\n", "[With 2 bits]: 0.65 ~ 3 / 2^2 (0.75) [error = 15/100] \n", "[With 4 bits]: 0.65 ~ 10 / 2^4 (0.625) [error = 4/100] \n", "[With 6 bits]: 0.65 ~ 42 / 2^6 (0.65625) [error = 1/100] \n", "[With 8 bits]: 0.65 ~ 166 / 2^8 (0.648438) [error = 0/100] \n", "\n", "[With 2 bits]: 0.35 ~ 3 / 2^3 (0.375) [error = 7/100] \n", "[With 4 bits]: 0.35 ~ 11 / 2^5 (0.34375) [error = 2/100] \n", "[With 6 bits]: 0.35 ~ 45 / 2^7 (0.351562) [error = 0/100] \n", "[With 8 bits]: 0.35 ~ 179 / 2^9 (0.349609) [error = 0/100] \n", "[With 2 bits]: 0.65 ~ 0.75 (3/2^2) [error = 15/100]\n", "[With 4 bits]: 0.65 ~ 0.625 (10/2^4) [error = 4/100]\n", "[With 6 bits]: 0.65 ~ 0.65625 (42/2^6) [error = 1/100]\n", "[With 8 bits]: 0.65 ~ 0.648438 (166/2^8) [error = 0/100]\n", "\n", "[With 2 bits]: 0.35 ~ 0.375 (3/2^3) [error = 7/100]\n", "[With 4 bits]: 0.35 ~ 0.34375 (11/2^5) [error = 2/100]\n", "[With 6 bits]: 0.35 ~ 0.351562 (45/2^7) [error = 0/100]\n", "[With 8 bits]: 0.35 ~ 0.349609 (179/2^9) [error = 0/100]\n", "````" ] }, ... ... @@ -346,24 +343,24 @@ "_Expected result:_\n", "\n", "````\n", "[With 2 bits]: 0.65 ~ 3 / 2^2 (0.75) [error = 15/100] \n", "[With 4 bits]: 0.65 ~ 10 / 2^4 (0.625) [error = 4/100] \n", "[With 6 bits]: 0.65 ~ 42 / 2^6 (0.65625) [error = 1/100] \n", "[With 8 bits]: 0.65 ~ 166 / 2^8 (0.648438) [error = 0/100] \n", " \n", "[With 2 bits]: 0.35 ~ 3 / 2^3 (0.375) [error = 7/100] \n", "[With 4 bits]: 0.35 ~ 11 / 2^5 (0.34375) [error = 2/100] \n", "[With 6 bits]: 0.35 ~ 45 / 2^7 (0.351562) [error = 0/100] \n", "[With 8 bits]: 0.35 ~ 179 / 2^9 (0.349609) [error = 0/100] \n", " \n", "[With 1 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 2965 \n", "[With 2 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4448 \n", "[With 3 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4008 \n", "[With 4 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3857 \n", "[With 5 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3967 \n", "[With 6 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4004 \n", "[With 7 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3977 \n", "[With 8 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3968 \n", "[With 2 bits]: 0.65 ~ 0.75 (3/2^2) [error = 15/100]\n", "[With 4 bits]: 0.65 ~ 0.625 (10/2^4) [error = 4/100]\n", "[With 6 bits]: 0.65 ~ 0.65625 (42/2^6) [error = 1/100]\n", "[With 8 bits]: 0.65 ~ 0.648438 (166/2^8) [error = 0/100]\n", "\n", "[With 2 bits]: 0.35 ~ 0.375 (3/2^3) [error = 7/100]\n", "[With 4 bits]: 0.35 ~ 0.34375 (11/2^5) [error = 2/100]\n", "[With 6 bits]: 0.35 ~ 0.351562 (45/2^7) [error = 0/100]\n", "[With 8 bits]: 0.35 ~ 0.349609 (179/2^9) [error = 0/100]\n", "\n", "[With 1 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 2965\n", "[With 2 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4448\n", "[With 3 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4008\n", "[With 4 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3857\n", "[With 5 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3967\n", "[With 6 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4004\n", "[With 7 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3977\n", "[With 8 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3968\n", "````" ] }, ... ... @@ -371,7 +368,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ "### __EXERCICE 9: display sum of two `multiply`\n", "### EXERCICE 9: display sum of two `multiply`\n", "\n", "Exactly as we did previously in exercice 2, move the display of the sum of multiplication result in a dedicated function `display_sum()` (which will hence takes 5 arguments: number of bits, two reals and their associated integer coefficient).\n", "\n", ... ... @@ -419,24 +416,24 @@ "_Expected result_:\n", "\n", "````\n", "[With 2 bits]: 0.65 ~ 3 / 2^2 (0.75) [error = 15/100] \n", "[With 4 bits]: 0.65 ~ 10 / 2^4 (0.625) [error = 4/100] \n", "[With 6 bits]: 0.65 ~ 42 / 2^6 (0.65625) [error = 1/100] \n", "[With 8 bits]: 0.65 ~ 166 / 2^8 (0.648438) [error = 0/100] \n", " \n", "[With 2 bits]: 0.35 ~ 3 / 2^3 (0.375) [error = 7/100] \n", "[With 4 bits]: 0.35 ~ 11 / 2^5 (0.34375) [error = 2/100] \n", "[With 6 bits]: 0.35 ~ 45 / 2^7 (0.351562) [error = 0/100] \n", "[With 8 bits]: 0.35 ~ 179 / 2^9 (0.349609) [error = 0/100] \n", " \n", "[With 1 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 2965 [error = 254/1000] \n", "[With 2 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4448 [error = 119/1000] \n", "[With 3 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4008 [error = 8/1000] \n", "[With 4 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3857 [error = 30/1000] \n", "[With 5 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3967 [error = 2/1000] \n", "[With 6 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4004 [error = 7/1000] \n", "[With 7 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3977 [error = 0/1000] \n", "[With 8 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3968 [error = 2/1000] \n", "[With 2 bits]: 0.65 ~ 0.75 (3/2^2) [error = 15/100]\n", "[With 4 bits]: 0.65 ~ 0.625 (10/2^4) [error = 4/100]\n", "[With 6 bits]: 0.65 ~ 0.65625 (42/2^6) [error = 1/100]\n", "[With 8 bits]: 0.65 ~ 0.648438 (166/2^8) [error = 0/100]\n", "\n", "[With 2 bits]: 0.35 ~ 0.375 (3/2^3) [error = 7/100]\n", "[With 4 bits]: 0.35 ~ 0.34375 (11/2^5) [error = 2/100]\n", "[With 6 bits]: 0.35 ~ 0.351562 (45/2^7) [error = 0/100]\n", "[With 8 bits]: 0.35 ~ 0.349609 (179/2^9) [error = 0/100]\n", "\n", "[With 1 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 2965 [error = 254/1000]\n", "[With 2 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4448 [error = 119/1000]\n", "[With 3 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4008 [error = 8/1000]\n", "[With 4 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3857 [error = 30/1000]\n", "[With 5 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3967 [error = 2/1000]\n", "[With 6 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4004 [error = 7/1000]\n", "[With 7 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3977 [error = 0/1000]\n", "[With 8 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3968 [error = 2/1000]\n", "````\n" ] }, ... ...
 %% Cell type:markdown id: tags: # [Getting started in C++](/) - [Procedural programming](/notebooks/1-ProceduralProgramming/0-main.ipynb) - [TP 1](/notebooks/1-ProceduralProgramming/4b-TP.ipynb) %% Cell type:markdown id: tags:

 ... ... @@ -111,7 +111,7 @@ void display_power_of_2_approx(int Nbits, double value) int error = round_as_int(100. * std::fabs(value - approx) / value); std::cout << "[With " << Nbits << " bits]: " << value << " ~ " << numerator << " / 2^" << exponent << " (" << approx << ')' << value << " ~ " << approx << " (" << numerator << "/2^" << exponent << ')' << " [error = " << error << "/" << "100]" << std::endl ; } ... ...
 ... ... @@ -114,7 +114,7 @@ void display_power_of_2_approx(int Nbits, double value) int error = round_as_int(100. * std::fabs(value - approx) / value); std::cout << "[With " << Nbits << " bits]: " << value << " ~ " << numerator << " / 2^" << exponent << " (" << approx << ')' << value << " ~ " << approx << " (" << numerator << "/2^" << exponent << ')' << " [error = " << error << "/" << "100]" << std::endl ; } ... ...
 ... ... @@ -124,7 +124,7 @@ void display_power_of_2_approx(std::ostream& out, int Nbits, double value) int error = round_as_int(100. * std::fabs(value - approx) / value); out << "[With " << Nbits << " bits]: " << value << " ~ " << numerator << " / 2^" << exponent << " (" << approx << ')' << value << " ~ " << approx << " (" << numerator << "/2^" << exponent << ')' << " [error = " << error << "/" << "100]" << std::endl ; } ... ...
 ... ... @@ -64,13 +64,13 @@ int round_as_int(double x) void display_power_of_2_approx(double value) { for (int i = 1; i <= 8; ++i) for (int exponent = 1; exponent <= 8; ++exponent) { int denominator = times_power_of_2(1, i); int denominator = times_power_of_2(1, exponent); int numerator = round_as_int(value * denominator); double approx = static_cast(numerator) / denominator; std::cout << value << " ~ " << numerator << " / 2^" << i << " (" << approx << ')' << std::endl ; std::cout << value << " ~ " << approx << " (" << numerator << "/2^" << exponent << ')' << std::endl; } std::cout << std::endl; ... ...
 ... ... @@ -85,6 +85,6 @@ void display_power_of_2_approx(int max_numerator, double value) double approx = static_cast(numerator) / denominator; std::cout << "[With numerator < " << max_numerator << "]: " << value << " ~ " << numerator << " / 2^" << exponent << " (" << approx << ')' << std::endl ; << value << " ~ " << approx << " (" << numerator << "/2^" << exponent << ')' << std::endl ; }
 ... ... @@ -92,7 +92,7 @@ void display_power_of_2_approx(int Nbits, double value) double approx = static_cast(numerator) / denominator; std::cout << "[With " << Nbits << " bits]: " << value << " ~ " << numerator << " / 2^" << exponent << " (" << approx << ')' << std::endl ; << value << " ~ " << approx << " (" << numerator << "/2^" << exponent << ')' << std::endl ; } ... ...
 ... ... @@ -85,7 +85,7 @@ void display_power_of_2_approx(int Nbits, double value) double approx = static_cast(numerator) / denominator; std::cout << "[With " << Nbits << " bits]: " << value << " ~ " << numerator << " / 2^" << exponent << " (" << approx << ')' << std::endl ; << value << " ~ " << approx << " (" << numerator << "/2^" << exponent << ')' << std::endl ; } ... ...
 ... ... @@ -86,7 +86,7 @@ void display_power_of_2_approx(int Nbits, double value) int error = round_as_int(100. * std::fabs(value - approx) / value); std::cout << "[With " << Nbits << " bits]: " << value << " ~ " << numerator << " / 2^" << exponent << " (" << approx << ')' << value << " ~ " << approx << " (" << numerator << "/2^" << exponent << ')' << " [error = " << error << "/" << "100]" << std::endl ; } ... ...
 ... ... @@ -110,7 +110,7 @@ void display_power_of_2_approx(int Nbits, double value) int error = round_as_int(100. * std::fabs(value - approx) / value); std::cout << "[With " << Nbits << " bits]: " << value << " ~ " << numerator << " / 2^" << exponent << " (" << approx << ')' << value << " ~ " << approx << " (" << numerator << "/2^" << exponent << ')' << " [error = " << error << "/" << "100]" << std::endl ; } ... ...
 ... ... @@ -111,7 +111,7 @@ void display_power_of_2_approx(int Nbits, double value) int error = round_as_int(100. * std::fabs(value - approx) / value); std::cout << "[With " << Nbits << " bits]: " << value << " ~ " << numerator << " / 2^" << exponent << " (" << approx << ')' << value << " ~ " << approx << " (" << numerator << "/2^" << exponent << ')' << " [error = " << error << "/" << "100]" << std::endl ; } ... ...
 ... ... @@ -14,7 +14,7 @@ }, "source": [ "