[Tutor] (no subject)

Alan Gauld alan.gauld at btinternet.com
Sat Feb 4 14:31:37 CET 2012


On 04/02/12 12:08, Debashish Saha wrote:

> f(11)
> Out[109]: 2.5132741228718345
>
> f(11.0)
> Out[110]: 2.7646015351590183
>
> *question:*
> why did i get different values for the same input?

Because they are not the same inputs.
11 is an integer and 11.0 is a float and Python (along with many 
programming languages) treats division slightly differently for
ints and floats. You can bypass this by making your divisor a
float:

######
def f(y):
      return (y/5.0)*2*(np.pi)*0.2
##########


Now it will return the same result regardless of int or float.

In Python v3 this is the default behavior

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list