Attention sous python (v < 3) quand on divise deux entiers on obtient un nombre arrondi à la valeur entière immédiatement inférieure. Par exemple:
>>> a = 1>>> b = 2>>> c = a / b>>> c0
1/2 donne ici 0 au lieu de 0.5. Note: c est de type float:
>>> type(a)<type 'int'>>>> type(b)<type 'int'>>>> type(c)<type 'float'>
Pour obtenir un nombre décimal il existe plusieurs solutions:
Solution 1:
>>> c = 1.0 * a / b>>> c0.5
Solution 2:
>>> c = float(a) / b>>> c0.5
Solution 3:
>>> from __future__ import division>>> a / b0.5
Recherches associées
| Liens | Site |
|---|---|
| how can I force division to be floating point in Python? | stackoverflow |
