Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions concepts/numbers/.meta/config.json
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": [
]
}
29 changes: 29 additions & 0 deletions concepts/numbers/about.md
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.
~~~~
44 changes: 44 additions & 0 deletions concepts/numbers/introduction.md
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

```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

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

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/
14 changes: 14 additions & 0 deletions concepts/numbers/links.json
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"
}
]
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down