简体   繁体   中英

Using integer division in Python

Can you help me to figure out this Python division

base = 12.0

height = 16

Area = 1/2 * Base * Height

Solution shows of it this

area = (base * height)/2;

what confuses me that 1/2 will be float and Python will show only 0 not 0.5 why solution shows /2 in this case ...

Python2.7 automatically uses the / operator as integer (whole number) division, which will always produce a whole number.

For example:

1/2 = 0

3/4 = 0

100/30 = 3

To do float division, you must have one or both of the values as a float type.

Like this:

Area = 1.0/2 * Base * Height # 1.0 is a float type, 1 is an integer type

The results are not what you expected since Python evaluates the expressions using integer division and order of operations.

If you evaluate 16 * 12 / 2 in Python, Python interprets this as (16 * 12) / 2 , or 192 / 2 = 96

If you evaluate 1/2 * 16 * 12 , Python interprets this as (((1/2) * 16) * 12) = (0 * 16) * 12

Further examples:

Area = Base * Height * (1.0/2.0)

Python evaluates (1.0/2.0) first this time, since order of operations dictates that the parentheses are evaluated first. And since 1.0 and 2.0 are floats and not integers, Python is fine with performing float division. You get this:

Base * Height * (0.5)
= 192 * 0.5
= 96

, which gives you what you expect.

In contrast:

Base * Height * (1/2)
= Base * Height * (0) # since 1/2 rounds down to 0 in integer division
= 192 * 0
= 0


Alternative solution from Carpetsmoker:

from __future__ import division

This line will give you Python3.x behaviour in a Python2.x program, and Python3.x accepts the / operator as float division, even if both values are integer types. Thus, after you import this, / becomes the float division operator.

>>> from __future__ import division
>>> 1/2
0.5

Integer division is where the remainder past the one's place is simply discarded. Dividing 7 into 3 will give us 3 piles of 2, with one left over, thus:

7 / 3 = 2

Python has two commonly used types of numbers: integers, which can only express whole numbers, and floats, which can express a decimal number. Traditionally, in many programming languages (C, Java, etc), performing a basic operation, such as + , - , * , or / on two objects of the same type would give you another object of that type. If they were different, one of the objects would be safely typecast to the more generic form. This usually results in an integer, which cannot express fractions, being cast to a float, which can. The two floats are then divided, yielding another float.

So, in Python 2, when you use the operator / with two integers, the result will be an integer. 1/2 is 0.5 , but the fractional part is discarded so you are left with 0 .

However, in Python 3 / was modified to always cast to a floating point number, resulting in true division. 1/2 = 0.5 , and 2/1 = 2.0 . For new programmers, this is the "expected" behavior, and can yield cleaner code for existing programmers. This new behavior can be emulated in Python 2 by adding a future import at the top of your script:

from __future__ import division

If you do want a truncating division in Python 2.5+ or 3.x, there is the new // operator which emulates the old / .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM