Basic mathematical computing usage in Python Programming

Source: Internet
Author: User
Tags integer division
This article mainly introduces the use of basic mathematical computing in Python programming. it focuses on division operations and the use of related division modules, for more information about how to use basic mathematical computing in Python programming, refer to this article. This article focuses on division and the use of related pision modules. For more information, see

Quantity
In Python, the provisions on logarithm are relatively simple and can be understood basically at the level of elementary school mathematics.

So, as a zero-basic learning, we should start with the calculation of primary school mathematics questions. From here on, the basic knowledge of mathematics must pass through.


>>> 33>>> 33333333333333333333333333333333333333333333333333333333333333333333333333333333L>>> 3.2222223.222222


In interactive mode, if 3 is input, 3 is displayed. such a number is called an integer, which is the same as that in elementary school mathematics.

If you enter a relatively large number and the second one, then an integer consisting of multiple three is called a long integer in Python. To indicate that a number is a long integer, Python displays an L at the end of it. In fact, Python can now automatically regard a large integer as a long integer. You do not have to make any difference in this regard.

Third, it is called decimal in mathematics. you can still call it here, but like many programming languages, it is often called a "floating point number ". The origin of this name is also a bit mentioned. if you are interested, google.

In the preceding example, all values are unsigned (or non-negative). to represent a negative number, just like the representation method in mathematics, add a negative number.

It is worth noting that we are talking about the decimal number here.

In addition to decimal, binary, octal, and hexadecimal are all used in programming. of course, there is less time to use the 60-digit notation (in fact, the time record method is a typical 60-digit notation ).

Each number is an object in Python. for example, 3 entered above is an object. Each object has its own address in the memory. this is its identity.


>>> id(3)140574872>>> id(3.222222)140612356>>> id(3.0)140612356>>>


You can use the built-in function id () to view the memory address of each object, that is, the identity.

The built-in Function is a built-in Function in English. you can guess the number of built-in functions by name. Good, it is an internal function that has been defined in Python.
The preceding three numbers are three different objects with three different memory addresses. In Mathematics, 3 and 3.0 are equal, but here they are different objects.

The memory address obtained by using id () is read-only and cannot be modified.

After learning about "identity", let's look at "type". there is also a built-in function for using type ().


>>> type(3)
 
  >>> type(3.0)
  
   >>> type(3.222222)
   
  
 


You can use built-in functions to view object types. , Meaning 3 is an integer (Interger ); The object is Floating point real number ). Similar to the result of id (), the result of type () is read-only.

As for the object value, here it is the object itself.

It seems that the object is not hard to understand. Please be confident and continue.

Variable
Writing 3, 4, and 5 is far from enough. in programming languages, "variables" and "numbers" are often used (strictly speaking, objects in Python) create a ING. For example:


>>> x = 5>>> x5>>> x = 6>>> x6


In this example, x = 5 establishes a ing between the variable (x) and the number (5), and then establishes a ing between x and 6. We can see that x first "yes" 5, and then "yes" 6.

In Python, such a statement is very important: the object has a type and the variable has no type. How can this problem be solved?

First, values 5 and 6 are all integers. in Python, they are named "integer" type data, or the data type is an integer, which is represented by int.

When we write 5 and 6 in Python, the computer girl will automatically create these two objects for us somewhere in her memory (the definition of the object will be described later, here you will first use, gradually clarify the meaning). It is like building two sculptures. one is 5 and the other is 6. These two objects are of the int type.

What about x? It is like a tag. when x = 5, the tag x is tied to 5. the tag x is extended to 5, in the interactive mode, >>> the output result of x is 5. it seems that x is 5, and the fact is that x is attached to 5. In the same way, when x = 6, the tag is changed and pasted to 6.

Therefore, the tag x has no type. it can be attached not only to integer objects, but also to other types of objects, such as the str (string) type object.

This is a very important difference between Python and some languages.

Arithmetic operation
Run in interactive mode according to the following requirements to check whether the obtained results are consistent with those obtained after the basic mathematics knowledge calculation.


>>> 2+57>>> 5-23>>> 10/25>>> 5*210>>> 10/5+13>>> 2*3-42


The preceding operations involve four operators: plus (+), minus (-), multiplication (*), and division (/)

In addition, I believe that the audience has discovered an important principle:

In a computer, the four arithmetic operations are the same as those learned in elementary school mathematics.

If a person is a high-tech animal, he must inherit the knowledge he has already learned from his own history. The great scientists, when designing a computer, thought of the needs of the column space for learning now. they must not let the future generations learn new computing rules and use the primary school mathematics. I would like to thank those pioneers of scientists, who were later on.

The following three arithmetic questions are calculated to see what the result is.


4 + 24.0 + 24.0 + 2.0


An official may be angry. it is a waste of time to drive a computer for such a simple question.

Don't worry. You still need to calculate the result and check whether the result is different? Observe carefully.


>>> 4+26>>> 4.0+26.0>>> 4.0+2.06.0


The difference is that the first sub-result is 6, which is an integer; the last two are 6.0, which is a floating point.

Definition 1: a number in the form of 4,-2, 129486655,-988654, and 0. it is called an integer.
Definition 2: a number in the format of 4.0,-2.0, 2344.123, and 3.1415926. it is called a floating point number.
There is no need to memorize these two definitions. google it. Remember the sentence that Einstein said: I don't remember anything in the book? I don't remember it anyway ). He didn't say anything in the last half. I added: google is the only thing you forget.

It seems that the computer does some arithmetic operations, but you must note that in mathematics, integers can be infinitely large, but in computers, an integer cannot be infinitely large. Why? (I recommend that you go to google. actually, you must have learned some basic computer knowledge .) Therefore, it may occur that the number involved in the operation or the calculation result exceeds the maximum number in the computer. this problem is called an integer overflow problem ".

Integer overflow
Here is an article devoted to this issue. we recommend that you read this article: integer overflow.

For other languages, integer overflow must be taken into consideration. However, in Python, you will not be worried about it. The reason is that Python solves this problem for us. please read the following article: multiply large integers

OK! You can experiment with the IDE to multiply large integers.


>>> 123456789870987654321122343445567678890098876*1233455667789990099876543332387665443345566152278477193527562870044352587576277277562328362032444339019158937017801601677976183816L


It's lucky to look at Python, so it's time to learn Python.

The number calculated above has an L at the end, which indicates that this number is a long integer. However, you don't have to worry about this. it's done by Python.

Before the end of this section, there are two symbols that need to be remembered by the reader (you can google at any time, but remember to use it more conveniently)

Integer, represented by int, from the word: integer
Floating point number, represented by float, is the word: float
You can use a command: type (object) to check the type of a number.


>>> Type (4)
 
  
#4 is int, integer >>> type (5.0)
  
   
# Float for 5.0, floating point number type (988776544222112233445566778899887766554433221133344455566677788998776543222344556678)
   
    
# A long integer or an integer
   
  
 


Division
Division: not only Python.

Integer divided by integer
After entering the Python interaction mode (later in this tutorial, you may not repeat this type of description, as long as you see >>>, it means that it is in the interaction mode), and practice the following operations:

>>> 2 / 50>>> 2.0 / 50.4>>> 2 / 5.00.4>>> 2.0 / 5.00.4


No? This is troublesome (in Python2.x). according to the mathematical calculation, the above four calculation results should be 0.4. However, the first three matching results are 0. Why?

Because, in Python (strictly speaking, Python2.x, Python3 will change), there is a rule that, like division in 2/5, is to take an integer (that is, remove decimal places, but not rounding them down ). 2 divided by 5, the quotient is 0 (integer), and the remainder is 2 (integer ). If this form is used: 2/5, the calculation result is the quotient integer. It can be understood as follows: an integer is divided by an integer, and the result is an integer (quotient ).

For example:

>>> 5 / 22>>> 7 / 23>>> 8 / 24


Note: The result is a quotient (integer), instead of an integer containing decimal places, and then rounded up by rounding. For example: 5/2, the result is Quotient 2, remainder 1, and final 5/2 = 2. Not rounding 2.5.

Float points and integers
The title format is different from the title format 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", but no, it is now a "floating point number and integer division", which means:

Assume that x is divided by y. X may be an integer or floating point number. y may be an integer or floating point number.
Before coming to the conclusion, we should do the experiment first:

>>> 9.0 / 24.5>>> 9 / 2.04.5>>> 9.0 / 2.04.5>>> 8.0 / 24.0>>> 8 / 2.04.0>>> 8.0 / 2.04.0


Induction to obtain the rule: whether it is a divisor or a divisor, if one number is a floating point, the result is a floating point. Therefore, if there is a remainder in the result of division, it will not be the same as before, but return a floating point number, which is the same as the result of learning mathematics.


>>> 10.0 / 33.3333333333333335


This is a bit strange. according to the mathematical knowledge, it should be 33333..., followed by a 3 loop. So your computer won't be able to stop. the full screen is 3. To avoid this, Python ended the loop arbitrarily, but sadly it was not terminated in accordance with the principle of rounding. Of course, there will be even more amazing appearances:


>>> 0.1 + 0.20.30000000000000004>>> 0.1 + 0.1 - 0.20.0>>> 0.1 + 0.1 + 0.1 - 0.35.551115123125783e-17>>> 0.1 + 0.1 + 0.1 - 0.20.10000000000000003


Why is the computer girl so confused about the simplicity of computing? Not a computer girl, she is still ice and snow smart. The reason is that in decimal and binary conversion, the computer girl uses binary for calculation. in the above example, we input decimal, and she will convert the decimal number to binary, then calculate. However, in conversion, if a floating point number is converted into a binary number, a problem occurs.

For example, convert decimal 0.1 to binary: 0. 0001100110011001100110011001100110011001100110011...

That is to say, after being converted to binary, it is not exactly equal to 0.1 in decimal format. At the same time, there are limits on the number of digits stored by computers.

This problem is not only encountered in Python, but is not a Python bug in all programming languages that support floating-point calculation.

After understanding the cause of the problem, how can this problem be solved? For Python floating-point operations, the error of each calculation on most machines cannot exceed 2 ** 53 points. This is enough for most tasks, but remember that this is not a decimal algorithm, and each floating point calculation may bring a new round-off error.

Generally, as long as the final displayed result is simply rounded to the expected decimal digit, the expected final result is obtained.

You can use the decimal module to perform decimal operations that require high accuracy. In addition, the fractions module supports another form of operation, which implements operations based on rational numbers (so numbers such as 1/3 can be accurately expressed ). The highest requirement is that the Numerical Python package provided by SciPy and other packages for mathematics and statistics are used. Listing these things is just to make the viewer understand that there are many ways to solve the problem, and some of these methods will be used later to solve the problem.

For the infinite loop decimal problem, I have a link to recommend to you. it is not as simple as you think. Please read: Wikipedia entry: 0. 999... will you have any in-depth experience?

Additional information for interested friends to read: Floating-point number algorithms: disputes and restrictions
Python always provides a variety of solutions to the problem, which is her style.

Reference module solves division-enable wheel
A very important reason why Python is popular is that there are many wheels. This is a metaphor. What should I do if you want to run fast? It is impossible to practice running every day and use wheels. Finding a bicycle is much faster. It is not fast enough. you can change the battery car, the car, and the high-speed train... you can choose a lot. However, most of the things that let you run fast are not made by yourself. you can use them instead. Even two legs are a gift to my parents. It is precisely because of the large number of wheels that you can choose and enjoy at different speeds.

The wheel is a great invention of mankind.

Python is like this, there are various wheels, we only need to use. But those wheels in Python are not called bicycles, cars, or modules. some people take the names of other languages, such as class libraries and classes ". No matter what the name is. It is something that is made by others. we can use it here.

How to use it? You can use the following two methods:

Format 1: import module-name. Import is followed by a space, and then the module name, for example, import OS
Form 2: from module1 import module11. Module1 is a large module with a sub-module module11 in it. I just want to use module11, so I wrote it.
No worries. Experiment 1:


>>> from future import pision>>> 5 / 22.5>>> 9 / 24.5>>> 9.0 / 24.5>>> 9 / 2.04.5


Note: After a module is referenced and division is performed, the floating point number is obtained in no matter what the situation is.

This is the power of the wheel.

Remainder
In the previous calculation of 5/2, the quotient is 2, and the remainder is 1.

How can I get the remainder? In Python (in most languages, too), use the % symbol to obtain the remainder of the two numbers division.

Perform the following operations:


>>> 5 % 21>>> 6%42>>> 5.0%21.0


Symbol: %, which is the remainder of the division to get two numbers (which can be integers or floating-point numbers.

As mentioned above, Python has many interesting wheels (modules), and many built-in functions will help us do a lot of things. For example, the function pmod ()


>>> Pmod (5, 2) # indicates 5 divided by 2, returns the quotient (2, 1) >>> pmod (9, 2) (4, 1) >>> pmod (5.0, 2) (2.0, 1.0)


Rounding
The last one must be insisted. today it is indeed a bit cool. To implement rounding, it is very easy, that is, 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

The above is the basic mathematical computing usage details in Python programming. For more information, see other related articles in the first PHP community!

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.