issue79:tutoriel_-_python_49
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue79:tutoriel_-_python_49 [2014/03/16 21:02] – [3] fredphil91 | issue79:tutoriel_-_python_49 [2014/03/29 17:57] (Version actuelle) – [8] auntiee | ||
---|---|---|---|
Ligne 10: | Ligne 10: | ||
WHAT? | WHAT? | ||
- | Alors que je travaillais | + | Alors que j' |
- | Prenez par exemple un simple calcul : 1.1 + 2.2 | + | Prenez par exemple un simple calcul : 1.1 + 2.2 |
- | La réponse, direz-vous, est 3,3 ! Toute enfant qui a manipulé des fractions à l' | + | La réponse, direz-vous, est 3.3 ! Toute enfant qui a manipulé des fractions à l' |
QUOI ?!?!? | QUOI ?!?!? | ||
Ligne 31: | Ligne 31: | ||
Maintenant, confus, vous tapez à l' | Maintenant, confus, vous tapez à l' | ||
- | 1.1+2.2 | + | 1.1+2.2 |
- | Et la réponse est : 3,3000000000000003 Vous regardez fixement l' | + | Et la réponse est : 3.3000000000000003 Vous regardez fixement l' |
- | 2.2+3.3 | + | 2.2+3.3 |
- | 5.5 Maintenant, vous êtes encore plus confus et vous vous dites « D' | + | 5.5 Maintenant, vous êtes encore plus confus et vous vous dites : « D' |
====== 3 ====== | ====== 3 ====== | ||
Ligne 61: | Ligne 61: | ||
====== 4 ====== | ====== 4 ====== | ||
- | Ok. Reality is back. No, not really. Python is simply showing you a rounded version of the answer. So, how do we see the “real” answer? We can use the decimal library to see what’s really happening. | + | **Ok. Reality is back. No, not really. Python is simply showing you a rounded version of the answer. So, how do we see the “real” answer? We can use the decimal library to see what’s really happening. |
from decimal import * | from decimal import * | ||
Ligne 70: | Ligne 70: | ||
Decimal(1.1+2.2) | Decimal(1.1+2.2) | ||
- | Decimal(' | + | Decimal(' |
+ | |||
+ | Bon. La réalité est de retour. Non, pas vraiment. Python vous montre simplement une version arrondie de la réponse. Alors, comment voyons-nous la « vraie » réponse ? Nous pouvons utiliser la bibliothèque décimale pour voir ce qui se passe réellement. | ||
+ | |||
+ | from decimal import * | ||
+ | Decimal(1/ | ||
+ | |||
+ | Decimal(' | ||
+ | |||
+ | Decimal(1.1+2.2) | ||
+ | |||
+ | Decimal(' | ||
====== 5 ====== | ====== 5 ====== | ||
- | This is called Representation Error, and exists in almost every modern programming language (Python, C, C++, Java, and even Fortran and more), and on almost every modern computer. This is because these machines use IEEE-754 floating-point arithmetic which (on most machines and OS platforms) maps to an IEEE-754 double-precision number. This double-precision number has a precision of 53 bits. So, our 0.1, when represented in this 53-bit double-precision, | + | **This is called Representation Error, and exists in almost every modern programming language (Python, C, C++, Java, and even Fortran and more), and on almost every modern computer. This is because these machines use IEEE-754 floating-point arithmetic which (on most machines and OS platforms) maps to an IEEE-754 double-precision number. This double-precision number has a precision of 53 bits. So, our 0.1, when represented in this 53-bit double-precision, |
- | So what do we do about it? Well, the quick answer is that you probably can live with it for 90% of the things we have to do out there in the real world – by using the round() method. While you have to decide on the number of decimal points that you must have in your world to carry the precision that you need, for the most part, this will be an acceptable workaround. | + | So what do we do about it? Well, the quick answer is that you probably can live with it for 90% of the things we have to do out there in the real world – by using the round() method. While you have to decide on the number of decimal points that you must have in your world to carry the precision that you need, for the most part, this will be an acceptable workaround.** |
+ | |||
+ | C'est ce qu'on appelle une Erreur de représentation, | ||
+ | |||
+ | Alors, que faisons-nous avec ça ? Eh bien, la réponse rapide est que vous pouvez probablement vivre avec dans 90% des cas que nous avons à traiter dans le monde réel - en utilisant la méthode round(). Vous devrez décider le nombre de décimales dont vous avez besoin dans votre monde pour avoir la précision dont vous avez besoin, mais la plupart du temps ce sera une solution acceptable. | ||
====== 6 ====== | ====== 6 ====== | ||
- | I honestly don’t remember if we have gone over the round method, so I’ll briefly go over it. The syntax is very simple: round(v,d) where v is the value you want to round and d is the number of decimals (maximum) you want after the decimal point. According to the Python documentation, | + | **I honestly don’t remember if we have gone over the round method, so I’ll briefly go over it. The syntax is very simple: round(v,d) where v is the value you want to round and d is the number of decimals (maximum) you want after the decimal point. According to the Python documentation, |
+ | |||
+ | Je ne me souviens pas vraiment si nous avons vu la méthode round, donc je vais la décrire brièvement. La syntaxe est très simple : round(v,d) où v est la valeur que vous souhaitez arrondir et d est le nombre de décimales (maximum) que vous voulez après la virgule. Selon la documentation Python, « Les valeurs sont arrondies au plus proche multiple de 10 à la puissance moins n, si deux multiples sont à égale distance, l' | ||
====== 7 ====== | ====== 7 ====== | ||
- | For example, let’s use the value of pi that comes from the math library. (You must import the math library before you can do this, by the way.) | + | **For example, let’s use the value of pi that comes from the math library. (You must import the math library before you can do this, by the way.) |
math.pi | math.pi | ||
Ligne 90: | Ligne 107: | ||
3.14159 That is the “standard” value of pi that most everyone knows off the top of their head. That’s great. However, if we set the number of decimal places to be returned to 4, look what happens. | 3.14159 That is the “standard” value of pi that most everyone knows off the top of their head. That’s great. However, if we set the number of decimal places to be returned to 4, look what happens. | ||
+ | |||
+ | round(math.pi, | ||
+ | |||
+ | Par exemple, utilisons la valeur de pi qui provient de la bibliothèque mathématique. (Vous devez importer la bibliothèque « math » avant de pouvoir le faire, d' | ||
+ | |||
+ | math.pi | ||
+ | |||
+ | 3.141592653589793 Maintenant, si nous voulons arrondir cette valeur à 5 décimales, on peut utiliser : | ||
+ | |||
+ | round(math.pi, | ||
+ | |||
+ | 3.14159 C'est la valeur « standard » de pi que presque tout le monde connaît par cœur. C'est très bien. Cependant, si nous fixons le nombre de décimales à renvoyer à 4, regardez ce qui se passe. | ||
round(math.pi, | round(math.pi, | ||
====== 8 ====== | ====== 8 ====== | ||
- | 3.1416 All that sounds good until you run into a value like 2.675 and try to round it to 2 decimal places. The assumption (since it is exactly halfway between 2.67 and 2.68) is that the returned value will be 2.68. Try it. | + | **3.1416 All that sounds good until you run into a value like 2.675 and try to round it to 2 decimal places. The assumption (since it is exactly halfway between 2.67 and 2.68) is that the returned value will be 2.68. Try it. |
round(2.675, | round(2.675, | ||
Ligne 102: | Ligne 131: | ||
The bottom line here is when trying to compare floating-point numbers, be aware that some things just don’t translate well. | The bottom line here is when trying to compare floating-point numbers, be aware that some things just don’t translate well. | ||
- | See you next time! | + | See you next time!** |
+ | |||
+ | 3,1416 Tout cela fonctionne parfaitement, | ||
+ | |||
+ | round(2.675, | ||
+ | |||
+ | 2.67 Cela pourrait poser un problème. Et on revient à la question initiale dont nous parlions. Avec la conversion en un nombre binaire à virgule flottante de 53 bits de long, le nombre devient : 2, | ||
+ | |||
+ | L' | ||
+ | |||
+ | Rendez-vous la prochaine fois ! | ||
issue79/tutoriel_-_python_49.1395000157.txt.gz · Dernière modification : 2014/03/16 21:02 de fredphil91