diff --git a/1-ProceduralProgramming/4b-hands-on.ipynb b/1-ProceduralProgramming/4b-hands-on.ipynb index 9baebc0198a9478443e16123fdd12a453ade6c96..0df347f946f6eb10f92f654555e95cbf95a00d1d 100644 --- a/1-ProceduralProgramming/4b-hands-on.ipynb +++ b/1-ProceduralProgramming/4b-hands-on.ipynb @@ -23,7 +23,7 @@ "source": [ "### __EXERCICE 2: Adding a function__\n", "\n", - "Introduce in the previous program a function called `display_power_of_2_approx()` which takes as argument the actual value to be approximated. Modify the main program to call this function with values 0.65 and 0.35. The main program must be:" + "Introduce in the previous program a function called `DisplayPowerOf2Approx()` which takes as argument the actual value to be approximated. Modify the main program to call this function with values 0.65 and 0.35. The main program must be:" ] }, { @@ -34,8 +34,8 @@ "source": [ "int main()\n", "{\n", - " display_power_of_2_approx(.65);\n", - " display_power_of_2_approx(.35);\n", + " DisplayPowerOf2Approx(.65);\n", + " DisplayPowerOf2Approx(.35);\n", " \n", " return EXIT_SUCCESS;\n", "}" @@ -72,7 +72,7 @@ "source": [ "### __EXERCICE 3: compute the approximation__\n", "\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<double>`(3) / 4 is 0.75.\n", + "Add in the `DisplayPowerOf2Approx()` 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<double>`(3) / 4 is 0.75.\n", "\n", "We will modify the display slightly so that the output looks like:\n", "\n", @@ -105,7 +105,7 @@ "\n", "The larger the numerator and the exponent to the denominator, the more accurate the approximation. \n", "\n", - "In `display_power_of_2_approx()`, modify the loop so that it looks for the best numerator / exponent pair, without the numerator exceeding a certain maximum value, passed as an argument to the function.\n", + "In `DisplayPowerOf2Approx()`, modify the loop so that it looks for the best numerator / exponent pair, without the numerator exceeding a certain maximum value, passed as an argument to the function.\n", "\n", "Keep the display only for this best solution, and add in this display the value of the maximum allowed numerator.\n", "\n", @@ -120,11 +120,11 @@ "source": [ "int main()\n", "{\n", - " display_power_of_2_approx(15, 0.65);\n", - " display_power_of_2_approx(255, 0.65);\n", + " DisplayPowerOf2Approx(15, 0.65);\n", + " DisplayPowerOf2Approx(255, 0.65);\n", "\n", - " display_power_of_2_approx(15, 0.35);\n", - " display_power_of_2_approx(255, 0.35);\n", + " DisplayPowerOf2Approx(15, 0.35);\n", + " DisplayPowerOf2Approx(255, 0.35);\n", "\n", " return EXIT_SUCCESS;\n", "}" @@ -152,7 +152,7 @@ "\n", "The highest usable value for the numerator depends on the number of bits used to represent this integer. \n", "\n", - "In `display_power_of_2_approx()` arguments, replace the argument designating the maximum numerator with an argument designating the maximum number of bits and correct the body of the function accordingly, using the `max_int()` function given below.\n", + "In `DisplayPowerOf2Approx()` arguments, replace the argument designating the maximum numerator with an argument designating the maximum number of bits and correct the body of the function accordingly, using the `MaxInt()` function given below.\n", "\n", "On display, replace the maximum numerator with the number of bits. \n" ] @@ -163,10 +163,10 @@ "metadata": {}, "outputs": [], "source": [ - "// Maximum integer that might be represented with `Nbits` bits.\n", - "int max_int(int Nbits)\n", + "// Maximum integer that might be represented with `nbits` bits.\n", + "int MaxInt(int nbits)\n", "{ \n", - " return (times_power_of_2(1, Nbits) - 1);\n", + " return (TimesPowerOf2(1, nbits) - 1);\n", "}" ] }, @@ -188,13 +188,13 @@ " static_cast<void>(argc); // to silence warning about unused argc - don't bother \n", " static_cast<void>(argv); // to silence warning about unused argv - don't bother \n", " \n", - " for (int Nbits = 2; Nbits <= 8; Nbits += 2)\n", - " display_power_of_2_approx(Nbits, 0.65);\n", + " for (int nbits = 2; nbits <= 8; nbits += 2)\n", + " DisplayPowerOf2Approx(nbits, 0.65);\n", " \n", " std::cout << std::endl;\n", "\n", - " for (int Nbits = 2; Nbits <= 8; Nbits += 2)\n", - " display_power_of_2_approx(Nbits, 0.35);\n", + " for (int nbits = 2; nbits <= 8; nbits += 2)\n", + " DisplayPowerOf2Approx(nbits, 0.35);\n", "\n", " return EXIT_SUCCESS;\n", "}" @@ -225,14 +225,14 @@ "source": [ "### __EXERCICE 6: moving display to a new intermediate function__\n", "\n", - "Currently, `display_power_of_2_approx()` is in charge of two different operations:\n", + "Currently, `DisplayPowerOf2Approx()` is in charge of two different operations:\n", "\n", "* Computing the values.\n", "* Displaying them on screen.\n", "\n", "It is often advisable to give one main functionality to a given function; we will thefore separate here the computation from the display.\n", "\n", - "Write a new function named `compute_power_of_2_approx()` that will be called in `display_power_of_2_approx()`.\n", + "Write a new function named `ComputePowerOf2Approx()` that will be called in `DisplayPowerOf2Approx()`.\n", "\n", "This new function should:\n", "* Return the floating point approximation.\n", @@ -247,7 +247,7 @@ "source": [ "### __EXERCICE 7: adding error in display__\n", "\n", - "Add in `display_power_of_2_approx()` the relative error of the approximation, computed by\n", + "Add in `DisplayPowerOf2Approx()` the relative error of the approximation, computed by\n", "\n", "````\n", "|number - approx| / number\n", diff --git a/1-ProceduralProgramming/6b-hands-on.ipynb b/1-ProceduralProgramming/6b-hands-on.ipynb index b60aedef088008081d702a90c4a92da7c5aa61a1..a5cd32a7ac4ed4a784225822db1fe09f6375e1e0 100644 --- a/1-ProceduralProgramming/6b-hands-on.ipynb +++ b/1-ProceduralProgramming/6b-hands-on.ipynb @@ -14,7 +14,7 @@ }, "source": [ "<h1>Table of contents<span class=\"tocSkip\"></span></h1>\n", - "<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-display_sum()\" data-toc-modified-id=\"EXERCICE-10:-print-error-in-display_sum()-3\"><strong>EXERCICE 10: print error in <code>display_sum()</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 <code>Multiply</code></a></span></li><li><span><a href=\"#EXERCICE-10:-print-error-in-display_sum()\" data-toc-modified-id=\"EXERCICE-10:-print-error-in-display_sum()-3\"><strong>EXERCICE 10: print error in <code>display_sum()</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>" ] }, { @@ -23,11 +23,11 @@ "source": [ "### __EXERCICE 8: Multiplication by an integer__\n", "\n", - "Write the `multiply()` function that calculates the approximate product of a real by an integer coefficient and returns an integer. \n", + "Write the `Multiply()` function that calculates the approximate product of a real by an integer coefficient and returns an integer. \n", "\n", - "This function must approximate the real using the `compute_power_of_2_approx()` function above; the returned integer shall use `times_power_of_2()` function.\n", + "This function must approximate the real using the `ComputePowerOf2Approx()` function above; the returned integer shall use `TimesPowerOf2()` function.\n", "\n", - "The arguments of `multiply()` are the maximum number of bits for approximation, the real and the integer coefficient.\n", + "The arguments of `Multiply()` are the maximum number of bits for approximation, the real and the integer coefficient.\n", "\n", "The new main will be:" ] @@ -43,22 +43,22 @@ " static_cast<void>(argc); // to silence warning about unused argc - don't bother \n", " static_cast<void>(argv); // to silence warning about unused argv - don't bother \n", " \n", - " for (int Nbits = 2; Nbits <= 8; Nbits += 2)\n", - " display_power_of_2_approx(Nbits, 0.65) ;\n", + " for (int nbits = 2; nbits <= 8; nbits += 2)\n", + " DisplayPowerOf2Approx(nbits, 0.65) ;\n", " \n", " std::cout << std::endl;\n", "\n", - " for (int Nbits = 2; Nbits <= 8; Nbits += 2)\n", - " display_power_of_2_approx(Nbits, 0.35) ;\n", + " for (int nbits = 2; nbits <= 8; nbits += 2)\n", + " DisplayPowerOf2Approx(nbits, 0.35) ;\n", " \n", " std::cout << std::endl;\n", "\n", - " for (int Nbits = 1; Nbits <= 8; ++Nbits)\n", + " for (int nbits = 1; nbits <= 8; ++nbits)\n", " {\n", " double exact = 0.65 * 3515;\n", - " int rounded = round_as_int(exact);\n", - " int approx = multiply(Nbits, 0.65, 3515);\n", - " std::cout << \"[With \" << Nbits << \" bits]: 0.65 * 3515 = \" \n", + " int rounded = RoundAsInt(exact);\n", + " int approx = Multiply(nbits, 0.65, 3515);\n", + " std::cout << \"[With \" << nbits << \" bits]: 0.65 * 3515 = \" \n", " << rounded << \" ~ \" << approx << std::endl;\n", " }\n", "\n", @@ -98,7 +98,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### EXERCICE 9: display sum of two `multiply`\n", + "### EXERCICE 9: display sum of two `Multiply`\n", "\n", "Write a `display_sum` function which will write the computation of the sum of two real numbers through their approximation.\n", "\n", @@ -122,18 +122,18 @@ " static_cast<void>(argc); // to silence warning about unused argc - don't bother \n", " static_cast<void>(argv); // to silence warning about unused argv - don't bother \n", " \n", - " for (int Nbits = 2; Nbits <= 8; Nbits += 2)\n", - " display_power_of_2_approx(0.65, Nbits);\n", + " for (int nbits = 2; nbits <= 8; nbits += 2)\n", + " DisplayPowerOf2Approx(0.65, nbits);\n", "\n", " std::cout << std::endl;\n", "\n", - " for (int Nbits = 2; Nbits <= 8; Nbits += 2)\n", - " display_power_of_2_approx(0.35, Nbits);\n", + " for (int nbits = 2; nbits <= 8; nbits += 2)\n", + " DisplayPowerOf2Approx(0.35, nbits);\n", " \n", " std::cout << std::endl;\n", " \n", - " for (int Nbits = 1; Nbits <= 8; ++Nbits)\n", - " display_multiply(Nbits, 0.65, 3515, 0.35, 4832); // to compute 0.65 * 3515 + 0.35 * 4832\n", + " for (int nbits = 1; nbits <= 8; ++nbits)\n", + " DisplayMultiply(nbits, 0.65, 3515, 0.35, 4832); // to compute 0.65 * 3515 + 0.35 * 4832\n", " \n", " return EXIT_SUCCESS;\n", "}" @@ -203,14 +203,14 @@ "source": [ "### [optional] EXERCICE 11: function pointers\n", "\n", - "Create a `loop()` function that takes as an argument :\n", + "Create a `Loop()` function that takes as an argument :\n", "\n", "* An initial number of bits\n", "* A final number of bits\n", "* An increment to be applied to the number of bits\n", "* A pointer to a function to be executed for each number of bits\n", "\n", - "You will need the following intermediate function to be able to use them in `loop()` (as a specific signature is expected):" + "You will need the following intermediate function to be able to use them in `Loop()` (as a specific signature is expected):" ] }, { @@ -219,26 +219,26 @@ "metadata": {}, "outputs": [], "source": [ - "void display_065(int Nbits)\n", + "void Display065(int nbits)\n", "{ \n", - " display_power_of_2_approx(Nbits, 0.65); \n", + " DisplayPowerOf2Approx(nbits, 0.65); \n", "}\n", "\n", - "void display_035(int Nbits)\n", + "void Display035(int nbits)\n", "{\n", - " display_power_of_2_approx(Nbits, 0.35); \n", + " DisplayPowerOf2Approx(nbits, 0.35); \n", "}\n", "\n", - "void display_065_3515_035_4832(int Nbits)\n", + "void Display065_3515_035_4832(int nbits)\n", "{ \n", - " display_sum(Nbits, 0.65, 3515, 0.35, 4832); \n", + " display_sum(nbits, 0.65, 3515, 0.35, 4832); \n", "}\n", "\n", "int main()\n", "{\n", - " loop(2, 8, 2, display_065);\n", - " loop(2, 8, 2, display_035);\n", - " loop(1, 8, 1, display_065_3515_035_4832);\n", + " Loop(2, 8, 2, Display065);\n", + " Loop(2, 8, 2, Display035);\n", + " Loop(1, 8, 1, Display065_3515_035_4832);\n", " return EXIT_SUCCESS;\n", "}\n" ] @@ -249,7 +249,7 @@ "source": [ "### [optional] EXERCICE 12: write in output file \n", "\n", - "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.\n", + "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.\n", "\n", "The following `main()` which writes part of the outputs in a file should work:" ] @@ -274,9 +274,9 @@ " // Open the stream to the file.\n", " std::ofstream output_file(argv[1]);\n", " \n", - " loop(output_file, 2, 8, 2, display_065);\n", - " loop(output_file, 2, 8, 2, display_035);\n", - " loop(std::cout, 1, 8, 1, display_065_3515_035_4832);\n", + " Loop(output_file, 2, 8, 2, Display065);\n", + " Loop(output_file, 2, 8, 2, Display035);\n", + " Loop(std::cout, 1, 8, 1, Display065_3515_035_4832);\n", "\n", " return EXIT_SUCCESS;\n", "}" diff --git a/2-ObjectProgramming/1b-hands-on.ipynb b/2-ObjectProgramming/1b-hands-on.ipynb index 107bd2ce73ffab6d11ea63a8dc6daad6e7867424..c0e65b7ec3a1ec70270172c8b62a2e819cc40762 100644 --- a/2-ObjectProgramming/1b-hands-on.ipynb +++ b/2-ObjectProgramming/1b-hands-on.ipynb @@ -32,7 +32,7 @@ "\n", "We will tackle the same problem with a dedicated `struct` called `PowerOfTwoApprox` which will be used to represent a real by two integers: the numerator and the exponent to the power of 2 used in the denominator.\n", "\n", - "Create this structure and use it throughout the program (`compute_power_of_2_approx()` signature should be modified accordingly).\n", + "Create this structure and use it throughout the program (`ComputePowerOf2Approx()` signature should be modified accordingly).\n", "\n", "The expected output is unchanged from the initial file:\n", "\n", diff --git a/2-ObjectProgramming/2b-hands-on.ipynb b/2-ObjectProgramming/2b-hands-on.ipynb index c2230e85e15900b7a98ddb1370af676f550d826f..a4c19a5f0b0127f8ded430434b34749730bd8dbd 100644 --- a/2-ObjectProgramming/2b-hands-on.ipynb +++ b/2-ObjectProgramming/2b-hands-on.ipynb @@ -14,18 +14,18 @@ }, "source": [ "<h1>Table of contents<span class=\"tocSkip\"></span></h1>\n", - "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#EXERCICE-14:-transform-compute_power_of_2_approx()-into-a-PowerOfTwoApprox-method\" data-toc-modified-id=\"EXERCICE-14:-transform-compute_power_of_2_approx()-into-a-PowerOfTwoApprox-method-1\">EXERCICE 14: transform <code>compute_power_of_2_approx()</code> into a <code>PowerOfTwoApprox</code> method</a></span></li></ul></div>" + "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#EXERCICE-14:-transform-ComputePowerOf2Approx()-into-a-PowerOfTwoApprox-method\" data-toc-modified-id=\"EXERCICE-14:-transform-ComputePowerOf2Approx()-into-a-PowerOfTwoApprox-method-1\">EXERCICE 14: transform <code>ComputePowerOf2Approx()</code> into a <code>PowerOfTwoApprox</code> method</a></span></li></ul></div>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### EXERCICE 14: transform `compute_power_of_2_approx()` into a `PowerOfTwoApprox` method\n", + "### EXERCICE 14: transform `ComputePowerOf2Approx()` into a `PowerOfTwoApprox` method\n", "\n", "What we did in exercice 1 is a bit clunky: the object is introduced but we keep spending time fetching the data attribute to act upon them (even initialization to 0 is done manually several times).\n", "\n", - "We'll streamline this a bit by making some of the functions methods of the `PowerOfTwoApprox` struct; starting with `compute_power_of_2_approx()` that will become a method named `Compute()`. This method will change the internal data attributes rather than changing an output argument.\n", + "We'll streamline this a bit by making some of the functions methods of the `PowerOfTwoApprox` struct; starting with `ComputePowerOf2Approx()` that will become a method named `Compute()`. This method will change the internal data attributes rather than changing an output argument.\n", "\n", "Output should remain unchanged after this modification." ] diff --git a/2-ObjectProgramming/4b-hands-on.ipynb b/2-ObjectProgramming/4b-hands-on.ipynb index 100b6ebd1a71e6418b35ac2a059547f9df294ee5..c0b411f60c6b1cb36125b3cc35b90dd54b87e021 100644 --- a/2-ObjectProgramming/4b-hands-on.ipynb +++ b/2-ObjectProgramming/4b-hands-on.ipynb @@ -14,7 +14,7 @@ }, "source": [ "<h1>Table of contents<span class=\"tocSkip\"></span></h1>\n", - "<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>" + "<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-DisplayPowerOf2Approx()-into-a-class\" data-toc-modified-id=\"EXERCICE-18:-transform-DisplayPowerOf2Approx()-into-a-class-3\">EXERCICE 18: transform <code>DisplayPowerOf2Approx()</code> into a class</a></span></li><li><span><a href=\"#EXERCICE-19:-transform-DisplayMultiply()-into-a-class\" data-toc-modified-id=\"EXERCICE-19:-transform-DisplayMultiply()-into-a-class-4\">EXERCICE 19: transform <code>DisplayMultiply()</code> into a class</a></span></li><li><span><a href=\"#EXERCICE-20:-introduce-common-PrintLine()-function-for-outputs\" data-toc-modified-id=\"EXERCICE-20:-introduce-common-PrintLine()-function-for-outputs-5\">EXERCICE 20: introduce common <code>PrintLine()</code> function for outputs</a></span></li></ul></div>" ] }, { @@ -35,11 +35,11 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### EXERCICE 17: transform `multiply()` into a method `Multiply()` of `PowerOfTwoApprox`\n", + "### EXERCICE 17: transform `Multiply()` into a method `Multiply()` of `PowerOfTwoApprox`\n", "\n", "The method will take as argument only the integer coefficient.\n", "\n", - "`display_multiply()` will of course need also some light rewriting to accomodate that change.\n", + "`DisplayMultiply()` will of course need also some light rewriting to accomodate that change.\n", "\n", "Expected output is the same as previously.\n" ] @@ -48,7 +48,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### EXERCICE 18: transform `display_power_of_2_approx()` into a class\n", + "### EXERCICE 18: transform `DisplayPowerOf2Approx()` into a class\n", "\n", "Create a class `TestDisplayPowerOfTwoApprox` which will be in charge of printing the display for the 0.65 and 0.35 values.\n", "\n", @@ -73,13 +73,13 @@ " \n", " TestDisplayPowerOfTwoApprox test_display_approx;\n", " \n", - " for (int Nbits = 2; Nbits <= 8; Nbits += 2)\n", - " test_display_approx.Do(Nbits); \n", + " for (int nbits = 2; nbits <= 8; nbits += 2)\n", + " test_display_approx.Do(nbits); \n", "\n", " std::cout << std::endl;\n", "\n", - " for (int Nbits = 1; Nbits <= 8; ++Nbits)\n", - " display_multiply(Nbits, 0.65, 3515, 0.35, 4832);\n", + " for (int nbits = 1; nbits <= 8; ++nbits)\n", + " DisplayMultiply(nbits, 0.65, 3515, 0.35, 4832);\n", "\n", " return EXIT_SUCCESS;\n", "}" @@ -116,7 +116,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### EXERCICE 19: transform `display_multiply()` into a class\n", + "### EXERCICE 19: transform `DisplayMultiply()` into a class\n", "\n", "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:\n", "\n", @@ -140,15 +140,15 @@ " \n", " TestDisplayPowerOfTwoApprox test_display_approx;\n", " \n", - " for (int Nbits = 2; Nbits <= 8; Nbits += 2)\n", - " test_display_approx.Do(Nbits); \n", + " for (int nbits = 2; nbits <= 8; nbits += 2)\n", + " test_display_approx.Do(nbits); \n", "\n", " std::cout << std::endl;\n", "\n", - " TestDisplayMultiply test_display_multiply;\n", + " TestDisplayMultiply test_DisplayMultiply;\n", "\n", - " for (int Nbits = 1; Nbits <= 8; ++Nbits)\n", - " test_display_multiply.Do(Nbits);\n", + " for (int nbits = 1; nbits <= 8; ++nbits)\n", + " test_DisplayMultiply.Do(nbits);\n", "\n", " return EXIT_SUCCESS;\n", "}" @@ -158,11 +158,11 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### EXERCICE 20: introduce common `print_line()` function for outputs\n", + "### EXERCICE 20: introduce common `PrintLine()` function for outputs\n", "\n", "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.\n", "\n", - "We will therefore introduce a `print_line()` function which will be in charge of printing the line for a given number of bits.\n", + "We will therefore introduce a `PrintLine()` function which will be in charge of printing the line for a given number of bits.\n", "\n", "At the moment, we will renege the sound principle of separating the functionalities (humor me for the moment...) and this function will:\n", "\n", @@ -183,10 +183,10 @@ "enum class RoundToInteger { no, yes };\n", "````\n", "\n", - "To sum up, the signature or `print_line()` should be:\n", + "To sum up, the signature or `PrintLine()` should be:\n", "\n", "````\n", - "void print_line(int Nbits, double exact, double approx, int maximum_error_index, \n", + "void PrintLine(int nbits, double exact, double approx, int maximum_error_index, \n", " std::string optional_string1, std::string optional_string2,\n", " RoundToInteger round_to_integer);\n", "````\n" diff --git a/2-ObjectProgramming/7b-hands-on.ipynb b/2-ObjectProgramming/7b-hands-on.ipynb index 33227f8d43d92984aa3ad1d1a07cf4062f25b135..874f19212aaa5a3834303d607b3946e1b5108d9b 100644 --- a/2-ObjectProgramming/7b-hands-on.ipynb +++ b/2-ObjectProgramming/7b-hands-on.ipynb @@ -28,7 +28,7 @@ "This class:\n", "\n", "* Should get a constructor which sets the resolution.\n", - "* Includes a protected method named `PrintLine()` that will replace the `print_line()` we introduced in previous exercice.\n", + "* Includes a protected method named `PrintLine()` that will replace the `PrintLine()` we introduced in previous exercice.\n", "\n", "The constructors of derived classes will of course have to be modified accordingly: so far we relied on default ones." ] @@ -61,22 +61,22 @@ "\n", " TestDisplayPowerOfTwoApprox065 test_display_approx065(100); \n", "\n", - " for (int Nbits = 2; Nbits <= 8; Nbits += 2)\n", - " test_display_approx065.Do(Nbits); \n", + " for (int nbits = 2; nbits <= 8; nbits += 2)\n", + " test_display_approx065.Do(nbits); \n", "\n", " std::cout << std::endl;\n", " \n", " TestDisplayPowerOfTwoApprox035 test_display_approx035(100); \n", "\n", - " for (int Nbits = 2; Nbits <= 8; Nbits += 2)\n", - " test_display_approx035.Do(Nbits); \n", + " for (int nbits = 2; nbits <= 8; nbits += 2)\n", + " test_display_approx035.Do(nbits); \n", " \n", " std::cout << std::endl;\n", "\n", - " TestDisplayMultiply test_display_multiply(1000);\n", + " TestDisplayMultiply test_DisplayMultiply(1000);\n", "\n", - " for (int Nbits = 1; Nbits <= 8; ++Nbits)\n", - " test_display_multiply.Do(Nbits);\n", + " for (int nbits = 1; nbits <= 8; ++nbits)\n", + " test_DisplayMultiply.Do(nbits);\n", "\n", " return EXIT_SUCCESS;\n", "}" @@ -133,7 +133,7 @@ "\n", " //! Add a new test_display_register.\n", " //! At each call, the item to be registered is put at the first available position and internal current_position_\n", - " //! is incremented. If the end-user attempts to register more than three items, the error() function is called.\n", + " //! is incremented. If the end-user attempts to register more than three items, the Error() function is called.\n", " void Register(TestDisplay* test_display);\n", " \n", " //! For each container stored, loop oover all those bits and print the result on screen.\n", @@ -164,7 +164,7 @@ "outputs": [], "source": [ "//! Function for error handling. We will see later how to fulfill the same functionality more properly.\n", - "[[noreturn]] void error(std::string explanation)\n", + "[[noreturn]] void Error(std::string explanation)\n", "{\n", " std::cout << \"ERROR: \" << explanation << std::endl;\n", " exit(EXIT_FAILURE);\n", @@ -258,7 +258,7 @@ "\n", "Replace the `Do()` method by a free function with signature:\n", "````\n", - "void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container)\n", + "void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container)\n", "````\n", "\n", "To do so, you will need to add several methods to `TestDisplayContainer`:\n", @@ -290,7 +290,7 @@ " container.Register(new TestDisplayPowerOfTwoApprox035(1000000));\n", " container.Register(new TestDisplayMultiply(10000));\n", " \n", - " loop(4, 16, 4, container);\n", + " Loop(4, 16, 4, container);\n", " \n", " return EXIT_SUCCESS;\n", "}" diff --git a/4-Templates/1b-hands-on.ipynb b/4-Templates/1b-hands-on.ipynb index 5e5cf23f91b536c86af752c2da65ba4c46bdde16..8193add8902e47fb4d0d24853463845241314f2b 100644 --- a/4-Templates/1b-hands-on.ipynb +++ b/4-Templates/1b-hands-on.ipynb @@ -107,7 +107,7 @@ "\n", "````\n", "../exercice31.cpp:84:49: warning: implicit conversion loses integer precision: 'long' to 'int' [-Wshorten-64-to-32]\n", - " return times_power_of_2(approx.GetNumerator() * coefficient, -approx.GetExponent());\n", + " return TimesPowerOf2(approx.GetNumerator() * coefficient, -approx.GetExponent());\n", " ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~\n", "../exercice31.cpp:362:64: note: in instantiation of function template specialization 'operator*<long>' requested here\n", " int approx = approximation1 * coefficient1 + coefficient2 * approximation2;\n", @@ -120,9 +120,9 @@ "\n", "At this point the code should be able to compile... but there are still loose ends, as your compiler will tell you (more or less clearly):\n", "\n", - "* `times_power_of_2()` should also be transformed into a template function. Therefore `max_int()` should as well.\n", - "* `round_as_int()` should also be modified\n", - "* And also `helper_compute_power_of_2_approx()` if you are using the proposed solution.\n", + "* `TimesPowerOf2()` should also be transformed into a template function. Therefore `MaxInt()` should as well.\n", + "* `RoundAsInt()` should also be modified\n", + "* And also `HelperComputePowerOf2Approx()` if you are using the proposed solution.\n", "\n", "* And finally `TestDisplay::PrintLine()` as well. At the moment, keep hardcoding the type for `TestDisplayPowerOfTwoApprox` which is not (yet) a template class.\n", "\n", @@ -131,10 +131,10 @@ "But there is still a line which might be wrong (unless you were very cautious) without compiler warning: make sure the template argument is used in `PowerOfTwoApprox<IntT>::operator double`. This one was tricky as the code was \n", "\n", "````\n", - "int denominator = times_power_of_2(1, exponent_);\n", + "int denominator = TimesPowerOf2(1, exponent_);\n", "````\n", "\n", - "which doesn't have to trigger a warning: `1` is an `int`... So the `int` specialization of `times_power_of_2`, and that is used to compute the floating point value returned! And you may end up with an unwanted `inf` instead of the expected numeric value. This is yet another reminder that magic numbers are dangerous, even if they look as innocuous as a 1...\n", + "which doesn't have to trigger a warning: `1` is an `int`... So the `int` specialization of `TimesPowerOf2`, and that is used to compute the floating point value returned! And you may end up with an unwanted `inf` instead of the expected numeric value. This is yet another reminder that magic numbers are dangerous, even if they look as innocuous as a 1...\n", "\n", "_Expected results (with `long`):_\n", "\n", diff --git a/5-UsefulConceptsAndSTL/1b-hands-on.ipynb b/5-UsefulConceptsAndSTL/1b-hands-on.ipynb index 4145ada5c51a4a5ec54da98ee9151c71c1f98123..248e6b16532716aae283ccfc59518530e9175ed2 100644 --- a/5-UsefulConceptsAndSTL/1b-hands-on.ipynb +++ b/5-UsefulConceptsAndSTL/1b-hands-on.ipynb @@ -52,7 +52,7 @@ "\n", "We would like to print _Overflow!_ in the lines when the chosen number of bits is not compatible with the chosen integer type. Contrary to what we did in previous exercice, we want to recover after such an exception, not to terminate immediately the program.\n", "\n", - "This [StackOverflow post](https://stackoverflow.com/questions/199333/how-to-detect-unsigned-integer-multiply-overflow) provides an interesting non-standard feature introduced in clang and gcc to deal with this: special functions that tell if an operation will overflow (other replies give hints to do so manually). The functions to use are:\n", + "This [StackOverflow post](https://stackoverflow.com/questions/199333/how-to-detect-unsigned-integer-Multiply-overflow) provides an interesting non-standard feature introduced in clang and gcc to deal with this: special functions that tell if an operation will overflow (other replies give hints to do so manually). The functions to use are:\n", "\n", "````\n", "__builtin_add_overflow(T a, T b, T* sum)\n", @@ -65,7 +65,7 @@ "\n", "* Replace `long` by `int` to make the issues reappear.\n", "* Use the two functions above to identify when an overflow occur. There are three such checks to add (even if only two plays out with the `int` case).\n", - "* Define a `void TestDisplay::PrintOverflow(int Nbits, const Error& e) const` method that will provide the print when an overflow occurs. This method will be used in the `catch` blocks for each line.\n", + "* Define a `void TestDisplay::PrintOverflow(int nbits, const Error& e) const` method that will provide the print when an overflow occurs. This method will be used in the `catch` blocks for each line.\n", "\n", "_Expected results:_\n", "\n", @@ -77,7 +77,7 @@ " [Width 20 bits]: 0.65 ~ 0.65 (681574/2^20) [error = 59/100000000]\n", " [Width 24 bits]: 0.65 ~ 0.65 (10905190/2^24) [error = 4/100000000]\n", " [Width 28 bits]: 0.65 ~ 0.65 (174483046/2^28) [error = 0/100000000]\n", - " [Width 32 bits]: Overflow! (in times_power_of_2())\n", + " [Width 32 bits]: Overflow! (in TimesPowerOf2())\n", "\n", " [Width 4 bits]: 0.35 ~ 0.34375 (11/2^5) [error = 1785714/100000000]\n", " [Width 8 bits]: 0.35 ~ 0.349609 (179/2^9) [error = 111607/100000000]\n", @@ -86,7 +86,7 @@ " [Width 20 bits]: 0.35 ~ 0.35 (734003/2^21) [error = 27/100000000]\n", " [Width 24 bits]: 0.35 ~ 0.35 (11744051/2^25) [error = 2/100000000]\n", " [Width 28 bits]: 0.35 ~ 0.35 (187904819/2^29) [error = 0/100000000]\n", - " [Width 32 bits]: Overflow! (in times_power_of_2())\n", + " [Width 32 bits]: Overflow! (in TimesPowerOf2())\n", "\n", " [Width 4 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3857 [error = 29917/1000000]\n", " [Width 8 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3968 [error = 2000/1000000]\n", @@ -95,7 +95,7 @@ " [Width 20 bits]: Overflow! (in operator*(IntT, const PowerOfTwoApprox<IntT>&))\n", " [Width 24 bits]: Overflow! (in operator*(IntT, const PowerOfTwoApprox<IntT>&))\n", " [Width 28 bits]: Overflow! (in operator*(IntT, const PowerOfTwoApprox<IntT>&))\n", - " [Width 32 bits]: Overflow! (in times_power_of_2())\n", + " [Width 32 bits]: Overflow! (in TimesPowerOf2())\n", "````" ] }, diff --git a/5-UsefulConceptsAndSTL/6b-hands-on.ipynb b/5-UsefulConceptsAndSTL/6b-hands-on.ipynb index cbdd7e1733ac3be71bad6594f7c578f79ffa0fe2..5542a1d9a6629b8489f9980b8b1eefd5ab7fa77d 100644 --- a/5-UsefulConceptsAndSTL/6b-hands-on.ipynb +++ b/5-UsefulConceptsAndSTL/6b-hands-on.ipynb @@ -47,7 +47,7 @@ "\n", "If you look closely at your current `TestDisplayContainer`, you should realize it is nothing more than a thin wrapper over its internal `std::vector`. Remove the class entirely and replace it by a `std::vector<str::unique_ptr<TestDisplay>>` (you may if you wish create an alias to this type), that might even be named... `TestDisplayContainer`).\n", "\n", - "Consider optimizing `loop()` function with new `for` syntax." + "Consider optimizing `Loop()` function with new `for` syntax." ] }, { diff --git a/6-InRealEnvironment/2b-hands-on.ipynb b/6-InRealEnvironment/2b-hands-on.ipynb index 05cf3e6754d8f8264056796ffc8811f1d1146e9d..ec53297b9e73d48939258236bbd59619e3e1bd48 100644 --- a/6-InRealEnvironment/2b-hands-on.ipynb +++ b/6-InRealEnvironment/2b-hands-on.ipynb @@ -36,9 +36,9 @@ "\n", "Reorganize the project in several files:\n", "\n", - "- Only `loop()` and `main()` functions should be put in _main.cpp_.\n", + "- Only `Loop()` and `main()` functions should be put in _main.cpp_.\n", "- `Error` should be declared in `Error.hpp` and defined in `Error.cpp`.\n", - "- Free functions such as `times_power_of_2` and `round_as_int` should be put in `Tools` files. As all these definitions are template, they should be in a header file (that might be the same as the one used for declaration, or a different one if you want to split declaration and definition - in this case don't forget to include it at the end of the hpp file!)\n", + "- Free functions such as `TimesPowerOf2` and `RoundAsInt` should be put in `Tools` files. As all these definitions are template, they should be in a header file (that might be the same as the one used for declaration, or a different one if you want to split declaration and definition - in this case don't forget to include it at the end of the hpp file!)\n", "- Likewise for `PowerOfTwoApprox`. Closely related free functions (such as `operator<<`) should be put in the same file(s).\n", "- You may put all `TestDIsplay` functionalities together. Beware: some definitions are template and must be in header files, whereas some aren't and should therefore be compiler.\n", "\n", diff --git a/HandsOn/1-ProceduralProgramming/Solution/exercice1.cpp b/HandsOn/1-ProceduralProgramming/Solution/exercice1.cpp index 3dbb0ce18f6954bb9b1e08fd015236c03db0a1a0..908114737bafad389210f353eb9b3acb0298afd9 100644 --- a/HandsOn/1-ProceduralProgramming/Solution/exercice1.cpp +++ b/HandsOn/1-ProceduralProgramming/Solution/exercice1.cpp @@ -3,7 +3,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -21,7 +21,7 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } @@ -39,7 +39,7 @@ int main(int argc, char** argv) for (int exponent = 1; exponent < 9; ++exponent) { - int numerator = round_as_int(0.65 * times_power_of_2(1, exponent)); + int numerator = RoundAsInt(0.65 * TimesPowerOf2(1, exponent)); std::cout << "0.65 ~ " << numerator << " / 2^" << exponent << std::endl; } diff --git a/HandsOn/1-ProceduralProgramming/Solution/exercice10.cpp b/HandsOn/1-ProceduralProgramming/Solution/exercice10.cpp index cd4e8d155cab11717eee33c3042447e84e72bf9d..108b8e47b065b01d470ada4d0fe418b601a137f5 100644 --- a/HandsOn/1-ProceduralProgramming/Solution/exercice10.cpp +++ b/HandsOn/1-ProceduralProgramming/Solution/exercice10.cpp @@ -3,7 +3,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -21,25 +21,25 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } //! Helper function that computes numerator and denominator. //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } @@ -48,9 +48,9 @@ void helper_compute_power_of_2_approx(double value, int exponent, int& numerator //! \param[out] numerator Computed numerator. //! \param[out] exponent Computed exponent. //! \return The approximation as a floating point. -double compute_power_of_2_approx(int Nbits, double value, int& numerator, int& exponent) +double ComputePowerOf2Approx(int Nbits, double value, int& numerator, int& exponent) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); numerator = 0; exponent = 0; int denominator {}; @@ -59,53 +59,53 @@ double compute_power_of_2_approx(int Nbits, double value, int& numerator, int& e { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); return static_cast<double>(numerator) / denominator; } //! Display the approximation of the given argument that fits in the given number of bits. -void display_power_of_2_approx(int Nbits, double value) +void DisplayPowerOf2Approx(int Nbits, double value) { int numerator {}, exponent {}; - const double approx = compute_power_of_2_approx(Nbits, value, numerator, exponent); + const double approx = ComputePowerOf2Approx(Nbits, value, numerator, exponent); const double error = std::fabs(value - approx) / value; std::cout << "[With " << Nbits << " bits]: " << value << " ~ " << approx << " (" << numerator << - " / 2^" << exponent << ") [error = " << round_as_int(100. * error) << "/100]" << std::endl; + " / 2^" << exponent << ") [error = " << RoundAsInt(100. * error) << "/100]" << std::endl; } //! Multiply an approximated value by an integer. -int multiply(int Nbits, double value, int coefficient) +int Multiply(int Nbits, double value, int coefficient) { int numerator {}, exponent {}; - compute_power_of_2_approx(Nbits, value, numerator, exponent); + ComputePowerOf2Approx(Nbits, value, numerator, exponent); - return times_power_of_2(numerator * coefficient, -exponent); + return TimesPowerOf2(numerator * coefficient, -exponent); } //! Compute value1 * coefficient1 + value2 * coefficient2 over Nbits bits. -void display_multiply(int Nbits, double value1, int coefficient1, double value2, int coefficient2) +void DisplayMultiply(int Nbits, double value1, int coefficient1, double value2, int coefficient2) { double exact = value1 * coefficient1 + value2 * coefficient2; - int rounded = round_as_int(exact); - int approx = multiply(Nbits, value1, coefficient1) + multiply(Nbits, value2, coefficient2); + int rounded = RoundAsInt(exact); + int approx = Multiply(Nbits, value1, coefficient1) + Multiply(Nbits, value2, coefficient2); const double error = std::fabs(exact - approx) / exact; std::cout << "[With " << Nbits << " bits]: " << value1 << " * " << coefficient1 << " + " << value2 << " * " << coefficient2 << " = " << rounded << " ~ " << approx - << " [error = " << round_as_int(1000. * error) << "/1000]" << std::endl; + << " [error = " << RoundAsInt(1000. * error) << "/1000]" << std::endl; } @@ -118,18 +118,18 @@ 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) - display_power_of_2_approx(Nbits, 0.65); + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.65); std::cout << std::endl; - for (int Nbits = 2; Nbits <= 8; Nbits += 2) - display_power_of_2_approx(Nbits, 0.35); + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.35); std::cout << std::endl; - for (int Nbits = 1; Nbits <= 8; ++Nbits) - display_multiply(Nbits, 0.65, 3515, 0.35, 4832); // to compute 0.65 * 3515 + 0.35 * 4832 + for (int nbits = 1; nbits <= 8; ++nbits) + DisplayMultiply(nbits, 0.65, 3515, 0.35, 4832); // to compute 0.65 * 3515 + 0.35 * 4832 return EXIT_SUCCESS; diff --git a/HandsOn/1-ProceduralProgramming/Solution/exercice11.cpp b/HandsOn/1-ProceduralProgramming/Solution/exercice11.cpp index f548416a595f117e7340d6c909bd43924b717521..b82209e707a18b6d8855ce7bcce309e45c57b166 100644 --- a/HandsOn/1-ProceduralProgramming/Solution/exercice11.cpp +++ b/HandsOn/1-ProceduralProgramming/Solution/exercice11.cpp @@ -3,7 +3,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -21,25 +21,25 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } //! Helper function that computes numerator and denominator. //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } @@ -48,9 +48,9 @@ void helper_compute_power_of_2_approx(double value, int exponent, int& numerator //! \param[out] numerator Computed numerator. //! \param[out] exponent Computed exponent. //! \return The approximation as a floating point. -double compute_power_of_2_approx(int Nbits, double value, int& numerator, int& exponent) +double ComputePowerOf2Approx(int Nbits, double value, int& numerator, int& exponent) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); numerator = 0; exponent = 0; int denominator {}; @@ -59,72 +59,72 @@ double compute_power_of_2_approx(int Nbits, double value, int& numerator, int& e { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); return static_cast<double>(numerator) / denominator; } //! Display the approximation of the given argument that fits in the given number of bits. -void display_power_of_2_approx(int Nbits, double value) +void DisplayPowerOf2Approx(int Nbits, double value) { int numerator {}, exponent {}; - const double approx = compute_power_of_2_approx(Nbits, value, numerator, exponent); + const double approx = ComputePowerOf2Approx(Nbits, value, numerator, exponent); const double error = std::fabs(value - approx) / value; std::cout << "[With " << Nbits << " bits]: " << value << " ~ " << approx << " (" << numerator << - " / 2^" << exponent << ") [error = " << round_as_int(100. * error) << "/100]" << std::endl; + " / 2^" << exponent << ") [error = " << RoundAsInt(100. * error) << "/100]" << std::endl; } //! Multiply an approximated value by an integer. -int multiply(int Nbits, double value, int coefficient) +int Multiply(int Nbits, double value, int coefficient) { int numerator {}, exponent {}; - compute_power_of_2_approx(Nbits, value, numerator, exponent); + ComputePowerOf2Approx(Nbits, value, numerator, exponent); - return times_power_of_2(numerator * coefficient, -exponent); + return TimesPowerOf2(numerator * coefficient, -exponent); } //! Compute value1 * coefficient1 + value2 * coefficient2 over Nbits bits. -void display_multiply(int Nbits, double value1, int coefficient1, double value2, int coefficient2) +void DisplayMultiply(int Nbits, double value1, int coefficient1, double value2, int coefficient2) { double exact = value1 * coefficient1 + value2 * coefficient2; - int rounded = round_as_int(exact); - int approx = multiply(Nbits, value1, coefficient1) + multiply(Nbits, value2, coefficient2); + int rounded = RoundAsInt(exact); + int approx = Multiply(Nbits, value1, coefficient1) + Multiply(Nbits, value2, coefficient2); const double error = std::fabs(exact - approx) / exact; std::cout << "[With " << Nbits << " bits]: " << value1 << " * " << coefficient1 << " + " << value2 << " * " << coefficient2 << " = " << rounded << " ~ " << approx - << " [error = " << round_as_int(1000. * error) << "/1000]" << std::endl; + << " [error = " << RoundAsInt(1000. * error) << "/1000]" << std::endl; } -void display_065(int Nbits) +void Display065(int Nbits) { - display_power_of_2_approx(Nbits, 0.65); + DisplayPowerOf2Approx(Nbits, 0.65); } -void display_035(int Nbits) +void Display035(int Nbits) { - display_power_of_2_approx(Nbits, 0.35); + DisplayPowerOf2Approx(Nbits, 0.35); } -void display_065_3515_035_4832(int Nbits) +void Display06535150354832(int Nbits) { - display_multiply(Nbits, 0.65, 3515, 0.35, 4832); + DisplayMultiply(Nbits, 0.65, 3515, 0.35, 4832); } -void loop(int initial, int final, int increment, void(*display_function)(int)) +void Loop(int initial, int final, int increment, void(*display_function)(int)) { for (int bits = initial; bits <= final; bits += increment) display_function(bits); @@ -142,9 +142,9 @@ 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 - loop(2, 8, 2, display_065); - loop(2, 8, 2, display_035); - loop(1, 8, 1, display_065_3515_035_4832); + Loop(2, 8, 2, Display065); + Loop(2, 8, 2, Display035); + Loop(1, 8, 1, Display06535150354832); return EXIT_SUCCESS; } diff --git a/HandsOn/1-ProceduralProgramming/Solution/exercice12.cpp b/HandsOn/1-ProceduralProgramming/Solution/exercice12.cpp index 1e029c61d8d4a3f6b4d735e3d18219fee3bd60e0..b80134c2f39ea9d69f78addc8d612a09f3b4759b 100644 --- a/HandsOn/1-ProceduralProgramming/Solution/exercice12.cpp +++ b/HandsOn/1-ProceduralProgramming/Solution/exercice12.cpp @@ -4,7 +4,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -22,25 +22,25 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } //! Helper function that computes numerator and denominator. //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } @@ -49,9 +49,9 @@ void helper_compute_power_of_2_approx(double value, int exponent, int& numerator //! \param[out] numerator Computed numerator. //! \param[out] exponent Computed exponent. //! \return The approximation as a floating point. -double compute_power_of_2_approx(int Nbits, double value, int& numerator, int& exponent) +double ComputePowerOf2Approx(int Nbits, double value, int& numerator, int& exponent) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); numerator = 0; exponent = 0; int denominator {}; @@ -60,72 +60,72 @@ double compute_power_of_2_approx(int Nbits, double value, int& numerator, int& e { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); return static_cast<double>(numerator) / denominator; } //! Display the approximation of the given argument that fits in the given number of bits. -void display_power_of_2_approx(std::ostream& out, int Nbits, double value) +void DisplayPowerOf2Approx(std::ostream& out, int Nbits, double value) { int numerator {}, exponent {}; - const double approx = compute_power_of_2_approx(Nbits, value, numerator, exponent); + const double approx = ComputePowerOf2Approx(Nbits, value, numerator, exponent); const double error = std::fabs(value - approx) / value; out << "[With " << Nbits << " bits]: " << value << " ~ " << approx << " (" << numerator << - " / 2^" << exponent << ") [error = " << round_as_int(100. * error) << "/100]" << std::endl; + " / 2^" << exponent << ") [error = " << RoundAsInt(100. * error) << "/100]" << std::endl; } //! Multiply an approximated value by an integer. -int multiply(int Nbits, double value, int coefficient) +int Multiply(int Nbits, double value, int coefficient) { int numerator {}, exponent {}; - compute_power_of_2_approx(Nbits, value, numerator, exponent); + ComputePowerOf2Approx(Nbits, value, numerator, exponent); - return times_power_of_2(numerator * coefficient, -exponent); + return TimesPowerOf2(numerator * coefficient, -exponent); } //! Compute value1 * coefficient1 + value2 * coefficient2 over Nbits bits. -void display_multiply(std::ostream& out, int Nbits, double value1, int coefficient1, double value2, int coefficient2) +void DisplayMultiply(std::ostream& out, int Nbits, double value1, int coefficient1, double value2, int coefficient2) { double exact = value1 * coefficient1 + value2 * coefficient2; - int rounded = round_as_int(exact); - int approx = multiply(Nbits, value1, coefficient1) + multiply(Nbits, value2, coefficient2); + int rounded = RoundAsInt(exact); + int approx = Multiply(Nbits, value1, coefficient1) + Multiply(Nbits, value2, coefficient2); const double error = std::fabs(exact - approx) / exact; out << "[With " << Nbits << " bits]: " << value1 << " * " << coefficient1 << " + " << value2 << " * " << coefficient2 << " = " << rounded << " ~ " << approx - << " [error = " << round_as_int(1000. * error) << "/1000]" << std::endl; + << " [error = " << RoundAsInt(1000. * error) << "/1000]" << std::endl; } -void display_065(std::ostream& out, int Nbits) +void Display065(std::ostream& out, int Nbits) { - display_power_of_2_approx(out, Nbits, 0.65); + DisplayPowerOf2Approx(out, Nbits, 0.65); } -void display_035(std::ostream& out, int Nbits) +void Display035(std::ostream& out, int Nbits) { - display_power_of_2_approx(out, Nbits, 0.35); + DisplayPowerOf2Approx(out, Nbits, 0.35); } -void display_065_3515_035_4832(std::ostream& out, int Nbits) +void Display06535150354832(std::ostream& out, int Nbits) { - display_multiply(out, Nbits, 0.65, 3515, 0.35, 4832); + DisplayMultiply(out, Nbits, 0.65, 3515, 0.35, 4832); } -void loop(std::ostream& out, int initial, int final, int increment, void(*display_function)(std::ostream&, int)) +void Loop(std::ostream& out, int initial, int final, int increment, void(*display_function)(std::ostream&, int)) { for (int bits = initial; bits <= final; bits += increment) display_function(out, bits); @@ -153,9 +153,9 @@ int main(int argc, char** argv) // Open the stream to the file. std::ofstream output_file(argv[1]); - 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); + Loop(output_file, 2, 8, 2, Display065); + Loop(output_file, 2, 8, 2, Display035); + Loop(std::cout, 1, 8, 1, Display06535150354832); return EXIT_SUCCESS; } diff --git a/HandsOn/1-ProceduralProgramming/Solution/exercice2.cpp b/HandsOn/1-ProceduralProgramming/Solution/exercice2.cpp index c1959f44bcbe7c3cbd6e9f7e119dfe01b78a3741..8a0f9232322074e7c646f08acbc130c027cda502 100644 --- a/HandsOn/1-ProceduralProgramming/Solution/exercice2.cpp +++ b/HandsOn/1-ProceduralProgramming/Solution/exercice2.cpp @@ -3,7 +3,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -21,18 +21,18 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } //! Display the approximation of the given argument for the exponents from 1 to 8. -void display_power_of_2_approx(double value) +void DisplayPowerOf2Approx(double value) { for (int exponent = 1; exponent <= 8; ++exponent) { - int numerator = round_as_int(value * times_power_of_2(1, exponent)); + int numerator = RoundAsInt(value * TimesPowerOf2(1, exponent)); std::cout << value << " ~ " << numerator << " / 2^" << exponent << std::endl; } @@ -51,8 +51,8 @@ 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 - display_power_of_2_approx(.65); - display_power_of_2_approx(.35); + DisplayPowerOf2Approx(.65); + DisplayPowerOf2Approx(.35); return EXIT_SUCCESS; } diff --git a/HandsOn/1-ProceduralProgramming/Solution/exercice3.cpp b/HandsOn/1-ProceduralProgramming/Solution/exercice3.cpp index 57b5321bc1e8c7c06f529effcda4ab78e11cdac4..067d451dd1fcea4c850dc5c64f731c682ffd0d67 100644 --- a/HandsOn/1-ProceduralProgramming/Solution/exercice3.cpp +++ b/HandsOn/1-ProceduralProgramming/Solution/exercice3.cpp @@ -3,7 +3,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -21,19 +21,19 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } //! Display the approximation of the given argument for the exponents from 1 to 8. -void display_power_of_2_approx(double value) +void DisplayPowerOf2Approx(double value) { for (int exponent = 1; exponent <= 8; ++exponent) { - int denominator = times_power_of_2(1, exponent); - int numerator = round_as_int(value * denominator); + int denominator = TimesPowerOf2(1, exponent); + int numerator = RoundAsInt(value * denominator); double approx = static_cast<double>(numerator) / denominator; std::cout << value << " ~ " << approx << " (" << numerator << @@ -55,8 +55,8 @@ 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 - display_power_of_2_approx(.65); - display_power_of_2_approx(.35); + DisplayPowerOf2Approx(.65); + DisplayPowerOf2Approx(.35); return EXIT_SUCCESS; } diff --git a/HandsOn/1-ProceduralProgramming/Solution/exercice4.cpp b/HandsOn/1-ProceduralProgramming/Solution/exercice4.cpp index 5dbb9674807f2ce1bb656e49e63d098664271e14..1588a07fedb778506d125f33961b1665bb2b879a 100644 --- a/HandsOn/1-ProceduralProgramming/Solution/exercice4.cpp +++ b/HandsOn/1-ProceduralProgramming/Solution/exercice4.cpp @@ -3,7 +3,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -21,7 +21,7 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } @@ -29,15 +29,15 @@ int round_as_int(double x) //! Helper function that computes numerator and denominator. //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! -void helper_display_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperDisplayPowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } //! Display the approximation of the given argument that does not exceed the chosen maximum numerator. -void display_power_of_2_approx(int max_numerator, double value) +void DisplayPowerOf2Approx(int max_numerator, double value) { int numerator {}, denominator{}, exponent {}; @@ -45,11 +45,11 @@ void display_power_of_2_approx(int max_numerator, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_display_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperDisplayPowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_display_power_of_2_approx(value, --exponent, numerator, denominator); + HelperDisplayPowerOf2Approx(value, --exponent, numerator, denominator); double approx = static_cast<double>(numerator) / denominator; @@ -68,11 +68,11 @@ 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 - display_power_of_2_approx(15, 0.65); - display_power_of_2_approx(255, 0.65); + DisplayPowerOf2Approx(15, 0.65); + DisplayPowerOf2Approx(255, 0.65); - display_power_of_2_approx(15, 0.35); - display_power_of_2_approx(255, 0.35); + DisplayPowerOf2Approx(15, 0.35); + DisplayPowerOf2Approx(255, 0.35); return EXIT_SUCCESS; } diff --git a/HandsOn/1-ProceduralProgramming/Solution/exercice5.cpp b/HandsOn/1-ProceduralProgramming/Solution/exercice5.cpp index 77333673b9b840563a7dae27f66e25593739731e..cc606b87681a3742b95412b005bd66688f0d946c 100644 --- a/HandsOn/1-ProceduralProgramming/Solution/exercice5.cpp +++ b/HandsOn/1-ProceduralProgramming/Solution/exercice5.cpp @@ -3,7 +3,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -21,43 +21,43 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } //! Helper function that computes numerator and denominator. //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! -void helper_display_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperDisplayPowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } //! Display the approximation of the given argument that fits in the given number of bits. -void display_power_of_2_approx(int Nbits, double value) +void DisplayPowerOf2Approx(int Nbits, double value) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); int numerator {}, denominator{}, exponent {}; do { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_display_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperDisplayPowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_display_power_of_2_approx(value, --exponent, numerator, denominator); + HelperDisplayPowerOf2Approx(value, --exponent, numerator, denominator); double approx = static_cast<double>(numerator) / denominator; @@ -76,13 +76,13 @@ 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) - display_power_of_2_approx(Nbits, 0.65) ; + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.65) ; std::cout << std::endl; - for (int Nbits = 2; Nbits <= 8; Nbits += 2) - display_power_of_2_approx(Nbits, 0.35) ; + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.35) ; return EXIT_SUCCESS; } diff --git a/HandsOn/1-ProceduralProgramming/Solution/exercice6.cpp b/HandsOn/1-ProceduralProgramming/Solution/exercice6.cpp index b7797bf063aceae0f8da5a3c53e93d60cfaedca8..f4b0c9c06074e6dd46485c631845790158e5478a 100644 --- a/HandsOn/1-ProceduralProgramming/Solution/exercice6.cpp +++ b/HandsOn/1-ProceduralProgramming/Solution/exercice6.cpp @@ -3,7 +3,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -21,25 +21,25 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } //! Helper function that computes numerator and denominator. //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } @@ -48,9 +48,9 @@ void helper_compute_power_of_2_approx(double value, int exponent, int& numerator //! \param[out] numerator Computed numerator. //! \param[out] exponent Computed exponent. //! \return The approximation as a floating point. -double compute_power_of_2_approx(int Nbits, double value, int& numerator, int& exponent) +double ComputePowerOf2Approx(int Nbits, double value, int& numerator, int& exponent) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); numerator = 0; exponent = 0; int denominator {}; @@ -59,21 +59,21 @@ double compute_power_of_2_approx(int Nbits, double value, int& numerator, int& e { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); return static_cast<double>(numerator) / denominator; } //! Display the approximation of the given argument that fits in the given number of bits. -void display_power_of_2_approx(int Nbits, double value) +void DisplayPowerOf2Approx(int Nbits, double value) { int numerator {}, exponent {}; - const double approx = compute_power_of_2_approx(Nbits, value, numerator, exponent); + const double approx = ComputePowerOf2Approx(Nbits, value, numerator, exponent); std::cout << "[With " << Nbits << " bits]: " << value << " ~ " << approx << " (" << numerator << " / 2^" << exponent << ')' << std::endl; @@ -90,13 +90,13 @@ 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) - display_power_of_2_approx(Nbits, 0.65) ; + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.65) ; std::cout << std::endl; - for (int Nbits = 2; Nbits <= 8; Nbits += 2) - display_power_of_2_approx(Nbits, 0.35) ; + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.35) ; return EXIT_SUCCESS; } diff --git a/HandsOn/1-ProceduralProgramming/Solution/exercice7.cpp b/HandsOn/1-ProceduralProgramming/Solution/exercice7.cpp index 93aca818dcc8782dcffd021a26ed402890694cfa..3679fa086bc698489b6a0a8b36c495832213fc7c 100644 --- a/HandsOn/1-ProceduralProgramming/Solution/exercice7.cpp +++ b/HandsOn/1-ProceduralProgramming/Solution/exercice7.cpp @@ -3,7 +3,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -21,25 +21,25 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } //! Helper function that computes numerator and denominator. //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } @@ -48,9 +48,9 @@ void helper_compute_power_of_2_approx(double value, int exponent, int& numerator //! \param[out] numerator Computed numerator. //! \param[out] exponent Computed exponent. //! \return The approximation as a floating point. -double compute_power_of_2_approx(int Nbits, double value, int& numerator, int& exponent) +double ComputePowerOf2Approx(int Nbits, double value, int& numerator, int& exponent) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); numerator = 0; exponent = 0; int denominator {}; @@ -59,26 +59,26 @@ double compute_power_of_2_approx(int Nbits, double value, int& numerator, int& e { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); return static_cast<double>(numerator) / denominator; } //! Display the approximation of the given argument that fits in the given number of bits. -void display_power_of_2_approx(int Nbits, double value) +void DisplayPowerOf2Approx(int Nbits, double value) { int numerator {}, exponent {}; - const double approx = compute_power_of_2_approx(Nbits, value, numerator, exponent); + const double approx = ComputePowerOf2Approx(Nbits, value, numerator, exponent); const double error = std::fabs(value - approx) / value; std::cout << "[With " << Nbits << " bits]: " << value << " ~ " << approx << " (" << numerator << - " / 2^" << exponent << ") [error = " << round_as_int(100. * error) << "/100]" << std::endl; + " / 2^" << exponent << ") [error = " << RoundAsInt(100. * error) << "/100]" << std::endl; } @@ -92,13 +92,13 @@ 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) - display_power_of_2_approx(Nbits, 0.65) ; + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.65) ; std::cout << std::endl; - for (int Nbits = 2; Nbits <= 8; Nbits += 2) - display_power_of_2_approx(Nbits, 0.35) ; + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.35) ; return EXIT_SUCCESS; } diff --git a/HandsOn/1-ProceduralProgramming/Solution/exercice8.cpp b/HandsOn/1-ProceduralProgramming/Solution/exercice8.cpp index ae29fca93fea23251ae5dc9029557ccb06703332..f3880a67b46571f3124e6c35e8bce24579bc3601 100644 --- a/HandsOn/1-ProceduralProgramming/Solution/exercice8.cpp +++ b/HandsOn/1-ProceduralProgramming/Solution/exercice8.cpp @@ -3,7 +3,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -21,25 +21,25 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } //! Helper function that computes numerator and denominator. //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } @@ -48,9 +48,9 @@ void helper_compute_power_of_2_approx(double value, int exponent, int& numerator //! \param[out] numerator Computed numerator. //! \param[out] exponent Computed exponent. //! \return The approximation as a floating point. -double compute_power_of_2_approx(int Nbits, double value, int& numerator, int& exponent) +double ComputePowerOf2Approx(int Nbits, double value, int& numerator, int& exponent) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); numerator = 0; exponent = 0; int denominator {}; @@ -59,37 +59,37 @@ double compute_power_of_2_approx(int Nbits, double value, int& numerator, int& e { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); return static_cast<double>(numerator) / denominator; } //! Display the approximation of the given argument that fits in the given number of bits. -void display_power_of_2_approx(int Nbits, double value) +void DisplayPowerOf2Approx(int Nbits, double value) { int numerator {}, exponent {}; - const double approx = compute_power_of_2_approx(Nbits, value, numerator, exponent); + const double approx = ComputePowerOf2Approx(Nbits, value, numerator, exponent); const double error = std::fabs(value - approx) / value; std::cout << "[With " << Nbits << " bits]: " << value << " ~ " << approx << " (" << numerator << - " / 2^" << exponent << ") [error = " << round_as_int(100. * error) << "/100]" << std::endl; + " / 2^" << exponent << ") [error = " << RoundAsInt(100. * error) << "/100]" << std::endl; } //! Multiply an approximated value by an integer. -int multiply(int Nbits, double value, int coefficient) +int Multiply(int Nbits, double value, int coefficient) { int numerator {}, exponent {}; - compute_power_of_2_approx(Nbits, value, numerator, exponent); + ComputePowerOf2Approx(Nbits, value, numerator, exponent); - return times_power_of_2(numerator * coefficient, -exponent); + return TimesPowerOf2(numerator * coefficient, -exponent); } @@ -102,22 +102,22 @@ 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) - display_power_of_2_approx(Nbits, 0.65); + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.65); std::cout << std::endl; - for (int Nbits = 2; Nbits <= 8; Nbits += 2) - display_power_of_2_approx(Nbits, 0.35); + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.35); std::cout << std::endl; - for (int Nbits = 1; Nbits <= 8; ++Nbits) + for (int nbits = 1; nbits <= 8; ++nbits) { double exact = 0.65 * 3515; - int rounded = round_as_int(exact); - int approx = multiply(Nbits, 0.65, 3515); - std::cout << "[With " << Nbits << " bits]: 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; } diff --git a/HandsOn/1-ProceduralProgramming/Solution/exercice9.cpp b/HandsOn/1-ProceduralProgramming/Solution/exercice9.cpp index af49ff4f158ea1c7482df1b93ba030d8aa255863..24fa26d00b7230b671480019385037b9bd36c7da 100644 --- a/HandsOn/1-ProceduralProgramming/Solution/exercice9.cpp +++ b/HandsOn/1-ProceduralProgramming/Solution/exercice9.cpp @@ -3,7 +3,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -21,25 +21,25 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } //! Helper function that computes numerator and denominator. //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } @@ -48,9 +48,9 @@ void helper_compute_power_of_2_approx(double value, int exponent, int& numerator //! \param[out] numerator Computed numerator. //! \param[out] exponent Computed exponent. //! \return The approximation as a floating point. -double compute_power_of_2_approx(int Nbits, double value, int& numerator, int& exponent) +double ComputePowerOf2Approx(int Nbits, double value, int& numerator, int& exponent) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); numerator = 0; exponent = 0; int denominator {}; @@ -59,46 +59,46 @@ double compute_power_of_2_approx(int Nbits, double value, int& numerator, int& e { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); return static_cast<double>(numerator) / denominator; } //! Display the approximation of the given argument that fits in the given number of bits. -void display_power_of_2_approx(int Nbits, double value) +void DisplayPowerOf2Approx(int Nbits, double value) { int numerator {}, exponent {}; - const double approx = compute_power_of_2_approx(Nbits, value, numerator, exponent); + const double approx = ComputePowerOf2Approx(Nbits, value, numerator, exponent); const double error = std::fabs(value - approx) / value; std::cout << "[With " << Nbits << " bits]: " << value << " ~ " << approx << " (" << numerator << - " / 2^" << exponent << ") [error = " << round_as_int(100. * error) << "/100]" << std::endl; + " / 2^" << exponent << ") [error = " << RoundAsInt(100. * error) << "/100]" << std::endl; } //! Multiply an approximated value by an integer. -int multiply(int Nbits, double value, int coefficient) +int Multiply(int Nbits, double value, int coefficient) { int numerator {}, exponent {}; - compute_power_of_2_approx(Nbits, value, numerator, exponent); + ComputePowerOf2Approx(Nbits, value, numerator, exponent); - return times_power_of_2(numerator * coefficient, -exponent); + return TimesPowerOf2(numerator * coefficient, -exponent); } //! Compute value1 * coefficient1 + value2 * coefficient2 over Nbits bits. -void display_multiply(int Nbits, double value1, int coefficient1, double value2, int coefficient2) +void DisplayMultiply(int Nbits, double value1, int coefficient1, double value2, int coefficient2) { double exact = value1 * coefficient1 + value2 * coefficient2; - int rounded = round_as_int(exact); - int approx = multiply(Nbits, value1, coefficient1) + multiply(Nbits, value2, coefficient2); + int rounded = RoundAsInt(exact); + int approx = Multiply(Nbits, value1, coefficient1) + Multiply(Nbits, value2, coefficient2); std::cout << "[With " << Nbits << " bits]: " << value1 << " * " << coefficient1 << " + " << value2 << " * " << coefficient2 << " = " << rounded << " ~ " << approx << std::endl; @@ -114,18 +114,18 @@ 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) - display_power_of_2_approx(Nbits, 0.65); + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.65); std::cout << std::endl; - for (int Nbits = 2; Nbits <= 8; Nbits += 2) - display_power_of_2_approx(Nbits, 0.35); + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.35); std::cout << std::endl; - for (int Nbits = 1; Nbits <= 8; ++Nbits) - display_multiply(Nbits, 0.65, 3515, 0.35, 4832); // to compute 0.65 * 3515 + 0.35 * 4832 + for (int nbits = 1; nbits <= 8; ++nbits) + DisplayMultiply(nbits, 0.65, 3515, 0.35, 4832); // to compute 0.65 * 3515 + 0.35 * 4832 return EXIT_SUCCESS; diff --git a/HandsOn/1-ProceduralProgramming/initial_file.cpp b/HandsOn/1-ProceduralProgramming/initial_file.cpp index 2139f26b81787d9d49499b0bef1416c0190489d7..068502348e64781da4c86e0e54dff1c7919f58bd 100644 --- a/HandsOn/1-ProceduralProgramming/initial_file.cpp +++ b/HandsOn/1-ProceduralProgramming/initial_file.cpp @@ -3,7 +3,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -21,7 +21,7 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } @@ -37,13 +37,13 @@ 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 - int numerator1 = round_as_int(0.65 * times_power_of_2(1, 1)); + int numerator1 = RoundAsInt(0.65 * TimesPowerOf2(1, 1)); std::cout << "0.65 ~ " << numerator1 << " / 2^1" << std::endl ; - int numerator2 = round_as_int(0.65 * times_power_of_2(1, 2)); + int numerator2 = RoundAsInt(0.65 * TimesPowerOf2(1, 2)); std::cout << "0.65 ~ " << numerator2 << " / 2^2" << std::endl ; - int numerator3 = round_as_int(0.65 * times_power_of_2(1, 3)); + int numerator3 = RoundAsInt(0.65 * TimesPowerOf2(1, 3)); std::cout << "0.65 ~ " << numerator3 << " / 2^3" << std::endl ; std::cout << std::endl ; diff --git a/HandsOn/2-ObjectProgramming/Solution/exercice13.cpp b/HandsOn/2-ObjectProgramming/Solution/exercice13.cpp index 4db26229e9d0f03e7ba9b4f668c80a2f6763f905..49ebb2330bfec6b0bb5351241871e2ddd78c0890 100644 --- a/HandsOn/2-ObjectProgramming/Solution/exercice13.cpp +++ b/HandsOn/2-ObjectProgramming/Solution/exercice13.cpp @@ -3,7 +3,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -21,16 +21,16 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } @@ -44,10 +44,10 @@ struct PowerOfTwoApprox //! Helper function that computes numerator and denominator. //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } @@ -55,12 +55,12 @@ void helper_compute_power_of_2_approx(double value, int exponent, int& numerator //! Compute the best possible approximation of `value` with `Nbits` //! \param[out] approximation Computed approximation. //! \return The approximation as a floating point. -double compute_power_of_2_approx(int Nbits, double value, PowerOfTwoApprox& approximation) +double ComputePowerOf2Approx(int Nbits, double value, PowerOfTwoApprox& approximation) { int& numerator = approximation.numerator_; // alias! int& exponent = approximation.exponent_; - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); numerator = 0; exponent = 0; int denominator {}; @@ -69,54 +69,54 @@ double compute_power_of_2_approx(int Nbits, double value, PowerOfTwoApprox& appr { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); return static_cast<double>(numerator) / denominator; } //! Display the approximation of the given argument that fits in the given number of bits. -void display_power_of_2_approx(int Nbits, double value) +void DisplayPowerOf2Approx(int Nbits, double value) { PowerOfTwoApprox approximation; - const double approx = compute_power_of_2_approx(Nbits, value, approximation); + const double approx = ComputePowerOf2Approx(Nbits, value, approximation); const double error = std::fabs(value - approx) / value; std::cout << "[With " << Nbits << " bits]: " << value << " ~ " << approx << " (" << approximation.numerator_ << - " / 2^" << approximation.exponent_ << ") [error = " << round_as_int(100. * error) << "/100]" << std::endl; + " / 2^" << approximation.exponent_ << ") [error = " << RoundAsInt(100. * error) << "/100]" << std::endl; } //! Multiply an approximated value by an integer. -int multiply(int Nbits, double value, int coefficient) +int Multiply(int Nbits, double value, int coefficient) { PowerOfTwoApprox approximation; - compute_power_of_2_approx(Nbits, value, approximation); + ComputePowerOf2Approx(Nbits, value, approximation); - return times_power_of_2(approximation.numerator_ * coefficient, -approximation.exponent_); + return TimesPowerOf2(approximation.numerator_ * coefficient, -approximation.exponent_); } //! Compute value1 * coefficient1 + value2 * coefficient2 over Nbits bits. -void display_multiply(int Nbits, double value1, int coefficient1, double value2, int coefficient2) +void DisplayMultiply(int Nbits, double value1, int coefficient1, double value2, int coefficient2) { double exact = value1 * coefficient1 + value2 * coefficient2; - int rounded = round_as_int(exact); - int approx = multiply(Nbits, value1, coefficient1) + multiply(Nbits, value2, coefficient2); + int rounded = RoundAsInt(exact); + int approx = Multiply(Nbits, value1, coefficient1) + Multiply(Nbits, value2, coefficient2); const double error = std::fabs(exact - approx) / exact; std::cout << "[With " << Nbits << " bits]: " << value1 << " * " << coefficient1 << " + " << value2 << " * " << coefficient2 << " = " << rounded << " ~ " << approx - << " [error = " << round_as_int(1000. * error) << "/1000]" << std::endl; + << " [error = " << RoundAsInt(1000. * error) << "/1000]" << std::endl; } @@ -129,18 +129,18 @@ 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) - display_power_of_2_approx(Nbits, 0.65); + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.65); std::cout << std::endl; - for (int Nbits = 2; Nbits <= 8; Nbits += 2) - display_power_of_2_approx(Nbits, 0.35); + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.35); std::cout << std::endl; - for (int Nbits = 1; Nbits <= 8; ++Nbits) - display_multiply(Nbits, 0.65, 3515, 0.35, 4832); // to compute 0.65 * 3515 + 0.35 * 4832 + for (int nbits = 1; nbits <= 8; ++nbits) + DisplayMultiply(nbits, 0.65, 3515, 0.35, 4832); // to compute 0.65 * 3515 + 0.35 * 4832 return EXIT_SUCCESS; diff --git a/HandsOn/2-ObjectProgramming/Solution/exercice14.cpp b/HandsOn/2-ObjectProgramming/Solution/exercice14.cpp index 487999c00398e85a49d9b2bc6653967ef028a41a..b1d75df4d32a9cdf901c9abb7eeaba94564cc7c1 100644 --- a/HandsOn/2-ObjectProgramming/Solution/exercice14.cpp +++ b/HandsOn/2-ObjectProgramming/Solution/exercice14.cpp @@ -3,7 +3,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -21,16 +21,16 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } @@ -52,16 +52,16 @@ struct PowerOfTwoApprox //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } double PowerOfTwoApprox::Compute(int Nbits, double value) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); int& numerator = numerator_; int& exponent = exponent_; @@ -74,18 +74,18 @@ double PowerOfTwoApprox::Compute(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); return static_cast<double>(numerator) / denominator; } //! Display the approximation of the given argument that fits in the given number of bits. -void display_power_of_2_approx(int Nbits, double value) +void DisplayPowerOf2Approx(int Nbits, double value) { PowerOfTwoApprox approximation; @@ -94,34 +94,34 @@ void display_power_of_2_approx(int Nbits, double value) const double error = std::fabs(value - approx) / value; std::cout << "[With " << Nbits << " bits]: " << value << " ~ " << approx << " (" << approximation.numerator_ << - " / 2^" << approximation.exponent_ << ") [error = " << round_as_int(100. * error) << "/100]" << std::endl; + " / 2^" << approximation.exponent_ << ") [error = " << RoundAsInt(100. * error) << "/100]" << std::endl; } //! Multiply an approximated value by an integer. -int multiply(int Nbits, double value, int coefficient) +int Multiply(int Nbits, double value, int coefficient) { PowerOfTwoApprox approximation; approximation.Compute(Nbits, value); - return times_power_of_2(approximation.numerator_ * coefficient, -approximation.exponent_); + return TimesPowerOf2(approximation.numerator_ * coefficient, -approximation.exponent_); } //! Compute value1 * coefficient1 + value2 * coefficient2 over Nbits bits. -void display_multiply(int Nbits, double value1, int coefficient1, double value2, int coefficient2) +void DisplayMultiply(int Nbits, double value1, int coefficient1, double value2, int coefficient2) { double exact = value1 * coefficient1 + value2 * coefficient2; - int rounded = round_as_int(exact); - int approx = multiply(Nbits, value1, coefficient1) + multiply(Nbits, value2, coefficient2); + int rounded = RoundAsInt(exact); + int approx = Multiply(Nbits, value1, coefficient1) + Multiply(Nbits, value2, coefficient2); const double error = std::fabs(exact - approx) / exact; std::cout << "[With " << Nbits << " bits]: " << value1 << " * " << coefficient1 << " + " << value2 << " * " << coefficient2 << " = " << rounded << " ~ " << approx - << " [error = " << round_as_int(1000. * error) << "/1000]" << std::endl; + << " [error = " << RoundAsInt(1000. * error) << "/1000]" << std::endl; } @@ -134,18 +134,18 @@ 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) - display_power_of_2_approx(Nbits, 0.65); + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.65); std::cout << std::endl; - for (int Nbits = 2; Nbits <= 8; Nbits += 2) - display_power_of_2_approx(Nbits, 0.35); + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.35); std::cout << std::endl; - for (int Nbits = 1; Nbits <= 8; ++Nbits) - display_multiply(Nbits, 0.65, 3515, 0.35, 4832); // to compute 0.65 * 3515 + 0.35 * 4832 + for (int nbits = 1; nbits <= 8; ++nbits) + DisplayMultiply(nbits, 0.65, 3515, 0.35, 4832); // to compute 0.65 * 3515 + 0.35 * 4832 return EXIT_SUCCESS; diff --git a/HandsOn/2-ObjectProgramming/Solution/exercice15.cpp b/HandsOn/2-ObjectProgramming/Solution/exercice15.cpp index 5a5e512838682b26c70c0ea97e77e09a18d2cdc3..7eb0cc234f3ac8592d9ff51be19dd2c8b38f93d0 100644 --- a/HandsOn/2-ObjectProgramming/Solution/exercice15.cpp +++ b/HandsOn/2-ObjectProgramming/Solution/exercice15.cpp @@ -3,7 +3,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -21,16 +21,16 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } @@ -54,16 +54,16 @@ struct PowerOfTwoApprox //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); int& numerator = numerator_; int& exponent = exponent_; @@ -76,23 +76,23 @@ PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } double PowerOfTwoApprox::AsDouble() const { - int denominator = times_power_of_2(1, exponent_); + int denominator = TimesPowerOf2(1, exponent_); return static_cast<double>(numerator_) / denominator; } //! Display the approximation of the given argument that fits in the given number of bits. -void display_power_of_2_approx(int Nbits, double value) +void DisplayPowerOf2Approx(int Nbits, double value) { PowerOfTwoApprox approximation(Nbits, value); @@ -101,33 +101,33 @@ void display_power_of_2_approx(int Nbits, double value) const double error = std::fabs(value - approx) / value; std::cout << "[With " << Nbits << " bits]: " << value << " ~ " << approx << " (" << approximation.numerator_ << - " / 2^" << approximation.exponent_ << ") [error = " << round_as_int(100. * error) << "/100]" << std::endl; + " / 2^" << approximation.exponent_ << ") [error = " << RoundAsInt(100. * error) << "/100]" << std::endl; } //! Multiply an approximated value by an integer. -int multiply(int Nbits, double value, int coefficient) +int Multiply(int Nbits, double value, int coefficient) { PowerOfTwoApprox approximation(Nbits, value); - return times_power_of_2(approximation.numerator_ * coefficient, -approximation.exponent_); + return TimesPowerOf2(approximation.numerator_ * coefficient, -approximation.exponent_); } //! Compute value1 * coefficient1 + value2 * coefficient2 over Nbits bits. -void display_multiply(int Nbits, double value1, int coefficient1, double value2, int coefficient2) +void DisplayMultiply(int Nbits, double value1, int coefficient1, double value2, int coefficient2) { double exact = value1 * coefficient1 + value2 * coefficient2; - int rounded = round_as_int(exact); - int approx = multiply(Nbits, value1, coefficient1) + multiply(Nbits, value2, coefficient2); + int rounded = RoundAsInt(exact); + int approx = Multiply(Nbits, value1, coefficient1) + Multiply(Nbits, value2, coefficient2); const double error = std::fabs(exact - approx) / exact; std::cout << "[With " << Nbits << " bits]: " << value1 << " * " << coefficient1 << " + " << value2 << " * " << coefficient2 << " = " << rounded << " ~ " << approx - << " [error = " << round_as_int(1000. * error) << "/1000]" << std::endl; + << " [error = " << RoundAsInt(1000. * error) << "/1000]" << std::endl; } @@ -140,18 +140,18 @@ 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) - display_power_of_2_approx(Nbits, 0.65); + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.65); std::cout << std::endl; - for (int Nbits = 2; Nbits <= 8; Nbits += 2) - display_power_of_2_approx(Nbits, 0.35); + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.35); std::cout << std::endl; - for (int Nbits = 1; Nbits <= 8; ++Nbits) - display_multiply(Nbits, 0.65, 3515, 0.35, 4832); // to compute 0.65 * 3515 + 0.35 * 4832 + for (int nbits = 1; nbits <= 8; ++nbits) + DisplayMultiply(nbits, 0.65, 3515, 0.35, 4832); // to compute 0.65 * 3515 + 0.35 * 4832 return EXIT_SUCCESS; diff --git a/HandsOn/2-ObjectProgramming/Solution/exercice16.cpp b/HandsOn/2-ObjectProgramming/Solution/exercice16.cpp index d4c7d2320329368e7fdb5e4348dc2514e17796d4..50db4e06481fea532d1a1389ccdaebb600464988 100644 --- a/HandsOn/2-ObjectProgramming/Solution/exercice16.cpp +++ b/HandsOn/2-ObjectProgramming/Solution/exercice16.cpp @@ -3,7 +3,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -21,16 +21,16 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } @@ -64,16 +64,16 @@ private: //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); int& numerator = numerator_; int& exponent = exponent_; @@ -86,17 +86,17 @@ PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } double PowerOfTwoApprox::AsDouble() const { - int denominator = times_power_of_2(1, exponent_); + int denominator = TimesPowerOf2(1, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -114,7 +114,7 @@ int PowerOfTwoApprox::GetExponent() const //! Display the approximation of the given argument that fits in the given number of bits. -void display_power_of_2_approx(int Nbits, double value) +void DisplayPowerOf2Approx(int Nbits, double value) { PowerOfTwoApprox approximation(Nbits, value); @@ -123,33 +123,33 @@ void display_power_of_2_approx(int Nbits, double value) const double error = std::fabs(value - approx) / value; std::cout << "[With " << Nbits << " bits]: " << value << " ~ " << approx << " (" << approximation.GetNumerator() << - " / 2^" << approximation.GetExponent() << ") [error = " << round_as_int(100. * error) << "/100]" << std::endl; + " / 2^" << approximation.GetExponent() << ") [error = " << RoundAsInt(100. * error) << "/100]" << std::endl; } //! Multiply an approximated value by an integer. -int multiply(int Nbits, double value, int coefficient) +int Multiply(int Nbits, double value, int coefficient) { PowerOfTwoApprox approximation(Nbits, value); - return times_power_of_2(approximation.GetNumerator() * coefficient, -approximation.GetExponent()); + return TimesPowerOf2(approximation.GetNumerator() * coefficient, -approximation.GetExponent()); } //! Compute value1 * coefficient1 + value2 * coefficient2 over Nbits bits. -void display_multiply(int Nbits, double value1, int coefficient1, double value2, int coefficient2) +void DisplayMultiply(int Nbits, double value1, int coefficient1, double value2, int coefficient2) { double exact = value1 * coefficient1 + value2 * coefficient2; - int rounded = round_as_int(exact); - int approx = multiply(Nbits, value1, coefficient1) + multiply(Nbits, value2, coefficient2); + int rounded = RoundAsInt(exact); + int approx = Multiply(Nbits, value1, coefficient1) + Multiply(Nbits, value2, coefficient2); const double error = std::fabs(exact - approx) / exact; std::cout << "[With " << Nbits << " bits]: " << value1 << " * " << coefficient1 << " + " << value2 << " * " << coefficient2 << " = " << rounded << " ~ " << approx - << " [error = " << round_as_int(1000. * error) << "/1000]" << std::endl; + << " [error = " << RoundAsInt(1000. * error) << "/1000]" << std::endl; } @@ -162,18 +162,18 @@ 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) - display_power_of_2_approx(Nbits, 0.65); + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.65); std::cout << std::endl; - for (int Nbits = 2; Nbits <= 8; Nbits += 2) - display_power_of_2_approx(Nbits, 0.35); + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.35); std::cout << std::endl; - for (int Nbits = 1; Nbits <= 8; ++Nbits) - display_multiply(Nbits, 0.65, 3515, 0.35, 4832); // to compute 0.65 * 3515 + 0.35 * 4832 + for (int nbits = 1; nbits <= 8; ++nbits) + DisplayMultiply(nbits, 0.65, 3515, 0.35, 4832); // to compute 0.65 * 3515 + 0.35 * 4832 return EXIT_SUCCESS; diff --git a/HandsOn/2-ObjectProgramming/Solution/exercice17.cpp b/HandsOn/2-ObjectProgramming/Solution/exercice17.cpp index 2664e540ef4c58034ea4f087952e91eeb2928696..33f442aff9e523e57eb35284ca84af8f32cf3589 100644 --- a/HandsOn/2-ObjectProgramming/Solution/exercice17.cpp +++ b/HandsOn/2-ObjectProgramming/Solution/exercice17.cpp @@ -3,7 +3,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -21,16 +21,16 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } @@ -72,16 +72,16 @@ private: //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); int& numerator = numerator_; int& exponent = exponent_; @@ -94,17 +94,17 @@ PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } double PowerOfTwoApprox::AsDouble() const { - int denominator = times_power_of_2(1, exponent_); + int denominator = TimesPowerOf2(1, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -123,12 +123,12 @@ int PowerOfTwoApprox::GetExponent() const int PowerOfTwoApprox::Multiply(int coefficient) const { - return times_power_of_2(GetNumerator() * coefficient, -GetExponent()); + return TimesPowerOf2(GetNumerator() * coefficient, -GetExponent()); } //! Display the approximation of the given argument that fits in the given number of bits. -void display_power_of_2_approx(int Nbits, double value) +void DisplayPowerOf2Approx(int Nbits, double value) { PowerOfTwoApprox approximation(Nbits, value); @@ -137,15 +137,15 @@ void display_power_of_2_approx(int Nbits, double value) const double error = std::fabs(value - approx) / value; std::cout << "[With " << Nbits << " bits]: " << value << " ~ " << approx << " (" << approximation.GetNumerator() << - " / 2^" << approximation.GetExponent() << ") [error = " << round_as_int(100. * error) << "/100]" << std::endl; + " / 2^" << approximation.GetExponent() << ") [error = " << RoundAsInt(100. * error) << "/100]" << std::endl; } //! Compute value1 * coefficient1 + value2 * coefficient2 over Nbits bits. -void display_multiply(int Nbits, double value1, int coefficient1, double value2, int coefficient2) +void DisplayMultiply(int Nbits, double value1, int coefficient1, double value2, int coefficient2) { double exact = value1 * coefficient1 + value2 * coefficient2; - int rounded = round_as_int(exact); + int rounded = RoundAsInt(exact); PowerOfTwoApprox approximation1(Nbits, value1); PowerOfTwoApprox approximation2(Nbits, value2); @@ -157,7 +157,7 @@ void display_multiply(int Nbits, double value1, int coefficient1, double value2, std::cout << "[With " << Nbits << " bits]: " << value1 << " * " << coefficient1 << " + " << value2 << " * " << coefficient2 << " = " << rounded << " ~ " << approx - << " [error = " << round_as_int(1000. * error) << "/1000]" << std::endl; + << " [error = " << RoundAsInt(1000. * error) << "/1000]" << std::endl; } @@ -170,18 +170,18 @@ 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) - display_power_of_2_approx(Nbits, 0.65); + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.65); std::cout << std::endl; - for (int Nbits = 2; Nbits <= 8; Nbits += 2) - display_power_of_2_approx(Nbits, 0.35); + for (int nbits = 2; nbits <= 8; nbits += 2) + DisplayPowerOf2Approx(nbits, 0.35); std::cout << std::endl; - for (int Nbits = 1; Nbits <= 8; ++Nbits) - display_multiply(Nbits, 0.65, 3515, 0.35, 4832); // to compute 0.65 * 3515 + 0.35 * 4832 + for (int nbits = 1; nbits <= 8; ++nbits) + DisplayMultiply(nbits, 0.65, 3515, 0.35, 4832); // to compute 0.65 * 3515 + 0.35 * 4832 return EXIT_SUCCESS; diff --git a/HandsOn/2-ObjectProgramming/Solution/exercice18.cpp b/HandsOn/2-ObjectProgramming/Solution/exercice18.cpp index 744d8c340511df0ba3bfc3ed5039e494a7c2bf5a..0f4bf09b7d0857b212b3dbe3afb28857d39fbf31 100644 --- a/HandsOn/2-ObjectProgramming/Solution/exercice18.cpp +++ b/HandsOn/2-ObjectProgramming/Solution/exercice18.cpp @@ -3,7 +3,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -21,16 +21,16 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } @@ -72,16 +72,16 @@ private: //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); int& numerator = numerator_; int& exponent = exponent_; @@ -94,17 +94,17 @@ PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } double PowerOfTwoApprox::AsDouble() const { - int denominator = times_power_of_2(1, exponent_); + int denominator = TimesPowerOf2(1, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -123,7 +123,7 @@ int PowerOfTwoApprox::GetExponent() const int PowerOfTwoApprox::Multiply(int coefficient) const { - return times_power_of_2(GetNumerator() * coefficient, -GetExponent()); + return TimesPowerOf2(GetNumerator() * coefficient, -GetExponent()); } @@ -158,15 +158,15 @@ void TestDisplayPowerOfTwoApprox::Display(int Nbits, double value) const const double error = std::fabs(value - approx) / value; std::cout << "[With " << Nbits << " bits]: " << value << " ~ " << approx << " (" << approximation.GetNumerator() << - " / 2^" << approximation.GetExponent() << ") [error = " << round_as_int(100. * error) << "/100]" << std::endl; + " / 2^" << approximation.GetExponent() << ") [error = " << RoundAsInt(100. * error) << "/100]" << std::endl; } //! Compute value1 * coefficient1 + value2 * coefficient2 over Nbits bits. -void display_multiply(int Nbits, double value1, int coefficient1, double value2, int coefficient2) +void DisplayMultiply(int Nbits, double value1, int coefficient1, double value2, int coefficient2) { double exact = value1 * coefficient1 + value2 * coefficient2; - int rounded = round_as_int(exact); + int rounded = RoundAsInt(exact); PowerOfTwoApprox approximation1(Nbits, value1); PowerOfTwoApprox approximation2(Nbits, value2); @@ -178,7 +178,7 @@ void display_multiply(int Nbits, double value1, int coefficient1, double value2, std::cout << "[With " << Nbits << " bits]: " << value1 << " * " << coefficient1 << " + " << value2 << " * " << coefficient2 << " = " << rounded << " ~ " << approx - << " [error = " << round_as_int(1000. * error) << "/1000]" << std::endl; + << " [error = " << RoundAsInt(1000. * error) << "/1000]" << std::endl; } @@ -193,13 +193,13 @@ int main(int argc, char** argv) TestDisplayPowerOfTwoApprox test_display_approx; - for (int Nbits = 2; Nbits <= 8; Nbits += 2) - test_display_approx.Do(Nbits); + 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); + for (int nbits = 1; nbits <= 8; ++nbits) + DisplayMultiply(nbits, 0.65, 3515, 0.35, 4832); return EXIT_SUCCESS; } diff --git a/HandsOn/2-ObjectProgramming/Solution/exercice19.cpp b/HandsOn/2-ObjectProgramming/Solution/exercice19.cpp index 114fa0bf41ab662f7c4f663bb00a4407febb7ac0..e30bf17e8d2df1242b556cd05d6c0bb6d46ecb25 100644 --- a/HandsOn/2-ObjectProgramming/Solution/exercice19.cpp +++ b/HandsOn/2-ObjectProgramming/Solution/exercice19.cpp @@ -3,7 +3,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -21,16 +21,16 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } @@ -72,16 +72,16 @@ private: //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); int& numerator = numerator_; int& exponent = exponent_; @@ -94,17 +94,17 @@ PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } double PowerOfTwoApprox::AsDouble() const { - int denominator = times_power_of_2(1, exponent_); + int denominator = TimesPowerOf2(1, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -123,7 +123,7 @@ int PowerOfTwoApprox::GetExponent() const int PowerOfTwoApprox::Multiply(int coefficient) const { - return times_power_of_2(GetNumerator() * coefficient, -GetExponent()); + return TimesPowerOf2(GetNumerator() * coefficient, -GetExponent()); } @@ -158,7 +158,7 @@ void TestDisplayPowerOfTwoApprox::Display(int Nbits, double value) const const double error = std::fabs(value - approx) / value; std::cout << "[With " << Nbits << " bits]: " << value << " ~ " << approx << " (" << approximation.GetNumerator() << - " / 2^" << approximation.GetExponent() << ") [error = " << round_as_int(100. * error) << "/100]" << std::endl; + " / 2^" << approximation.GetExponent() << ") [error = " << RoundAsInt(100. * error) << "/100]" << std::endl; } @@ -186,7 +186,7 @@ void TestDisplayMultiply::Do(int Nbits) const void TestDisplayMultiply::Display(int Nbits, double value1, int coefficient1, double value2, int coefficient2) const { double exact = value1 * coefficient1 + value2 * coefficient2; - int rounded = round_as_int(exact); + int rounded = RoundAsInt(exact); PowerOfTwoApprox approximation1(Nbits, value1); PowerOfTwoApprox approximation2(Nbits, value2); @@ -198,7 +198,7 @@ void TestDisplayMultiply::Display(int Nbits, double value1, int coefficient1, do std::cout << "[With " << Nbits << " bits]: " << value1 << " * " << coefficient1 << " + " << value2 << " * " << coefficient2 << " = " << rounded << " ~ " << approx - << " [error = " << round_as_int(1000. * error) << "/1000]" << std::endl; + << " [error = " << RoundAsInt(1000. * error) << "/1000]" << std::endl; } @@ -213,15 +213,15 @@ int main(int argc, char** argv) TestDisplayPowerOfTwoApprox test_display_approx; - for (int Nbits = 2; Nbits <= 8; Nbits += 2) - test_display_approx.Do(Nbits); + 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); + for (int nbits = 1; nbits <= 8; ++nbits) + test_display_multiply.Do(nbits); return EXIT_SUCCESS; } diff --git a/HandsOn/2-ObjectProgramming/Solution/exercice20.cpp b/HandsOn/2-ObjectProgramming/Solution/exercice20.cpp index 49c8128b96b9cc52477491f804860a057775c6b5..6bda15baaea5c8658c59c81c3c4bf952b5bf99b1 100644 --- a/HandsOn/2-ObjectProgramming/Solution/exercice20.cpp +++ b/HandsOn/2-ObjectProgramming/Solution/exercice20.cpp @@ -5,7 +5,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -23,16 +23,16 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } @@ -74,16 +74,16 @@ private: //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); int& numerator = numerator_; int& exponent = exponent_; @@ -96,17 +96,17 @@ PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } double PowerOfTwoApprox::AsDouble() const { - int denominator = times_power_of_2(1, exponent_); + int denominator = TimesPowerOf2(1, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -125,7 +125,7 @@ int PowerOfTwoApprox::GetExponent() const int PowerOfTwoApprox::Multiply(int coefficient) const { - return times_power_of_2(GetNumerator() * coefficient, -GetExponent()); + return TimesPowerOf2(GetNumerator() * coefficient, -GetExponent()); } @@ -139,14 +139,14 @@ enum class RoundToInteger { no, yes }; * \param[in] optional_string2 String that might appear just before the "[error = ...]". * \param[in] do_round_to_integer If yes, the exact result is approximated as an integer. */ -void print_line(int Nbits, double exact, double approx, int maximum_error_index, +void PrintLine(int Nbits, double exact, double approx, int maximum_error_index, std::string optional_string1 = "", std::string optional_string2 = "", RoundToInteger do_round_to_integer = RoundToInteger::no) { - int error = round_as_int(maximum_error_index * std::fabs(exact - approx) / exact); + int error = RoundAsInt(maximum_error_index * std::fabs(exact - approx) / exact); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << error << "/" << maximum_error_index << "]" << std::endl; @@ -183,7 +183,7 @@ void TestDisplayPowerOfTwoApprox::Display(int Nbits, double value) const std::ostringstream oconv; oconv << " (" << approximation.GetNumerator() << "/2^" << approximation.GetExponent() << ")"; - print_line(Nbits, value, approx, 100, "", oconv.str()); + PrintLine(Nbits, value, approx, 100, "", oconv.str()); } @@ -220,7 +220,7 @@ void TestDisplayMultiply::Display(int Nbits, double value1, int coefficient1, do std::ostringstream oconv; oconv << value1 << " * " << coefficient1 << " + " << value2 << " * " << coefficient2 << " = "; - print_line(Nbits, exact, static_cast<double>(approx), 1000, oconv.str(), "", RoundToInteger::yes); + PrintLine(Nbits, exact, static_cast<double>(approx), 1000, oconv.str(), "", RoundToInteger::yes); } @@ -235,15 +235,15 @@ int main(int argc, char** argv) TestDisplayPowerOfTwoApprox test_display_approx; - for (int Nbits = 2; Nbits <= 8; Nbits += 2) - test_display_approx.Do(Nbits); + 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); + for (int nbits = 1; nbits <= 8; ++nbits) + test_display_multiply.Do(nbits); return EXIT_SUCCESS; } diff --git a/HandsOn/2-ObjectProgramming/Solution/exercice21.cpp b/HandsOn/2-ObjectProgramming/Solution/exercice21.cpp index fe5ba5a6095e30382b3fc65a2ec0058f20cefc86..4ceb57d4e11542ec90c21d6b590df86203ee8c58 100644 --- a/HandsOn/2-ObjectProgramming/Solution/exercice21.cpp +++ b/HandsOn/2-ObjectProgramming/Solution/exercice21.cpp @@ -5,7 +5,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -23,16 +23,16 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } @@ -74,16 +74,16 @@ private: //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); int& numerator = numerator_; int& exponent = exponent_; @@ -96,17 +96,17 @@ PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } double PowerOfTwoApprox::AsDouble() const { - int denominator = times_power_of_2(1, exponent_); + int denominator = TimesPowerOf2(1, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -125,7 +125,7 @@ int PowerOfTwoApprox::GetExponent() const int PowerOfTwoApprox::Multiply(int coefficient) const { - return times_power_of_2(GetNumerator() * coefficient, -GetExponent()); + return TimesPowerOf2(GetNumerator() * coefficient, -GetExponent()); } @@ -176,10 +176,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - int error = round_as_int(resolution_ * std::fabs(exact - approx) / exact); + int error = RoundAsInt(resolution_ * std::fabs(exact - approx) / exact); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << error << "/" << resolution_ << "]" << std::endl; @@ -295,15 +295,15 @@ int main(int argc, char** argv) TestDisplayPowerOfTwoApprox test_display_approx(100); - for (int Nbits = 2; Nbits <= 8; Nbits += 2) - test_display_approx.Do(Nbits); + for (int nbits = 2; nbits <= 8; nbits += 2) + test_display_approx.Do(nbits); std::cout << std::endl; TestDisplayMultiply test_display_multiply(1000); - for (int Nbits = 1; Nbits <= 8; ++Nbits) - test_display_multiply.Do(Nbits); + for (int nbits = 1; nbits <= 8; ++nbits) + test_display_multiply.Do(nbits); return EXIT_SUCCESS; } diff --git a/HandsOn/2-ObjectProgramming/Solution/exercice22.cpp b/HandsOn/2-ObjectProgramming/Solution/exercice22.cpp index 5b366d9fb9b9e0d939306bbad7687eccb0338f44..b065c9d188a863121605ddfb6fd4781a54610166 100644 --- a/HandsOn/2-ObjectProgramming/Solution/exercice22.cpp +++ b/HandsOn/2-ObjectProgramming/Solution/exercice22.cpp @@ -5,7 +5,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -23,16 +23,16 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } @@ -74,16 +74,16 @@ private: //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); int& numerator = numerator_; int& exponent = exponent_; @@ -96,17 +96,17 @@ PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } double PowerOfTwoApprox::AsDouble() const { - int denominator = times_power_of_2(1, exponent_); + int denominator = TimesPowerOf2(1, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -125,7 +125,7 @@ int PowerOfTwoApprox::GetExponent() const int PowerOfTwoApprox::Multiply(int coefficient) const { - return times_power_of_2(GetNumerator() * coefficient, -GetExponent()); + return TimesPowerOf2(GetNumerator() * coefficient, -GetExponent()); } @@ -176,10 +176,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - int error = round_as_int(resolution_ * std::fabs(exact - approx) / exact); + int error = RoundAsInt(resolution_ * std::fabs(exact - approx) / exact); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << error << "/" << resolution_ << "]" << std::endl; @@ -345,22 +345,22 @@ int main(int argc, char** argv) TestDisplayPowerOfTwoApprox065 test_display_approx065(100); - for (int Nbits = 2; Nbits <= 8; Nbits += 2) - test_display_approx065.Do(Nbits); + for (int nbits = 2; nbits <= 8; nbits += 2) + test_display_approx065.Do(nbits); std::cout << std::endl; TestDisplayPowerOfTwoApprox035 test_display_approx035(100); - for (int Nbits = 2; Nbits <= 8; Nbits += 2) - test_display_approx035.Do(Nbits); + for (int nbits = 2; nbits <= 8; nbits += 2) + test_display_approx035.Do(nbits); std::cout << std::endl; TestDisplayMultiply test_display_multiply(1000); - for (int Nbits = 1; Nbits <= 8; ++Nbits) - test_display_multiply.Do(Nbits); + for (int nbits = 1; nbits <= 8; ++nbits) + test_display_multiply.Do(nbits); return EXIT_SUCCESS; } diff --git a/HandsOn/2-ObjectProgramming/Solution/exercice23.cpp b/HandsOn/2-ObjectProgramming/Solution/exercice23.cpp index dd34fbb5b94cddcb4446523889eff3cad166e396..2afa4e44d92ac822bed459ed32dd3f73d8b9a919 100644 --- a/HandsOn/2-ObjectProgramming/Solution/exercice23.cpp +++ b/HandsOn/2-ObjectProgramming/Solution/exercice23.cpp @@ -5,7 +5,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -23,16 +23,16 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } @@ -74,16 +74,16 @@ private: //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); int& numerator = numerator_; int& exponent = exponent_; @@ -96,17 +96,17 @@ PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } double PowerOfTwoApprox::AsDouble() const { - int denominator = times_power_of_2(1, exponent_); + int denominator = TimesPowerOf2(1, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -125,7 +125,7 @@ int PowerOfTwoApprox::GetExponent() const int PowerOfTwoApprox::Multiply(int coefficient) const { - return times_power_of_2(GetNumerator() * coefficient, -GetExponent()); + return TimesPowerOf2(GetNumerator() * coefficient, -GetExponent()); } @@ -185,10 +185,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - int error = round_as_int(resolution_ * std::fabs(exact - approx) / exact); + int error = RoundAsInt(resolution_ * std::fabs(exact - approx) / exact); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << error << "/" << resolution_ << "]" << std::endl; @@ -347,7 +347,7 @@ void TestDisplayMultiply::Display(int Nbits, double value1, int coefficient1, do //! Function for error handling. We will see later how to fulfill the same functionality more properly. -[[noreturn]] void error(std::string explanation) +[[noreturn]] void Error(std::string explanation) { std::cout << "ERROR: " << explanation << std::endl; exit(EXIT_FAILURE); @@ -380,7 +380,7 @@ private: void TestDisplayContainer::Register(TestDisplay* test_display) { if (current_position_ >= 3) - error("TestDisplayContainer is already full; impossible to register a new element!"); + Error("TestDisplayContainer is already full; impossible to register a new element!"); list_[current_position_] = test_display; ++current_position_; @@ -391,8 +391,8 @@ void TestDisplayContainer::Do(int initial_Nbit, int final_Nbit, int increment_Nb { for (int i = 0; i < current_position_; ++i) { - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - list_[i]->Do(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + list_[i]->Do(nbits); std::cout << std::endl; } } diff --git a/HandsOn/2-ObjectProgramming/Solution/exercice24.cpp b/HandsOn/2-ObjectProgramming/Solution/exercice24.cpp index bb5b5414941613fdda988da5350bda78df730128..635383240ef2cbd26bbb8fb9b967dd66ea1171c3 100644 --- a/HandsOn/2-ObjectProgramming/Solution/exercice24.cpp +++ b/HandsOn/2-ObjectProgramming/Solution/exercice24.cpp @@ -5,7 +5,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -23,16 +23,16 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } @@ -74,16 +74,16 @@ private: //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); int& numerator = numerator_; int& exponent = exponent_; @@ -96,17 +96,17 @@ PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } double PowerOfTwoApprox::AsDouble() const { - int denominator = times_power_of_2(1, exponent_); + int denominator = TimesPowerOf2(1, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -125,7 +125,7 @@ int PowerOfTwoApprox::GetExponent() const int PowerOfTwoApprox::Multiply(int coefficient) const { - return times_power_of_2(GetNumerator() * coefficient, -GetExponent()); + return TimesPowerOf2(GetNumerator() * coefficient, -GetExponent()); } @@ -185,10 +185,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - int error = round_as_int(resolution_ * std::fabs(exact - approx) / exact); + int error = RoundAsInt(resolution_ * std::fabs(exact - approx) / exact); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << error << "/" << resolution_ << "]" << std::endl; @@ -347,7 +347,7 @@ void TestDisplayMultiply::Display(int Nbits, double value1, int coefficient1, do //! Function for error handling. We will see later how to fulfill the same functionality more properly. -[[noreturn]] void error(std::string explanation) +[[noreturn]] void Error(std::string explanation) { std::cout << "ERROR: " << explanation << std::endl; exit(EXIT_FAILURE); @@ -408,7 +408,7 @@ TestDisplayContainer::~TestDisplayContainer() void TestDisplayContainer::Register(TestDisplay* test_display) { if (current_position_ >= size_) - error("TestDisplayContainer is already full; impossible to register a new element!"); + Error("TestDisplayContainer is already full; impossible to register a new element!"); list_[current_position_] = test_display; ++current_position_; @@ -419,8 +419,8 @@ void TestDisplayContainer::Do(int initial_Nbit, int final_Nbit, int increment_Nb { for (auto i = 0ul; i < current_position_; ++i) { - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - list_[i]->Do(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + list_[i]->Do(nbits); std::cout << std::endl; } } diff --git a/HandsOn/2-ObjectProgramming/Solution/exercice25.cpp b/HandsOn/2-ObjectProgramming/Solution/exercice25.cpp index 4fc0646d265e88c0ec5c37506e717488c56eb45b..6e37babf183b9d758808b529e5aa0f2cc3ba064e 100644 --- a/HandsOn/2-ObjectProgramming/Solution/exercice25.cpp +++ b/HandsOn/2-ObjectProgramming/Solution/exercice25.cpp @@ -5,7 +5,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -23,16 +23,16 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } @@ -74,16 +74,16 @@ private: //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); int& numerator = numerator_; int& exponent = exponent_; @@ -96,17 +96,17 @@ PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } double PowerOfTwoApprox::AsDouble() const { - int denominator = times_power_of_2(1, exponent_); + int denominator = TimesPowerOf2(1, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -125,7 +125,7 @@ int PowerOfTwoApprox::GetExponent() const int PowerOfTwoApprox::Multiply(int coefficient) const { - return times_power_of_2(GetNumerator() * coefficient, -GetExponent()); + return TimesPowerOf2(GetNumerator() * coefficient, -GetExponent()); } @@ -185,10 +185,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - int error = round_as_int(resolution_ * std::fabs(exact - approx) / exact); + int error = RoundAsInt(resolution_ * std::fabs(exact - approx) / exact); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << error << "/" << resolution_ << "]" << std::endl; @@ -347,7 +347,7 @@ void TestDisplayMultiply::Display(int Nbits, double value1, int coefficient1, do //! Function for error handling. We will see later how to fulfill the same functionality more properly. -[[noreturn]] void error(std::string explanation) +[[noreturn]] void Error(std::string explanation) { std::cout << "ERROR: " << explanation << std::endl; exit(EXIT_FAILURE); @@ -417,7 +417,7 @@ TestDisplayContainer::~TestDisplayContainer() void TestDisplayContainer::Register(TestDisplay* test_display) { if (current_position_ >= capacity_) - error("TestDisplayContainer is already full; impossible to register a new element!"); + Error("TestDisplayContainer is already full; impossible to register a new element!"); list_[current_position_] = test_display; ++current_position_; @@ -427,10 +427,10 @@ void TestDisplayContainer::Register(TestDisplay* test_display) const TestDisplay& TestDisplayContainer::GetElement(std::size_t i) const { if (i >= GetCapacity()) - error("You try to access an element out of bounds!"); + Error("You try to access an element out of bounds!"); if (list_[i] == nullptr) // equivalent to i >= GetSize() - error("You try to access an element that was never initialized!"); + Error("You try to access an element that was never initialized!"); return *list_[i]; } @@ -449,14 +449,14 @@ std::size_t TestDisplayContainer::GetSize() const //! For each container stored, loop oover all those bits and print the result on screen. -void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) +void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) { for (auto i = 0ul; i < container.GetSize(); ++i) { decltype(auto) current_test_display = container.GetElement(i); - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - current_test_display.Do(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + current_test_display.Do(nbits); std::cout << std::endl; } } @@ -477,7 +477,7 @@ int main(int argc, char** argv) container.Register(new TestDisplayPowerOfTwoApprox035(1000000)); container.Register(new TestDisplayMultiply(10000)); - loop(4, 16, 4, container); + Loop(4, 16, 4, container); return EXIT_SUCCESS; } diff --git a/HandsOn/3-Operators/Solution/exercice26.cpp b/HandsOn/3-Operators/Solution/exercice26.cpp index 09c55dd7400e0c807cdf775220685ae7df6ba59c..59b804f50876d23d59699f80c919937d2be4ebb6 100644 --- a/HandsOn/3-Operators/Solution/exercice26.cpp +++ b/HandsOn/3-Operators/Solution/exercice26.cpp @@ -5,7 +5,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -23,16 +23,16 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } @@ -74,16 +74,16 @@ private: //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); int& numerator = numerator_; int& exponent = exponent_; @@ -96,17 +96,17 @@ PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } PowerOfTwoApprox::operator double() const { - int denominator = times_power_of_2(1, exponent_); + int denominator = TimesPowerOf2(1, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -125,7 +125,7 @@ int PowerOfTwoApprox::GetExponent() const int PowerOfTwoApprox::Multiply(int coefficient) const { - return times_power_of_2(GetNumerator() * coefficient, -GetExponent()); + return TimesPowerOf2(GetNumerator() * coefficient, -GetExponent()); } @@ -185,10 +185,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - int error = round_as_int(resolution_ * std::fabs(exact - approx) / exact); + int error = RoundAsInt(resolution_ * std::fabs(exact - approx) / exact); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << error << "/" << resolution_ << "]" << std::endl; @@ -347,7 +347,7 @@ void TestDisplayMultiply::Display(int Nbits, double value1, int coefficient1, do //! Function for error handling. We will see later how to fulfill the same functionality more properly. -[[noreturn]] void error(std::string explanation) +[[noreturn]] void Error(std::string explanation) { std::cout << "ERROR: " << explanation << std::endl; exit(EXIT_FAILURE); @@ -417,7 +417,7 @@ TestDisplayContainer::~TestDisplayContainer() void TestDisplayContainer::Register(TestDisplay* test_display) { if (current_position_ >= capacity_) - error("TestDisplayContainer is already full; impossible to register a new element!"); + Error("TestDisplayContainer is already full; impossible to register a new element!"); list_[current_position_] = test_display; ++current_position_; @@ -427,10 +427,10 @@ void TestDisplayContainer::Register(TestDisplay* test_display) const TestDisplay& TestDisplayContainer::GetElement(std::size_t i) const { if (i >= GetCapacity()) - error("You try to access an element out of bounds!"); + Error("You try to access an element out of bounds!"); if (list_[i] == nullptr) // equivalent to i >= GetSize() - error("You try to access an element that was never initialized!"); + Error("You try to access an element that was never initialized!"); return *list_[i]; } @@ -449,14 +449,14 @@ std::size_t TestDisplayContainer::GetSize() const //! For each container stored, loop oover all those bits and print the result on screen. -void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) +void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) { for (auto i = 0ul; i < container.GetSize(); ++i) { decltype(auto) current_test_display = container.GetElement(i); - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - current_test_display.Do(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + current_test_display.Do(nbits); std::cout << std::endl; } } @@ -477,7 +477,7 @@ int main(int argc, char** argv) container.Register(new TestDisplayPowerOfTwoApprox035(1000000)); container.Register(new TestDisplayMultiply(10000)); - loop(4, 16, 4, container); + Loop(4, 16, 4, container); return EXIT_SUCCESS; } diff --git a/HandsOn/3-Operators/Solution/exercice27.cpp b/HandsOn/3-Operators/Solution/exercice27.cpp index b12f62e92cf8596c245edc4182d03c1c746d0105..a6bed9aaa4ac82960bae9e1deb9d15b07535103e 100644 --- a/HandsOn/3-Operators/Solution/exercice27.cpp +++ b/HandsOn/3-Operators/Solution/exercice27.cpp @@ -5,7 +5,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -23,16 +23,16 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } @@ -71,7 +71,7 @@ private: */ int operator*(int coefficient, const PowerOfTwoApprox& approx) { - return times_power_of_2(approx.GetNumerator() * coefficient, -approx.GetExponent()); + return TimesPowerOf2(approx.GetNumerator() * coefficient, -approx.GetExponent()); } @@ -85,16 +85,16 @@ private: //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); int& numerator = numerator_; int& exponent = exponent_; @@ -107,17 +107,17 @@ PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } PowerOfTwoApprox::operator double() const { - int denominator = times_power_of_2(1, exponent_); + int denominator = TimesPowerOf2(1, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -190,10 +190,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - int error = round_as_int(resolution_ * std::fabs(exact - approx) / exact); + int error = RoundAsInt(resolution_ * std::fabs(exact - approx) / exact); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << error << "/" << resolution_ << "]" << std::endl; @@ -353,7 +353,7 @@ void TestDisplayMultiply::Display(int Nbits, double value1, int coefficient1, do //! Function for error handling. We will see later how to fulfill the same functionality more properly. -[[noreturn]] void error(std::string explanation) +[[noreturn]] void Error(std::string explanation) { std::cout << "ERROR: " << explanation << std::endl; exit(EXIT_FAILURE); @@ -423,7 +423,7 @@ TestDisplayContainer::~TestDisplayContainer() void TestDisplayContainer::Register(TestDisplay* test_display) { if (current_position_ >= capacity_) - error("TestDisplayContainer is already full; impossible to register a new element!"); + Error("TestDisplayContainer is already full; impossible to register a new element!"); list_[current_position_] = test_display; ++current_position_; @@ -433,10 +433,10 @@ void TestDisplayContainer::Register(TestDisplay* test_display) const TestDisplay& TestDisplayContainer::GetElement(std::size_t i) const { if (i >= GetCapacity()) - error("You try to access an element out of bounds!"); + Error("You try to access an element out of bounds!"); if (list_[i] == nullptr) // equivalent to i >= GetSize() - error("You try to access an element that was never initialized!"); + Error("You try to access an element that was never initialized!"); return *list_[i]; } @@ -455,14 +455,14 @@ std::size_t TestDisplayContainer::GetSize() const //! For each container stored, loop oover all those bits and print the result on screen. -void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) +void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) { for (auto i = 0ul; i < container.GetSize(); ++i) { decltype(auto) current_test_display = container.GetElement(i); - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - current_test_display.Do(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + current_test_display.Do(nbits); std::cout << std::endl; } } @@ -483,7 +483,7 @@ int main(int argc, char** argv) container.Register(new TestDisplayPowerOfTwoApprox035(1000000)); container.Register(new TestDisplayMultiply(10000)); - loop(4, 16, 4, container); + Loop(4, 16, 4, container); return EXIT_SUCCESS; } diff --git a/HandsOn/3-Operators/Solution/exercice28.cpp b/HandsOn/3-Operators/Solution/exercice28.cpp index 29d32ddefcce033c37c1f677ec1db4224de8f1f5..66180944176866c0f8ed9d14e226ce3dbfcad149 100644 --- a/HandsOn/3-Operators/Solution/exercice28.cpp +++ b/HandsOn/3-Operators/Solution/exercice28.cpp @@ -5,7 +5,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -23,16 +23,16 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } @@ -71,7 +71,7 @@ private: */ int operator*(int coefficient, const PowerOfTwoApprox& approx) { - return times_power_of_2(approx.GetNumerator() * coefficient, -approx.GetExponent()); + return TimesPowerOf2(approx.GetNumerator() * coefficient, -approx.GetExponent()); } @@ -85,16 +85,16 @@ private: //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); int& numerator = numerator_; int& exponent = exponent_; @@ -107,17 +107,17 @@ PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } PowerOfTwoApprox::operator double() const { - int denominator = times_power_of_2(1, exponent_); + int denominator = TimesPowerOf2(1, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -190,10 +190,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - int error = round_as_int(resolution_ * std::fabs(exact - approx) / exact); + int error = RoundAsInt(resolution_ * std::fabs(exact - approx) / exact); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << error << "/" << resolution_ << "]" << std::endl; @@ -353,7 +353,7 @@ void TestDisplayMultiply::Display(int Nbits, double value1, int coefficient1, do //! Function for error handling. We will see later how to fulfill the same functionality more properly. -[[noreturn]] void error(std::string explanation) +[[noreturn]] void Error(std::string explanation) { std::cout << "ERROR: " << explanation << std::endl; exit(EXIT_FAILURE); @@ -423,7 +423,7 @@ TestDisplayContainer::~TestDisplayContainer() void TestDisplayContainer::Register(TestDisplay* test_display) { if (current_position_ >= capacity_) - error("TestDisplayContainer is already full; impossible to register a new element!"); + Error("TestDisplayContainer is already full; impossible to register a new element!"); list_[current_position_] = test_display; ++current_position_; @@ -433,10 +433,10 @@ void TestDisplayContainer::Register(TestDisplay* test_display) const TestDisplay& TestDisplayContainer::operator[](std::size_t i) const { if (i >= GetCapacity()) - error("You try to access an element out of bounds!"); + Error("You try to access an element out of bounds!"); if (list_[i] == nullptr) // equivalent to i >= GetSize() - error("You try to access an element that was never initialized!"); + Error("You try to access an element that was never initialized!"); return *list_[i]; } @@ -455,14 +455,14 @@ std::size_t TestDisplayContainer::GetSize() const //! For each container stored, loop oover all those bits and print the result on screen. -void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) +void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) { for (auto i = 0ul; i < container.GetSize(); ++i) { decltype(auto) current_test_display = container[i]; - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - current_test_display.Do(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + current_test_display.Do(nbits); std::cout << std::endl; } } @@ -483,7 +483,7 @@ int main(int argc, char** argv) container.Register(new TestDisplayPowerOfTwoApprox035(1000000)); container.Register(new TestDisplayMultiply(10000)); - loop(4, 16, 4, container); + Loop(4, 16, 4, container); return EXIT_SUCCESS; } diff --git a/HandsOn/3-Operators/Solution/exercice29.cpp b/HandsOn/3-Operators/Solution/exercice29.cpp index c3b14c448407f153bda6840f5bd7d46b2b8028c1..107bf1a96fe1ee1e52beb9b055bca5a22de60ab5 100644 --- a/HandsOn/3-Operators/Solution/exercice29.cpp +++ b/HandsOn/3-Operators/Solution/exercice29.cpp @@ -5,7 +5,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -23,16 +23,16 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } @@ -78,7 +78,7 @@ std::ostream& operator<<(std::ostream& out, const PowerOfTwoApprox& approximatio */ int operator*(int coefficient, const PowerOfTwoApprox& approx) { - return times_power_of_2(approx.GetNumerator() * coefficient, -approx.GetExponent()); + return TimesPowerOf2(approx.GetNumerator() * coefficient, -approx.GetExponent()); } @@ -92,16 +92,16 @@ std::ostream& operator<<(std::ostream& out, const PowerOfTwoApprox& approximatio //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); int& numerator = numerator_; int& exponent = exponent_; @@ -114,17 +114,17 @@ PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } PowerOfTwoApprox::operator double() const { - int denominator = times_power_of_2(1, exponent_); + int denominator = TimesPowerOf2(1, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -197,10 +197,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - int error = round_as_int(resolution_ * std::fabs(exact - approx) / exact); + int error = RoundAsInt(resolution_ * std::fabs(exact - approx) / exact); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << error << "/" << resolution_ << "]" << std::endl; @@ -360,7 +360,7 @@ void TestDisplayMultiply::Display(int Nbits, double value1, int coefficient1, do //! Function for error handling. We will see later how to fulfill the same functionality more properly. -[[noreturn]] void error(std::string explanation) +[[noreturn]] void Error(std::string explanation) { std::cout << "ERROR: " << explanation << std::endl; exit(EXIT_FAILURE); @@ -430,7 +430,7 @@ TestDisplayContainer::~TestDisplayContainer() void TestDisplayContainer::Register(TestDisplay* test_display) { if (current_position_ >= capacity_) - error("TestDisplayContainer is already full; impossible to register a new element!"); + Error("TestDisplayContainer is already full; impossible to register a new element!"); list_[current_position_] = test_display; ++current_position_; @@ -440,10 +440,10 @@ void TestDisplayContainer::Register(TestDisplay* test_display) const TestDisplay& TestDisplayContainer::operator[](std::size_t i) const { if (i >= GetCapacity()) - error("You try to access an element out of bounds!"); + Error("You try to access an element out of bounds!"); if (list_[i] == nullptr) // equivalent to i >= GetSize() - error("You try to access an element that was never initialized!"); + Error("You try to access an element that was never initialized!"); return *list_[i]; } @@ -462,14 +462,14 @@ std::size_t TestDisplayContainer::GetSize() const //! For each container stored, loop oover all those bits and print the result on screen. -void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) +void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) { for (auto i = 0ul; i < container.GetSize(); ++i) { decltype(auto) current_test_display = container[i]; - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - current_test_display.Do(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + current_test_display.Do(nbits); std::cout << std::endl; } } @@ -490,7 +490,7 @@ int main(int argc, char** argv) container.Register(new TestDisplayPowerOfTwoApprox035(1000000)); container.Register(new TestDisplayMultiply(10000)); - loop(4, 16, 4, container); + Loop(4, 16, 4, container); return EXIT_SUCCESS; } diff --git a/HandsOn/3-Operators/Solution/exercice30.cpp b/HandsOn/3-Operators/Solution/exercice30.cpp index f0b14ea81f2842d4cd8dfb1eb9056ed4b618a8d1..8b96d061335861d7e0a8f692f64681b5b0d9719a 100644 --- a/HandsOn/3-Operators/Solution/exercice30.cpp +++ b/HandsOn/3-Operators/Solution/exercice30.cpp @@ -5,7 +5,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -23,16 +23,16 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } @@ -78,7 +78,7 @@ std::ostream& operator<<(std::ostream& out, const PowerOfTwoApprox& approximatio */ int operator*(int coefficient, const PowerOfTwoApprox& approx) { - return times_power_of_2(approx.GetNumerator() * coefficient, -approx.GetExponent()); + return TimesPowerOf2(approx.GetNumerator() * coefficient, -approx.GetExponent()); } @@ -92,16 +92,16 @@ std::ostream& operator<<(std::ostream& out, const PowerOfTwoApprox& approximatio //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); int& numerator = numerator_; int& exponent = exponent_; @@ -114,17 +114,17 @@ PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } PowerOfTwoApprox::operator double() const { - int denominator = times_power_of_2(1, exponent_); + int denominator = TimesPowerOf2(1, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -197,10 +197,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - int error = round_as_int(resolution_ * std::fabs(exact - approx) / exact); + int error = RoundAsInt(resolution_ * std::fabs(exact - approx) / exact); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << error << "/" << resolution_ << "]" << std::endl; @@ -360,7 +360,7 @@ void TestDisplayMultiply::Display(int Nbits, double value1, int coefficient1, do //! Function for error handling. We will see later how to fulfill the same functionality more properly. -[[noreturn]] void error(std::string explanation) +[[noreturn]] void Error(std::string explanation) { std::cout << "ERROR: " << explanation << std::endl; exit(EXIT_FAILURE); @@ -430,7 +430,7 @@ TestDisplayContainer::~TestDisplayContainer() void TestDisplayContainer::Register(TestDisplay* test_display) { if (current_position_ >= capacity_) - error("TestDisplayContainer is already full; impossible to register a new element!"); + Error("TestDisplayContainer is already full; impossible to register a new element!"); list_[current_position_] = test_display; ++current_position_; @@ -440,10 +440,10 @@ void TestDisplayContainer::Register(TestDisplay* test_display) const TestDisplay& TestDisplayContainer::operator[](std::size_t i) const { if (i >= GetCapacity()) - error("You try to access an element out of bounds!"); + Error("You try to access an element out of bounds!"); if (list_[i] == nullptr) // equivalent to i >= GetSize() - error("You try to access an element that was never initialized!"); + Error("You try to access an element that was never initialized!"); return *list_[i]; } @@ -462,14 +462,14 @@ std::size_t TestDisplayContainer::GetSize() const //! For each container stored, loop oover all those bits and print the result on screen. -void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) +void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) { for (auto i = 0ul; i < container.GetSize(); ++i) { decltype(auto) current_test_display = container[i]; - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - current_test_display(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + current_test_display(nbits); std::cout << std::endl; } } @@ -490,7 +490,7 @@ int main(int argc, char** argv) container.Register(new TestDisplayPowerOfTwoApprox035(1000000)); container.Register(new TestDisplayMultiply(10000)); - loop(4, 16, 4, container); + Loop(4, 16, 4, container); return EXIT_SUCCESS; } diff --git a/HandsOn/3-Operators/Solution/exercice31.cpp b/HandsOn/3-Operators/Solution/exercice31.cpp index f0b14ea81f2842d4cd8dfb1eb9056ed4b618a8d1..8b96d061335861d7e0a8f692f64681b5b0d9719a 100644 --- a/HandsOn/3-Operators/Solution/exercice31.cpp +++ b/HandsOn/3-Operators/Solution/exercice31.cpp @@ -5,7 +5,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -23,16 +23,16 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } @@ -78,7 +78,7 @@ std::ostream& operator<<(std::ostream& out, const PowerOfTwoApprox& approximatio */ int operator*(int coefficient, const PowerOfTwoApprox& approx) { - return times_power_of_2(approx.GetNumerator() * coefficient, -approx.GetExponent()); + return TimesPowerOf2(approx.GetNumerator() * coefficient, -approx.GetExponent()); } @@ -92,16 +92,16 @@ std::ostream& operator<<(std::ostream& out, const PowerOfTwoApprox& approximatio //! You're not obliged to use a function, but this way you enforce the Don't Repeat Yourself (DRY) principle! //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. -void helper_compute_power_of_2_approx(double value, int exponent, int& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, int& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); int& numerator = numerator_; int& exponent = exponent_; @@ -114,17 +114,17 @@ PowerOfTwoApprox::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } PowerOfTwoApprox::operator double() const { - int denominator = times_power_of_2(1, exponent_); + int denominator = TimesPowerOf2(1, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -197,10 +197,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - int error = round_as_int(resolution_ * std::fabs(exact - approx) / exact); + int error = RoundAsInt(resolution_ * std::fabs(exact - approx) / exact); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << error << "/" << resolution_ << "]" << std::endl; @@ -360,7 +360,7 @@ void TestDisplayMultiply::Display(int Nbits, double value1, int coefficient1, do //! Function for error handling. We will see later how to fulfill the same functionality more properly. -[[noreturn]] void error(std::string explanation) +[[noreturn]] void Error(std::string explanation) { std::cout << "ERROR: " << explanation << std::endl; exit(EXIT_FAILURE); @@ -430,7 +430,7 @@ TestDisplayContainer::~TestDisplayContainer() void TestDisplayContainer::Register(TestDisplay* test_display) { if (current_position_ >= capacity_) - error("TestDisplayContainer is already full; impossible to register a new element!"); + Error("TestDisplayContainer is already full; impossible to register a new element!"); list_[current_position_] = test_display; ++current_position_; @@ -440,10 +440,10 @@ void TestDisplayContainer::Register(TestDisplay* test_display) const TestDisplay& TestDisplayContainer::operator[](std::size_t i) const { if (i >= GetCapacity()) - error("You try to access an element out of bounds!"); + Error("You try to access an element out of bounds!"); if (list_[i] == nullptr) // equivalent to i >= GetSize() - error("You try to access an element that was never initialized!"); + Error("You try to access an element that was never initialized!"); return *list_[i]; } @@ -462,14 +462,14 @@ std::size_t TestDisplayContainer::GetSize() const //! For each container stored, loop oover all those bits and print the result on screen. -void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) +void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) { for (auto i = 0ul; i < container.GetSize(); ++i) { decltype(auto) current_test_display = container[i]; - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - current_test_display(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + current_test_display(nbits); std::cout << std::endl; } } @@ -490,7 +490,7 @@ int main(int argc, char** argv) container.Register(new TestDisplayPowerOfTwoApprox035(1000000)); container.Register(new TestDisplayMultiply(10000)); - loop(4, 16, 4, container); + Loop(4, 16, 4, container); return EXIT_SUCCESS; } diff --git a/HandsOn/4-Templates/Solution/exercice31.cpp b/HandsOn/4-Templates/Solution/exercice31.cpp index e4dbfb8ce995c1c5765bda87eb2a898e9f897d9d..9e72bb9ccde8a2bd18faedfcbbed407efd0b6515 100644 --- a/HandsOn/4-Templates/Solution/exercice31.cpp +++ b/HandsOn/4-Templates/Solution/exercice31.cpp @@ -5,7 +5,7 @@ //! Returns `number` * (2 ^ `exponent`) -int times_power_of_2(int number, int exponent) +int TimesPowerOf2(int number, int exponent) { while (exponent > 0) { @@ -23,16 +23,16 @@ int times_power_of_2(int number, int exponent) //! Round to `x` the nearest integer. -int round_as_int(double x) +int RoundAsInt(double x) { return static_cast<int>(std::round(x)); } // Maximum integer that might be represented with `Nbits` bits. -int max_int(int Nbits) +int MaxInt(int Nbits) { - return (times_power_of_2(1, Nbits) - 1); + return (TimesPowerOf2(1, Nbits) - 1); } @@ -81,7 +81,7 @@ std::ostream& operator<<(std::ostream& out, const PowerOfTwoApprox<IntT>& approx template<class IntT> int operator*(int coefficient, const PowerOfTwoApprox<IntT>& approx) { - return times_power_of_2(approx.GetNumerator() * coefficient, -approx.GetExponent()); + return TimesPowerOf2(approx.GetNumerator() * coefficient, -approx.GetExponent()); } @@ -97,17 +97,17 @@ int operator*(const PowerOfTwoApprox<IntT>& approx, int coefficient) //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. template<class IntT> -void helper_compute_power_of_2_approx(double value, int exponent, IntT& numerator, int& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, IntT& numerator, int& denominator) { - denominator = times_power_of_2(1, exponent); - numerator = round_as_int(value * denominator); + denominator = TimesPowerOf2(1, exponent); + numerator = RoundAsInt(value * denominator); } template<class IntT> PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { - int max_numerator = max_int(Nbits); + int max_numerator = MaxInt(Nbits); auto& numerator = numerator_; int& exponent = exponent_; @@ -120,18 +120,18 @@ PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } template<class IntT> PowerOfTwoApprox<IntT>::operator double() const { - int denominator = times_power_of_2(1, exponent_); + int denominator = TimesPowerOf2(1, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -206,10 +206,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - int error = round_as_int(resolution_ * std::fabs(exact - approx) / exact); + int error = RoundAsInt(resolution_ * std::fabs(exact - approx) / exact); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << error << "/" << resolution_ << "]" << std::endl; @@ -369,7 +369,7 @@ void TestDisplayMultiply::Display(int Nbits, double value1, int coefficient1, do //! Function for error handling. We will see later how to fulfill the same functionality more properly. -[[noreturn]] void error(std::string explanation) +[[noreturn]] void Error(std::string explanation) { std::cout << "ERROR: " << explanation << std::endl; exit(EXIT_FAILURE); @@ -439,7 +439,7 @@ TestDisplayContainer::~TestDisplayContainer() void TestDisplayContainer::Register(TestDisplay* test_display) { if (current_position_ >= capacity_) - error("TestDisplayContainer is already full; impossible to register a new element!"); + Error("TestDisplayContainer is already full; impossible to register a new element!"); list_[current_position_] = test_display; ++current_position_; @@ -449,10 +449,10 @@ void TestDisplayContainer::Register(TestDisplay* test_display) const TestDisplay& TestDisplayContainer::operator[](std::size_t i) const { if (i >= GetCapacity()) - error("You try to access an element out of bounds!"); + Error("You try to access an element out of bounds!"); if (list_[i] == nullptr) // equivalent to i >= GetSize() - error("You try to access an element that was never initialized!"); + Error("You try to access an element that was never initialized!"); return *list_[i]; } @@ -471,14 +471,14 @@ std::size_t TestDisplayContainer::GetSize() const //! For each container stored, loop oover all those bits and print the result on screen. -void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) +void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) { for (auto i = 0ul; i < container.GetSize(); ++i) { decltype(auto) current_test_display = container[i]; - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - current_test_display(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + current_test_display(nbits); std::cout << std::endl; } } @@ -499,7 +499,7 @@ int main(int argc, char** argv) container.Register(new TestDisplayPowerOfTwoApprox035(1000000)); container.Register(new TestDisplayMultiply(10000)); - loop(4, 32, 4, container); + Loop(4, 32, 4, container); return EXIT_SUCCESS; } diff --git a/HandsOn/4-Templates/Solution/exercice32.cpp b/HandsOn/4-Templates/Solution/exercice32.cpp index d6031e4719fc670b8f6aa99170b7f1c2109a39af..3e369380227023c9033d92b1b20fe6f53b4298b9 100644 --- a/HandsOn/4-Templates/Solution/exercice32.cpp +++ b/HandsOn/4-Templates/Solution/exercice32.cpp @@ -6,7 +6,7 @@ //! Returns `number` * (2 ^ `exponent`) template<class IntT> -IntT times_power_of_2(IntT number, int exponent) +IntT TimesPowerOf2(IntT number, int exponent) { constexpr IntT two = static_cast<IntT>(2); @@ -27,7 +27,7 @@ IntT times_power_of_2(IntT number, int exponent) //! Round to `x` the nearest integer. template<class IntT> -IntT round_as_int(double x) +IntT RoundAsInt(double x) { return static_cast<IntT>(std::round(x)); } @@ -35,10 +35,10 @@ IntT round_as_int(double x) // Maximum integer that might be represented with `Nbits` bits. template<class IntT> -IntT max_int(int Nbits) +IntT MaxInt(int Nbits) { constexpr IntT one = static_cast<IntT>(1); - return (times_power_of_2(one, Nbits) - one); + return (TimesPowerOf2(one, Nbits) - one); } @@ -87,7 +87,7 @@ std::ostream& operator<<(std::ostream& out, const PowerOfTwoApprox<IntT>& approx template<class IntT> IntT operator*(IntT coefficient, const PowerOfTwoApprox<IntT>& approx) { - return times_power_of_2(approx.GetNumerator() * coefficient, -approx.GetExponent()); + return TimesPowerOf2(approx.GetNumerator() * coefficient, -approx.GetExponent()); } @@ -103,18 +103,18 @@ IntT operator*(const PowerOfTwoApprox<IntT>& approx, IntT coefficient) //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. template<class IntT> -void helper_compute_power_of_2_approx(double value, int exponent, IntT& numerator, IntT& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, IntT& numerator, IntT& denominator) { constexpr IntT one = static_cast<IntT>(1); - denominator = times_power_of_2(one, exponent); - numerator = round_as_int<IntT>(value * denominator); + denominator = TimesPowerOf2(one, exponent); + numerator = RoundAsInt<IntT>(value * denominator); } template<class IntT> PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { - IntT max_numerator = max_int<IntT>(Nbits); + IntT max_numerator = MaxInt<IntT>(Nbits); auto& numerator = numerator_; int& exponent = exponent_; @@ -127,11 +127,11 @@ PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } @@ -139,7 +139,7 @@ template<class IntT> PowerOfTwoApprox<IntT>::operator double() const { constexpr IntT one = static_cast<IntT>(1); - IntT denominator = times_power_of_2(one, exponent_); + IntT denominator = TimesPowerOf2(one, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -216,10 +216,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - IntT error = round_as_int<IntT>(resolution_ * std::fabs(exact - approx) / exact); + IntT error = RoundAsInt<IntT>(resolution_ * std::fabs(exact - approx) / exact); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int<IntT>(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt<IntT>(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << error << "/" << resolution_ << "]" << std::endl; @@ -384,7 +384,7 @@ void TestDisplayMultiply<IntT>::Display(int Nbits, double value1, IntT coefficie //! Function for error handling. We will see later how to fulfill the same functionality more properly. -[[noreturn]] void error(std::string explanation) +[[noreturn]] void Error(std::string explanation) { std::cout << "ERROR: " << explanation << std::endl; exit(EXIT_FAILURE); @@ -454,7 +454,7 @@ TestDisplayContainer::~TestDisplayContainer() void TestDisplayContainer::Register(TestDisplay* test_display) { if (current_position_ >= capacity_) - error("TestDisplayContainer is already full; impossible to register a new element!"); + Error("TestDisplayContainer is already full; impossible to register a new element!"); list_[current_position_] = test_display; ++current_position_; @@ -464,10 +464,10 @@ void TestDisplayContainer::Register(TestDisplay* test_display) const TestDisplay& TestDisplayContainer::operator[](std::size_t i) const { if (i >= GetCapacity()) - error("You try to access an element out of bounds!"); + Error("You try to access an element out of bounds!"); if (list_[i] == nullptr) // equivalent to i >= GetSize() - error("You try to access an element that was never initialized!"); + Error("You try to access an element that was never initialized!"); return *list_[i]; } @@ -486,14 +486,14 @@ std::size_t TestDisplayContainer::GetSize() const //! For each container stored, loop oover all those bits and print the result on screen. -void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) +void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) { for (auto i = 0ul; i < container.GetSize(); ++i) { decltype(auto) current_test_display = container[i]; - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - current_test_display(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + current_test_display(nbits); std::cout << std::endl; } } @@ -516,7 +516,7 @@ int main(int argc, char** argv) container.Register(new TestDisplayPowerOfTwoApprox035(1000000)); container.Register(new TestDisplayMultiply<integer_type>(10000)); - loop(4, 32, 4, container); + Loop(4, 32, 4, container); return EXIT_SUCCESS; } diff --git a/HandsOn/4-Templates/Solution/exercice33.cpp b/HandsOn/4-Templates/Solution/exercice33.cpp index 11ca7c283abc9b2fc163740e47dfdb9ef90fd88a..a38ac9fe7e42ee430780dd0810e5a1bb470a5a32 100644 --- a/HandsOn/4-Templates/Solution/exercice33.cpp +++ b/HandsOn/4-Templates/Solution/exercice33.cpp @@ -6,7 +6,7 @@ //! Returns `number` * (2 ^ `exponent`) template<class IntT> -IntT times_power_of_2(IntT number, int exponent) +IntT TimesPowerOf2(IntT number, int exponent) { constexpr IntT two = static_cast<IntT>(2); @@ -27,7 +27,7 @@ IntT times_power_of_2(IntT number, int exponent) //! Round to `x` the nearest integer. template<class IntT> -IntT round_as_int(double x) +IntT RoundAsInt(double x) { return static_cast<IntT>(std::round(x)); } @@ -35,10 +35,10 @@ IntT round_as_int(double x) // Maximum integer that might be represented with `Nbits` bits. template<class IntT> -IntT max_int(int Nbits) +IntT MaxInt(int Nbits) { constexpr IntT one = static_cast<IntT>(1); - return (times_power_of_2(one, Nbits) - one); + return (TimesPowerOf2(one, Nbits) - one); } @@ -87,7 +87,7 @@ std::ostream& operator<<(std::ostream& out, const PowerOfTwoApprox<IntT>& approx template<class IntT> IntT operator*(IntT coefficient, const PowerOfTwoApprox<IntT>& approx) { - return times_power_of_2(approx.GetNumerator() * coefficient, -approx.GetExponent()); + return TimesPowerOf2(approx.GetNumerator() * coefficient, -approx.GetExponent()); } @@ -103,18 +103,18 @@ IntT operator*(const PowerOfTwoApprox<IntT>& approx, IntT coefficient) //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. template<class IntT> -void helper_compute_power_of_2_approx(double value, int exponent, IntT& numerator, IntT& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, IntT& numerator, IntT& denominator) { constexpr IntT one = static_cast<IntT>(1); - denominator = times_power_of_2(one, exponent); - numerator = round_as_int<IntT>(value * denominator); + denominator = TimesPowerOf2(one, exponent); + numerator = RoundAsInt<IntT>(value * denominator); } template<class IntT> PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { - IntT max_numerator = max_int<IntT>(Nbits); + IntT max_numerator = MaxInt<IntT>(Nbits); auto& numerator = numerator_; int& exponent = exponent_; @@ -127,11 +127,11 @@ PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } @@ -139,7 +139,7 @@ template<class IntT> PowerOfTwoApprox<IntT>::operator double() const { constexpr IntT one = static_cast<IntT>(1); - IntT denominator = times_power_of_2(one, exponent_); + IntT denominator = TimesPowerOf2(one, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -216,10 +216,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - IntT error = round_as_int<IntT>(resolution_ * std::fabs(exact - approx) / exact); + IntT error = RoundAsInt<IntT>(resolution_ * std::fabs(exact - approx) / exact); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int<IntT>(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt<IntT>(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << error << "/" << resolution_ << "]" << std::endl; @@ -397,7 +397,7 @@ void TestDisplayMultiply<IntT>::Display(int Nbits, double value1, IntT coefficie //! Function for error handling. We will see later how to fulfill the same functionality more properly. -[[noreturn]] void error(std::string explanation) +[[noreturn]] void Error(std::string explanation) { std::cout << "ERROR: " << explanation << std::endl; exit(EXIT_FAILURE); @@ -467,7 +467,7 @@ TestDisplayContainer::~TestDisplayContainer() void TestDisplayContainer::Register(TestDisplay* test_display) { if (current_position_ >= capacity_) - error("TestDisplayContainer is already full; impossible to register a new element!"); + Error("TestDisplayContainer is already full; impossible to register a new element!"); list_[current_position_] = test_display; ++current_position_; @@ -477,10 +477,10 @@ void TestDisplayContainer::Register(TestDisplay* test_display) const TestDisplay& TestDisplayContainer::operator[](std::size_t i) const { if (i >= GetCapacity()) - error("You try to access an element out of bounds!"); + Error("You try to access an element out of bounds!"); if (list_[i] == nullptr) // equivalent to i >= GetSize() - error("You try to access an element that was never initialized!"); + Error("You try to access an element that was never initialized!"); return *list_[i]; } @@ -499,14 +499,14 @@ std::size_t TestDisplayContainer::GetSize() const //! For each container stored, loop oover all those bits and print the result on screen. -void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) +void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) { for (auto i = 0ul; i < container.GetSize(); ++i) { decltype(auto) current_test_display = container[i]; - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - current_test_display(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + current_test_display(nbits); std::cout << std::endl; } } @@ -529,7 +529,7 @@ int main(int argc, char** argv) container.Register(new TestDisplayPowerOfTwoApprox035<integer_type>(100000000)); container.Register(new TestDisplayMultiply<integer_type>(1000000)); - loop(4, 32, 4, container); + Loop(4, 32, 4, container); return EXIT_SUCCESS; } diff --git a/HandsOn/4-Templates/Solution/exercice34.cpp b/HandsOn/4-Templates/Solution/exercice34.cpp index 1becc146d1b94ec818c5f560f6ff5c8a9a16b2a4..3b53174fde7d3b7f0a46deab77f48b4ba0f0a264 100644 --- a/HandsOn/4-Templates/Solution/exercice34.cpp +++ b/HandsOn/4-Templates/Solution/exercice34.cpp @@ -6,7 +6,7 @@ //! Returns `number` * (2 ^ `exponent`) template<class IntT> -IntT times_power_of_2(IntT number, int exponent) +IntT TimesPowerOf2(IntT number, int exponent) { constexpr IntT two = static_cast<IntT>(2); @@ -27,7 +27,7 @@ IntT times_power_of_2(IntT number, int exponent) //! Round to `x` the nearest integer. template<class IntT> -IntT round_as_int(double x) +IntT RoundAsInt(double x) { return static_cast<IntT>(std::round(x)); } @@ -35,10 +35,10 @@ IntT round_as_int(double x) // Maximum integer that might be represented with `Nbits` bits. template<class IntT> -IntT max_int(int Nbits) +IntT MaxInt(int Nbits) { constexpr IntT one = static_cast<IntT>(1); - return (times_power_of_2(one, Nbits) - one); + return (TimesPowerOf2(one, Nbits) - one); } @@ -87,7 +87,7 @@ std::ostream& operator<<(std::ostream& out, const PowerOfTwoApprox<IntT>& approx template<class IntT> IntT operator*(IntT coefficient, const PowerOfTwoApprox<IntT>& approx) { - return times_power_of_2(approx.GetNumerator() * coefficient, -approx.GetExponent()); + return TimesPowerOf2(approx.GetNumerator() * coefficient, -approx.GetExponent()); } @@ -103,18 +103,18 @@ IntT operator*(const PowerOfTwoApprox<IntT>& approx, IntT coefficient) //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. template<class IntT> -void helper_compute_power_of_2_approx(double value, int exponent, IntT& numerator, IntT& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, IntT& numerator, IntT& denominator) { constexpr IntT one = static_cast<IntT>(1); - denominator = times_power_of_2(one, exponent); - numerator = round_as_int<IntT>(value * denominator); + denominator = TimesPowerOf2(one, exponent); + numerator = RoundAsInt<IntT>(value * denominator); } template<class IntT> PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { - IntT max_numerator = max_int<IntT>(Nbits); + IntT max_numerator = MaxInt<IntT>(Nbits); auto& numerator = numerator_; int& exponent = exponent_; @@ -127,11 +127,11 @@ PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } @@ -139,7 +139,7 @@ template<class IntT> PowerOfTwoApprox<IntT>::operator double() const { constexpr IntT one = static_cast<IntT>(1); - IntT denominator = times_power_of_2(one, exponent_); + IntT denominator = TimesPowerOf2(one, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -216,10 +216,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - IntT error = round_as_int<IntT>(resolution_ * std::fabs(exact - approx) / exact); + IntT error = RoundAsInt<IntT>(resolution_ * std::fabs(exact - approx) / exact); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int<IntT>(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt<IntT>(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << error << "/" << resolution_ << "]" << std::endl; @@ -397,7 +397,7 @@ void TestDisplayMultiply<IntT>::Display(int Nbits, double value1, IntT coefficie //! Function for error handling. We will see later how to fulfill the same functionality more properly. -[[noreturn]] void error(std::string explanation) +[[noreturn]] void Error(std::string explanation) { std::cout << "ERROR: " << explanation << std::endl; exit(EXIT_FAILURE); @@ -467,7 +467,7 @@ template<std::size_t CapacityN> void TestDisplayContainer<CapacityN>::Register(TestDisplay* test_display) { if (current_position_ >= CapacityN) - error("TestDisplayContainer is already full; impossible to register a new element!"); + Error("TestDisplayContainer is already full; impossible to register a new element!"); list_[current_position_] = test_display; ++current_position_; @@ -478,10 +478,10 @@ template<std::size_t CapacityN> const TestDisplay& TestDisplayContainer<CapacityN>::operator[](std::size_t i) const { if (i >= GetCapacity()) - error("You try to access an element out of bounds!"); + Error("You try to access an element out of bounds!"); if (list_[i] == nullptr) // equivalent to i >= GetSize() - error("You try to access an element that was never initialized!"); + Error("You try to access an element that was never initialized!"); return *list_[i]; } @@ -503,14 +503,14 @@ std::size_t TestDisplayContainer<CapacityN>::GetSize() const //! For each container stored, loop oover all those bits and print the result on screen. template<std::size_t CapacityN> -void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer<CapacityN>& container) +void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer<CapacityN>& container) { for (auto i = 0ul; i < container.GetSize(); ++i) { decltype(auto) current_test_display = container[i]; - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - current_test_display(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + current_test_display(nbits); std::cout << std::endl; } } @@ -533,7 +533,7 @@ int main(int argc, char** argv) container.Register(new TestDisplayPowerOfTwoApprox035<integer_type>(100000000)); container.Register(new TestDisplayMultiply<integer_type>(1000000)); - loop(4, 32, 4, container); + Loop(4, 32, 4, container); return EXIT_SUCCESS; } diff --git a/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice35.cpp b/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice35.cpp index a0826c54b0593281bb09cec1d4eb2c3762ba1f41..7f2696f253c7405e1d268429b65c7c133fd67dc8 100644 --- a/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice35.cpp +++ b/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice35.cpp @@ -6,7 +6,7 @@ //! Returns `number` * (2 ^ `exponent`) template<class IntT> -IntT times_power_of_2(IntT number, int exponent) +IntT TimesPowerOf2(IntT number, int exponent) { constexpr IntT two = static_cast<IntT>(2); @@ -27,7 +27,7 @@ IntT times_power_of_2(IntT number, int exponent) //! Round to `x` the nearest integer. template<class IntT> -IntT round_as_int(double x) +IntT RoundAsInt(double x) { return static_cast<IntT>(std::round(x)); } @@ -35,10 +35,10 @@ IntT round_as_int(double x) // Maximum integer that might be represented with `Nbits` bits. template<class IntT> -IntT max_int(int Nbits) +IntT MaxInt(int Nbits) { constexpr IntT one = static_cast<IntT>(1); - return (times_power_of_2(one, Nbits) - one); + return (TimesPowerOf2(one, Nbits) - one); } @@ -87,7 +87,7 @@ std::ostream& operator<<(std::ostream& out, const PowerOfTwoApprox<IntT>& approx template<class IntT> IntT operator*(IntT coefficient, const PowerOfTwoApprox<IntT>& approx) { - return times_power_of_2(approx.GetNumerator() * coefficient, -approx.GetExponent()); + return TimesPowerOf2(approx.GetNumerator() * coefficient, -approx.GetExponent()); } @@ -103,18 +103,18 @@ IntT operator*(const PowerOfTwoApprox<IntT>& approx, IntT coefficient) //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. template<class IntT> -void helper_compute_power_of_2_approx(double value, int exponent, IntT& numerator, IntT& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, IntT& numerator, IntT& denominator) { constexpr IntT one = static_cast<IntT>(1); - denominator = times_power_of_2(one, exponent); - numerator = round_as_int<IntT>(value * denominator); + denominator = TimesPowerOf2(one, exponent); + numerator = RoundAsInt<IntT>(value * denominator); } template<class IntT> PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { - IntT max_numerator = max_int<IntT>(Nbits); + IntT max_numerator = MaxInt<IntT>(Nbits); auto& numerator = numerator_; int& exponent = exponent_; @@ -127,11 +127,11 @@ PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } @@ -139,7 +139,7 @@ template<class IntT> PowerOfTwoApprox<IntT>::operator double() const { constexpr IntT one = static_cast<IntT>(1); - IntT denominator = times_power_of_2(one, exponent_); + IntT denominator = TimesPowerOf2(one, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -216,10 +216,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - IntT error = round_as_int<IntT>(resolution_ * std::fabs(exact - approx) / exact); + IntT error = RoundAsInt<IntT>(resolution_ * std::fabs(exact - approx) / exact); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int<IntT>(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt<IntT>(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << error << "/" << resolution_ << "]" << std::endl; @@ -525,14 +525,14 @@ std::size_t TestDisplayContainer<CapacityN>::GetSize() const //! For each container stored, loop oover all those bits and print the result on screen. template<std::size_t CapacityN> -void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer<CapacityN>& container) +void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer<CapacityN>& container) { for (auto i = 0ul; i < container.GetSize(); ++i) { decltype(auto) current_test_display = container[i]; - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - current_test_display(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + current_test_display(nbits); std::cout << std::endl; } } @@ -557,7 +557,7 @@ int main(int argc, char** argv) container.Register(new TestDisplayPowerOfTwoApprox035<integer_type>(100000000)); container.Register(new TestDisplayMultiply<integer_type>(1000000)); - loop(4, 32, 4, container); + Loop(4, 32, 4, container); } catch(const std::exception& e) { diff --git a/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice36.cpp b/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice36.cpp index b1cf2680aded8677cc4067184029c13e08c7c3a5..630bd0e515c83989de9366cf6a5a513f1cd2084b 100644 --- a/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice36.cpp +++ b/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice36.cpp @@ -35,7 +35,7 @@ const char* Error::what() const noexcept //! Returns `number` * (2 ^ `exponent`) template<class IntT> -IntT times_power_of_2(IntT number, int exponent) +IntT TimesPowerOf2(IntT number, int exponent) { constexpr IntT two = static_cast<IntT>(2); @@ -61,7 +61,7 @@ IntT times_power_of_2(IntT number, int exponent) //! Round to `x` the nearest integer. template<class IntT> -IntT round_as_int(double x) +IntT RoundAsInt(double x) { return static_cast<IntT>(std::round(x)); } @@ -69,10 +69,10 @@ IntT round_as_int(double x) // Maximum integer that might be represented with `Nbits` bits. template<class IntT> -IntT max_int(int Nbits) +IntT MaxInt(int Nbits) { constexpr IntT one = static_cast<IntT>(1); - return (times_power_of_2(one, Nbits) - one); + return (TimesPowerOf2(one, Nbits) - one); } @@ -126,7 +126,7 @@ IntT operator*(IntT coefficient, const PowerOfTwoApprox<IntT>& approx) if (__builtin_mul_overflow(approx.GetNumerator(), coefficient, &product)) throw Error("Overflow! (in operator*(IntT, const PowerOfTwoApprox<IntT>&))"); - return times_power_of_2(product, -approx.GetExponent()); + return TimesPowerOf2(product, -approx.GetExponent()); } @@ -142,18 +142,18 @@ IntT operator*(const PowerOfTwoApprox<IntT>& approx, IntT coefficient) //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. template<class IntT> -void helper_compute_power_of_2_approx(double value, int exponent, IntT& numerator, IntT& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, IntT& numerator, IntT& denominator) { constexpr IntT one = static_cast<IntT>(1); - denominator = times_power_of_2(one, exponent); - numerator = round_as_int<IntT>(value * denominator); + denominator = TimesPowerOf2(one, exponent); + numerator = RoundAsInt<IntT>(value * denominator); } template<class IntT> PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { - IntT max_numerator = max_int<IntT>(Nbits); + IntT max_numerator = MaxInt<IntT>(Nbits); auto& numerator = numerator_; int& exponent = exponent_; @@ -166,11 +166,11 @@ PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } @@ -178,7 +178,7 @@ template<class IntT> PowerOfTwoApprox<IntT>::operator double() const { constexpr IntT one = static_cast<IntT>(1); - IntT denominator = times_power_of_2(one, exponent_); + IntT denominator = TimesPowerOf2(one, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -259,10 +259,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - IntT error = round_as_int<IntT>(resolution_ * std::fabs(exact - approx) / exact); + IntT error = RoundAsInt<IntT>(resolution_ * std::fabs(exact - approx) / exact); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int<IntT>(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt<IntT>(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << error << "/" << resolution_ << "]" << std::endl; @@ -565,14 +565,14 @@ std::size_t TestDisplayContainer<CapacityN>::GetSize() const //! For each container stored, loop oover all those bits and print the result on screen. template<std::size_t CapacityN> -void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer<CapacityN>& container) +void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer<CapacityN>& container) { for (auto i = 0ul; i < container.GetSize(); ++i) { decltype(auto) current_test_display = container[i]; - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - current_test_display(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + current_test_display(nbits); std::cout << std::endl; } } @@ -597,7 +597,7 @@ int main(int argc, char** argv) container.Register(new TestDisplayPowerOfTwoApprox035<integer_type>(100000000)); container.Register(new TestDisplayMultiply<integer_type>(1000000)); - loop(4, 32, 4, container); + Loop(4, 32, 4, container); } catch(const std::exception& e) { diff --git a/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice37_constexpr.cpp b/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice37_constexpr.cpp index 9c9f830be0513185f4eaf113bf73a11984c2bc1b..c808aec92e994ff6f4dffb0132568488e2fb739f 100644 --- a/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice37_constexpr.cpp +++ b/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice37_constexpr.cpp @@ -36,7 +36,7 @@ const char* Error::what() const noexcept //! Returns `number` * (2 ^ `exponent`) template<class IntT> -IntT times_power_of_2(IntT number, int exponent) +IntT TimesPowerOf2(IntT number, int exponent) { constexpr IntT two = static_cast<IntT>(2); @@ -62,7 +62,7 @@ IntT times_power_of_2(IntT number, int exponent) //! Round to `x` the nearest integer. template<class IntT> -IntT round_as_int(double x) +IntT RoundAsInt(double x) { return static_cast<IntT>(std::round(x)); } @@ -70,10 +70,10 @@ IntT round_as_int(double x) // Maximum integer that might be represented with `Nbits` bits. template<class IntT> -IntT max_int(int Nbits) +IntT MaxInt(int Nbits) { constexpr IntT one = static_cast<IntT>(1); - return (times_power_of_2(one, Nbits) - one); + return (TimesPowerOf2(one, Nbits) - one); } @@ -132,7 +132,7 @@ IntT operator*(IntT coefficient, const PowerOfTwoApprox<IntT>& approx) if (__builtin_mul_overflow(approx.GetNumerator(), coefficient, &product)) throw Error("Overflow! (in operator*(IntT, const PowerOfTwoApprox<IntT>&))"); - return times_power_of_2(product, -approx.GetExponent()); + return TimesPowerOf2(product, -approx.GetExponent()); } @@ -148,18 +148,18 @@ IntT operator*(const PowerOfTwoApprox<IntT>& approx, IntT coefficient) //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. template<class IntT> -void helper_compute_power_of_2_approx(double value, int exponent, IntT& numerator, IntT& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, IntT& numerator, IntT& denominator) { constexpr IntT one = static_cast<IntT>(1); - denominator = times_power_of_2(one, exponent); - numerator = round_as_int<IntT>(value * denominator); + denominator = TimesPowerOf2(one, exponent); + numerator = RoundAsInt<IntT>(value * denominator); } template<class IntT> PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { - IntT max_numerator = max_int<IntT>(Nbits); + IntT max_numerator = MaxInt<IntT>(Nbits); auto& numerator = numerator_; int& exponent = exponent_; @@ -172,11 +172,11 @@ PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } @@ -184,7 +184,7 @@ template<class IntT> PowerOfTwoApprox<IntT>::operator double() const { constexpr IntT one = static_cast<IntT>(1); - IntT denominator = times_power_of_2(one, exponent_); + IntT denominator = TimesPowerOf2(one, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -265,10 +265,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - IntT error = round_as_int<IntT>(resolution_ * std::fabs(exact - approx) / exact); + IntT error = RoundAsInt<IntT>(resolution_ * std::fabs(exact - approx) / exact); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int<IntT>(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt<IntT>(exact) : exact) << " ~ " << approx << optional_string2 << " [error = "; @@ -577,14 +577,14 @@ std::size_t TestDisplayContainer<CapacityN>::GetSize() const //! For each container stored, loop oover all those bits and print the result on screen. template<std::size_t CapacityN> -void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer<CapacityN>& container) +void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer<CapacityN>& container) { for (auto i = 0ul; i < container.GetSize(); ++i) { decltype(auto) current_test_display = container[i]; - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - current_test_display(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + current_test_display(nbits); std::cout << std::endl; } } @@ -609,7 +609,7 @@ int main(int argc, char** argv) container.Register(new TestDisplayPowerOfTwoApprox035<integer_type>(100000000)); container.Register(new TestDisplayMultiply<integer_type>(1000000)); - loop(4, 32, 4, container); + Loop(4, 32, 4, container); } catch(const std::exception& e) { diff --git a/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice37_struct.cpp b/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice37_struct.cpp index 37e614a431afa672c209fc130b73cc133ba6ce30..acaa788f71d60914abebd56d1db0841baf8517a3 100644 --- a/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice37_struct.cpp +++ b/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice37_struct.cpp @@ -70,7 +70,7 @@ const char* Error::what() const noexcept //! Returns `number` * (2 ^ `exponent`) template<class IntT> -IntT times_power_of_2(IntT number, int exponent) +IntT TimesPowerOf2(IntT number, int exponent) { constexpr IntT two = static_cast<IntT>(2); @@ -96,7 +96,7 @@ IntT times_power_of_2(IntT number, int exponent) //! Round to `x` the nearest integer. template<class IntT> -IntT round_as_int(double x) +IntT RoundAsInt(double x) { return static_cast<IntT>(std::round(x)); } @@ -104,10 +104,10 @@ IntT round_as_int(double x) // Maximum integer that might be represented with `Nbits` bits. template<class IntT> -IntT max_int(int Nbits) +IntT MaxInt(int Nbits) { constexpr IntT one = static_cast<IntT>(1); - return (times_power_of_2(one, Nbits) - one); + return (TimesPowerOf2(one, Nbits) - one); } @@ -161,7 +161,7 @@ IntT operator*(IntT coefficient, const PowerOfTwoApprox<IntT>& approx) if (__builtin_mul_overflow(approx.GetNumerator(), coefficient, &product)) throw Error("Overflow! (in operator*(IntT, const PowerOfTwoApprox<IntT>&))"); - return times_power_of_2(product, -approx.GetExponent()); + return TimesPowerOf2(product, -approx.GetExponent()); } @@ -177,18 +177,18 @@ IntT operator*(const PowerOfTwoApprox<IntT>& approx, IntT coefficient) //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. template<class IntT> -void helper_compute_power_of_2_approx(double value, int exponent, IntT& numerator, IntT& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, IntT& numerator, IntT& denominator) { constexpr IntT one = static_cast<IntT>(1); - denominator = times_power_of_2(one, exponent); - numerator = round_as_int<IntT>(value * denominator); + denominator = TimesPowerOf2(one, exponent); + numerator = RoundAsInt<IntT>(value * denominator); } template<class IntT> PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { - IntT max_numerator = max_int<IntT>(Nbits); + IntT max_numerator = MaxInt<IntT>(Nbits); auto& numerator = numerator_; int& exponent = exponent_; @@ -201,11 +201,11 @@ PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } @@ -213,7 +213,7 @@ template<class IntT> PowerOfTwoApprox<IntT>::operator double() const { constexpr IntT one = static_cast<IntT>(1); - IntT denominator = times_power_of_2(one, exponent_); + IntT denominator = TimesPowerOf2(one, exponent_); return static_cast<double>(numerator_) / denominator; } @@ -294,10 +294,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - IntT error = round_as_int<IntT>(resolution_ * std::fabs(exact - approx) / exact); + IntT error = RoundAsInt<IntT>(resolution_ * std::fabs(exact - approx) / exact); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int<IntT>(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt<IntT>(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << PrintInt(error) << "/" << resolution_ << "]" << std::endl; @@ -600,14 +600,14 @@ std::size_t TestDisplayContainer<CapacityN>::GetSize() const //! For each container stored, loop oover all those bits and print the result on screen. template<std::size_t CapacityN> -void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer<CapacityN>& container) +void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer<CapacityN>& container) { for (auto i = 0ul; i < container.GetSize(); ++i) { decltype(auto) current_test_display = container[i]; - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - current_test_display(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + current_test_display(nbits); std::cout << std::endl; } } @@ -632,7 +632,7 @@ int main(int argc, char** argv) container.Register(new TestDisplayPowerOfTwoApprox035<integer_type>(100000000)); container.Register(new TestDisplayMultiply<integer_type>(1000000)); - loop(4, 32, 4, container); + Loop(4, 32, 4, container); } catch(const std::exception& e) { diff --git a/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice38.cpp b/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice38.cpp index 63a83776b33ce6931f6f9d9801b02db8c254e8ce..944e26d6fdfe8e8874ae878376618d818fa7cb07 100644 --- a/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice38.cpp +++ b/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice38.cpp @@ -71,7 +71,7 @@ const char* Error::what() const noexcept //! Returns `number` * (2 ^ `exponent`) template<class IntT> -IntT times_power_of_2(IntT number, int exponent) +IntT TimesPowerOf2(IntT number, int exponent) { constexpr IntT two = static_cast<IntT>(2); @@ -100,7 +100,7 @@ IntT times_power_of_2(IntT number, int exponent) //! Round to `x` the nearest integer. template<class IntT> -IntT round_as_int(double x) +IntT RoundAsInt(double x) { return static_cast<IntT>(std::round(x)); } @@ -108,10 +108,10 @@ IntT round_as_int(double x) // Maximum integer that might be represented with `Nbits` bits. template<class IntT> -IntT max_int(int Nbits) +IntT MaxInt(int Nbits) { constexpr IntT one = static_cast<IntT>(1); - return (times_power_of_2(one, Nbits) - one); + return (TimesPowerOf2(one, Nbits) - one); } @@ -172,7 +172,7 @@ IntT operator*(IntT coefficient, const PowerOfTwoApprox<IntT>& approx) if (__builtin_mul_overflow(approx.GetNumerator(), coefficient, &product)) throw Error("Overflow! (in operator*(IntT, const PowerOfTwoApprox<IntT>&))"); - return approx.GetSignAsNumber() * times_power_of_2(product, -approx.GetExponent()); + return approx.GetSignAsNumber() * TimesPowerOf2(product, -approx.GetExponent()); } @@ -188,18 +188,18 @@ IntT operator*(const PowerOfTwoApprox<IntT>& approx, IntT coefficient) //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. template<class IntT> -void helper_compute_power_of_2_approx(double value, int exponent, IntT& numerator, IntT& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, IntT& numerator, IntT& denominator) { constexpr IntT one = static_cast<IntT>(1); - denominator = times_power_of_2(one, exponent); - numerator = round_as_int<IntT>(value * denominator); + denominator = TimesPowerOf2(one, exponent); + numerator = RoundAsInt<IntT>(value * denominator); } template<class IntT> PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { - IntT max_numerator = max_int<IntT>(Nbits); + IntT max_numerator = MaxInt<IntT>(Nbits); auto& numerator = numerator_; int& exponent = exponent_; @@ -218,11 +218,11 @@ PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } @@ -230,7 +230,7 @@ template<class IntT> PowerOfTwoApprox<IntT>::operator double() const { constexpr IntT one = static_cast<IntT>(1); - IntT denominator = times_power_of_2(one, exponent_); + IntT denominator = TimesPowerOf2(one, exponent_); return static_cast<double>(numerator_) * sign_ / denominator; } @@ -331,10 +331,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - IntT error = round_as_int<IntT>(resolution_ * std::fabs(exact - approx) / std::fabs(exact)); + IntT error = RoundAsInt<IntT>(resolution_ * std::fabs(exact - approx) / std::fabs(exact)); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int<IntT>(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt<IntT>(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << PrintInt(error) << "/" << resolution_ << "]" << std::endl; @@ -637,14 +637,14 @@ std::size_t TestDisplayContainer<CapacityN>::GetSize() const //! For each container stored, loop oover all those bits and print the result on screen. template<std::size_t CapacityN> -void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer<CapacityN>& container) +void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer<CapacityN>& container) { for (auto i = 0ul; i < container.GetSize(); ++i) { decltype(auto) current_test_display = container[i]; - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - current_test_display(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + current_test_display(nbits); std::cout << std::endl; } } @@ -669,7 +669,7 @@ int main(int argc, char** argv) container.Register(new TestDisplayPowerOfTwoApprox035<integer_type>(100000000)); container.Register(new TestDisplayMultiply<integer_type>(1000000)); - loop(4, 32, 4, container); + Loop(4, 32, 4, container); } catch(const std::exception& e) { diff --git a/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice39.cpp b/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice39.cpp index 6106e52ec189ef75ac1aebc1e840ac715a14eccd..f9e7cd7b5ac691ce279978b9b2e09594f4655057 100644 --- a/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice39.cpp +++ b/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice39.cpp @@ -72,7 +72,7 @@ const char* Error::what() const noexcept //! Returns `number` * (2 ^ `exponent`) template<class IntT> -IntT times_power_of_2(IntT number, int exponent) +IntT TimesPowerOf2(IntT number, int exponent) { constexpr IntT two = static_cast<IntT>(2); @@ -101,7 +101,7 @@ IntT times_power_of_2(IntT number, int exponent) //! Round to `x` the nearest integer. template<class IntT> -IntT round_as_int(double x) +IntT RoundAsInt(double x) { return static_cast<IntT>(std::round(x)); } @@ -109,10 +109,10 @@ IntT round_as_int(double x) // Maximum integer that might be represented with `Nbits` bits. template<class IntT> -IntT max_int(int Nbits) +IntT MaxInt(int Nbits) { constexpr IntT one = static_cast<IntT>(1); - return (times_power_of_2(one, Nbits) - one); + return (TimesPowerOf2(one, Nbits) - one); } @@ -173,7 +173,7 @@ IntT operator*(IntT coefficient, const PowerOfTwoApprox<IntT>& approx) if (__builtin_mul_overflow(approx.GetNumerator(), coefficient, &product)) throw Error("Overflow! (in operator*(IntT, const PowerOfTwoApprox<IntT>&))"); - return approx.GetSignAsNumber() * times_power_of_2(product, -approx.GetExponent()); + return approx.GetSignAsNumber() * TimesPowerOf2(product, -approx.GetExponent()); } @@ -189,18 +189,18 @@ IntT operator*(const PowerOfTwoApprox<IntT>& approx, IntT coefficient) //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. template<class IntT> -void helper_compute_power_of_2_approx(double value, int exponent, IntT& numerator, IntT& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, IntT& numerator, IntT& denominator) { constexpr IntT one = static_cast<IntT>(1); - denominator = times_power_of_2(one, exponent); - numerator = round_as_int<IntT>(value * denominator); + denominator = TimesPowerOf2(one, exponent); + numerator = RoundAsInt<IntT>(value * denominator); } template<class IntT> PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { - IntT max_numerator = max_int<IntT>(Nbits); + IntT max_numerator = MaxInt<IntT>(Nbits); auto& numerator = numerator_; int& exponent = exponent_; @@ -219,11 +219,11 @@ PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } @@ -231,7 +231,7 @@ template<class IntT> PowerOfTwoApprox<IntT>::operator double() const { constexpr IntT one = static_cast<IntT>(1); - IntT denominator = times_power_of_2(one, exponent_); + IntT denominator = TimesPowerOf2(one, exponent_); return static_cast<double>(numerator_) * sign_ / denominator; } @@ -332,10 +332,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - IntT error = round_as_int<IntT>(resolution_ * std::fabs(exact - approx) / std::fabs(exact)); + IntT error = RoundAsInt<IntT>(resolution_ * std::fabs(exact - approx) / std::fabs(exact)); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int<IntT>(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt<IntT>(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << PrintInt(error) << "/" << resolution_ << "]" << std::endl; @@ -607,14 +607,14 @@ std::size_t TestDisplayContainer::GetSize() const //! For each container stored, loop oover all those bits and print the result on screen. -void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) +void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) { for (auto i = 0ul; i < container.GetSize(); ++i) { decltype(auto) current_test_display = container[i]; - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - current_test_display(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + current_test_display(nbits); std::cout << std::endl; } } @@ -639,7 +639,7 @@ int main(int argc, char** argv) container.Register(new TestDisplayPowerOfTwoApprox035<integer_type>(100000000)); container.Register(new TestDisplayMultiply<integer_type>(1000000)); - loop(4, 32, 4, container); + Loop(4, 32, 4, container); } catch(const std::exception& e) { diff --git a/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice40.cpp b/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice40.cpp index 22a7013b1c1d8bbb70872b43f3cfc5158143ee86..e538579274753569ac7c8b19b81b8a082803d292 100644 --- a/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice40.cpp +++ b/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice40.cpp @@ -73,7 +73,7 @@ const char* Error::what() const noexcept //! Returns `number` * (2 ^ `exponent`) template<class IntT> -IntT times_power_of_2(IntT number, int exponent) +IntT TimesPowerOf2(IntT number, int exponent) { constexpr IntT two = static_cast<IntT>(2); @@ -102,7 +102,7 @@ IntT times_power_of_2(IntT number, int exponent) //! Round to `x` the nearest integer. template<class IntT> -IntT round_as_int(double x) +IntT RoundAsInt(double x) { return static_cast<IntT>(std::round(x)); } @@ -110,10 +110,10 @@ IntT round_as_int(double x) // Maximum integer that might be represented with `Nbits` bits. template<class IntT> -IntT max_int(int Nbits) +IntT MaxInt(int Nbits) { constexpr IntT one = static_cast<IntT>(1); - return (times_power_of_2(one, Nbits) - one); + return (TimesPowerOf2(one, Nbits) - one); } @@ -174,7 +174,7 @@ IntT operator*(IntT coefficient, const PowerOfTwoApprox<IntT>& approx) if (__builtin_mul_overflow(approx.GetNumerator(), coefficient, &product)) throw Error("Overflow! (in operator*(IntT, const PowerOfTwoApprox<IntT>&))"); - return approx.GetSignAsNumber() * times_power_of_2(product, -approx.GetExponent()); + return approx.GetSignAsNumber() * TimesPowerOf2(product, -approx.GetExponent()); } @@ -190,18 +190,18 @@ IntT operator*(const PowerOfTwoApprox<IntT>& approx, IntT coefficient) //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. template<class IntT> -void helper_compute_power_of_2_approx(double value, int exponent, IntT& numerator, IntT& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, IntT& numerator, IntT& denominator) { constexpr IntT one = static_cast<IntT>(1); - denominator = times_power_of_2(one, exponent); - numerator = round_as_int<IntT>(value * denominator); + denominator = TimesPowerOf2(one, exponent); + numerator = RoundAsInt<IntT>(value * denominator); } template<class IntT> PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { - IntT max_numerator = max_int<IntT>(Nbits); + IntT max_numerator = MaxInt<IntT>(Nbits); auto& numerator = numerator_; int& exponent = exponent_; @@ -220,11 +220,11 @@ PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } @@ -232,7 +232,7 @@ template<class IntT> PowerOfTwoApprox<IntT>::operator double() const { constexpr IntT one = static_cast<IntT>(1); - IntT denominator = times_power_of_2(one, exponent_); + IntT denominator = TimesPowerOf2(one, exponent_); return static_cast<double>(numerator_) * sign_ / denominator; } @@ -333,10 +333,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - IntT error = round_as_int<IntT>(resolution_ * std::fabs(exact - approx) / std::fabs(exact)); + IntT error = RoundAsInt<IntT>(resolution_ * std::fabs(exact - approx) / std::fabs(exact)); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int<IntT>(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt<IntT>(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << PrintInt(error) << "/" << resolution_ << "]" << std::endl; @@ -597,14 +597,14 @@ std::size_t TestDisplayContainer::GetSize() const //! For each container stored, loop oover all those bits and print the result on screen. -void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) +void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) { for (auto i = 0ul; i < container.GetSize(); ++i) { decltype(auto) current_test_display = container[i]; - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - current_test_display(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + current_test_display(nbits); std::cout << std::endl; } } @@ -629,7 +629,7 @@ int main(int argc, char** argv) container.Register(std::make_shared<TestDisplayPowerOfTwoApprox035<integer_type>>(100000000)); container.Register(std::make_shared<TestDisplayMultiply<integer_type>>(1000000)); - loop(4, 32, 4, container); + Loop(4, 32, 4, container); } catch(const std::exception& e) { diff --git a/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice41.cpp b/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice41.cpp index 564aa8bf2c9e479cb6e33acfcfa493a7fa8d11c3..7686d75b7ce3b0a9f8ca66227943176edb083f0e 100644 --- a/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice41.cpp +++ b/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice41.cpp @@ -73,7 +73,7 @@ const char* Error::what() const noexcept //! Returns `number` * (2 ^ `exponent`) template<class IntT> -IntT times_power_of_2(IntT number, int exponent) +IntT TimesPowerOf2(IntT number, int exponent) { constexpr IntT two = static_cast<IntT>(2); @@ -102,7 +102,7 @@ IntT times_power_of_2(IntT number, int exponent) //! Round to `x` the nearest integer. template<class IntT> -IntT round_as_int(double x) +IntT RoundAsInt(double x) { return static_cast<IntT>(std::round(x)); } @@ -110,10 +110,10 @@ IntT round_as_int(double x) // Maximum integer that might be represented with `Nbits` bits. template<class IntT> -IntT max_int(int Nbits) +IntT MaxInt(int Nbits) { constexpr IntT one = static_cast<IntT>(1); - return (times_power_of_2(one, Nbits) - one); + return (TimesPowerOf2(one, Nbits) - one); } @@ -174,7 +174,7 @@ IntT operator*(IntT coefficient, const PowerOfTwoApprox<IntT>& approx) if (__builtin_mul_overflow(approx.GetNumerator(), coefficient, &product)) throw Error("Overflow! (in operator*(IntT, const PowerOfTwoApprox<IntT>&))"); - return approx.GetSignAsNumber() * times_power_of_2(product, -approx.GetExponent()); + return approx.GetSignAsNumber() * TimesPowerOf2(product, -approx.GetExponent()); } @@ -190,18 +190,18 @@ IntT operator*(const PowerOfTwoApprox<IntT>& approx, IntT coefficient) //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. template<class IntT> -void helper_compute_power_of_2_approx(double value, int exponent, IntT& numerator, IntT& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, IntT& numerator, IntT& denominator) { constexpr IntT one = static_cast<IntT>(1); - denominator = times_power_of_2(one, exponent); - numerator = round_as_int<IntT>(value * denominator); + denominator = TimesPowerOf2(one, exponent); + numerator = RoundAsInt<IntT>(value * denominator); } template<class IntT> PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { - IntT max_numerator = max_int<IntT>(Nbits); + IntT max_numerator = MaxInt<IntT>(Nbits); auto& numerator = numerator_; int& exponent = exponent_; @@ -220,11 +220,11 @@ PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } @@ -232,7 +232,7 @@ template<class IntT> PowerOfTwoApprox<IntT>::operator double() const { constexpr IntT one = static_cast<IntT>(1); - IntT denominator = times_power_of_2(one, exponent_); + IntT denominator = TimesPowerOf2(one, exponent_); return static_cast<double>(numerator_) * sign_ / denominator; } @@ -333,10 +333,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - IntT error = round_as_int<IntT>(resolution_ * std::fabs(exact - approx) / std::fabs(exact)); + IntT error = RoundAsInt<IntT>(resolution_ * std::fabs(exact - approx) / std::fabs(exact)); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int<IntT>(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt<IntT>(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << PrintInt(error) << "/" << resolution_ << "]" << std::endl; @@ -597,14 +597,14 @@ std::size_t TestDisplayContainer::GetSize() const //! For each container stored, loop oover all those bits and print the result on screen. -void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) +void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const TestDisplayContainer& container) { for (auto i = 0ul; i < container.GetSize(); ++i) { decltype(auto) current_test_display = container[i]; - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - current_test_display(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + current_test_display(nbits); std::cout << std::endl; } } @@ -629,7 +629,7 @@ int main(int argc, char** argv) container.Register(std::make_unique<TestDisplayPowerOfTwoApprox035<integer_type>>(100000000)); container.Register(std::make_unique<TestDisplayMultiply<integer_type>>(1000000)); - loop(4, 32, 4, container); + Loop(4, 32, 4, container); } catch(const std::exception& e) { diff --git a/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice42.cpp b/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice42.cpp index d57b9dd6711df09a014ee20339a142a352377aa6..759af4a4ac8cf21c78cc2e3d5278e2a1bbc46f86 100644 --- a/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice42.cpp +++ b/HandsOn/5-UsefulConceptsAndSTL/Solution/exercice42.cpp @@ -73,7 +73,7 @@ const char* Error::what() const noexcept //! Returns `number` * (2 ^ `exponent`) template<class IntT> -IntT times_power_of_2(IntT number, int exponent) +IntT TimesPowerOf2(IntT number, int exponent) { constexpr IntT two = static_cast<IntT>(2); @@ -102,7 +102,7 @@ IntT times_power_of_2(IntT number, int exponent) //! Round to `x` the nearest integer. template<class IntT> -IntT round_as_int(double x) +IntT RoundAsInt(double x) { return static_cast<IntT>(std::round(x)); } @@ -110,10 +110,10 @@ IntT round_as_int(double x) // Maximum integer that might be represented with `Nbits` bits. template<class IntT> -IntT max_int(int Nbits) +IntT MaxInt(int Nbits) { constexpr IntT one = static_cast<IntT>(1); - return (times_power_of_2(one, Nbits) - one); + return (TimesPowerOf2(one, Nbits) - one); } @@ -174,7 +174,7 @@ IntT operator*(IntT coefficient, const PowerOfTwoApprox<IntT>& approx) if (__builtin_mul_overflow(approx.GetNumerator(), coefficient, &product)) throw Error("Overflow! (in operator*(IntT, const PowerOfTwoApprox<IntT>&))"); - return approx.GetSignAsNumber() * times_power_of_2(product, -approx.GetExponent()); + return approx.GetSignAsNumber() * TimesPowerOf2(product, -approx.GetExponent()); } @@ -190,18 +190,18 @@ IntT operator*(const PowerOfTwoApprox<IntT>& approx, IntT coefficient) //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. template<class IntT> -void helper_compute_power_of_2_approx(double value, int exponent, IntT& numerator, IntT& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, IntT& numerator, IntT& denominator) { constexpr IntT one = static_cast<IntT>(1); - denominator = times_power_of_2(one, exponent); - numerator = round_as_int<IntT>(value * denominator); + denominator = TimesPowerOf2(one, exponent); + numerator = RoundAsInt<IntT>(value * denominator); } template<class IntT> PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { - IntT max_numerator = max_int<IntT>(Nbits); + IntT max_numerator = MaxInt<IntT>(Nbits); auto& numerator = numerator_; int& exponent = exponent_; @@ -220,11 +220,11 @@ PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } @@ -232,7 +232,7 @@ template<class IntT> PowerOfTwoApprox<IntT>::operator double() const { constexpr IntT one = static_cast<IntT>(1); - IntT denominator = times_power_of_2(one, exponent_); + IntT denominator = TimesPowerOf2(one, exponent_); return static_cast<double>(numerator_) * sign_ / denominator; } @@ -333,10 +333,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - IntT error = round_as_int<IntT>(resolution_ * std::fabs(exact - approx) / std::fabs(exact)); + IntT error = RoundAsInt<IntT>(resolution_ * std::fabs(exact - approx) / std::fabs(exact)); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int<IntT>(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt<IntT>(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << PrintInt(error) << "/" << resolution_ << "]" << std::endl; @@ -542,15 +542,15 @@ void TestDisplayMultiply<IntT>::Display(int Nbits, double value1, IntT coefficie //! For each container stored, loop oover all those bits and print the result on screen. -void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const std::vector<std::unique_ptr<TestDisplay>>& container) +void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const std::vector<std::unique_ptr<TestDisplay>>& container) { for (const auto& ptr : container) { assert(ptr != nullptr); const auto& current_test_display = *ptr; - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - current_test_display(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + current_test_display(nbits); std::cout << std::endl; } @@ -576,7 +576,7 @@ int main(int argc, char** argv) container.emplace_back(std::make_unique<TestDisplayPowerOfTwoApprox035<integer_type>>(100000000)); container.emplace_back(std::make_unique<TestDisplayMultiply<integer_type>>(1000000)); - loop(4, 32, 4, container); + Loop(4, 32, 4, container); } catch(const std::exception& e) { diff --git a/HandsOn/6-InRealEnvironment/Solution/Exercice43/CMakeLists.txt b/HandsOn/6-InRealEnvironment/Solution/Exercice43/CMakeLists.txt index df2fc32ae7be99605202ccb8621c9e8012c70d4e..77f055f4c9fdc72b46d0994d38baf06db3e6e061 100644 --- a/HandsOn/6-InRealEnvironment/Solution/Exercice43/CMakeLists.txt +++ b/HandsOn/6-InRealEnvironment/Solution/Exercice43/CMakeLists.txt @@ -1,3 +1,5 @@ +project(GettingStartedWithModernCpp_HandsOn_InRealEnvironment) + add_library(exercice43_lib SHARED # Header files are not strictly necessary but may be useful for some CMake generators. diff --git a/HandsOn/6-InRealEnvironment/Solution/Exercice43/PowerOfTwoApprox.hpp b/HandsOn/6-InRealEnvironment/Solution/Exercice43/PowerOfTwoApprox.hpp index 4fee079db55dc39d233ddb0e592232d166f627e6..c63624d70164c8b2298d51f6bf111f878a4589be 100644 --- a/HandsOn/6-InRealEnvironment/Solution/Exercice43/PowerOfTwoApprox.hpp +++ b/HandsOn/6-InRealEnvironment/Solution/Exercice43/PowerOfTwoApprox.hpp @@ -52,7 +52,7 @@ std::ostream& operator<<(std::ostream& out, const PowerOfTwoApprox<IntT>& approx //! Note: could be put in the struct... but may be kept a free function as well! We will see much later //! the anonymous namespace, in which I would typically put such a free function. template<class IntT> -void helper_compute_power_of_2_approx(double value, int exponent, IntT& numerator, IntT& denominator); +void HelperComputePowerOf2Approx(double value, int exponent, IntT& numerator, IntT& denominator); diff --git a/HandsOn/6-InRealEnvironment/Solution/Exercice43/PowerOfTwoApprox.hxx b/HandsOn/6-InRealEnvironment/Solution/Exercice43/PowerOfTwoApprox.hxx index 06dc0a5978f7bf6a2436b7498657e310e368dc76..6203b520ddce5427bf2323afe7d41fb83c3b6397 100644 --- a/HandsOn/6-InRealEnvironment/Solution/Exercice43/PowerOfTwoApprox.hxx +++ b/HandsOn/6-InRealEnvironment/Solution/Exercice43/PowerOfTwoApprox.hxx @@ -1,17 +1,17 @@ template<class IntT> -void helper_compute_power_of_2_approx(double value, int exponent, IntT& numerator, IntT& denominator) +void HelperComputePowerOf2Approx(double value, int exponent, IntT& numerator, IntT& denominator) { constexpr IntT one = static_cast<IntT>(1); - denominator = times_power_of_2(one, exponent); - numerator = round_as_int<IntT>(value * denominator); + denominator = TimesPowerOf2(one, exponent); + numerator = RoundAsInt<IntT>(value * denominator); } template<class IntT> PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { - IntT max_numerator = max_int<IntT>(Nbits); + IntT max_numerator = MaxInt<IntT>(Nbits); auto& numerator = numerator_; int& exponent = exponent_; @@ -30,11 +30,11 @@ PowerOfTwoApprox<IntT>::PowerOfTwoApprox(int Nbits, double value) { // I used here the preffix increment '++exponent' but you may put it on a separate line if you're not // comfortable with it. - helper_compute_power_of_2_approx(value, ++exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, ++exponent, numerator, denominator); } while (numerator <= max_numerator); - helper_compute_power_of_2_approx(value, --exponent, numerator, denominator); + HelperComputePowerOf2Approx(value, --exponent, numerator, denominator); } @@ -42,7 +42,7 @@ template<class IntT> PowerOfTwoApprox<IntT>::operator double() const { constexpr IntT one = static_cast<IntT>(1); - IntT denominator = times_power_of_2(one, exponent_); + IntT denominator = TimesPowerOf2(one, exponent_); return static_cast<double>(numerator_) * sign_ / denominator; } @@ -97,7 +97,7 @@ IntT operator*(IntT coefficient, const PowerOfTwoApprox<IntT>& approx) if (__builtin_mul_overflow(approx.GetNumerator(), coefficient, &product)) throw Error("Overflow! (in operator*(IntT, const PowerOfTwoApprox<IntT>&))"); - return approx.GetSignAsNumber() * times_power_of_2(product, -approx.GetExponent()); + return approx.GetSignAsNumber() * TimesPowerOf2(product, -approx.GetExponent()); } diff --git a/HandsOn/6-InRealEnvironment/Solution/Exercice43/TestDisplay.hxx b/HandsOn/6-InRealEnvironment/Solution/Exercice43/TestDisplay.hxx index 2b0a67096463937ee5b2fde4da899b03340a6e4d..00f871122df4fedd395ff91bdbdf58e9fd114b09 100644 --- a/HandsOn/6-InRealEnvironment/Solution/Exercice43/TestDisplay.hxx +++ b/HandsOn/6-InRealEnvironment/Solution/Exercice43/TestDisplay.hxx @@ -5,10 +5,10 @@ void TestDisplay::PrintLine(int Nbits, double exact, double approx, std::string optional_string1, std::string optional_string2, RoundToInteger do_round_to_integer) const { - IntT error = round_as_int<IntT>(resolution_ * std::fabs(exact - approx) / std::fabs(exact)); + IntT error = RoundAsInt<IntT>(resolution_ * std::fabs(exact - approx) / std::fabs(exact)); std::cout << "[With " << Nbits << " bits]: " << optional_string1 - << (do_round_to_integer == RoundToInteger::yes ? round_as_int<IntT>(exact) : exact) << " ~ " << approx + << (do_round_to_integer == RoundToInteger::yes ? RoundAsInt<IntT>(exact) : exact) << " ~ " << approx << optional_string2 << " [error = " << PrintInt(error) << "/" << resolution_ << "]" << std::endl; diff --git a/HandsOn/6-InRealEnvironment/Solution/Exercice43/Tools.hpp b/HandsOn/6-InRealEnvironment/Solution/Exercice43/Tools.hpp index 6f7aea5f3d616f5465c59efd201b1fefaf058e53..0cbf8b033e31b5276be92349d931f8256cdcb5b3 100644 --- a/HandsOn/6-InRealEnvironment/Solution/Exercice43/Tools.hpp +++ b/HandsOn/6-InRealEnvironment/Solution/Exercice43/Tools.hpp @@ -42,17 +42,17 @@ auto PrintInt(T value); //! Returns `number` * (2 ^ `exponent`) template<class IntT> -IntT times_power_of_2(IntT number, int exponent); +IntT TimesPowerOf2(IntT number, int exponent); //! Round to `x` the nearest integer. template<class IntT> -IntT round_as_int(double x); +IntT RoundAsInt(double x); // Maximum integer that might be represented with `Nbits` bits. template<class IntT> -IntT max_int(int Nbits); +IntT MaxInt(int Nbits); #include "Tools.hxx" diff --git a/HandsOn/6-InRealEnvironment/Solution/Exercice43/Tools.hxx b/HandsOn/6-InRealEnvironment/Solution/Exercice43/Tools.hxx index d7425f4d9bd21e1f8dd68182fa0a631ff041d385..bf0f258f86282024a7f16f53fba033f58a09633f 100644 --- a/HandsOn/6-InRealEnvironment/Solution/Exercice43/Tools.hxx +++ b/HandsOn/6-InRealEnvironment/Solution/Exercice43/Tools.hxx @@ -14,7 +14,7 @@ inline auto PrintInt(T value) //! Returns `number` * (2 ^ `exponent`) template<class IntT> -IntT times_power_of_2(IntT number, int exponent) +IntT TimesPowerOf2(IntT number, int exponent) { constexpr IntT two = static_cast<IntT>(2); @@ -25,7 +25,7 @@ IntT times_power_of_2(IntT number, int exponent) if (__builtin_mul_overflow(number, two, &product)) { std::cout << "OVERFLOW for number " << number << std::endl; - throw Error("Overflow! (in times_power_of_2())"); + throw Error("Overflow! (in TimesPowerOf2())"); } number = product; @@ -42,15 +42,15 @@ IntT times_power_of_2(IntT number, int exponent) template<class IntT> -inline IntT round_as_int(double x) +inline IntT RoundAsInt(double x) { return static_cast<IntT>(std::round(x)); } template<class IntT> -inline IntT max_int(int Nbits) +inline IntT MaxInt(int Nbits) { constexpr IntT one = static_cast<IntT>(1); - return (times_power_of_2(one, Nbits) - one); + return (TimesPowerOf2(one, Nbits) - one); } diff --git a/HandsOn/6-InRealEnvironment/Solution/Exercice43/main.cpp b/HandsOn/6-InRealEnvironment/Solution/Exercice43/main.cpp index c966f3ccc51d304744b3daf038389ba2022cba43..abdf6cfe959cc60e676ee9f42b5e46e9490e9e06 100644 --- a/HandsOn/6-InRealEnvironment/Solution/Exercice43/main.cpp +++ b/HandsOn/6-InRealEnvironment/Solution/Exercice43/main.cpp @@ -9,15 +9,15 @@ //! For each container stored, loop oover all those bits and print the result on screen. -void loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const std::vector<std::unique_ptr<TestDisplay>>& container) +void Loop(int initial_Nbit, int final_Nbit, int increment_Nbit, const std::vector<std::unique_ptr<TestDisplay>>& container) { for (const auto& ptr : container) { assert(ptr != nullptr); const auto& current_test_display = *ptr; - for (int Nbits = initial_Nbit; Nbits <= final_Nbit; Nbits += increment_Nbit) - current_test_display(Nbits); + for (int nbits = initial_Nbit; nbits <= final_Nbit; nbits += increment_Nbit) + current_test_display(nbits); std::cout << std::endl; } @@ -43,7 +43,7 @@ int main(int argc, char** argv) container.emplace_back(std::make_unique<TestDisplayPowerOfTwoApprox035<integer_type>>(100000000)); container.emplace_back(std::make_unique<TestDisplayMultiply<integer_type>>(1000000)); - loop(4, 32, 4, container); + Loop(4, 32, 4, container); } catch(const std::exception& e) { diff --git a/HandsOn/Config/.clang-format b/HandsOn/Config/.clang-format new file mode 100644 index 0000000000000000000000000000000000000000..092ac992b67b361bed3be20cf2b08780eeab261c --- /dev/null +++ b/HandsOn/Config/.clang-format @@ -0,0 +1,17 @@ +BasedOnStyle: None +AlignTrailingComments: true +BreakBeforeBraces: Allman +IndentWidth: 4 +KeepEmptyLinesAtTheStartOfBlocks: false +ObjCSpaceAfterProperty: true +ObjCSpaceBeforeProtocolList: true +PointerBindsToType: false +SpacesBeforeTrailingComments: 1 +TabWidth: 8 +UseTab: Never + +# Settings for C/C++ +Language: Cpp +# 120 chars per line, reflow comments so they stay within limits +ColumnLimit: '120' +ReflowComments: 'true' diff --git a/HandsOn/Config/.clang-tidy b/HandsOn/Config/.clang-tidy new file mode 100644 index 0000000000000000000000000000000000000000..406452859f0e5d60f448480a44bf4ab1efea5177 --- /dev/null +++ b/HandsOn/Config/.clang-tidy @@ -0,0 +1,10 @@ +Checks: '-*,readability-identifier-naming' +CheckOptions: + - { key: readability-identifier-naming.NamespaceCase, value: CamelCase } + - { key: readability-identifier-naming.ClassCase, value: CamelCase } + - { key: readability-identifier-naming.PrivateMemberSuffix, value: _ } + - { key: readability-identifier-naming.StructCase, value: CamelCase } + - { key: readability-identifier-naming.FunctionCase, value: CamelCase } + - { key: readability-identifier-naming.VariableCase, value: lower_case } + - { key: readability-identifier-naming.GlobalConstantCase, value: UPPER_CASE } + - { key: readability-identifier-naming.TemplateParameterCase, value: CamelCase }