• Aucun résultat trouvé

.com I2

N/A
N/A
Protected

Academic year: 2022

Partager ".com I2"

Copied!
4
0
0

Texte intégral

(1)

I2

Représentation des nombres et conséquences

CodeI2.1: De l’intérêt de toujours définir sa base de comptage...

If you get an 11/100 on a CS test,

.com

but you claim it should be counted as a "C", they'll probably decide you deserve the upgrade.

Code I2.2: 0,1 + 0,2ne donne pas 0,3 !!

>>> 0.1 + 0.1 == 0.2 ## Normal

True>>> 0.2 + 0.2 == 0.4 ## Normal

True>>> 0.1 + 0.2 == 0.3 ## Bizarre !

False

>>> 3*0.1 == 0.3 ## Aussi bizarre ! (mais cohérent avec le précédent)

False

1

(2)

EBougnol, JJFleck, MHeckmann& MKostyra,

Kléber, PCSI& - I2 Repr´esentation des nombres et cons´equences 2/4

CodeI2.3: Pour quelques CS de plus...

>>> t = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1,1.2]

>>> for i in range(1,13):

... print(’%02d*0.1 == %25.25f ?: %s’ % (i,t[i-1],i*0.1 == t[i-1])) ...01*0.1 == 0.1000000000000000055511151 ?: True

02*0.1 == 0.2000000000000000111022302 ?: True 03*0.1 == 0.2999999999999999888977698 ?: False 04*0.1 == 0.4000000000000000222044605 ?: True 05*0.1 == 0.5000000000000000000000000 ?: True 06*0.1 == 0.5999999999999999777955395 ?: False 07*0.1 == 0.6999999999999999555910790 ?: False 08*0.1 == 0.8000000000000000444089210 ?: True 09*0.1 == 0.9000000000000000222044605 ?: True 10*0.1 == 1.0000000000000000000000000 ?: True 11*0.1 == 1.1000000000000000888178420 ?: True 12*0.1 == 1.1999999999999999555910790 ?: False

CodeI2.4: Les calculs sont faux !

>>> a = 10**205+1 ## a est un entier

>>> a-10**205 == 1 ## le calcul en entier est correct

True>>> b = 10**205+1.0 ## b est un flottant

>>> b-10**205 == 1 ## Et là, la limite de précision intervient

False

>>> b - 10**205-1

-1.0

CodeI2.5: Toujours comparer à ε près

>>> epsilon = 0.1 + 0.2 - 0.3

>>> epsilon

5.551115123125783e-17

>>> def compare_a_epsilon_pres(x,y,epsilon=1e-9):

... return abs(x-y)<epsilon

...>>> compare_a_epsilon_pres(0.1+0.2,0.3)

True

(3)

EBougnol, JJFleck, MHeckmann& MKostyra,

Kléber, PCSI& - I2 Repr´esentation des nombres et cons´equences 3/4

CodeI2.6: Même la solution d’une équation du second degré est fausse !

>>> from math import * ## Pour les fonctions mathématiques

>>> a,b,c = 1,-2,-5 ## Une équation du type ax^2 + bx + c = 0

>>> delta = b**2 - 4*a*c ## Le discrimant correspondant

>>> ## Et les deux solutions

>>> x1,x2 = (-b+sqrt(delta))/(2.0*a),(-b-sqrt(delta))/(2.0*a)

>>> ## Ne reste qu’à vérifier si elles marchent

>>> a*x1**2 + b*x1 + c ## Caramba !

-1.7763568394002505e-15

>>> a*x2**2 + b*x2 + c ## Encore raté !

-8.881784197001252e-16

CodeI2.7: Un problème de précision ?

Also, I hear the 4th root of (9^2 + 19^2/22) is pi.

.com

CodeI2.8: Format « simple precision » à 32 bits et « double precision » à 64 bits

(4)

EBougnol, JJFleck, MHeckmann& MKostyra,

Kléber, PCSI& - I2 Repr´esentation des nombres et cons´equences 4/4

CodeI2.9: Next stop: Python

I wrote 20 short programs in Python yesterday. It was wonderful.

.com

Perl, I'm leaving you.

Références

Documents relatifs

Detailed descriptions of the Logic Optimizer, Placer, Router, Delay Modeling, Back Annotation, and ATVG tools can be found in Sections 2 and 3 of this manual.. The Sequencer tool

Later, we'll learn how to tell perl how many repetitions of a character or group of characters we want to match without spelling it out directly. What, then, if we wanted to

Although you don’t need to know a single thing about Perl to begin reading this book, we recommend that you already have familiarity with basic programming concepts such as

• Si encara no tens un perfil a ORCID, fes-te’l i informa’l als diferents perfils acadèmics que tinguis (Ein@/e-Greta, Publons, Scopus, etc.)?. Si ja el tens, identifica què

(e) En déduire que la stabilité de la poutre est assurée pour des valeurs de F supérieures à celles autorisées dans la partie précédente et donner la nouvelle valeur potentielle

Il ne couvre pas tous les aspects du langage, sa lecture ne dispense pas de consulter les ouvrages de r´ef´erences cit´es dans la bibliographie.. Une version ´electronique

Reloading configuration files. First, lets look at a simple case, in which we just have to look after a simple configuration file like the one below. Imagine a script that tells you

[ Library Home | Perl in a Nutshell | Learning Perl | Learning Perl on Win32 | Programming Perl | Advanced Perl Programming | Perl Cookbook ]... Previous: What This Book