With old Ziko Python's long-winded division _python

Source: Internet
Author: User
Tags arithmetic in python

The division is verbose, not just python.

Integer divided by integer

Reader after you start idle, practice the following operation:

>>> 2/5
0
>>> 2.0/5
0.4
>>> 2/5.0
0.4
>>> 2.0/5.0
0.4

Do you see it? Trouble out, if from elementary School Mathematics knowledge Division, above four arithmetic result should be 0.4. But we see the following three match, the first incredibly result is 0. Why?

Because there's a rule in Python, like division 2/5, that's going to be rounding. 2 divided by 5, the quotient is 0 (integer) and the remainder is 2 (integer). So if you use this form: 2/5, the result is the whole number of the quotient. Or it can be understood that an integer divided by an integer, the result is an integer (quotient).

Continue the experiment to verify this conclusion:

>>> 5/2
2
>>> 6/3
2
>>> 5/2
2
>>> 6/2
3
>>> 7/2
3
>>> 8/2
4
>>> 9/2
4

Note: Here is the quotient of integers, not the result of "rounding" after the results contain decimal digits. For example 5/2, get the quotient 2, the remainder 1, the final 5/2=2. Not rounding 2.5.

To divide a floating-point number with an integer

As reader note, this title is not the same as the title above, the title above is "Integer divided by integer", and if the style consistent, the title of this section should be "floating-point number divided by integer", but no, now it is "dividing floating-point numbers with integers" because of the following three cases:

Dividend is a floating-point number, and a divisor is an integer
Dividend is an integer, and a divisor is a floating-point number
The divisor and divisor are floating-point numbers.
Before you make a conclusion, do the experiment first:

>>> 9.0/2
4.5
>>> 9/2.0
4.5
>>> 9.0/2.0
4.5
>>> 8.0/2
4.0
>>> 8/2.0
4.0
>>> 8.0/2.0
4.0

Summed up, get the law: whether it is divisor or divisor, as long as there is a number of floating-point numbers, the result is floating point numbers. So, if the result of dividing is more than a few, it will not be the same as before, but to return a floating-point number, which is the same as the results of the study of mathematics.

>>> 10.0/3
3.3333333333333335


This is not a bit funny, according to mathematical knowledge, should be 3.33333 ..., followed by a 3 cycle. Then your computer will not stop, full screen is 3. To avoid this, python arbitrarily ended the loop, but sadly it was not terminated in accordance with the principle of rounding.

On the infinite circulation decimal problem, primary school has learned, but this is not a simple question, look at Wikipedia's entry: 0.999 ..., will there be in-depth experience?

In short, to use Python, you have to follow her rules, the previous two rules are clear.

Add a material for interested friends to read: Floating point algorithm: Controversy and limitations
Description: The above division rule, is for python2, in Python3, will 5/2 and 5.0/2 equate. However, if you want to get that part of the integer, you can do it in a different way: the floor is removed.

>>> 9/2
4
>>> 9//2
4

Python always has to offer a variety of solutions to the problem, which is her style.

Start with the wheel

One of the most important reasons why Python is popular is that there are many wheels. This is a metaphor. It's like you're running fast, what do you do? It is not good to practice running every day, but to use wheels. It's a lot quicker to find a bike. Not too fast enough, and then change the car, and then change cars, and then change the high-speed rail ... You can choose a lot anyway. But these things that make you run fast, most of which are not made by yourself, are made by others, and you use them. Even two legs are thanks to the parents for their gifts. It is because of the many wheels, can choose more, can enjoy at various speed.

That's how python is, there are all kinds of wheels that other people make, we just need to use. But those wheels in Python's name is not called the bicycle, the automobile, is called "the module", the person accepts the other language the name, is called "Class Library", "class". No matter what name you call it. It's someone else who made something good. We use it.

How do you use it? This can be done in two ways:

Form 1:import Module-name. Import follows a space followed by a module name, for example: import OS
Form 2:from Module1 import module11. Module1 is a large module, which also has a sub module module11, just want to use MODULE11, it is so written. For example, the following examples:
No more long-winded, experiment one:

>>> from __future__ Import Division
>>> 5/2
2.5
>>> 9/2
4.5
>> > 9.0/2
4.5
>>> 9/2.0
4.5

Note that after referencing a module, and then division, no matter what the case, is the result of the floating-point number.

This is the power of the wheel.

About remainder

When we calculate 5/2, the quotient is 2 and the remainder is 1.

How do we get the remainder?

Experiment with the following actions:

>>> 5%2
1
>>> 9%2
1
>>> 7%3
1
>>> 6%4
2
> >> 5.0%2
1.0

Symbol:% is the remainder that is divided by two numbers (which can be integers or floating-point numbers).

Python has a lot of human-love wheels (modules), and she also has a wealth of built-in functions that will help us do a lot of things. such as function Divmod ()

>>> divmod (5,2) #表示5除以2, returned the quotient and remainder
(2, 1)
>>> divmod (9,2)
(4, 1)
>>> Divmod (5.0,2)
(2.0, 1.0)

Rounded

The last one, must insist, today's indeed a bit long-winded. To achieve rounding, it's very simple, that is, built-in functions: round ()

Give it a try:

>>> round (1.234567,2)
1.23
>>> round (1.234567,3)
1.235
>>> Round ( 10.0/3,4)
3.3333

It's easy. The simpler the time, the more careful, when you encounter the following situation, there is a suspicion:

>>> round (1.2345,3)
1.234        #应该是: 1.235
>>> round (2.235,2)
2.23        #应该是: 2.24

Haha, I found a bug in Python, too excited.

Don't be so excited, if it's really a bug, it's not my turn to be so obvious. Why? For specific explanations look here, excerpts from the official document are as follows:

Note:
The behavior of round () to floats can be surprising:for example, round (2.675, 2) gives 2.67 of the EXP Ected 2.68. This isn't a bug:it ' s a result of the fact which most decimal fractions can ' t be represented exactly as a float. Floating point arithmetic:issues and limitations for more information.

It turns out that it's really not my turn. (with a downcast shape.) )

It seems that the division of the problem to the end, in fact, far from, but as beginners, this can be. Also left a lot of topics, such as how to deal with decimal problems, I certainly will not let the exploration of the spirit of the friend disappointed, in my GitHub have such a wheel, if you want to further study, you can come here to try.

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.