Barbie explique IEEE754 : pourquoi les calculs informatiques sont faux!

Preview:

Citation preview

Shaker technologies Javascript-IE

EE

754 version 1.1

IEEE754Manipulation des

nombres flottants : exemples avec

Javascript

Shaker technologies Javascript-IE

EE

754 version 1.1

Human talkerMickaël Ruau : !• consultant en qualité logicielle

(forges logicielles)

• spécialiste de l’informatique médicale (norme DICOM)

• formateur à l’AFPA d’Angers

Shaker technologies Javascript-IE

EE

754 version 1.1

Référenceshttp://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

Shaker technologies Javascript-IE

EE

754 version 1.1

DisclaimerLe 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!

Shaker technologies Javascript-IE

EE

754 version 1.1

Shaker technologies Javascript-IE

EE

754 version 1.1

Shaker technologies Javascript-IE

EE

754 version 1.1

Shaker technologies Javascript-IE

EE

754 version 1.1

Shaker technologies Javascript-IE

EE

754 version 1.1

La minute nécessaire

Shaker technologies Javascript-IE

EE

754 version 1.1

Etonnant, non?!console.log(0.1+0.2); //0.30000000000000004

Shaker technologies Javascript-IE

EE

754 version 1.1

Etonnant, non?! (suite)console.log(0.1*0.2); //0.020000000000000004

Shaker technologies Javascript-IE

EE

754 version 1.1

Etonnant, non?! (re-suite)console.log(0.11/0.10); //1.0999999999999999

Shaker technologies Javascript-IE

EE

754 version 1.1

Etonnant, non?! (fin)console.log(0.3 - 0.2 ); //0.09999999999999998

Shaker technologies Javascript-IE

EE

754 version 1.1

Explication

Shaker technologies Javascript-IE

EE

754 version 1.1

La cancellationhttp://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 ».

Shaker technologies Javascript-IE

EE

754 version 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 !

Shaker technologies Javascript-IE

EE

754 version 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.

Shaker technologies Javascript-IE

EE

754 version 1.1

Personne ne coule?Vers l’infini et au-delà avec les flottants…

Shaker technologies Javascript-IE

EE

754 version 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

Shaker technologies Javascript-IE

EE

754 version 1.1

Mini, mini, miniconsole.log(1.00000000000000009);// affiche 1 !console.log(0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001); // affiche 0

Shaker technologies Javascript-IE

EE

754 version 1.1

" Il faut une infinie patience pour attendre toujours ce qui n'arrive jamais. " (PIERRE DAC)

console.log(999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999); //affiche Infinity

Shaker technologies Javascript-IE

EE

754 version 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

Shaker technologies Javascript-IE

EE

754 version 1.1

Explication

Shaker technologies Javascript-IE

EE

754 version 1.1

IEEE 754 overflowhttp://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.

Shaker technologies Javascript-IE

EE

754 version 1.1

Quelles solutions?

photo : http://blog.megdesk.com/computer-engineer-barbie-revised/

Shaker technologies Javascript-IE

EE

754 version 1.1

Number() = IEEE 64 bitJavaScript 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");

Shaker technologies Javascript-IE

EE

754 version 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);

Shaker technologies Javascript-IE

EE

754 version 1.1

Shaker technologies Javascript-IE

EE

754 version 1.1

Shaker technologies Javascript-IE

EE

754 version 1.1

@HeforSheDedicated 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.

Recommended