Python division in the lower version

  • 2020-05-05 11:26:21
  • OfStack

The first thing to mention is the division operation in python. There are two kinds of division operations in python 2.5, namely the so-called true division and floor division. When x/y form is used for division operation, if x and y are both plastic, the operator will intercept the result and take the integer part of the operation. For example, the result of 2/3 operation is 0. If one of x and y is a floating point number, then the so-called true division is performed, such as the result of 2.0/3 is 0.66666666666666663. The other division is in the form of x//y, so what is used here is the so-called floor division, which is to get the maximum integer value that is not greater than the result, which is independent of the operand. So 2 over 3 is 0, minus 2 over 3 is minus 1, minus 2.0 over 3 is minus 1.0.

      in future python 3.0, x/y will only perform true division, regardless of operands. x//y performs floor division. If you need to do this with version 2.5 of python, you will need to add the declaration of from s s 32en__ import division before the code. Such as:

 
from __future__ import division 
a=2/3                 
from __future__ import division a=2/3

The result of a will be 0.6666666666666666663 instead of the original 3.


Related articles: