Python double-asterisk ** power operator behaving unexpectedly -
i've written function replace scipy.interp1d approximating relationship between 2 variables (elev , maxq) in order speed code. equation fourth order polynomial. function able compute q values single inputs , 1d array inputs. function shown below.
def calculatemaxflow(elev): size=np.shape(elev) if size==(): if (elev>367.8): #minimum elev flow return -0.00028194553726719*elev**4+0.284027992763652*elev**3-80.3765236558431*elev**2+1900880.72298153 else: return 0 else: maxq=np.zeros(np.shape(elev)[0]) in range(np.shape(elev)[0]): if (elev[i]>367.8): #4th order polynomial. not exact okay speeding code maxq[i]= -0.00028194553726719*((elev[i])**4)+0.284027992763652*((elev[i])**3)-80.3765236558431*((elev[i])**2)+1900880.72298153 else: maxq[i]= 0 return maxq elev1=380 elev5a=380+np.zeros(5) elev5b=np.asarray([380,380,380,380,380]) q1=calculatemaxflow(elev1) print("q1:"+ str(q1)) q2=calculatemaxflow(elev5a) print("q2:"+str(q2)) q3=calculatemaxflow(elev5b) print("q3:"+str(q3))
the answers follows:
q1:746.828053304 q2:[ 746.8280533 746.8280533 746.8280533 746.8280533 746.8280533] q3:[ 6055481.13713196 6055481.13713196 6055481.13713196 6055481.13713196 6055481.13713196]
q1 , q2 give me answer expect. reason, q3 not. i'm wondering why is. difference can see in console between eleva , elevb is float64 , b int32. why change result of equation? why then, result q1 (which int) work expected?
with q3, elev[i]
numpy.int32
instance, , elev[i]**4
overflows. q1, elev
python int, , elev**4
uses arbitrary-precision arithmetic.
Comments
Post a Comment