python - Piecewise function not working -
i defined piecewise function :
def c1(d,x): if ((x<d/2) , (x>-d/2)): return (1/d) else: return 0
but return me 0
in [23]: c1(1000,1) out[23]: 0
in python, division sign operator (/) performs integer division, meaning decimal points truncated, if numerator , denominator both integers. when (1/d), both 1 , d integers does, example, 1/1000 0.001 in integer division lose decimal instead 0. fix this, make of defined numbers floats adding .0. example, (x < d/2.0), , (x > -d/2.0) , (1.0/d) should give proper results.
Comments
Post a Comment