In the C / C + + language, the division of integer number will be carried out (rounding off the decimal part). For example, int a = 15 / 10; the result of a is 1.
The same is true in Java, so when dividing two int data and returning a floating-point data, you need to force type conversion. For example, float a = (float) BGC, where B and C are int data.
Python is divided into three kinds of division: traditional division, precise division and floor division.
Traditional division
If it is an integer division, perform the floor division, if it is a floating-point division, perform the precise division.
>>>1/2
0
>>>1.0/2.0
0.5
Precise division
Division always returns the real quotient, whether the operands are integer or floating-point. Execute from__ future__ The import division directive can do this.
>>>from __future__ import division
>>>1/2
0.5
>>>1.0/2.0
0.5
Floor removal
Starting from Python 2.2, an operator / / is added to perform floor Division: / / division. Regardless of the numeric type of the operands, the decimal part is always discarded and the nearest number in the number sequence smaller than the real quotient is returned.
>>>1//2
0
>>>1.0//2
0
>>>-1//2.0
-1
Built in function divmod()
divmod (a, b), return (A / / B, a% B)
>>>divmod(1,2)
(0,1)
>>>divmod(3.14159,1.5)
(2.0,0.4159000000000002)
>>>5+6j//3+2j
2+0j
>>>5+6j%3+2j
-1+2j
>>>divmod(5+6j,3+2j)
((2+0j),(-1+2j))
=
Read More:
- Zero division error: float division by zero
- [Solved] Using / for division is deprecated and will be removed in Dart Sass 2.0.0.
- The function of structured shuffleplit() in SK learn realizes the division of data set
- Latex point multiplication, cross multiplication and division
- Solve the problem of unable to locate package python3.6 when upgrading from python3.5 to python3.6 in ubantu16.04
- Python – [encoding] in Python os.system Solution to Chinese garbled code when calling CMD command
- Can’t find Python executable “D:\python3\python.exe”, you can set the PYTHON env variable.
- Solve the problem of Python in Windows environment: Fatal error in launcher: Unable to create process using’”‘ in pip installation
- The python version output from the command line is inconsistent with the python version in the current CONDA environment
- From in Python__ future__ The role of import *
- The problem of “value error: zero length field name in format” in Python 2.6.6 of CentOS 6.9
- Pychar reported an error. Unable to set the python SDK in Python 3.8. This SDK appears to be invalid.
- In Python, import XXX does not report an error, but in IPython (Jupiter notebook)
- Time, strftime and strptime in Python
- Memory error in Python numpy matrix
- Python error unhandled exception in thread started by error in sys.excepthook
- Installing PyQt4 in Windows + Python 3.6
- Translate() and maketrans() methods of string in Python
- Error when starting Python in Windows
- The usage of typing.union in Python