a = 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
print(a)
Numbers that are potentially not whole (3.14, 5.0, 4.0, 10000.5) there is a number fixed of digits that can be in a float
Floats are stored in scientific notation:
$$a\times2^b$$.
For example, $1.2345\times10^7$ is a number writter in scientific notation.
$a$ is called the mantissa, and $b$ is called the exponent. The number of digits that the mantissa and the exponent contain are limited (the equivalent of about 3 digits for the exponent and 17 digits for the mantissa. This limits the precision with which floating-point numbers can be stored and the magnitude of the numbers.
Because the exponent can have only a limited number of digits, a float cannot be arbitrarily large (or arbitrarily close to 0).
Because the matissa can only have a limited number of digits, a float
cannot be arbitrarily precise (i.e., basically, it cannot have arbitrarily #many digits after the decimal points).
Here is an example: we cannot store $10^350$ as a float
(Python considers the power of 10.0 to always be a float
)
10.0**350
We can store it as an int
10**350
We can convert between different types of values. For example, the value of str(3.14)
is "3.14"
(3.14
is a float
, but "3.14"
is a str
). The value of float("3.14")
is 3.14
(a float
). Here are some examples.
"42" + str(42)
42 + int(42)
'42' + 42
The last one didn't work, since we cannot add together a string and an integer.
We can convert between int
s and float
s:
int(3.14)
int(4.85)
int(-3.7)
What happens is that the fractional part is discarded (this is called "truncation"). This is not the same as rounding. If we know that x is positive, we can round as follows:
x = 3.7
int(x + 0.5)
x = 3.2
int(x + 0.5)
Work out why this works, and what should be done with negative numbers. In Python, you can always just use round
:
round(3.7)
round(-3.2)
Finally, you can convert from strings to float
s like this:
float("3.14")
This would not work, since 3.14 is not an whole number:
int("3.14")
However, this works fine:
int(float("3.14"))
float("3.14")
has the value 3.14
, which in turn can be converted to int
.