## Numerical types in Python¶

There are two numerical types of balues in Python

### Intergers (int)¶

Whole number (positive or negative). In Python 3, ints can be as large as you like, as long as there is enough space in memory to store them (so effectively, the size of an int is unlimited.

In :
a = 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
print(a)

100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000


### Floating point number (float)¶

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)

In :
10.0**350

---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
<ipython-input-2-33a9a401a09d> in <module>()
----> 1 10.0**350

OverflowError: (34, 'Numerical result out of range')

We can store it as an int

In :
10**350

Out:
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

### Converting between ints, floats, and strs¶

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.

In :
"42" + str(42)

Out:
'4242'
In :
42 + int(42)

Out:
84
In :
'42' + 42

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-baaa611a5d0b> in <module>()
----> 1 '42' + 42

TypeError: Can't convert 'int' object to str implicitly

The last one didn't work, since we cannot add together a string and an integer.

We can convert between ints and floats:

In :
int(3.14)

Out:
3
In :
int(4.85)

Out:
4
In :
int(-3.7)

Out:
-3

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:

In :
x = 3.7
int(x + 0.5)

Out:
4
In :
x = 3.2
int(x + 0.5)

Out:
3

Work out why this works, and what should be done with negative numbers. In Python, you can always just use round:

In :
round(3.7)

Out:
4
In :
round(-3.2)

Out:
-3

Finally, you can convert from strings to floats like this:

In :
float("3.14")

Out:
3.14

This would not work, since 3.14 is not an whole number:

In :
int("3.14")

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
ValueError: invalid literal for int() with base 10: '3.14'
int(float("3.14"))

3
float("3.14") has the value 3.14, which in turn can be converted to int.