views:

68

answers:

5

I am facing the problem while dividing

my max_sum = 14
total_no=4  

so when i do

print "x :", (total_sum/total_no)

, I get 3 and not 3.5

I tried many ways for printing but failed, can somebody let me know what way I get in 3.5 format?

Thank you

+1  A: 

make max_sum=14.0 or total_no=4.0

Timothy
Might be helpful to explain that @learner is experiencing integer division.
jball
-1 First-post answers are not helpful. Don't post one-liners that fix the problem if it's clear that the OP needs more help.
katrielalex
+6  A: 

In Python 2.x, dividing two integers by default dives you another integer. This is often confusing, and has been fixed in Python 3.x. You can bypass it by casting one of the numbers to a float, which will automatically cast the other:

float( 14 ) / 4 == 3.5

The relevant PEP is number 238:

The current division (/) operator has an ambiguous meaning for numerical arguments: it returns the floor of the mathematical result of division if the arguments are ints or longs, but it returns a reasonable approximation of the division result if the arguments are floats or complex. This makes expressions expecting float or complex results error-prone when integers are not expected but possible as inputs.

It was not changed in Python 2.x because of severe backwards-compatibility issues, but was one of the major changes in Python 3.x. You can force the new division with the line

from __future__ import division

at the top of your Python script. This is a __future__-import -- it is used to force syntax changes that otherwise might break your script. There are many other __future__ imports; they are often a good idea to use in preparation for a move to Python 3.x.

Note that the // operator always means integer division; if you really want this behaviour, you should use it in preference to /. Remember, "explicit is better than implicit"!

katrielalex
this works perfectly fine, thanks, but the idea is to be intuitive while returning results
learner
@learner: what? I don't understand your comment. If you want float division, cast one of the arguments to a float.
katrielalex
If you don't like adding the 'cruft' of `.0` or `float()`, the `from __future__ import division` will fix that for the entire script or interactive session (where you type commands in one by one).
Nick T
+1  A: 

You are dividing two integers, the result will be an integer too, in python 2. Try make one of your operands a float, like max_sum = 14.0 or total_no = 4.0 to get a float result.

If you want python 2.X behave a bit more intuitive in that matter you can add

from __future__ import division

on the top of your script. In python 3, division works as you expected it in your case.

The MYYN
what exactly this does for us?
learner
A: 

You're doing an integer operation so you get the nearest integer result : 3.

You should either explicitly/implicitly cast max_sum or total to float.

Eton B.
A: 

Another way of doing this would be to import division from Python 3:

>>> from __future__ import division
>>> max_sum = 14
>>> total_no = 4
>>> print max_sum / total_no  # the way you want it
3.5
>>> print max_sum // total_no
3
sukhbir