## 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 [1]:
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 [2]:
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 [3]:
10**350

Out[3]:
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 [4]:
"42" + str(42)

Out[4]:
'4242'
In [5]:
42 + int(42)

Out[5]:
84
In [6]:
'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 [7]:
int(3.14)

Out[7]:
3
In [8]:
int(4.85)

Out[8]:
4
In [9]:
int(-3.7)

Out[9]:
-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 [10]:
x = 3.7
int(x + 0.5)

Out[10]:
4
In [11]:
x = 3.2
int(x + 0.5)

Out[11]:
3

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

In [12]:
round(3.7)

Out[12]:
4
In [13]:
round(-3.2)

Out[13]:
-3

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

In [14]:
float("3.14")

Out[14]:
3.14

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

In [15]:
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.