B1 · Episode 3 · 4:15
Python Number Types: Integers and Floats
This episode explains Python's integer and floating-point number types, highlighting that integers have unlimited precision and exact arithmetic, while floats have limited precision. It also covers how the presence of a decimal point 'contaminates' calculations by converting them to float operations.
Source:jmbarbier / peertube.solidev.net · License:CC BY-SA 4.0
Transcript
à part évidemment la mémoire, la taille de la mémoire de l'ordinateur,
Of course, besides memory, there’s also the computer’s memory capacity,
et plus les nombres sont gros, forcément ça prendra plus de temps.
And the larger the numbers, the longer the computation inevitably takes.
Mais on peut avoir des nombres absolument monstrueux.
But we can use absolutely enormous numbers.
Tant que ça reste des nombres entiers.
As long as they remain integers.
Donc l'avantage des nombres entiers.
So the advantage of integers is:
On n'a pas d'erreur d'arrondi.
No rounding errors occur.
On reste sur des calculs exacts tout le temps, et
We always perform exact calculations,
leur taille. en Python.
and their size in Python,
est illimité, à part jusque dans la limite.
is theoretically unlimited—limited only by
de la mémoire de l'ordi
the computer’s memory.
d'accord ce qui nous permet de pouvoir écrire des trucs monstrueux comme ça print
Okay, this lets us write huge numbers like this: print
deux puissance mille avec un résultat exact alors que vous prendriez votre
2 ** 1000, yielding an exact result—whereas if you used your
calculatrice ou un résultat en faisant la même chose mais avec un nombre flottant
calculator—or performed the same calculation with a floating-point number—
print deux points zéro ou même deux points, comme ça,
for example, writing it as 2.0 or even just 2.,
ça suffit pour dire que c'est un nombre
is enough to indicate it’s a floating-point number,
flottant, puissance mille.
raised to the power of 1000.
Là vous voyez que j'ai
Here you can see that
beaucoup moins de précision.
my precision is much lower.
Donc voilà, dans la mesure du raisonnable,
In short, within reasonable limits,
on va essayer d'utiliser toujours des nombres entiers,
we should always try to use integers,
et éventuellement quand il y a quand même besoin,
and only when truly necessary,
qu'est- ce qui peut transformer un calcul en nombres entiers en calcul avec des
what operation turns integer arithmetic into decimal arithmetic?
virgules, c'est la division.
It’s division.
Donc il y a plusieurs stratégies,
So there are several strategies:
soit on multiplie tout par un certain nombre pour rester en mode entier,
One is to multiply everything by a fixed factor to stay in integer mode,
comme on a eu par exemple quand on peut faire le choix de choisir les dixièmes ou
like choosing tenths or hundredths of a cent when storing and calculating prices,
les centièmes de centimes pour stocker et calculer avec des prix,
as we discussed earlier;
ou alors l'autre possibilité, c'est on fait tous nos calculs et on fait la division
The other option is to do all our calculations first,
en dernier lieu uniquement à la fin.
and only perform division at the very end.
Donc il vaut mieux faire un x 100
So it’s better to multiply by 100 first
puis divisé par 1000 qu'un
then divide by 1000, rather than dividing by 1000 first
divisé par 1000 suivi d'un x 100.
then multiplying by 100.
Voilà pour les nombres, d'accord?
That’s all for numbers—got it?
Donc si je résume, vous avez à votre disposition les nombres entiers et les nombres
To sum up, you have two types of numbers available: integers and decimal numbers.
à virgule. Les nombres entiers, leur précision, elle est.
For integers, their precision is…
illimitée, leur taille est illimitée, les calculs sont exacts avec,
unlimited; their size is unlimited; calculations are exact;
vous avez deux divisions à votre disposition, division entière,
you have two division operations: integer division and floating-point division;
division virgule, et vous avez le reste de la division qui est le modulo.
and you also have the remainder operation—the modulo.
Lorsque, à partir du moment où il y a un point quelque part
As soon as a decimal point appears somewhere in a calculation,
dans un calcul, l'ensemble du calcul devient un calcul avec des virgules.
the entire calculation becomes a floating-point calculation.
Donc c'est toujours le plus
So, the ‘weaker’ type always dominates—
mauvais qui l'emporte, c'est toujours le point,
specifically, the decimal point—
le flottant qui l'emporte dans un calcul.
the floating-point number takes precedence in any calculation.
Petite remarque, le flottant
A small note: floating-point numbers
l'emporte. Toujours sur l'entier.
always take precedence over integers.
D'accord, si j'ai un calcul avec plein de nombres
Understood? For example, if I have a calculation with many integers,
entiers et que au milieu de ce truc- là, j'ai un nombre.
and right in the middle of it, I include a number—
Avec une virgule, mon résultat sera avec une virgule.
one with a decimal point—then my final result will also be a decimal number.
Dès que j'ai basculé en fait dans un nombre flottant, je reste en flottant.
Once you’ve actually switched to floating-point numbers, you stay in floating-point mode.
Donc exemple, je sais pas, un calcul
For instance, consider this calculation:
print 3 fois 5 moins 2 plus 8.
print 3 times 5 minus 2 plus 8
fois un virgule, j'ai que des nombres entiers sauf ce petit ici qui est un nombre,
times a decimal point—all numbers here are integers, except this one, which is a decimal number,
ben là vous voyez que mon résultat est un nombre à virgule.
and there—you see my result is a decimal number.
Donc le flottant va contaminer entre guillemets l'entier.
In other words, the floating-point number ‘contaminates’ the integers.
Il faut s'en rappeler
We need to remember this
pour savoir dans quel ordre par exemple faire les calculs.
for example, to know the order in which calculations should be performed.
Précisons, les parenthèses
Specifically, parentheses
fonctionnent pour les calculs.
work in calculations.
Donc là ici en fait je les ai pas mises mais.
So here, I actually didn’t include parentheses, but...
On est comme en maths en fait, ici c'est le 3 fois 5 qui est fait d'abord,
In fact, it’s just like in math: first we calculate 3 times 5,
puis le 8 fois 1, et ensuite après je vais faire 15 moins 2 plus 8.
then 8 times 1, and finally 15 minus 2 plus 8.
Et en fait là ici, le plus 8.
And here, this plus 8,
Comme j'avais un point, c'est devenu un 8.0, un 8 flottant,
because there was a decimal point earlier, became 8.0 — a floating-point number,
et c'est ça qui fait que l'ensemble du calcul devient un calcul flottant.
and that’s why the entire calculation becomes a floating-point operation.
Voilà pour les nombres.
That’s all for numbers.