I have an array which has values, if calcualted precisely: [3.056565 3.924560] however in python it prints rounded numbers and the array is [3.06, 3.93]. The array is the used to calculate something else and the result is different than expected. How can I make that array to stop rounding itself?
Here is the code where you can see that array is not precisely calcualted:
import datetime
import math
import numpy as np
def numGradient(f, x, h):
n = len(x)
g = [0]*n
g = np.array(g)
for i in range(n):
e = [0]*n
e[i] = 1
e=np.array(e)
g[i] = (f(x+e*h)-f(x-e*h))//(2*h)
return(g)
def myFun(x) :
return ( 0.6 + ((math.sin(x[0]**2-x[1]**2))**2-0.5)/((1+0.001*(x[0]**2+x[1]**2))**2) )
x_old=(3,4)
xthis = x_old - 0.01*numGradient(myFun, x_old, 10**(-6))
print(xthis)
numpy.array's are not "round themself" besides ordinary rounding that occur in floating-point arithmetic. (Rounding on printing can occur that does not affect the numbers itself.) Could you please provide more concise code snippet that demonstrate the behaviour you are asking about? Expected and obtained answers are also would be helpful.x_old- a*numGradient(f, x_old, 10**(-6)), where the result is[ 3.06 3.93]and it should be3.056565 3.924560when calculating outside python ( for example using R)3.056565. The difference between this number and3.06is what I mean under "discrepancy" in the comment above. It is not clear what is the source of this discrepancy. Probably, your implementation of this code in a different language is different in some other ways. This is why I believe that you have to reproduce the same effect with simple example (i.e. one arithmetic operation).