Skip to content
Snippets Groups Projects
Commit a8a719c9 authored by paul.albuquer's avatar paul.albuquer
Browse files
Added and updated lesson 4 to version 2020-21
parents 6194d4af bf4583cd
No related branches found
No related tags found
No related merge requests found
...@@ -386,17 +386,132 @@ $$ ...@@ -386,17 +386,132 @@ $$
* Nombres de $0=0000.0000$ à $15.9375=1111.1111$. * Nombres de $0=0000.0000$ à $15.9375=1111.1111$.
* Beaucoup de "trous" (au moins $0.0625$) entre deux nombres. * Beaucoup de "trous" (au moins $0.0625$) entre deux nombres.
## Solution? ## Solution partielle?
. . . . . .
* Rajouter des bits. * Rajouter des bits.
* Bouger la virgule. * Bouger la virgule.
# Nombres à virgule flottante (1/N) # Nombres à virgule flottante (1/2)
## Notation scientifique
* Les nombres sont représentés en terme:
* Une mantisse
* Une base
* Un exposant
$$
\underbrace{22.1214}_{\mbox{nombre}}=\underbrace{221214}_{\mbox{mantisse}}\cdot
{\underbrace{10}_{\mbox{base}}}{\overbrace{^{-4}}^{\mbox{exp.}}},
$$
. . .
On peut donc séparer la représentation en 2:
* La mantisse
* L'exposant
# Nombres à virgule flottante (2/2)
## Quel est l'avantage?
. . .
On peut représenter des nombres sur énormément d'ordres de grandeur avec un
nombre de bits fixés.
## Différence fondamentale avec la virgule fixe?
. . .
La précision des nombres est **variable**:
* On a uniquement un nombre de chiffres **significatifs**.
$$
123456\cdot 10^23+ 123456\cdot 10^{-23}.
$$
## Quel inconvénient y a-t-il?
. . .
Ce mélange d'échelles entraîne un **perte de précision**.
# Nombres à virgule flottante simple précision (1/N)
Aussi appelés *IEEE 754 single-precision binary floating point*.
![Nombres à virgule flottante 32 bits. Source:
[Wikipedia](https://en.wikipedia.org/wiki/Single-precision_floating-point_format#/media/File:Float_example.svg)](figs/Float_example_bare.svg)
## Spécification
* 1 bit de signe,
* 8 bits d'exposant,
* 23 bits de mantisse.
$$
(-1)^{b_{31}}\cdot 2^{(b_{30}b_{29}\dots b_{23})_{2}-127}\cdot (1.b_{22}b_{21}\dots b_{0})_{2},
$$
## Calculer la valeur décimale du nombre ci-dessus
# Nombres à virgule flottante simple précision (2/N)
![Un exercice de nombres à virgule flottante 32 bits. Source:
[Wikipedia](https://en.wikipedia.org/wiki/Single-precision_floating-point_format#/media/File:Float_example.svg)](figs/Float_example.svg)
. . .
\begin{align}
\mbox{exposant}&=\sum_{i=0}^7 b_{23+i}2^i=2^2+2^3+2^4+2^5+2^6=124-127,\\
\mbox{mantisse}&=1+\sum_{i=1}^{23}b_{23-i}2^{-i}=1+2^{-2}=1.25,\\
&\Rightarrow (-1)^0\cdot 2^{-3}\cdot 1.25=0.15625
\end{align}
# Nombres à virgule flottante simple précision (3/N)
## Quel nombre ne peux pas être vraiment représenté?
. . .
## Zéro: exception pour l'exposant
* Si l'exposant est `00000000` (zéro)
$$
(-1)^{\mbox{sign}}\cdot 2^{-126}\cdot 0.\mbox{mantisse},
$$
* Sinon si l'exposant est `00000001` à `11111110`
$$
\mbox{valeur normale},
$$
* Sinon `1111111` donne `NaN`.
# Nombres à virgule flottante simple précision (4/N)
## Quels sont les plus petits/grands nombres positifs représentables?
. . .
\begin{align}
0\ 0\dots0\ 0\dots01&=2^{-126}\cdot 2^{-23}=1.4...\cdot
10^{-45},\\
0\ 1\dots10\ 1\dots1&=2^{127}\cdot (2-2^{-23})=3.4...\cdot
10^{38}.
\end{align}
## Combien de chiffres significatifs en décimal?
. . .
* 24 bits ($23 + 1$) sont utiles pour la mantisse, soit $2^{24}-1$:
* $\sim2^{24}-1\sim 10^7$, ou encore
* $\sim \log_10(2^{24})\sim 7$,
* Environ **sept** chiffres significatifs.
# Erreurs d'arrondi
<!-- # TODO -- <!-- # TODO --
......
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="590" height="75">
<style>
text {
font-size: 14px;
font-family: Arial, sans-serif;
text-anchor: middle;
}
</style>
<defs>
<g id="bit-0">
<rect width="15" height="26" stroke="#000"/>
<text x="8" y="18.3" fill="#000">0</text>
</g>
<g id="bit-1">
<rect width="15" height="26" stroke="#000"/>
<text x="8" y="18.3" fill="#000">1</text>
</g>
</defs>
<g transform="translate(14.25,28.75)">
<use xlink:href="#bit-0" fill="#c5fcff"/>
<g fill="#9fffad">
<use xlink:href="#bit-0" x="15"/>
<use xlink:href="#bit-1" x="30"/>
<use xlink:href="#bit-1" x="45"/>
<use xlink:href="#bit-1" x="60"/>
<use xlink:href="#bit-1" x="75"/>
<use xlink:href="#bit-1" x="90"/>
<use xlink:href="#bit-0" x="105"/>
<use xlink:href="#bit-0" x="120"/>
</g>
<g fill="#ffaead">
<use xlink:href="#bit-0" x="135"/>
<use xlink:href="#bit-1" x="150"/>
<use xlink:href="#bit-0" x="165"/>
<use xlink:href="#bit-0" x="180"/>
<use xlink:href="#bit-0" x="195"/>
<use xlink:href="#bit-0" x="210"/>
<use xlink:href="#bit-0" x="225"/>
<use xlink:href="#bit-0" x="240"/>
<use xlink:href="#bit-0" x="255"/>
<use xlink:href="#bit-0" x="270"/>
<use xlink:href="#bit-0" x="285"/>
<use xlink:href="#bit-0" x="300"/>
<use xlink:href="#bit-0" x="315"/>
<use xlink:href="#bit-0" x="330"/>
<use xlink:href="#bit-0" x="345"/>
<use xlink:href="#bit-0" x="360"/>
<use xlink:href="#bit-0" x="375"/>
<use xlink:href="#bit-0" x="390"/>
<use xlink:href="#bit-0" x="405"/>
<use xlink:href="#bit-0" x="420"/>
<use xlink:href="#bit-0" x="435"/>
<use xlink:href="#bit-0" x="450"/>
<use xlink:href="#bit-0" x="465"/>
</g>
<g fill="none" stroke="#000" stroke-width="2">
<rect width="480" height="26"/>
<path d="m15,0 v26 M135,0 v26" fill="none" stroke="#000"/>
</g>
<path d="m7.5,-3.5 v-9.5" stroke="#008080"/>
<path d="m16,-3.5 v-9.5 h118 v9.5" stroke="#008000" fill="none"/>
<path d="m136,-3.5 v-9.5 h343 v9.5" stroke="#800000" fill="none"/>
<text y="-18">
<tspan x="2">sign</tspan>
<tspan x="75">exponent (8 bits)</tspan>
<tspan x="307">fraction (23 bits)</tspan>
</text>
<g transform="translate(0,30.5)" fill="#000">
<circle r="2" cx="7.5"/>
<circle r="2" cx="22.5"/>
<circle r="2" cx="127.5"/>
<circle r="2" cx="142.5"/>
<circle r="2" cx="472.5"/>
</g>
<text y="45.5">
<tspan x="6">31</tspan>
<tspan x="27">30</tspan>
<tspan x="126">23</tspan>
<tspan x="146">22</tspan>
<tspan x="472.5">0</tspan>
</text>
<text x="243" y="44.5">(bit index)</text>
</g>
<text x="504" y="48.4" style="text-anchor: start; font-size: 18.7px;">= 0.15625</text>
</svg>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="491.4209"
height="75.638672"
version="1.1"
id="svg134"
sodipodi:docname="Float_example_bare.svg"
inkscape:version="0.92.5 (2060ec1f9f, 2020-04-08)">
<metadata
id="metadata138">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<sodipodi:namedview
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1"
objecttolerance="10"
gridtolerance="10"
guidetolerance="10"
inkscape:pageopacity="0"
inkscape:pageshadow="2"
inkscape:window-width="1884"
inkscape:window-height="1052"
id="namedview136"
showgrid="false"
fit-margin-top="0"
fit-margin-left="0"
fit-margin-right="0"
fit-margin-bottom="0"
inkscape:zoom="1.2915254"
inkscape:cx="478.17825"
inkscape:cy="-102.24671"
inkscape:window-x="36"
inkscape:window-y="0"
inkscape:window-maximized="1"
inkscape:current-layer="svg134" />
<style
id="style2">
text {
font-size: 14px;
font-family: Arial, sans-serif;
text-anchor: middle;
}
</style>
<defs
id="defs14">
<g
id="bit-0">
<rect
width="15"
height="26"
id="rect4"
x="0"
y="0"
style="stroke:#000000" />
<text
x="8"
y="18.299999"
id="text6"
style="font-size:14px;font-family:Arial, sans-serif;text-anchor:middle;fill:#000000">0</text>
</g>
<g
id="bit-1">
<rect
width="15"
height="26"
id="rect9"
x="0"
y="0"
style="stroke:#000000" />
<text
x="8"
y="18.299999"
id="text11"
style="font-size:14px;font-family:Arial, sans-serif;text-anchor:middle;fill:#000000">1</text>
</g>
</defs>
<g
transform="translate(10.420898,28.192383)"
id="g130">
<use
xlink:href="#bit-0"
id="use16"
style="fill:#c5fcff"
x="0"
y="0"
width="100%"
height="100%" />
<g
id="g34"
style="fill:#9fffad">
<use
xlink:href="#bit-0"
x="15"
id="use18"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-1"
x="30"
id="use20"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-1"
x="45"
id="use22"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-1"
x="60"
id="use24"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-1"
x="75"
id="use26"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-1"
x="90"
id="use28"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="105"
id="use30"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="120"
id="use32"
y="0"
width="100%"
height="100%" />
</g>
<g
id="g82"
style="fill:#ffaead">
<use
xlink:href="#bit-0"
x="135"
id="use36"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-1"
x="150"
id="use38"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="165"
id="use40"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="180"
id="use42"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="195"
id="use44"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="210"
id="use46"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="225"
id="use48"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="240"
id="use50"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="255"
id="use52"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="270"
id="use54"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="285"
id="use56"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="300"
id="use58"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="315"
id="use60"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="330"
id="use62"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="345"
id="use64"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="360"
id="use66"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="375"
id="use68"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="390"
id="use70"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="405"
id="use72"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="420"
id="use74"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="435"
id="use76"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="450"
id="use78"
y="0"
width="100%"
height="100%" />
<use
xlink:href="#bit-0"
x="465"
id="use80"
y="0"
width="100%"
height="100%" />
</g>
<g
id="g88"
style="fill:none;stroke:#000000;stroke-width:2">
<rect
width="480"
height="26"
id="rect84"
x="0"
y="0" />
<path
d="M 15,0 V 26 M 135,0 v 26"
id="path86"
inkscape:connector-curvature="0"
style="fill:none;stroke:#000000" />
</g>
<path
d="M 7.5,-3.5 V -13"
id="path90"
inkscape:connector-curvature="0"
style="stroke:#008080" />
<path
d="M 16,-3.5 V -13 h 118 v 9.5"
id="path92"
inkscape:connector-curvature="0"
style="fill:none;stroke:#008000" />
<path
d="M 136,-3.5 V -13 h 343 v 9.5"
id="path94"
inkscape:connector-curvature="0"
style="fill:none;stroke:#800000" />
<text
y="-18"
id="text102"
style="font-size:14px;font-family:Arial, sans-serif;text-anchor:middle">
<tspan
x="2"
id="tspan96">sign</tspan>
<tspan
x="75"
id="tspan98">exponent (8 bits)</tspan>
<tspan
x="307"
id="tspan100">fraction (23 bits)</tspan>
</text>
<g
transform="translate(0,30.5)"
id="g114"
style="fill:#000000">
<circle
r="2"
cx="7.5"
id="circle104"
cy="0" />
<circle
r="2"
cx="22.5"
id="circle106"
cy="0" />
<circle
r="2"
cx="127.5"
id="circle108"
cy="0" />
<circle
r="2"
cx="142.5"
id="circle110"
cy="0" />
<circle
r="2"
cx="472.5"
id="circle112"
cy="0" />
</g>
<text
y="45.5"
id="text126"
style="font-size:14px;font-family:Arial, sans-serif;text-anchor:middle">
<tspan
x="6"
id="tspan116">31</tspan>
<tspan
x="27"
id="tspan118">30</tspan>
<tspan
x="126"
id="tspan120">23</tspan>
<tspan
x="146"
id="tspan122">22</tspan>
<tspan
x="472.5"
id="tspan124">0</tspan>
</text>
<text
x="243"
y="44.5"
id="text128"
style="font-size:14px;font-family:Arial, sans-serif;text-anchor:middle">(bit index)</text>
</g>
</svg>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment