Python Division algorithm _python in low version

Source: Internet
Author: User
Tags floor division in python

The first thing to say about division in Python is that there are two division operations in the Python 2.5 version, known as True division and floor division. When you use the X/y form for division operations, if X and Y are all shaped, then the operation will intercept the results, take the integer part of the operation, such as 2/3 of the operation result is 0; if one of the X and Y is a floating-point number, then the so-called true division is performed, for example, the result of 2.0/3 is 0.66666666666666663. Another kind of division is in the form of x//y, so here is the so-called floor division, which is not the result of the maximum integer value, this operation is independent of the operand. For example, the result of 2//3 is that the result of 0,-2//3 is -1,-2.0//3 1.0.

In the future of Python 3.0, x/y will perform only true division, regardless of operand, and x//y perform floor division. If you need to use this in version 2.5 Python, you need to add a declaration from the __FUTURE__ import division before your code. Such as:

Copy Code code as follows:

From __future__ Import Division
A=2/3
From __future__ Import Division A=2/3

Then the result of variable a will be 0.66666666666666663, not the original 3.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.