dimanche 25 janvier 2015

Return value of if/else recursive function incorrect but print value correct

Trying to use recursion to solve a riemann sum.



def f(x):
import math
return 10*math.e**(math.log(0.5)/5.27 * x)

liTotal = 0
def radExpo(start, stop, step):
global liTotal
x = start
area = f(x)*step
liTotal += area
numOfRects = (stop - start) / step
if start > (stop - (2 *step)):
return liTotal
else:
return radExpo((start+step), stop, step)

radiationExposure(12, 16, 1)


If I change if return statement to



print liTotal


or if I call the function with



print radiationExposure


It works correctly but if I call/return it regularly it returns the wrong value so I can't use what is returned.


Aucun commentaire:

Enregistrer un commentaire