This one is a big mystery to me.
I have a python script that consists entirely of the pasted code:
def onedutilfun(x):
u = -( (x/2)**(1/2) + 2*((1-x)/3)**(1/2))
return u
test = onedutilfun(0.01)
test2 = onedutilfun(99)
print(test)
print(test2)
I am running this code in Spyder. I have an installation of Spyder that runs python 3.6 and another that runs 2.7. If I run this in 2.7, it returns -3, regardless of what value I pass it. I've tried various conversions to floats (u and x already are floats, and Spyder agrees with me), to no avail.
If I run this same code in python 3.6, it works fine, returning exactly what it should return. What might be going on? I realize one solution would be to use newer python, but I need to provide code that runs in 2.7.
The value 99 returns a complex number, fyi.
I've pasted the code into Google's search bar, and the graphed function (over the function's domain in the real number system) looks good, too.
I have successfully used ** before in my copy of Python 2.7, and used it in cases where it can return complex values, or where it might throw an error if it returns complex unless I tell it to return a complex number.
Besides wanting working code, I would really enjoy an explanation of what is happening with my code. Thank you.