Mentions légales du service

Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • master
  • v19.05
  • v20.03
  • v21.05
  • v22.10
5 results

Target

Select target project
  • formations/cpp/gettingstartedwithmoderncpp
  • sbenamor/gettingstartedwithmoderncpp
  • steff/gettingstartedwithmoderncpp
  • sgilles/gettingstartedwithmoderncpp
  • vrouvrea/gettingstartedwithmoderncpp
  • fvergnet/gettingstartedwithmoderncpp
  • jediaz/gettingstartedwithmoderncpp
  • mmalanda/gettingstartedwithmoderncpp
  • bnguyenv/gettingstartedwithmoderncpp
9 results
Select Git revision
  • master
1 result
Show changes
Commits on Source (706)
Showing
with 3103 additions and 1443 deletions
.git
docker
### ATTENTION ###
# This is the GLOBAL gitignore, please do NOT change this file,
# unless it should be available for everyone
# Please change the LOCAL gitignore (.git/info/exclude) to customize
# excluding files/folders from being recognized by git
# Recovery files
*~
# Backup files
*.bak
# Most used editor files
*.vscode
*.directory
*.DS_Store
# Jupyter files
.ipynb_checkpoints .ipynb_checkpoints
*/.ipynb_checkpoints */.ipynb_checkpoints
Untitled.ipynb Untitled.ipynb
...@@ -6,5 +24,14 @@ Untitled.ipynb ...@@ -6,5 +24,14 @@ Untitled.ipynb
# Directory used to build code with CMake in the notebook about thirdparty library inclusion # Directory used to build code with CMake in the notebook about thirdparty library inclusion
build_clang build_clang
build_gcc build_gcc
build
# Default generated executable; object files
a.out
*.o
# A file created in a test from one notebook
1-ProceduralProgramming/test_stream.txt
1-ProceduralProgramming/test_stream.txt # Hello world executable
\ No newline at end of file hello
\ No newline at end of file
stages:
- prepare
- build
- check
# As suggested by https://blog.sparksuite.com/7-ways-to-speed-up-gitlab-ci-cd-times-29f60aab69f9
variables:
DOCKER_DRIVER: overlay2
check_notebooks_empty_outputs:
stage: prepare
image: python:3.10
tags:
- ci.inria.fr
script:
- pip install pre-commit
- pre-commit install
- pre-commit run --all-files
.build_docker_image: &build_docker_image
stage: build
image: docker
needs: []
tags:
- ci.inria.fr
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
script:
- docker build -f docker/Dockerfile.${NAME} -t ${NAME} .
# Push the image to the GitLab registry only if the pipeline was triggered by a tag
- |
if [ -z ${CI_COMMIT_TAG+x} ] ; then
echo "As this is not a tag, Docker image isn't pushed into registry"
else
docker tag ${NAME} $CI_REGISTRY/$CI_PROJECT_PATH/${NAME}:latest
docker push $CI_REGISTRY/$CI_PROJECT_PATH/${NAME}:latest
docker tag ${NAME} $CI_REGISTRY/$CI_PROJECT_PATH/${NAME}:${CI_COMMIT_TAG}
docker push $CI_REGISTRY/$CI_PROJECT_PATH/${NAME}:${CI_COMMIT_TAG}
fi
rules:
# Launch docker build if the Dockerfile has changed or if the pipeline was triggered by a tag
- if: '$CI_COMMIT_TAG'
- changes:
- docker/Dockerfile.${NAME}
build_fedora:
extends: .build_docker_image
variables:
NAME: "fedora_for_hands_on"
build_fedora_with_boost:
extends: .build_docker_image
variables:
NAME: "fedora_with_boost"
compile_hands_on_solutions:
image: $CI_REGISTRY/$CI_PROJECT_PATH/fedora_for_hands_on
stage: check
dependencies:
- build_fedora
tags:
- ci.inria.fr
script:
- cd HandsOn/1-ProceduralProgramming/Solution
- mkdir build && cd build
- cmake -G Ninja -DCMAKE_CXX_COMPILER=clang++ ..
- ninja
- cd ../../../2-ObjectProgramming/Solution
- mkdir build && cd build
- cmake -G Ninja -DCMAKE_CXX_COMPILER=clang++ ..
- ninja
- cd ../../../3-Operators/Solution
- mkdir build && cd build
- cmake -G Ninja -DCMAKE_CXX_COMPILER=clang++ ..
- ninja
- cd ../../../4-Templates/Solution
- mkdir build && cd build
- cmake -G Ninja -DCMAKE_CXX_COMPILER=clang++ ..
- ninja
- cd ../../../5-UsefulConceptsAndSTL/Solution
- mkdir build && cd build
- cmake -G Ninja -DCMAKE_CXX_COMPILER=clang++ ..
- ninja
- cd ../../../6-InRealEnvironment/Solution
- mkdir build && cd build
- cmake -G Ninja -DCMAKE_CXX_COMPILER=clang++ ..
- ninja
repos:
- repo: https://github.com/kynan/nbstripout
rev: 0.7.1
hooks:
- id: nbstripout
%% Cell type:markdown id: tags:
# [Getting started in C++](./) - [A brief introduction](./0-introduction-to-C++.ipynb)
%% Cell type:markdown id: tags:
## A very brief historic
- C++ was first published in 1985 by Bjarne Stroustrup with the idea of extending C with object programming; the first name of the language was _C with classes_
- The first standard was issued in 1998 and called _C++ 98_
- A first (minor) update was issued in 2003 (and dubbed *C++ 03*)
- The plan was to provide a major overhaul for the next version, which was called for a long time *C++ 0x*.
- The schedule failed, as the next standard turned out to be *C++ 11*. It is a major update, with lots of new features and syntactic sugar introduced.
- The plan was now to publish a release every three years, alternating minor and major ones. The committee followed the planned schedule more successfully than for *C++ 0x*, except for the minor/major:
* *C++ 14* was a polishing of *C++ 11*, as intended.
* *C++ 17* introduced more new stuff than *C++ 14*, but not as many as initially intended.
* *C++ 20* is a major update, almost as groundbreaking as *C++ 11*.
* *C++ 23* has been finalized in February 2023; but many features from both language and standard library aren't supported yet by compilers.
* *C++ 26* is under discussed and some of the features are already available.
%% Cell type:markdown id: tags:
## Which standard will be tackled in this lecture?
### C++ 11/14/17 rather than C++ 98/03
The new major standard is now widely supported by compilers, and introduces many features that are very useful. As it is much more pleasant to use, it would be a shame to restrict ourselves to the older versions of the standard.
However, you may have to tackle legacy code which is written in C++ 98/03, so we indicated as much as possible for each recent feature presented when it was actually introduced (if not specified assume it was already present in C++ 98).
### But which one should you use? 11, 14 or 17?
As indicated above, we tried to specify clearly in which standard specific features were introduced. Few guidelines:
- C++ 14 is now a safe bet for most compilers, so you should probably choose this one instead of C++ 11.
- C++ 17 support is now really widespread as well, but you may still lack some features if your distro is a bit backward (for instance default gcc compiler on still supported Ubuntu LTS 18.04 does not support the brand new filesystem library (yes C++ was not historically a _batteries included_ language...)). Vincent is working on a library named [Gudhi](https://gudhi.inria.fr/) that is rather conservative for the standard use (in the sense they want most users to be able to compile the code without having to install brand new environment) and they switched to C++ 17 in December 2022.
### And C++ 20?
C++ 20 will not be addressed much in this lecture... even though at least one of us (Sébastien) dabbled a lot with it since 2023.
It has been published officially at the end of 2020, but some of its features are still not widely supported by current compilers.
We will obviously update this lecture when it becomes widespread, as it will introduce very cool stuff (and the promise of much better compilation errors for templates...). We also have a technical difficulty in that Xeus-cling technology we're using is not really maintained currently, and the issue asking whether C++ 20 support is foreseen [remains unanswered](https://github.com/jupyter-xeus/xeus-cling/issues/510) when these lines are written (February 2024), while the underlying `cling` which is used to make interpreted-like C++ support it now (see for instance [this talk](https://indico.jlab.org/event/459/contributions/11563/attachments/9696/14191/CHEP_2023_cling_Interpreting_C__20.pdf)).
### Compiler support
We will mention it again later, but [following cppreference link for compiler support](https://en.cppreference.com/w/cpp/compiler_support) is a very handy link that tells which feature is supported by which compiler.
It may help you to choose which standard to use for your project depending on the expected user of the code (can you expect your users to have fairly up-to-date OS and compilers installed? Do you plan your code to be used only in a specific environment, or must it be cross-platform? etc...)
Don't worry we will tackle that again at the end of this training session, but if you intend to use C++ fairly regularly it's a good idea to bookmark this link!
%% Cell type:markdown id: tags:
## A multi-paradigm language
C++ was originally created with the will to provide object programming to C, but it is truly now a multi-paradigm language.
This lecture will cover three of them:
- [Procedural programming](1-ProceduralProgramming/0-main.ipynb)
- [Object programming](2-ObjectProgramming/0-main.ipynb)
- [Generic programming](4-Templates/0-main.ipynb)
There are actually even more: functional programming for instance seems to be gaining traction at the moment and will be eased in C++ 20 standard (see [Ivan Čukić's book](../bibliography.ipynb#Functional-Programming-in-C++) for more about functional programming in C++).
This richness is not always perceived as a boon: there is a section in the [Wikipedia page](https://en.wikipedia.org/wiki/C%2B%2B#Criticism) dedicated to the criticisms addressed at C++ by notorious developers (but also a defense by Brian Kernighan!)
%% Cell type:markdown id: tags:
[© Copyright](COPYRIGHT.md)
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# [Getting started in C++](/) - [Getting started with the tutorial](/notebooks/getting_started_with_tutorial.ipynb) # [Getting started in C++](./) - [Getting started with the tutorial](./getting_started_with_tutorial.ipynb)
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
<h1>Table of contents<span class="tocSkip"></span></h1> ## Jupyter notebook
<div class="toc"><ul class="toc-item"><li><span><a href="#About-the-choice-of-a-Jupyter-notebook" data-toc-modified-id="About-the-choice-of-a-Jupyter-notebook-1">About the choice of a Jupyter notebook</a></span></li><li><span><a href="#When-the-notebook-is-not-enough..." data-toc-modified-id="When-the-notebook-is-not-enough...-2">When the notebook is not enough...</a></span></li><li><span><a href="#Few-guidelines-about-Jupyter" data-toc-modified-id="Few-guidelines-about-Jupyter-3">Few guidelines about Jupyter</a></span><ul class="toc-item"><li><span><a href="#Restarting-the-kernel" data-toc-modified-id="Restarting-the-kernel-3.1">Restarting the kernel</a></span></li></ul></li><li><span><a href="#Very-basic-C++-syntax-(in-notebook-and-in-general)" data-toc-modified-id="Very-basic-C++-syntax-(in-notebook-and-in-general)-4">Very basic C++ syntax (in notebook and in general)</a></span><ul class="toc-item"><li><span><a href="#Semicolons" data-toc-modified-id="Semicolons-4.1">Semicolons</a></span></li><li><span><a href="#Blocks" data-toc-modified-id="Blocks-4.2">Blocks</a></span></li><li><span><a href="#Input-/-output" data-toc-modified-id="Input-/-output-4.3">Input / output</a></span></li><li><span><a href="#Comments" data-toc-modified-id="Comments-4.4">Comments</a></span></li></ul></li></ul></div>
%% Cell type:markdown id: tags: ### About the choice of a Jupyter notebook
We made the choice to use a Jupyter notebook for conveniency:
- We can delve directly into the syntax of the language itself, without muddying the water with peripherical stuff such as explaining from the beginning how a build system works.
(don't worry we'll cover real environments [in part 6](./6-InRealEnvironment/0-main.ipynb)
- Notebooks are a cool tool to meddle explanations and actual code that is directly executable.
However, Jupyter notebooks were clearly not initially designed with C++ in mind (Jupyter stands for **Ju**Lia **Py**thon and **R** - these three langages are either interpreted or just-in-time, whereas C++ is a compiled langage).
This didn't stop people from trying, leveraging the [cling](https://root.cern/cling) project from CERN which aims to provide an interpreter to the C++ langage.
Up to summer 2024, we were using [Xeus-cling](https://xeus-cling.readthedocs.io/en/latest) to run this tutorial; however this project was stagnating and in particular doesn't seem keen to adopt C++ 20, which is (partially) supported by the cling interpreter.
## About the choice of a Jupyter notebook We have therefore switched to a [homemade Jupyter kernel](https://gitlab.inria.fr/sed-saclay/cppyy_kernel) which uses up the fantastic [cppyy](https://cppyy.readthedocs.io) project, which enables running C++ code from a Python environment.
This notebook uses up [xeus-cling](https://xeus-cling.readthedocs.io/en/latest/), a special instance of Jupyter able to run C++ code based upon xeus (tool to build Jupyter kernels for any language) and cling (a creation from CERN to be able to run C++ as an interpreted language). That being said, it is important to keep in mind that this notebook's fancy interpreter is absolutely not a typical C++ environment.
The reasons for these choices is to really access directly to handle C++ code without the hassle of explaining how to compile and run stuff, which is an especially cumbersome way to start with this (or any really...) language. ### When cling / the notebook is not enough...
This is not to say this tutorial will ignore entirely these topics (see the dedicated [chapter](/notebooks/6-InRealEnvironment/0-main.ipynb), just that we will first focus on C++ code. However keep in mind that this notebook's fancy interpreter is not a typical C++ environment. Even if notebooks are really useful, there are some C++ operations that are not fully supported, be it due to cling limitations (either intrinsic or just because some new C++ features aren't yet covered) or to the way we implemented our kernel around cppyy.
Jupyter Xeus-Cling is still under active development: you should really get a recent version and keep it up-to-date. Some examples in this lecture didn't work at first and were properly dealt with with a version one month later! We try our best to make the most content available directly, and we designed our kernel accordingly. We used so-called *magics* to do so, so don't be surprised if some cells starts with a line such as:
```jupyter
%%cppmagics cppyy/cppdef
```
Such lines are dedicated to the running of the tutorial and have nothing to do with C++ itself; you may entirely ignore it (unless of course you intend to use our cppyy kernel for your own purposes).
The most common such magics are:
## When the notebook is not enough... - `%%cppmagics cppyy/cppdef`: cppyy in fact defines two different functions `cppexec` and `cppdef`. The former is for code deemed to be executed, the latter for code that defines classes, functions and so on. In our Jupyter kernel we use by default `cppexec`, which works just fine for most operations. However some really need to call under the hood `cppdef`, and that's the reason for this magics.
- `%%cppmagics clang`: this magics tells that the entire content of the cell is to be written into a file that is to be compiled by `clang++` compiler; the executable hence produced is then run directly and its output is printed in the notebook. Contrary to the usual behaviour, the content of the cell is sandboxed and self-contained.
As we shall see repeatedly, Xeus-cling notebooks are far from being full-proof: some stuff that are perfectly acceptable C++ aren't accepted in them, and some others required work-arounds. When such an issue appears: The list of all magics may be displayed with following cell:
* It will be indicated explicitly in the notebook if a specific work around is used. We do not want you to take Jupyter work-arounds as a legit advice on how to write proper C++. %% Cell type:code id: tags:
* If Jupyter can't deal with the code, we will use [Coliru](https://coliru.stacked-crooked.com/). Coliru is a C++ online compiler; others are listed [here](https://arne-mertz.de/2017/05/online-compilers/) ([Wandbox](https://wandbox.org/) deserves a shout out as it enables testing the same code with a great variety of compiler versions).
``` c++
%%cppmagics
;
```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Few guidelines about Jupyter ### Few guidelines about Jupyter
You might not be familiar with Jupyter notebooks, so here are few tips to run it smoothly (the _Help_ menu will help you find more if you need it). You might not be familiar with Jupyter notebooks, so here are few tips to run it smoothly (the _Help_ menu will help you find more if you need it).
In a Jupyter notebook the content is divided into _cells_, in our case we are using two kind of cells: In a Jupyter notebook the content is divided into _cells_, in our case we are using two kind of cells:
* Markdown cells, such as the ones into these very words are written. * Markdown cells, such as the ones into these very words are written.
* Code cells, which are running code. In these notebooks the chosen kernel is C++17, so the code is C++17 which is interpreted by cling. * Code cells, which are running code. In these notebooks the chosen kernel is C++17, so the code is C++17 which is interpreted by cling.
There are two modes: There are two modes:
* Edit mode, in which you might change the content of a cell. In this mode the left part of the cell is in green. * Edit mode, in which you might change the content of a cell. In this mode the left part of the cell is in green.
* Command mode, in which you might take actions such as changing the type of a cell, create or delete a new one, etc... * Command mode, in which you might take actions such as changing the type of a cell, create or delete a new one, etc...
To enter in edit mode, simply type on 'Enter'. To enter in edit mode, simply type on 'Enter'.
To enter in command mode, type 'Esc'. To enter in command mode, type 'Esc'.
To execute a cell, type 'Shift + Enter'. For a markdown cell it will edit nicely the content by interpreting the markdown, and for a code cell it will run the code. To execute a cell, type 'Shift + Enter'. For a markdown cell it will edit nicely the content by interpreting the markdown, and for a code cell it will run the code.
In command mode, several handy shortcuts are there; I would recommend especially: In command mode, several handy shortcuts are there; I would recommend especially:
* `a` (add a cell above) * `a` (add a cell above)
* `b` (add a cell below) * `b` (add a cell below)
* `x` (cut a cell) * `x` (cut a cell)
* `M` (change cell mode to Markdown) * `M` (change cell mode to Markdown)
The complete list is available in _Help_ > _Keyboard_ shortcut. The complete list is available in _Help_ > _Keyboard_ shortcut.
If for some reason the code in the notebook seems stuck, you might try to restart the kernel with one of the restart option in the _Kernel_ menu. If for some reason the code in the notebook seems stuck, you might try to restart the kernel with one of the restart option in the _Kernel_ menu.
### Restarting the kernel #### Restarting the kernel
Sometimes something that should work doesn't... In this case try restarting the kernel: it might fix your issue! Sometimes something that should work doesn't... In this case try restarting the kernel: it might fix your issue!
#### Table of contents
The table of content for a given notebook is available as a side panel if you go to _View_ > _Table of contents_ or if you click on the third item on the leftmost panel.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Very basic C++ syntax (in notebook and in general) ## Very basic C++ syntax (in notebook and in general)
### Semicolons ### Semicolons
In C++ most instructions end by a semicolon `;`. If you forget it, the underlying compiler doesn't understand the syntax. In C++ most instructions end by a semicolon `;`. If you forget it, the underlying compiler doesn't understand the syntax.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` c++
{ {
int foo = 5 // COMPILATION ERROR! int foo = 5 // COMPILATION ERROR!
} }
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` c++
{ {
int foo = 5; // OK int foo = 5; // OK
} }
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Spaces, end lines and tabulations act as word separators; utterly unreadable code as the one below is perfectly fine from the compiler standpoint: Spaces, end lines and tabulations act as word separators; utterly unreadable code as the one below is perfectly fine from the compiler standpoint:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` c++
# include <string> # include <string>
{ {
int nombre ; nombre = 1 int number ; number = 1
; std::string nom; ; std::string name;
nom= name=
"truc" ; "truc" ;
nombre = 2 number = 2
; ;
} }
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Blocks
You may have notices the braces `{` and `}` in the examples above. They are here a technicality to make cling interpreter work better in a Jupyter environment, but that is also useful in a real C++ code.
What is between both braces constitute a **block**; variables that are initialized inside are destroyed once the closing brace is past (don't worry - we will come back to that [later](/notebooks/1-ProceduralProgramming/5-DynamicAllocation.ipynb#Stack)).
This is an incredibly useful feature: you may ensure this way that a variable is not kept any longer than necessary. We will come back to this feature (called the **locality of reference** later); let's see why they are useful in a notebook environment.
In C++, at a given scope a same variable can't be defined twice. So for instance if I defined twice a same variable, the compiler will yell about redefinition of a variable:
%% Cell type:code id: tags:
``` C++17
int i = 2; // Should be fine on the first call.
// But if you have executed it already, a new attempt will make the compiler yell.
```
%% Cell type:code id: tags:
``` C++17
int i = 1; // If the cell above was run, compilation error as `i` is already defined.
```
%% Cell type:markdown id: tags:
The only way to circumvent this is to restart the kernel... and you may then run only one of this cell, and only once.
So you might now see the usefulness of the braces: by putting them I define the variables in a cell in a block, and it is only defined inside this block. Same variable may this way be defined in different cells:
%% Cell type:code id: tags:
``` C++17
{
int i = 2; // Fine
}
```
%% Cell type:code id: tags:
``` C++17
{
int i = 1; // Also fine
}
```
%% Cell type:markdown id: tags:
### Input / output ### Input / output
Inputs and outputs aren't directly a part of the language itself, but are in the standard library (often abbreviated as STL for Standard Template Library even if some purist may yell and explain it's not 100 % the same thing...). You therefore need to __include__ a file named `iostream`; doing so will enable the use of the input / output facilities. Inputs and outputs aren't directly a part of the language itself, but are in the standard library (often abbreviated as STL for *Standard Template Library* even if some purist may yell and explain it's not 100 % the same thing...). You therefore need to __include__ a file named `iostream`; doing so will enable the use of the input / output facilities.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` c++
{ {
std::cout << "Hello world!" << std::endl; // Should fail (unless you run a cell that includes iostream before) std::cout << "Hello world!" << std::endl; // Should fail (unless you run a cell that includes iostream before)
} }
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` c++
#include <iostream> #include <iostream>
{ {
std::cout << "Hello world!" << std::endl; // Should work: std::cout and std::endl are now known. std::cout << "Hello world!" << std::endl; // Should work: std::cout and std::endl are now known.
} }
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
- `std::cout` is the symbol to designate the standard output (i.e. your screen...) - `std::cout` is the symbol to designate the standard output (i.e. your screen...)
- `std::endl` is the symbol to clean-up the stream and go to next line. - `std::endl` is the symbol to clean-up the stream and go to next line.
The operator `<<` is used to indicate what you direct toward the stream; here std::cout << "Hello world!" tells to redirect the string toward the standard output. The operator `<<` is used to indicate what you direct toward the stream; here `std::cout << "Hello world!"` tells to redirect the string toward the standard output.
We will see that a bit more in detail in [a later chapter](/notebooks/1-ProceduralProgramming/6-Streams.ipynb), but printing something is really helpful early on hence this brief introduction here. We will see that a bit more in detail in [a later chapter](./1-ProceduralProgramming/6-Streams.ipynb), but printing something is really helpful early on hence this brief introduction here.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Comments ### Comments
There are two ways to comment code in C++: There are two ways to comment code in C++:
- `//` which comments all that is after this symbol on the same line - `//` which comments all that is after this symbol on the same line.
- `/*` ... `*/` which comments everything in the between the symbols. - `/*` ... `*/` which comments everything between the symbols.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` C++17 ``` c++
{ {
int i = 0; // Everything after // is commented until the end of the line int i = 0; // Everything after // is commented until the end of the line
/* /*
commented... commented...
also commented... also commented...
*/ */
int j = 5; // no longer commented int j = 5; // no longer commented
/* /*
// This type of comment might be in the other style // This type of comment might be used inside the other style
*/ */
} }
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
© _CNRS 2016_ - _Inria 2018-2019_ [© Copyright](COPYRIGHT.md)
_This notebook is an adaptation of a lecture prepared by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_
_The present version has been written by Sébastien Gilles and Vincent Rouvreau (Inria)_
......
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# [Getting started in C++](/) - [Procedural programming](/notebooks/1-ProceduralProgramming/0-main.ipynb) # [Getting started in C++](./) - [Procedural programming](./0-main.ipynb)
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
* [Variables, initialisation, affectation](/notebooks/1-ProceduralProgramming/1-Variables.ipynb) * [Variables, initialisation, assignment](./1-Variables.ipynb)
* [Condition and loops](/notebooks/1-ProceduralProgramming/2-Conditions-and-loops.ipynb) * [Condition and loops](./2-Conditions-and-loops.ipynb)
* [TP 1](/notebooks/1-ProceduralProgramming/2b-TP.ipynb) * [Hands-on 1](./2b-hands-on.ipynb)
* [Predefined types](/notebooks/1-ProceduralProgramming/3-Types.ipynb) * [Predefined types](./3-Types.ipynb)
* [Functions](/notebooks/1-ProceduralProgramming/4-Functions.ipynb) * [Functions](./4-Functions.ipynb)
* [TP 2](/notebooks/1-ProceduralProgramming/4b-TP.ipynb) * [Hands-on 2](./4b-hands-on.ipynb)
* [Dynamic allocation](/notebooks/1-ProceduralProgramming/5-DynamicAllocation.ipynb) * [Dynamic allocation](./5-DynamicAllocation.ipynb)
* [Input/output](/notebooks/1-ProceduralProgramming/6-Streams.ipynb) * [Input/output](./6-Streams.ipynb)
* [TP 3](/notebooks/1-ProceduralProgramming/6b-TP.ipynb) * [Hands-on 3](./6b-hands-on.ipynb)
* [Static and constexpr](./7-StaticAndConstexpr.ipynb)
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
© _CNRS 2016_ - _Inria 2018-2019_ [© Copyright](../COPYRIGHT.md)
_This notebook is an adaptation of a lecture prepared by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_
_The present version has been written by Sébastien Gilles and Vincent Rouvreau (Inria)_
......
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# [Getting started in C++](/) - [Procedural programming](/notebooks/1-ProceduralProgramming/0-main.ipynb) - [TP 1](/notebooks/1-ProceduralProgramming/4b-TP.ipynb) # [Getting started in C++](./) - [Procedural programming](./0-main.ipynb) - [Hands-on 1](./4b-hands-on.ipynb)
%% Cell type:markdown id: tags:
<h1>Table of contents<span class="tocSkip"></span></h1>
<div class="toc"><ul class="toc-item"><li><span><a href="#Introduction" data-toc-modified-id="Introduction-1">Introduction</a></span><ul class="toc-item"><li><span><a href="#How-to-do-the-TP?" data-toc-modified-id="How-to-do-the-TP?-1.1">How to do the TP?</a></span></li><li><span><a href="#The-problem-we'll-deal-with-throughout-the-TPs" data-toc-modified-id="The-problem-we'll-deal-with-throughout-the-TPs-1.2">The problem we'll deal with throughout the TPs</a></span></li><li><span><a href="#EXERCICE-1:-Adding-a-loop" data-toc-modified-id="EXERCICE-1:-Adding-a-loop-1.3"><strong>EXERCICE 1: Adding a loop</strong></a></span></li></ul></li></ul></div>
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Introduction ## Introduction
### How to do the TP? ### How to do the hands-on?
This [notebook](/notebooks/TP/HowTo.ipynb) explains very briefly your options to run the TP. This [notebook](../HandsOn/HowTo.ipynb) explains very briefly your options to run the hands-ons.
### The problem we'll deal with throughout the TPs ### The problem we'll deal with throughout the hands-ons
Any real number can be approximated by the ratio between two integers. Any real number can be approximated by the ratio between two integers.
We will choose here to approximate any real `r` by an expression `numerator / 2^exponent`, where both `numerator` and `exponent` are integers. We will choose here to approximate any real `r` by an expression `numerator / 2^exponent`, where both `numerator` and `exponent` are integers.
$$
\forall r \, \in \mathbb{R}, \quad r \simeq \frac{a}{2^b} \quad \text{with} \quad [a, \, b] \in \mathbb{Z}^2.
$$
In practice, if we fix the value for the exponent $b$ we can compute the numerator $a$ through $a = r \cdot 2^b$, which gives an approximation of $r$ as $r_{\text{approx}} = a \cdot 2^{-b}$.
The higher the numerator and exponent values, the more accurate the approximation can be. For example, 0.65 can be approximated successively by: The higher the numerator and exponent values, the more accurate the approximation can be. For example, 0.65 can be approximated successively by:
* 1 / 2<sup>1</sup> = 0.5 * 1 / 2<sup>1</sup> = 0.5
* 3 / 2<sup>2</sup> = 0.75 * 3 / 2<sup>2</sup> = 0.75
* 5 / 2<sup>3</sup> = 0.625 * 5 / 2<sup>3</sup> = 0.625
* ... * ...
The highest possible numbers will therefore be chosen, within the limits set by the system, i.e. by the number of bits available to encode these numbers. The highest possible numbers will therefore be chosen, within the limits set by the system, i.e. by the number of bits available to encode these numbers.
As part of the TP, the number of bits allowed to store the numerator will be arbitrarily fixed, and the effect on the accuracy of the approximation will be calculated. Note that if you have N bits to store an integer, the largest possible integer is 2<sup>N</sup> - 1. As part of the hands-on, the number of bits allowed to store the numerator will be arbitrarily fixed, and the effect on the accuracy of the approximation will be calculated. Note that if you have N bits to store an integer, the largest possible integer is 2<sup>N</sup> - 1.
The file you need to start is [provided](/notebooks/TP/1-ProceduralProgramming/initial_file.cpp) in the TP folder of ProceduralProgramming lectures. The file you need to start is [provided](../HandsOn/1-ProceduralProgramming/initial_file.cpp) in the _HandsOn_ folder of ProceduralProgramming lectures.
*Note*: you may found more about the context of the formation exercises in those articles:
* ["Exposing Floating Points](https://ciechanow.ski/exposing-floating-point/)
* ["What Every Computer Scientist Should Know About Floating-Point Arithmetic"](https://dl.acm.org/doi/pdf/10.1145/103162.103163).
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### __EXERCICE 1: Adding a loop__ ### __EXERCISE 1: Adding a loop__
Write a loop that displays the like of above up to the 2<sup>8</sup> for 0.65. Write a loop that displays the like of above up to the 2<sup>8</sup> for 0.65.
_Expected result_: _Expected result_:
0.65 ~ 1 / 2^1 0.65 ~ 1 / 2^1
0.65 ~ 3 / 2^2 0.65 ~ 3 / 2^2
0.65 ~ 5 / 2^3 0.65 ~ 5 / 2^3
0.65 ~ 10 / 2^4 0.65 ~ 10 / 2^4
0.65 ~ 21 / 2^5 0.65 ~ 21 / 2^5
0.65 ~ 42 / 2^6 0.65 ~ 42 / 2^6
0.65 ~ 83 / 2^7 0.65 ~ 83 / 2^7
0.65 ~ 166 / 2^8 0.65 ~ 166 / 2^8
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
© _CNRS 2016_ - _Inria 2018-2019_ [© Copyright](../COPYRIGHT.md)
_This notebook is an adaptation of a lecture prepared by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_
_The present version has been written by Sébastien Gilles and Vincent Rouvreau (Inria)_
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.