Calculating (sqrt(n+1)-sqrt(n))*1e9

Technical questions regarding the XTC tools and programming with XMOS.
kster59
XCore Addict
Posts: 162
Joined: Thu Dec 31, 2009 8:51 am

Calculating (sqrt(n+1)-sqrt(n))*1e9

Post by kster59 »

I need to calculate (sqrt(n+1)-sqrt(n))*1e9 on the XMOS with n=[1,100e3] rounded off to the nearest int?

Previously I stored an unsigne short lookup table * 1e5 for values n=[1,2048] but now I ran out of memory and need the memory for other stuff. I'd also like a bigger table so want to calculate it on the fly.

Any suggestions? On a processor with doubles I could use an iterative method but am new to a non floating point.


kster59
XCore Addict
Posts: 162
Joined: Thu Dec 31, 2009 8:51 am

Post by kster59 »

Nevermind I was able to use the C implement of sqrt with an Xc wrapper which was fast enough.
User avatar
Folknology
XCore Legend
Posts: 1274
Joined: Thu Dec 10, 2009 10:20 pm

Post by Folknology »

You also have 8K of OTP ROM to play around with which is useful for things like tables, worth remembering in these situations

regards
Al
User avatar
segher
XCore Expert
Posts: 844
Joined: Sun Jul 11, 2010 1:31 am

Post by segher »

kster59 wrote:I need to calculate (sqrt(n+1)-sqrt(n))*1e9 on the XMOS with n=[1,100e3] rounded off to the nearest int?
sqrt(n+1) and sqrt(n) are very close together, for bigger n, so you should try not to
calculate these separately. M*(sqrt(n+1)-sqrt(n)) = M/(sqrt(n+1)+sqrt(n)), which
behaves much better.

M/(sqrt(n+1)+sqrt(n)) is very close to M/sqrt(4n+2), maybe that is good enough
for your purposes? They differ by about M / (64*n*n*sqrt(n)), so no difference
at all for n>300 or so.

Either way, the best is usually to do a small lookup table and then refinement
(using Newton-Raphson for example).