-
-
Notifications
You must be signed in to change notification settings - Fork 234
Concept docs for "Numbers" (closes #600) #602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9f9f8e2
First draft
vaeng 478e3fe
fix continuity error
vaeng dc432f2
Implement more feedback
vaeng 799ce1f
Include comments
vaeng 74233f5
Include more fixes from comments
vaeng 68ea384
Adding a basic concept (closes #589) (#594)
vaeng aa13579
First draft
vaeng 53641b4
Adding a basic concept (closes #589) (#594)
vaeng ffbac78
Concept docs for "Namespaces" (closes #605)
vaeng b50bf39
Concept docs for "Includes" (closes #607)
vaeng 6a90096
First draft
vaeng 570b6b6
Adding a basic concept (closes #589) (#594)
vaeng 61fddcb
First draft
vaeng 10b6cde
Adding a basic concept (closes #589) (#594)
vaeng ef0bdd2
Concept docs for "Namespaces" (closes #605)
vaeng 2b2944f
Manually fix to match main
vaeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "blurb": "C++ has different types for numbers. Common are int for integers and double for decimals.", | ||
| "authors": [ | ||
| "vaeng" | ||
| ], | ||
| "contributors": [ | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<T>`. | ||
| 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<int>(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. | ||
| ~~~~ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # 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`. | ||
| 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 | ||
|
|
||
vaeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ```cpp | ||
| 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 | ||
vaeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| The floating-point literals come in two flavors. | ||
| 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 | ||
vaeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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 remainder operator and will return the remainder of an integer division: `5%3` is `2`. | ||
|
|
||
| [cpp_numerical_bases]: https://cplusplus.com/doc/hex/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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" | ||
| } | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.