julia lang - Different results for loops with decrement and increment -


i have come realize don't same result between iterations increment , decrement. slight difference when math expression n + (1/(i^4)) iterates , adds new value on 75+ times, being i number of iteration. under 75 iterations result each loop remains same. ideas of why happening? code running:

y=0 in 1:75    y = y + (1/(i^4))  end print("final y value: ",y,"\n")  x=0  in 75:-1:1     x = x + (1/(i^4)) end  print("final x value: ",x,"\n") 

and got x , y:

final y value: 1.0823224592496965 final x value: 1.0823224592496967 

but if change loop limits 74 or less (74 in following example), same result both loop:

final y value: 1.0823224276447583 final x value: 1.0823224276447583 

that because of floating point rounding errors take place during addition, because of precision of float64. can use arbitrary precision floats (i.e. bigfloats) overcome issue if rounding errors important.

y = bigfloat(0) #0.000000000000000000000000000000000000000000000000000000000000000000000000000000  in 1:75    y += 1/(i^4) end  x = bigfloat(0) in 75:-1:1    x += 1/(i^4) end  print("final x value: ",x,"\n") #final x value: 1.082322459249696627186876349853547531892905553263517504092305898666381835937500  print("final y value: ",y,"\n") #final y value: 1.082322459249696627186876349853547531892905553263517504092305898666381835937500 

note in original code define x , y int's , proceed add float64's them - slow down code ( https://docs.julialang.org/en/latest/manual/performance-tips/#avoid-changing-the-type-of-a-variable-1 )

also check out https://github.com/juliaarbtypes/arbfloats.jl


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -