Python double-asterisk ** power operator behaves unexpectedly

I wrote a function to replace scipy.interp1d by approximating the relationship between two variables (elev and MaxQ) to speed up my code. The equation is a fourth order polynomial. I would like the function to be able to compute Q values ​​for individual inputs and for 1D arrays. The function is shown below.

def CalculateMaxFlow(elev):
    size=np.shape(elev)
    if size==():
        if (elev>367.8): #minimum elev for 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])
        for i in range(np.shape(elev)[0]):
            if (elev[i]>367.8): #4th order polynomial. Not exact but okay for speeding up 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 are as 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 and Q2 give me the answer I expect. For some reason, Q3 doesn't. I'm wondering why this is so. The only difference I can see in my console between eleva and elevb is that a is float64 and b is int32. Why would this change the result of the equation? Why, then, does the result for Q1 (which is an int) also work as expected?

+3


source to share


1 answer


Since Q3, elev[i]

is instance numpy.int32

and elev[i]**4

overflows. Since Q1, elev

is a Python int, and elev**4

uses arbitrary precision arithmetic.



+4


source







All Articles