EBougnol, JJ Fleck,
MHeckmann & M Kostyra,
Kléber, PCSI& - I2 Repr´
esentation des nombres et cons´
equences 2/4
Code I2.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 iin 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
Code I2.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
Code I2.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