From 9f9f8e2286a6d8ace018113d71cc5dab05cf87d4 Mon Sep 17 00:00:00 2001 From: vaeng <34183939+vaeng@users.noreply.github.com> Date: Wed, 26 Apr 2023 15:22:50 +0200 Subject: [PATCH 01/16] First draft --- concepts/numbers/.meta/config.json | 8 +++++ concepts/numbers/about.md | 29 ++++++++++++++++ concepts/numbers/introduction.md | 53 ++++++++++++++++++++++++++++++ concepts/numbers/links.json | 14 ++++++++ config.json | 5 +++ 5 files changed, 109 insertions(+) create mode 100644 concepts/numbers/.meta/config.json create mode 100644 concepts/numbers/about.md create mode 100644 concepts/numbers/introduction.md create mode 100644 concepts/numbers/links.json diff --git a/concepts/numbers/.meta/config.json b/concepts/numbers/.meta/config.json new file mode 100644 index 00000000..fb82360d --- /dev/null +++ b/concepts/numbers/.meta/config.json @@ -0,0 +1,8 @@ +{ + "blurb": "C++ has different types for numbers. Common are int for integers and double for decimals.", + "authors": [ + "vaeng" + ], + "contributors": [ + ] +} \ No newline at end of file diff --git a/concepts/numbers/about.md b/concepts/numbers/about.md new file mode 100644 index 00000000..479fd840 --- /dev/null +++ b/concepts/numbers/about.md @@ -0,0 +1,29 @@ +# About + +TODO: Include text from introduction.md when final. + + +~~~~exercism/advanced +## Casting +Casting is the conversion from one type to another. +If a `double` is needed, an `int` can be used in its place. +The `5.0 / 2` example from above is handled by an implicit cast of `2` to `2.0`. +A `double`, that is cast into an `int` might lose information. +It is good practice to make these casts to another type T explicit via `static_cast`. +The old C-style cast via `(T)` is discouraged in modern C++, but should be known nonetheless. +```cpp +double pi{3.14}; +int about_2_times_pi_cpp ={static_cast(pi) * 2}; +// Old C-style cast: +int about_2_times_pi_c = {(int)pi * 2}; +``` + +## Precision & Representation + +Numbers in C++ cannot be abitrarily big. +`int` for example is represented by 32 bits. +That is enough for a set of `2^32` numbers - roughly 4 billion. +Including `0`, this sets the biggest and smallest `int` numbers as `-2^31` and `2^31 - 1`. + +Floating point numbers are usually implemented using 15 decimal places of precision, but will vary in representation based on the host system. +~~~~ \ No newline at end of file diff --git a/concepts/numbers/introduction.md b/concepts/numbers/introduction.md new file mode 100644 index 00000000..e020b171 --- /dev/null +++ b/concepts/numbers/introduction.md @@ -0,0 +1,53 @@ +# Introduction +The built-in number types in C++ can be divided into integers and floating points. +Integers are whole numbers like `0`, `691`, or `-2`. +Examples of floating point numbers are `6.02214076`, `0.1`, or `-1.616`. + +## Integers +When you assign a value to an `int` variable, you can do so directly with a literal. +There are different integer literals based on the base of the representation. +Decimal integer literals are the most common and use the numbers `0` to `9`. +By adding a special prefix it is possible to use other bases. +The example below shows the number `9421` in its four representations and prefixes. + +```cpp +int decimal{9241}; // base 10: 0-9 +int hexadecimal{0x24CD}; // base 16: 0-9 and A-F +int binary{0b10010000011001}; // base 2: 0-1 +int octal{022031}; // base 8: 0-7 + // Leading with a 0 not the letter o. +``` +You can use an apostrophe to separate digits for easier readability. +`9'241` is the same as `0b0100'100'0001'1001` or `92'4'1`. + +## Floating-Point Numbers + +The floating-point literals come in two flavors. +Next to the intuitive `0.0024` it is possible to use its scientific notation `2.4e-3`. +The most common floating-point type is `double`. + +## Arithmetic + +C++ supports `+`, `-`, `*`, `/`, `(` and `)` and `%` to form expressions. +The result from the operation between two integers is also an integer. +`5 / 2` will return `2`. +When one of the involved types is a floating-point type, the result will also be of a floating-point. +`5.0 / 2` and `5 / 2.0` will return `2.5`. +`%` is the modulus operator and will return the remainder of an integer division: `5%3` is `2`. + + +## The auto keyword + +It is possible to let the compiler figure out the correct type of a variable at the point of declaration with the `auto` keyword. +The practice can help during refactoring and shines with very long type names, that will come in later concepts. + +```cpp +auto feist{1234}; // feist is of type int, as 27 is an integer literal +auto red_cross{0xED1B2E}; // also an int +auto pythargoras{1.41421356}; // pythargoras is of type double + +auto two_times(int number) { + return number * 2; // the return type of two_times will be int because the +} // multiplication of two int types returns an int type +``` + diff --git a/concepts/numbers/links.json b/concepts/numbers/links.json new file mode 100644 index 00000000..f11210b0 --- /dev/null +++ b/concepts/numbers/links.json @@ -0,0 +1,14 @@ +[ + { + "url": "https://en.cppreference.com/w/cpp/language/operator_arithmetic", + "description": "Reference for Arithmetic operators" + }, + { + "url": "https://en.cppreference.com/w/cpp/language/auto", + "description": "Reference for the auto placeholder type specifier" + }, + { + "url": "https://en.wikipedia.org/wiki/Double-precision_floating-point_format", + "description": "Explanation on the floating point format" + } + ] \ No newline at end of file diff --git a/config.json b/config.json index fad373b9..de9ede74 100644 --- a/config.json +++ b/config.json @@ -830,6 +830,11 @@ "uuid": "7b9bcf07-b447-4c18-b838-a7f4167ff521", "slug": "namespaces", "name": "Namespaces" + }, + { + "uuid": "de675a2c-6fd2-427d-93c1-4190eeffcd95", + "slug": "numbers", + "name": "Numbers" } ], "key_features": [ From 478e3fee7597e4a7cf10038b8552675482b1a17c Mon Sep 17 00:00:00 2001 From: vaeng <34183939+vaeng@users.noreply.github.com> Date: Wed, 26 Apr 2023 20:05:50 +0200 Subject: [PATCH 02/16] fix continuity error --- concepts/numbers/introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/numbers/introduction.md b/concepts/numbers/introduction.md index e020b171..39319298 100644 --- a/concepts/numbers/introduction.md +++ b/concepts/numbers/introduction.md @@ -42,7 +42,7 @@ It is possible to let the compiler figure out the correct type of a variable at The practice can help during refactoring and shines with very long type names, that will come in later concepts. ```cpp -auto feist{1234}; // feist is of type int, as 27 is an integer literal +auto feist{1234}; // feist is of type int, as 1234 is an integer literal auto red_cross{0xED1B2E}; // also an int auto pythargoras{1.41421356}; // pythargoras is of type double From dc432f2dd47798cf3b15a3667f714f9babfcea0a Mon Sep 17 00:00:00 2001 From: vaeng <34183939+vaeng@users.noreply.github.com> Date: Wed, 26 Apr 2023 22:36:19 +0200 Subject: [PATCH 03/16] Implement more feedback --- concepts/numbers/introduction.md | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/concepts/numbers/introduction.md b/concepts/numbers/introduction.md index 39319298..12d07ebf 100644 --- a/concepts/numbers/introduction.md +++ b/concepts/numbers/introduction.md @@ -4,26 +4,32 @@ Integers are whole numbers like `0`, `691`, or `-2`. Examples of floating point numbers are `6.02214076`, `0.1`, or `-1.616`. ## Integers -When you assign a value to an `int` variable, you can do so directly with a literal. -There are different integer literals based on the base of the representation. -Decimal integer literals are the most common and use the numbers `0` to `9`. -By adding a special prefix it is possible to use other bases. -The example below shows the number `9421` in its four representations and prefixes. +The following example shows the declaration and initialization of four different variables ```cpp -int decimal{9241}; // base 10: 0-9 -int hexadecimal{0x24CD}; // base 16: 0-9 and A-F -int binary{0b10010000011001}; // base 2: 0-1 -int octal{022031}; // base 8: 0-7 - // Leading with a 0 not the letter o. +int m_morales{9241}; // base 10: 0-9 +int a_apaec{0x24CD}; // base 16: 0-9 and A-F +int m_gargan{0b10010000011001}; // base 2: 0-1 +int b_reilly{022031}; // base 8: 0-7 + // Leading with a 0 not the letter o. ``` +When you assign a value to an `int` variable, you can do so directly with a literal. +A literal is a hard-coded number like `9241`. +There are different integer literals for several bases of the representation. +Decimal integer literals are the most common and use the digits `0` to `9`. +By adding a special prefix, like `0x`, it is possible to use other bases. +The example above shows the number `9421` in its four representations and prefixes. +All variables are initialized to the same value. + +For more details on the different representation systems, take a look at [a small tutorial][cpp_numerical_bases]. + You can use an apostrophe to separate digits for easier readability. `9'241` is the same as `0b0100'100'0001'1001` or `92'4'1`. ## Floating-Point Numbers The floating-point literals come in two flavors. -Next to the intuitive `0.0024` it is possible to use its scientific notation `2.4e-3`. +In addition to the intuitive `0.0024` it is possible to use its scientific notation `2.4e-3`. The most common floating-point type is `double`. ## Arithmetic @@ -51,3 +57,4 @@ auto two_times(int number) { } // multiplication of two int types returns an int type ``` +[cpp_numerical_bases]: https://cplusplus.com/doc/hex/ \ No newline at end of file From 799ce1f4a2468c0616b623a7f91018bf09d7b0d1 Mon Sep 17 00:00:00 2001 From: vaeng <34183939+vaeng@users.noreply.github.com> Date: Tue, 23 May 2023 10:27:29 +0200 Subject: [PATCH 04/16] Include comments --- concepts/numbers/introduction.md | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/concepts/numbers/introduction.md b/concepts/numbers/introduction.md index 12d07ebf..dbc95508 100644 --- a/concepts/numbers/introduction.md +++ b/concepts/numbers/introduction.md @@ -1,7 +1,7 @@ # Introduction The built-in number types in C++ can be divided into integers and floating points. Integers are whole numbers like `0`, `691`, or `-2`. -Examples of floating point numbers are `6.02214076`, `0.1`, or `-1.616`. +Floating point numbers are numbers with a decimal point like `6.02214076`, `0.1`, or `-1.616`. ## Integers The following example shows the declaration and initialization of four different variables @@ -39,22 +39,6 @@ The result from the operation between two integers is also an integer. `5 / 2` will return `2`. When one of the involved types is a floating-point type, the result will also be of a floating-point. `5.0 / 2` and `5 / 2.0` will return `2.5`. -`%` is the modulus operator and will return the remainder of an integer division: `5%3` is `2`. - - -## The auto keyword - -It is possible to let the compiler figure out the correct type of a variable at the point of declaration with the `auto` keyword. -The practice can help during refactoring and shines with very long type names, that will come in later concepts. - -```cpp -auto feist{1234}; // feist is of type int, as 1234 is an integer literal -auto red_cross{0xED1B2E}; // also an int -auto pythargoras{1.41421356}; // pythargoras is of type double - -auto two_times(int number) { - return number * 2; // the return type of two_times will be int because the -} // multiplication of two int types returns an int type -``` +`%` is the remainder operator and will return the remainder of an integer division: `5%3` is `2`. [cpp_numerical_bases]: https://cplusplus.com/doc/hex/ \ No newline at end of file From 74233f538c06b9f275214f716b6c041f0eaed683 Mon Sep 17 00:00:00 2001 From: vaeng <34183939+vaeng@users.noreply.github.com> Date: Tue, 23 May 2023 10:29:35 +0200 Subject: [PATCH 05/16] Include more fixes from comments --- concepts/numbers/about.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/concepts/numbers/about.md b/concepts/numbers/about.md index 479fd840..bcef6d59 100644 --- a/concepts/numbers/about.md +++ b/concepts/numbers/about.md @@ -13,9 +13,9 @@ It is good practice to make these casts to another type T explicit via `static_c The old C-style cast via `(T)` is discouraged in modern C++, but should be known nonetheless. ```cpp double pi{3.14}; -int about_2_times_pi_cpp ={static_cast(pi) * 2}; +int about_2_times_pi_cpp{static_cast(pi) * 2}; // Old C-style cast: -int about_2_times_pi_c = {(int)pi * 2}; +int about_2_times_pi_c{(int)pi * 2}; ``` ## Precision & Representation From 68ea3846e0afeca921d95445c17c612308afc7b3 Mon Sep 17 00:00:00 2001 From: Christian Willner <34183939+vaeng@users.noreply.github.com> Date: Fri, 28 Apr 2023 21:49:57 +0200 Subject: [PATCH 06/16] Adding a basic concept (closes #589) (#594) --- config.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/config.json b/config.json index de9ede74..c3689982 100644 --- a/config.json +++ b/config.json @@ -835,6 +835,11 @@ "uuid": "de675a2c-6fd2-427d-93c1-4190eeffcd95", "slug": "numbers", "name": "Numbers" + }, + { + "uuid": "e52453a5-4997-4eba-b754-5bda7b97c701", + "slug": "basics", + "name": "Basics" } ], "key_features": [ From aa13579be68af35c1351da2aa1fb8bfaa1a561b0 Mon Sep 17 00:00:00 2001 From: vaeng <34183939+vaeng@users.noreply.github.com> Date: Wed, 26 Apr 2023 15:22:50 +0200 Subject: [PATCH 07/16] First draft --- config.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/config.json b/config.json index c3689982..0df67902 100644 --- a/config.json +++ b/config.json @@ -836,6 +836,11 @@ "slug": "numbers", "name": "Numbers" }, + { + "uuid": "de675a2c-6fd2-427d-93c1-4190eeffcd95", + "slug": "numbers", + "name": "Numbers" + }, { "uuid": "e52453a5-4997-4eba-b754-5bda7b97c701", "slug": "basics", From 53641b41fc9e34218e6a982947bbb6ae06b50928 Mon Sep 17 00:00:00 2001 From: Christian Willner <34183939+vaeng@users.noreply.github.com> Date: Fri, 28 Apr 2023 21:49:57 +0200 Subject: [PATCH 08/16] Adding a basic concept (closes #589) (#594) --- config.json | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/config.json b/config.json index 0df67902..84241335 100644 --- a/config.json +++ b/config.json @@ -816,6 +816,11 @@ "slug": "strings", "name": "Strings" }, + { + "uuid": "de675a2c-6fd2-427d-93c1-4190eeffcd95", + "slug": "numbers", + "name": "Numbers" + }, { "uuid": "e52453a5-4997-4eba-b754-5bda7b97c701", "slug": "basics", @@ -830,21 +835,6 @@ "uuid": "7b9bcf07-b447-4c18-b838-a7f4167ff521", "slug": "namespaces", "name": "Namespaces" - }, - { - "uuid": "de675a2c-6fd2-427d-93c1-4190eeffcd95", - "slug": "numbers", - "name": "Numbers" - }, - { - "uuid": "de675a2c-6fd2-427d-93c1-4190eeffcd95", - "slug": "numbers", - "name": "Numbers" - }, - { - "uuid": "e52453a5-4997-4eba-b754-5bda7b97c701", - "slug": "basics", - "name": "Basics" } ], "key_features": [ From ffbac7833773a7ecd24e78f981fad11616875bf8 Mon Sep 17 00:00:00 2001 From: Christian Willner <34183939+vaeng@users.noreply.github.com> Date: Tue, 16 May 2023 10:10:50 +0200 Subject: [PATCH 09/16] Concept docs for "Namespaces" (closes #605) --- config.json | 5 ----- 1 file changed, 5 deletions(-) diff --git a/config.json b/config.json index 84241335..80278881 100644 --- a/config.json +++ b/config.json @@ -826,11 +826,6 @@ "slug": "basics", "name": "Basics" }, - { - "uuid": "257598c7-c09f-47be-90db-514f93758420", - "slug": "includes", - "name": "Includes" - }, { "uuid": "7b9bcf07-b447-4c18-b838-a7f4167ff521", "slug": "namespaces", From b50bf39b55057968d556e22b3de7c54904b8fc4b Mon Sep 17 00:00:00 2001 From: Christian Willner <34183939+vaeng@users.noreply.github.com> Date: Wed, 17 May 2023 10:51:41 +0200 Subject: [PATCH 10/16] Concept docs for "Includes" (closes #607) --- config.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/config.json b/config.json index 80278881..84241335 100644 --- a/config.json +++ b/config.json @@ -826,6 +826,11 @@ "slug": "basics", "name": "Basics" }, + { + "uuid": "257598c7-c09f-47be-90db-514f93758420", + "slug": "includes", + "name": "Includes" + }, { "uuid": "7b9bcf07-b447-4c18-b838-a7f4167ff521", "slug": "namespaces", From 6a90096da5516bb40c2623f4532a5118fc10aec6 Mon Sep 17 00:00:00 2001 From: vaeng <34183939+vaeng@users.noreply.github.com> Date: Wed, 26 Apr 2023 15:22:50 +0200 Subject: [PATCH 11/16] First draft --- config.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/config.json b/config.json index 84241335..6294e7d0 100644 --- a/config.json +++ b/config.json @@ -835,6 +835,11 @@ "uuid": "7b9bcf07-b447-4c18-b838-a7f4167ff521", "slug": "namespaces", "name": "Namespaces" + }, + { + "uuid": "de675a2c-6fd2-427d-93c1-4190eeffcd95", + "slug": "numbers", + "name": "Numbers" } ], "key_features": [ From 570b6b636639d1bb8204569dd5e4b9ab6b4b1ba1 Mon Sep 17 00:00:00 2001 From: Christian Willner <34183939+vaeng@users.noreply.github.com> Date: Fri, 28 Apr 2023 21:49:57 +0200 Subject: [PATCH 12/16] Adding a basic concept (closes #589) (#594) --- config.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/config.json b/config.json index 6294e7d0..05f27bf5 100644 --- a/config.json +++ b/config.json @@ -840,6 +840,11 @@ "uuid": "de675a2c-6fd2-427d-93c1-4190eeffcd95", "slug": "numbers", "name": "Numbers" + }, + { + "uuid": "e52453a5-4997-4eba-b754-5bda7b97c701", + "slug": "basics", + "name": "Basics" } ], "key_features": [ From 61fddcb6a5471896ee0e02e50bc111bb7837f0cb Mon Sep 17 00:00:00 2001 From: vaeng <34183939+vaeng@users.noreply.github.com> Date: Wed, 26 Apr 2023 15:22:50 +0200 Subject: [PATCH 13/16] First draft --- config.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/config.json b/config.json index 05f27bf5..b6487c24 100644 --- a/config.json +++ b/config.json @@ -841,6 +841,11 @@ "slug": "numbers", "name": "Numbers" }, + { + "uuid": "de675a2c-6fd2-427d-93c1-4190eeffcd95", + "slug": "numbers", + "name": "Numbers" + }, { "uuid": "e52453a5-4997-4eba-b754-5bda7b97c701", "slug": "basics", From 10b6cdeb875c33010971482d2ecfec5256927651 Mon Sep 17 00:00:00 2001 From: Christian Willner <34183939+vaeng@users.noreply.github.com> Date: Fri, 28 Apr 2023 21:49:57 +0200 Subject: [PATCH 14/16] Adding a basic concept (closes #589) (#594) --- config.json | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/config.json b/config.json index b6487c24..84241335 100644 --- a/config.json +++ b/config.json @@ -835,21 +835,6 @@ "uuid": "7b9bcf07-b447-4c18-b838-a7f4167ff521", "slug": "namespaces", "name": "Namespaces" - }, - { - "uuid": "de675a2c-6fd2-427d-93c1-4190eeffcd95", - "slug": "numbers", - "name": "Numbers" - }, - { - "uuid": "de675a2c-6fd2-427d-93c1-4190eeffcd95", - "slug": "numbers", - "name": "Numbers" - }, - { - "uuid": "e52453a5-4997-4eba-b754-5bda7b97c701", - "slug": "basics", - "name": "Basics" } ], "key_features": [ From ef0bdd21fc43a0209eb71af6d6c6ec4ad3c44d76 Mon Sep 17 00:00:00 2001 From: Christian Willner <34183939+vaeng@users.noreply.github.com> Date: Tue, 16 May 2023 10:10:50 +0200 Subject: [PATCH 15/16] Concept docs for "Namespaces" (closes #605) --- config.json | 5 ----- 1 file changed, 5 deletions(-) diff --git a/config.json b/config.json index 84241335..80278881 100644 --- a/config.json +++ b/config.json @@ -826,11 +826,6 @@ "slug": "basics", "name": "Basics" }, - { - "uuid": "257598c7-c09f-47be-90db-514f93758420", - "slug": "includes", - "name": "Includes" - }, { "uuid": "7b9bcf07-b447-4c18-b838-a7f4167ff521", "slug": "namespaces", From 2b2944f19567836d903e051483cdf93e67c8ec63 Mon Sep 17 00:00:00 2001 From: vaeng <34183939+vaeng@users.noreply.github.com> Date: Tue, 23 May 2023 11:07:30 +0200 Subject: [PATCH 16/16] Manually fix to match main --- config.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/config.json b/config.json index 80278881..84241335 100644 --- a/config.json +++ b/config.json @@ -826,6 +826,11 @@ "slug": "basics", "name": "Basics" }, + { + "uuid": "257598c7-c09f-47be-90db-514f93758420", + "slug": "includes", + "name": "Includes" + }, { "uuid": "7b9bcf07-b447-4c18-b838-a7f4167ff521", "slug": "namespaces",