# Python 3 Numbers: Type Conversion and Mathematics Functions

Number data types store numeric values. They are immutable data types. This means, changing the value of a number data type results in a newly allocated object.

Python supports integers, floating point numbers and complex numbers. They are defined as `int`, `float` and `complex` class in Python.

• int (signed integers) − They are often called just integers or ints. They are positive or negative whole numbers with no decimal point. Integers in Python 3 are of unlimited size. Python 2 has two integer types - int and long. There is no 'long integer' in Python 3 anymore.

• float (floating point real values) − Also called floats, they represent real numbers and are written with a decimal point dividing the integer and the fractional parts. Floats may also be in scientific notation, with `E` or `e` indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).

• complex (complex numbers) − are written in the form, `x + yj`, where x is the real part and y is the imaginary part. Complex numbers are not used much in Python programming.

Number objects are created when you assign a value to them. For example:

``````# Create the number objects
a = 1
b = -1
c = 1.0
d = -1.0
e = +2e3
f = -2e3
g = 3.14j
h = -3.14j``````

We can use the `type()` function to know which class a variable or a value belongs to and `isinstance()` function to check if it belongs to a particular class.

``````a = 5

# Output: <class 'int'>
print(type(a))

# Output: <class 'float'>
print(type(5.0))

# Output: (8+3j)
c = 5 + 3j
print(c + 3)

# Output: True
print(isinstance(c, complex))``````

It is possible to represent an integer in hexa-decimal or octal form:

``````>>> number = 0xA0F #Hexa-decimal
>>> number
2575

>>> number = 0o37 #Octal
>>> number
31``````

Here are some examples of numbers.

int float complex
10 0.0 3.14j
100 15.20 45.j
-786 -21.9 9.322e-36j
080 32.3+e18 .876j
-0490 -90. -.6545+0J
-0×260 -32.54e100 3e+26J
0×69 70.2-E12 4.53e-7j

## Number Type Conversion

We can convert one type of number into another. This is also known as coercion.

Operations like addition, subtraction coerce integer to float implicitly (automatically), if one of the operand is float.

``````>>> 1 + 2.0
3.0``````

We can see above that 1 (integer) is coerced into 1.0 (float) for addition and the result is also a floating point number.

We can also use built-in functions like `int()`, `float()` and `complex()` to convert between types explicitly. These function can even convert from strings.

• Type `int(x)` to convert `x` to a plain integer. There is no 'long integer' in Python 3 anymore.

• Type `float(x)` to convert `x` to a floating-point number.

• Type `complex(x)` to convert `x` to a complex number with real part `x` and imaginary part zero.

• Type `complex(x, y)` to convert `x` and `y` to a complex number with real part `x` and imaginary part `y`. `x` and `y` are numeric expressions

Here are some examples:

``````>>> int(2.3)
2
>>> int(-2.8)
-2
>>> float(5)
5.0
>>> complex('3+5j')
(3+5j)``````

When converting from float to integer, the number gets truncated (integer that is closer to zero).

Here is an article about Python 3 Type Conversion where you can find out more number type conversion examples.

## Python Decimal

Python built-in class float performs some calculations that might amaze us. We all know that the sum of 1.1 and 2.2 is 3.3, but Python seems to disagree.

``````>>> (1.1 + 2.2) == 3.3
False``````

What is going on?

It turns out that floating-point numbers are implemented in computer hardware as binary fractions, as computer only understands binary (0 and 1). Due to this reason, most of the decimal fractions we know, cannot be accurately stored in our computer.

Let's take an example. We cannot represent the fraction 1/3 as a decimal number. This will give 0.33333333... which is infinitely long, and we can only approximate it.

Turns out decimal fraction 0.1 will result into an infinitely long binary fraction of 0.000110011001100110011... and our computer only stores a finite number of it.

This will only approximate 0.1 but never be equal. Hence, it is the limitation of our computer hardware and not an error in Python.

``````>>> 1.1 + 2.2
3.3000000000000003``````

To overcome this issue, we can use decimal module that comes with Python. While floating point numbers have precision up to 15 decimal places, the decimal module has user settable precision.

``````import decimal

# Output: 0.1
print(0.1)

# Output: Decimal('0.1000000000000000055511151231257827021181583404541015625')
print(decimal.Decimal(0.1))``````

This module is used when we want to carry out decimal calculations like we learned in school.

It also preserves significance. We know 25.50 kg is more accurate than 25.5 kg as it has two significant decimal places compared to one.

``````from decimal import Decimal as D
# Output: Decimal('3.3')
print(D('1.1') + D('2.2'))

# Output: Decimal('3.000')
print(D('1.2') * D('2.50'))``````

Notice the trailing zeroes in the above example.

We might ask, why not implement Decimal every time, instead of float? The main reason is efficiency. Floating point operations are carried out must faster than Decimal operations.

### When to use `Decimal` instead of `float`?

We generally use Decimal in the following cases.

• When we are making financial applications that need exact decimal representation.
• When we want to control the level of precision required.
• When we want to implement the notion of significant decimal places.
• When we want the operations to be carried out like we did at school.

## Python Fractions

Python provides operations involving fractional numbers through its `fractions` module.

A fraction has a numerator and a denominator, both of which are integers. This module has support for rational number arithmetic.

We can create Fraction objects in various ways.

``````import fractions

# Output: 3/2
print(fractions.Fraction(1.5))

# Output: 5
print(fractions.Fraction(5))

# Output: 1/3
print(fractions.Fraction(1,3))``````

While creating Fraction from `float`, we might get some unusual results. This is due to the imperfect binary floating point number representation as discussed in the previous section.

Fortunately, Fraction allows us to instantiate with string as well. This is the preferred options when using decimal numbers.

``````import fractions

# As float
# Output: 2476979795053773/2251799813685248
print(fractions.Fraction(1.1))

# As string
# Output: 11/10
print(fractions.Fraction('1.1'))``````

This datatype supports all basic operations. Here are few examples.

``````from fractions import Fraction as F

# Output: 2/3
print(F(1,3) + F(1,3))

# Output: 6/5
print(1 / F(5,6))

# Output: False
print(F(-3,10) > 0)

# Output: True
print(F(-3,10) < 0)``````

## Mathematical Functions

Function What it does ? Example
`abs(number)` Returns the absolute value of the number. In other words, the `abs()` function just returns the number without any sign. `abs(-12)` is `12`, `abs(112.21)`is `112.21`.
`pow(a, b)` Returns `a^b`. `pow(2, 3)` is `8`, `pow(10, 3)`is `1000`
`round(number)` Rounds the number to the nearest integer. `round(17.3)` is `17`, `round(8.6)` is `9`
`round(number, ndigits)` Rounds the `number` to `ndigits` after decimal point `round(3.14159, 2)` is `3.14`, `round(2.71828, 2)` is `2.72`
`min(arg1, arg2, ... argN)` Returns the smallest item among `arg1`, `arg2`, ... `argN` `min(12, 2, 44, 199)` is `2`, `min(4, -21, -99)` is `-99`
`max(arg1, arg2, ... argN)` Returns the largest item among `arg1`, `arg2`, ... `argN` `max(991, 22, 19)` is `991`, `max(-2, -1, -5)` is `-1`

The following table lists some standard mathematical functions and constants in the `math` module.

Function What it does ? Example
`math.pi` Returns the value of `pi` `math.pi` is `3.141592653589793`
`math.e` Returns the value of `e` `math.e` is `2.718281828459045`
`math.ceil(n)` Returns smallest integer greater than or equal to `n` `math.ceil(3.621)` is `4`
`math.floor(n)` Returns largest integer smaller than or equal to `n` `math.floor(3.621)` is `3`
`math.fabs(n)` Returns absolute value of `x` as `float` `math.fabs(5)` is `5.0`
`math.sqrt(n)` Returns the square root of x as float `math.sqrt(225)` is `15.0`
`math.log(n)` Returns the natural log of `n` to the base `e` `math.log(2)` is `0.6931`
`math.log(n, base)` Return the log of `n` to the given base `math.log(2, 2)` is `1.0`
`math.sin(n)` Returns the sine of `n` radians `math.sin(math.pi/2)` is `1.0`
`math.cos(n)` Returns the cosine of `n` radians `math.cos(0)` is `1.0`
`math.tan(n)` Returns the tangent of `n` radians `math.tan(45)` is `1.61`
`math.degrees(n)` Converts the angle from radians `math.degrees(math.pi/2)` is `90`
`math.radians()` Converts the angle from degrees to radians `math.radians(90)` is `1.5707`

temp