Board index » delphi » Fast Sqrt
Nickolai Delegna
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
|
Nickolai Delegna
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Fast Sqrt
Can anyone tell me an algorithm of calculating integer square root
of integer fast; i.e i need function FastSqrt ( X : Integer ) : Integer; with only integer calculations [ Round ( Sqrt ( X ) ) won't do]. Thanx in advance, |
Ing. Franz Glase
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:Fast SqrtQuoteNickolai Delegnan wrote: 1) estimate by collecting the bits, eg. could be made quicker with some sort of lookup table? 2) Est := (Est + X/Est) shr 1; In ASM surely a bit faster. As said before: an idea only... Franz Glaser |
Frank Peel
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:Fast SqrtQuoteIng. Franz Glaser wrote in message <36B0F127.8068E...@eunet.at>... LongInts, he could do the whole thing with a lookup table. Since 256*256=65536 each entry would fit in one byte and the whole table would fit in a quarter KB. That's small enough for a lookup table, and it would be fast. FP |
Dr John Stockto
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:Fast SqrtJRS: In article <36b091d...@news.ptt.ru> of Thu, 28 Jan 1999 19:31:54 in news:comp.lang.pascal.borland, Nickolai Delegnan <n...@dialup.ptt.ru> wrote: Quote> Can anyone tell me an algorithm of calculating integer square root to be 2 or 3, or don't-care ? If you mean integer, and are not going to need longint, the fastest way var J, JP : byte ; K, J2 : word ; If you can't afford the memory, use shifting to generate a rough square But it may not be possible to root much faster than with Sqrt, providing If the job is such that you will often call repeatedly with the same procedure Srt(const J : integer) : integer (untested} ; -- |
Osmo Ronkan
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:Fast SqrtIn article <78s37k$3n...@ezekiel.eunet.ie>, QuoteFrank Peelo <fpe...@indigo.ie> wrote: could use the high word as index and then check the that the result is actually OK. That is something like: z:=lut[hi(x)] The lut would be like 0,16,22,27,32,35,39,42,45,48,.. trunc(sqrt(256*i)). Where in the end the numbers are adjacent ones or even same. Or it z:=lut[hi(x)] Some consideration should be put to very large numbers though. Osmo |
Frank Peel
![]() Delphi Developer |
Wed, 18 Jun 1902 08:00:00 GMT
Re:Fast SqrtOsmo Ronkanen <ronka...@cc.helsinki.fi> wrote in article Quote> In article <78s37k$3n...@ezekiel.eunet.ie>, has posted it right so good night. FP |