1

Consider this Python division:

base = 12.0

height = 16

Area = 1/2 * Base * Height

The solution shows of it this

area = (base * height)/2;

It confuses me that 1/2 will be float and Python will show only 0, not 0.5. Why does the solution use /2 in this case?

3
  • 1
    use 1//2 for integer division Commented Feb 14, 2014 at 0:05
  • 1
    @BradAllred I believe he wants float division, and not integer division Commented Feb 14, 2014 at 0:06
  • Presumably for Python 2? Commented Jan 29, 2022 at 23:52

2 Answers 2

3

Python 2.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 Python 3.x behaviour in a Python 2.x program, and Python 3.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
Sign up to request clarification or add additional context in comments.

11 Comments

In Python 3 this is changed, and you get a float (0.5) as you expect. In Python 2 you can add from __future__ import division at the top of your script to get the Python 3 behaviour.
Ok can you tell me how come (base * height)/2 = (base * height) * (1.0/2.0) right side is equal to the left side
added for @Carpetsmoker 's information and added extra examples
If you do 1/2 * 16 * 12, Python evaluates this as (((1/2) * 16) * 12) = (0 * 16) * 12 I understand this but I can not understand why in the solution area = (base * height)/2; we have this ? I mean /2
@Serenity those two expressions are equal only if either 1) base or height are floats, or 2) base * height is an even number. If base and height were both 3, their product would be 9, and 9/2 = 4.
|
1

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 /.

1 Comment

Thank you for expanding info regarding different versions of Python .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.