[R] Numerical stability of: 1/(1 - cos(x)) - 2/x^2

2023-08-15 Thread Leonard Mada via R-help
Dear R-Users, I tried to compute the following limit: x = 1E-3; (-log(1 - cos(x)) - 1/(cos(x)-1)) / 2 - 1/(x^2) + log(x) # 0.4299226 log(2)/2 + 1/12 # 0.4299069 However, the result diverges as x decreases: x = 1E-4 (-log(1 - cos(x)) - 1/(cos(x)-1)) / 2 - 1/(x^2) + log(x) # 0.9543207 # correct: 0

Re: [R] Numerical stability of: 1/(1 - cos(x)) - 2/x^2

2023-08-15 Thread Iris Simmons
You could rewrite 1 - cos(x) as 2 * sin(x/2)^2 and that might give you more precision? On Wed, Aug 16, 2023, 01:50 Leonard Mada via R-help wrote: > Dear R-Users, > > I tried to compute the following limit: > x = 1E-3; > (-log(1 - cos(x)) - 1/(cos(x)-1)) / 2 - 1/(x^2) + log(x) > # 0.4299226 >