I was reading Mathematics today and I saw this question. Please take a look at the question and Brevan Ellefsen's answer.
The question asks about taking the derivative of f(x)=x^(2*x^(3*x^(4*x^(5*x^(6*x^(7*x^(...)))))))
(looks like SO doesn't support LaTeX)
Brevan Ellefsen didn't really answer the question, but took a numerical approach to the function. Motivated from that answer, I tried to implement the function in Python. I don't have any experience in numerical methods but I really like these kind of coding.
So here is my code:
# Precision is set to 50.
def f(k, x):
if (k < 1):
# First argument cannot be less than one.
return -1
try:
temp = 1
for i in xrange(k):
temp = (k-i)*(x**temp)
return temp
except TypeError:
# k should be an integer.
return -1
Now I'm calling the function for k = 4
and x = 0.5
and I'm comparing my function's output with WolframAlpha's output. Here are the results:
0.43854381197983005602480943707632832229137420654297 <-- My output
0.43854381197983004379857027951312617339018977615169 <-- WolframAlpha's output
As you can see, after some point, the ouputs are very different. It's not a very big difference but still enough to make me curious.
- Why is this happening?
For this part, this one is helpful and I've already seen it months ago, so I'm a little aware of floating number behaviour but I wasn't sure that was the reason in my case when I posted my question in the first place. I will still need help for the following ones anyway. - Is my function not working properly?
- If so, how should I fix it?
- If it's OK, which result is more precise?
- How can I optimize my code?