tags:

views:

170

answers:

9

when i do 1/2 in python why does it give me zero?.. even if i coerce it with float(1/2) still i get zero .. why??.. and how can i get around it??..

when i give arctan(1/2) i get 0 as answer.. but when i give arctan(.5) i get correct ans !!

+2  A: 
float(1)/float(2)

If you divide int / int you get an int, so float(0) still gives you 0.0

PaV
+3  A: 
atan(float(1)/2)

If you do:

atan(float(1/2))

in Python 2.x, but without:

from __future__ import division

the 1/2 is evaluated first as 0, then 0 is converted to a float, then atan(0.0) is called. This changes in Python 3, which uses float division by default even for integers. The short portable solution is what I first gave.

Matthew Flaschen
Python 3 changes this? That's going to mess with my mind. I'm used to doing everything with integers, in C, since I'm an embedded software engineer.
Craig McQueen
Yes. See http://www.python.org/dev/peps/pep-0238/ . You can still use // to truncate.
Matthew Flaschen
+2  A: 

From the standard:

The / (division) and // (floor division) operators yield the quotient of their arguments. The numeric arguments are first converted to a common type. Plain or long integer division yields an integer of the same type; the result is that of mathematical division with the ‘floor’ function applied to the result.

Suvesh Pratapa
+6  A: 

because python 2.x uses integer division for integers, so:

1/2 == 0

evaluates to True.

you want to do:

1.0/2

or do a

from __future__ import division
Autoplectic
+1  A: 

As these answers are implying, 1/2 doesn't return what you are expecting. It returns zero, because 1 and 2 are integers (integer division causes numbers to round down). Python 3 changes this behavior, by the way.

Jacob
+1  A: 

Your coercing doesn't stand a chance because the answer is already zero before you hand it to float.

Try 1./2

Nosredna
+1  A: 

In Python, dividing integers yields an integer -- 0 in this case.

There are two possible solutions. One is to force them into floats: 1/2. (note the trailing dot) or float(1)/2.

Another is to use "from future import division" at the top of your code, and use the behavior you need.

python -c 'from future import division;import math;print math.atan(1/2)' yields the correct 0.463647609001

moshez
+1  A: 

If 1/2 == 0 then float(1/2) will be 0.0. If you coerce it to float after it's been truncated it'll still be truncated.

There are a few options:

  • Add the following import: from __future__ import division. This will make the / operator divide "correctly" in that module. You can use // if you need truncating division.
  • Coerce either of the operands to a float. eg: float(1)/2
  • If you're actually using constants then just add a decimal point instead of using float(), eg: 1.0/2 or 1/2.0 or 1.0/2.0
Laurence Gonsalves
+5  A: 

First, 1/2 is integer division. Until Python 3.0.

>>> 1/2
0
>>> 1.0/2.0
0.5
>>>

Second, use math.atan2 for this kind of thing.

>>> math.atan2(1,2)
0.46364760900080609
>>> math.atan(.5)
0.46364760900080609
S.Lott