+ 2
What's the deal with floats?
Just started python and I'm a little confused as to why floats are a thing. How are they different from just normal numbers and what effects do they have on the code?
2 Answers
+ 11
Although in our everyday understanding we normally don't care a lot about the difference, a computer has to, because the numbers are stored and calculated differently.
Python makes it relatively easy for you to use them, because whenever a float *might* occur (like when you make a division) it converts the type automatically.
The biggest issue with floats are, that computers can't calculate them precisely - they are often off by a little bit, and if you do a lot of calculations with them, that can quickly add up and lead to problems.
So whenever precision matters, you usually look for some workaround where you make do only with integers.
It is a frequently occurring question, so use our search function here and you will find more information.
+ 9
The two well-known numeric type in Python is `int` which represents a whole number, and `float` which represents a floating point number or fractal number, obviously `float` has support for decimal points while `int` doesn't.
What effects they have?
IIRC a math operation involving a float number operand usually yields also a float.