"Floating-point numbers can represent a very large range of numbers, from the smallest to the largest, similarly to scientific notation. They are the prefered types for scientific computing. Yet, one must be aware of the many **rounding errors** which are implied. "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"First, in order to check visually the accuracy of some calculations, let's increase to 18 the output stream precision (this is 6 by default)."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"#include <iostream>\n",
"std::cout.precision(18) ;"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"The floating-point types have a limited number of digits available for their internal representation. Many numbers, such as `1./3.`, cannot be represented exactly:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.333333333333333315\n"
]
}
],
"source": [
"std::cout << (1./3.) << std::endl ;"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Less intuitive, some very simple numbers (for humans) do not have an exact base-two representation:"
"If you wish to perform the same calculations in single precision, just set `typedef` to `float`, compile and run."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## More flexible (but difficult) approach : templates\n",
"\n",
"Make any computing function a template, with the floating type as parameter. This allows to mix different precisions within different steps of a scientific computing application."
"Modern C++ will not bring any silver bullet for the rounding problems of floating point computing. You still have to rely on only some old-fashioned good practice, and externals tools that can help to locate greatest errors (CADNA, verificarlo, verrou)."
"*This document was created by David Chamont and translated by Olga Abramkina. It is available under the [Licence Creative Commons - Attribution - No commercial use - Shared under the conditions 4.0 International](http://creativecommons.org/licenses/by-nc-sa/4.0/)*"