Learn Python's long winded division from the old

  • 2020-04-02 14:05:39
  • OfStack

Division is not just verbose in python.

Integer divided by integer

After starting idle, practice the following operations:


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

See? Trouble is, if you divide from elementary school math, the above four operations should be 0.4. But when we look at the last three, the first one turns out to be 0. According to the & # 63;

Because, in python, there's a rule, like the division in 2/5, that you round. 5 goes into 2 0, and the remainder is 2. So if you take this form: 2/5, you get the quotient of that integer. Or it can be understood as: integer divided by 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: this is to get integer quotient, not to "round" the result with decimal place. For example, 5/2, you get a quotient of 2, remainder of 1, and you end up with 5/2 is equal to 2. It's not rounding 2.5.

A floating point number is divided by an integer

Note that this title is not in the same format as the title above. The title above is "integer divided by integer". If the style is consistent, the title of this section should be "floating point number divided by integer".

The dividend is a floating point number and the divisor is an integer
The dividend is an integer, and the divisor is a floating point number
Both the dividend and the divisor are floating point Numbers
Before coming to 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

The result is a floating-point number, whether it is a dividend or a divisor. So, if the result of dividing has a remainder, it's not going to be the same as before, but it's going to return a floating point number, which is the same as the result of learning math.


>>> 10.0/3
3.3333333333333335


This is not a bit strange, according to mathematical knowledge, should be 3.33333... And then we have the loop of 3. Then your computer won't be able to stop. The screen is full of 3's. To avoid this, python arbitrarily ends the loop, but sadly does not follow the "round" rule.

Elementary school students have learned about the infinite repeating decimal problem, but it's not a simple problem. Check out the wikipedia entry: 0.999... , will there be in-depth experience?

In short, to use python, you have to follow her rules, which are already clear.

A supplement for those interested in reading: floating point arithmetic: controversies and limitations
Explanation: the above division rule is for python2. In python3, 5/2 and 5.0/2 are equated. However, if you want to get to the top of that integer part, you can do it another way: by dividing the floor.


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

Python always offers multiple solutions to problems, which is her style.

Start on wheels

An important reason why python is so popular is that it has so many wheels. That's a metaphor. Like you want to run fast, what do you do? Light everyday practice run is not line drop, want to use wheel. Find a bike. It's much faster. Not fast enough, then change the battery car, then change the car, then change the high-speed train... You have a lot to choose from. However, most of the things that make you run fast are not made by yourself. They are made by others and used by you. Even two legs are thanks to parents. Just because there are so many wheels and so many options, you can enjoy it at different speeds.

Python is the way it is. There are all kinds of wheels built by other people that we just need to use. It's just that the wheels in python are not called bikes or cars, they're called "modules." some people take the names of other languages and call them "class libraries" or "classes." Whatever the name is. It's something that somebody else made and we take it and we use it.

How does it work? It can be used in two forms:

Form 1: import module-name. Import is followed by a space, followed by the module name, for example, import OS
Form 2: from module1 import module11. Module1 is a big module, it has a submodule module11 in it, it just wants to use module11, so let's just write that. Here's an example:
Without further ado, here's an experiment:


>>> from __future__ import division
>>> 5/2
2.5
>>> 9/2
4.5
>>> 9.0/2
4.5
>>> 9/2.0
4.5

Notice that when you do division after you reference a module, whatever the case is, you get a floating point number.

That's the power of the wheel.

About the remainder

When I did 5/2, the quotient was 2, and the remainder was 1

How do I get the remainder?

The following operation of the experiment:


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

Symbol: % is the remainder of dividing two Numbers (either integer or floating point).

As mentioned above, python has a lot of wheels (modules) that everyone loves, and it also has a lot of built-in functions that will help us do a lot of things. For example, the function divmod()


>>> divmod(5,2) # said 5 Divided by the 2 Returns 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 is really a bit wordy. Rounding is as simple as the built-in function round()

Try it:


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

Simple. The easier it is, the more careful you have to be. Here's how to be a little skeptical:


>>> round(1.2345,3)
1.234        # It should be: 1.235
>>> round(2.235,2)
2.23        # It should be: 2.24

Haha, I found a bug in python, so excited.

Don't get so worked up. If it's a bug, it's so obvious, it's not my turn. Why is that? Here's an excerpt from the official document:


Note:
The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it's a result of the fact that most decimal fractions can't be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information.

It's really not my turn. I'm down in the dumps.

It seems like this is the end of the division problem, which is far from it, but for starters, this is it. There are a lot of topics left, such as how to deal with recurring decimals, and I'm sure I won't disappoint my exploratory friends. I have a wheel on github that you can try if you want to dig deeper.


Related articles: