1. Re: Math Shortcuts
- Posted by Hawke <mdeland at NWINFO.NET> Jul 03, 1998
- 423 views
- Last edited Jul 04, 1998
what if: Xdis = floor(x2-x1) Xdis = power(Xdis,2) --now in theory, Xdis _should_ be 'integer', ie: --Xdis should have no 'real' or fractional part Ydis = floor(y2-y1) Xdis = power(Ydis,2) --Zdis = power(floor(z2-z1),2) DIS = SQRT( Xdis + Ydis ) -- + Zdis since you're starting with an 'integer' value sqrt should be faster... but if its not actually faster, then since it is an integer, you could in theory have a lookup table for the squareroots of all the integers in the range most likely for DIS to fall in... then if you used a binary search algorithm for that lookup table...after all the lookup table is of course inherently presorted... and binary searchs are -fast-...can you gain a speed increase over just plain dis = sqrt ( power(x2-x1,2) + power(y2-y1,2))???? no...i dont think so...i dont think any of the tricks i've mentioned or seen mentioned will actually improve speed noticeably (except of course R.McDougal optimization for deltaX=deltaY and his algorithm for longhanding it) unless youre in the middle of a loop iterating at least several thousand times...finding several thousand distances...then you may very well be able to see a difference...sqrt is not that slow... im not being pessimistic...well okay i am :/ im just saying that sometimes tweaking for speed is not worth the implementation effort to gain 0.000000001 % in a program that only takes 1/4 sec to run anyway :) i've caught myself doing tweaking like that...bonk myself later for it... and btw, nowadays, raising a number to a power as opposed to multiplying it times itself (as in a loop) won't hamper you... most power implementations will actually do better than loop multiplying since power can be optimized in various neat ways... *runs back to doghouse where his wife is kind enuff to let him sleep in :P *--Hawke'