Sunday, December 30, 2012

[2] - Numbers

Numeric Operations

In Python, the arithmetic addition, subtraction, multiplication operations are intuitive and similar to other common programming languages, but division is somewhat tricky. Python defines two operator / and // for division.

In 2.X, the / operator does classic division, performing truncating integer division if both operands are integers and float division (keeping remainders) otherwise. The // operator does floor division which always truncates the reminder no matter the operands are integers or floats. The following are some sample results for / and // in Python 2.X:

>>> 5 / 2
2
>>> 5 // 2
2
>>> 5 / 2.0
2.5
>>> 5 // 2.0
2.0


The / operator differs in 3.X, which always performs true division and returns a float result that include any remainder regardless of operand types. For example, the following are some results in Python 3.X:

>>> 5 / 2
2.5
>>> 5 / 2.0
2.5


There are no differences for the // operator between 2.X and 3.X.

In Python, there are no incremental or decremental operators (++ or --). For instance:

>>> x = 1
>>> x++
SyntaxError: invalid syntax


In order to increase a number, we have to add 1 explicitly:

>>> x = 1
>>> x += 1
>>> print x
2


Besides a built-in function pow, Python also provides  a ** operator for exponentiation. For example:

>>> pow(5, 2)
25
>>> 5 ** 2
25
>>> pow (25, 0.5)
5.0
>>> 25 ** 0.5
5.0


Big Numbers

Python 2.X has a separate type for long integers, and it automatically converts any number too large to store in a normal integer to this type:
 
>>> 2 ** 10, type(2 ** 10)
(1024, <type 'int'>)
>>> 2 ** 50, type(2 ** 50)
(1125899906842624L, <type 'long'>)

The built-in function type returns the type of any objects. Note the type difference in the output above.

Python 3.X integers support unlimited size.

Therefore, it is not necessary to worry about overflow problems when programming in Python.

Complex Numbers

Complex Numbers are represented as two floating-point numbers: the real and imaginary parts. The imaginary part is coded as number with a j or J suffix. For example:

>>> type(1-3j)
<type 'complex'>
>>> (1 - 3j) + (3 + 5j)
(4+2j)


Fraction Type

There are some inaccuracies and limitations of floating-point math, so a new numeric type, Fraction is introduced in Python, which essentially keeps both a numerator and denominator explicitly.

In order to utilize the Faction type, the type should be imported from a module named fractions first. Additionally, we can pass a numerator and denominator to create an Faction instance, as shown below:

>>> print x
1/6
>>> y = Fraction(1, 3)
>>> print y
1/3

>>> print x + y
1/2


Pool for Small Integers

Small integers are frequently used in Python. In order to avoid recreating the small integers over and over again, Python creates a pool for small integers. Let's have a look at the following Python code:

>>> a = 257
>>> b = 257
>>> id(a)
35351736
>>> id(b)
35351712
>>> c = 256
>>> d = 256
>>> id(c)
30341244
>>> id(d)
30341244

The built-in function id is used to get the identity of an object in Python. The two integers a and b are the same in value, but their identity are different. Therefore, a and b are two different integers. However, the identities of c and d are the same, so they are essentially identical to each other.

The range of the small integers in the pool is from -5 to 256 by default. If you want to modify the range in the pool, you have to modify the source code of Python and then rebuild it.

No comments:

Post a Comment