barbie explique ieee754 : pourquoi les calculs informatiques sont faux?
TRANSCRIPT
![Page 1: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/1.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
IEEE754Manipulation des
nombres flottants :
exemples avec
Javascript
![Page 2: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/2.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
Human talker
Mickaël Ruau :
• consultant en qualité logicielle
(forges logicielles)
• spécialiste de l’informatique
médicale (norme DICOM)
• formateur à l’AFPA d’Angers
![Page 3: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/3.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
Références
http://blog.oaxoa.com/2008/03/22/weird-math-aka-ieee-754-
double-precision-floating-point-number-sukcs/
http://grouper.ieee.org/groups/754/faq.html
http://www.haypocalc.com/wiki/Standards_IEEE_754_et_854
http://floating-point-gui.de/languages/javascript/
http://www.ecmascript.org/
Standard ECMA-262 3rd Edition - December 1999
ECMA-262 5.1 Edition - June 2011
![Page 4: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/4.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
Disclaimer
Le but est de sensibiliser
aux problèmes liés aux
calculs informatiques,
notamment en matière
financière (comptabilité,
paie, ecommerce...).
Je ne suis pas
mathématicien.
Je n’ai pas écrit la norme
IEEE754, je ne la connais
pas par coeur!
![Page 5: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/5.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
![Page 6: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/6.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
![Page 7: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/7.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
![Page 8: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/8.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
![Page 9: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/9.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
La minute nécessaire
![Page 10: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/10.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
Etonnant, non?!
console.log(0.1+0.2); //0.30000000000000004
![Page 11: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/11.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
Etonnant, non?! (suite)
console.log(0.1*0.2); //0.020000000000000004
![Page 12: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/12.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
Etonnant, non?! (re-suite)
console.log(0.11/0.10); //1.0999999999999999
![Page 13: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/13.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
Etonnant, non?! (fin)
console.log(0.3 - 0.2 ); //0.09999999999999998
![Page 14: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/14.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
Explication
![Page 15: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/15.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
La cancellation
http://fr.wikipedia.org/wiki/Virgule_flottante#Pr.C3.A9cautions_d.27emploi
Les calculs en virgule flottante (...) présentent divers
désagréments, notamment leur précision limitée, qui se
traduit par des arrondis. (...)
En particulier, la soustraction de deux nombres très proches
provoque une grande perte de précision relative : on parle de
« cancellation ».
![Page 16: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/16.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
Pourquoi 0.1 n’est pas 0.1?
http://www.haypocalc.com/wiki/Standards_IEEE_754_et_854
En gros, on stocke les nombres sous la forme :
(signe, mantisse, exposant)
ce qui donne x = signe * mantisse * (2 ^ exposant).
Le signe vaut +1 ou -1, la mantisse est un nombre réel
tel que 1.0 <= mantisse < 2.0,
et l'exposant est une valeur entière.
Bien sûr, l'ensemble est codé en binaire !
![Page 17: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/17.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
Exemples de format de stockage
Le nombre 2 est stocké (+1, 1, 1),
c'est-à-dire : 2 = (+1) * 1 * (2 ^ 1).
Le nombre 3 est stocké (+1, 1.5, 1) :
3 = (+1) * 1.5 * (2 ^ 1).
Le nombre 10 est stocké (+1, 1.25, 3) :
10 = (+1) * 1.25 * 2^3.
![Page 18: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/18.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
Personne ne coule?Vers l’infini et au-delà avec les flottants…
![Page 19: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/19.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
Passé les bornes, y’a plus de limite!
console.log(1.00000000000000009);// affiche 1
var n1 = 123456789012345672;
console.log(n1); // affiche 123456789012345660
var n2 = 123456789012345673;
console.log(n2); // affiche 123456789012345680
![Page 20: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/20.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
Mini, mini, mini
console.log(1.00000000000000009);// affiche 1
console.log(0.0000000000000000000000000000000000000
000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000001);
// affiche 0
![Page 21: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/21.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
" Il faut une infinie patience pour attendre toujours ce
qui n'arrive jamais. " (PIERRE DAC)
console.log(999999999999999999999999999999999999999
999999999999999999999999999999999999999999999999
999999999999999999999999999999999999999999999999
999999999999999999999999999999999999999999999999
999999999999999999999999999999999999999999999999
999999999999999999999999999999999999999999999999
999999999999999999999999999999);
//affiche Infinity
![Page 22: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/22.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
" Il faut toujours prendre le maximum de risques avec le
maximum de précautions. " Rudyard Kipling
console.log(3+999999999999999);
//affiche 1000000000000002
console.log(3+9999999999999999);
//affiche 1000000000000004
console.log(3+99999999999999999);
//affiche 100000000000000000
console.log(2+99999999999999999);
//affiche 100000000000000000
![Page 23: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/23.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
Explication
![Page 24: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/24.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
IEEE 754 overflow
http://fr.wikipedia.org/wiki/Virgule_flottante
La plage d'exposants est limitée :
• « overflows » lorsque le résultat d'une opération est plus
grand que la plus grande valeur représentable
• « underflows » lorsqu'un résultat est plus petit, en valeur
absolue, que le plus petit flottant normalisé positif
• puis des résultats n'ayant plus aucun sens.
![Page 25: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/25.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
Quelles solutions?
photo : http://blog.megdesk.com/computer-engineer-barbie-revised/
![Page 26: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/26.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
Number() = IEEE 64 bit
JavaScript is dynamically typed and will often convert
implicitly between strings and floating-point numbers (which
are IEEE 64 bit values).
To force a variable to floating-point, use the global
parseFloat() function.
var num = parseFloat("3.5");
![Page 27: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/27.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
https://github.com/dtrebbien/BigDecimal.js
Decimal Types
The best decimal type for JavaScript seems to be a port of
Java’s BigDecimal class, which also supports rounding
modes:
var a = new BigDecimal("0.01");
var b = new BigDecimal("0.02");
var c = a.add(b); // 0.03
var d = c.setScale(1, BigDecimal.prototype.ROUND_HALF_UP);
![Page 28: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/28.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
![Page 29: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/29.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
![Page 30: Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux?](https://reader030.vdocuments.fr/reader030/viewer/2022032617/55acf9831a28ab761d8b45b9/html5/thumbnails/30.jpg)
Shaker te
ch
nolo
gie
s J
avascrip
t -IEE
E754
vers
ion 1
.1
@HeforShe
Dedicated to
Pauline,
Stéphanie,
Sylvie,
Mum…
With love and kisses.
You girls rock in IT!
Credits : Photo : http://violasong.com/2010/03/what-a-computer-engineer-looks-like
https://computer-engineer-barbie.herokuapp.com
Cette présentation constitue une parodie au sens de l’article L.122-5
du Code de la Propriété Intellectuelle.
This non-commercial transformative work constitutes fair use
under Section 107 of the U.S. copyright act.
Use of copyrighted material is necessary
for the purpose of criticism and education, the images are only
at the resolution necessary for this purpose,
and this remix is clearly marked to avoid confusion with the original.