Java-Gaming.org
 Featured games (81) games approved by the League of Dukes Games in Showcase (497) Games in Android Showcase (114) games submitted by our members Games in WIP (563) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 sqrt(x) and 1/sqrt(x)  (Read 3114 times) 0 Members and 1 Guest are viewing this topic.
Roquen
 « Posted 2009-08-08 14:37:36 »

Popular way to approximate sqrt(x) and 1/sqrt(x):

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22 `  public static float isqrt(float x)  {    float hx = x * 0.5f;    int   ix;    float r;    // make initial guess    ix = Float.floatToRawIntBits(x);    ix = 0x5f3759df - (ix>>1);    r  = Float.intBitsToFloat(ix);    // do some number of newton-ralphson steps,    // each doubles the number of accurate    // binary digits.    r  = r*(1.5f-hx*r*r);  //r  = r*(1.5f-hx*r*r);  //r  = r*(1.5f-hx*r*r);  //r  = r*(1.5f-hx*r*r);    return r;    // 1/sqrt(x)  //return r*x;  // sqrt(x)  }`
woogley
 « Reply #1 - Posted 2009-08-08 17:45:26 »

Here's some background on how this technique works: http://betterexplained.com/articles/understanding-quakes-fast-inverse-square-root/
DzzD
 « Reply #2 - Posted 2009-08-08 17:57:32 »

very nice

EDIT : tested.... and unfortunatly due to the slowness of Float.floatToIntBits(x) it seems to doesn't give any performance gain

 Games published by our own members! Check 'em out!
Riven
« League of Dukes »

JGO Overlord

Medals: 799
Projects: 4
Exp: 16 years

Hand over your head.

 « Reply #3 - Posted 2009-08-08 18:37:39 »

replace
Float.floatToIntBits()

by
Float.floatToRawIntBits()

maybe it speeds up the 'conversion'

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Roquen
 « Reply #4 - Posted 2009-08-08 18:46:54 »

Yeah, I just logged in to note using 'raw' instead comment (bad me).  The 'guess' part can be changed to something else (such as a small table for limited ranges).

Another thing to note is for renormalizing vectors, quaternions, etc. ... the guessed value is '1' and normally a single N-R step will commonly give you the renormalization multiplier.

(edit: many typos)
Roquen
 « Reply #5 - Posted 2009-08-08 19:07:20 »

Yeah, I just logged in to note using 'raw' instead comment (bad me).  The 'guess' part can be changed to something else (such as a small table for limited ranges).

Another thing to note is for renormalizing vectors, quaternions, etc. ... the guessed value is '1' and normally a single N-R step will commonly give you the renormalization multiplier. (edit: I'm pretty sure I have a better method for refining than N-R, post here if you'd like me to try to dig it up:  Kahan-Ng refinement).

(edit: many typos)
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 BurntPizza (25 views) 2014-09-19 03:14:18 Dwinin (39 views) 2014-09-12 09:08:26 Norakomi (68 views) 2014-09-10 13:57:51 TehJavaDev (93 views) 2014-09-10 06:39:09 Tekkerue (47 views) 2014-09-09 02:24:56 mitcheeb (68 views) 2014-09-08 06:06:29 BurntPizza (51 views) 2014-09-07 01:13:42 Longarmx (38 views) 2014-09-07 01:12:14 Longarmx (44 views) 2014-09-07 01:11:22 Longarmx (40 views) 2014-09-07 01:10:19
 BurntPizza 37x Riven 18x Rayvolution 18x ags1 16x princec 16x basil_ 16x KevinWorkman 15x LiquidNitrogen 12x theagentd 11x kevglass 11x deathpat 11x nsigma 11x HeroesGraveDev 9x The Lion King 7x TehJavaDev 6x PandaMoniumHUN 6x
 List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50List of Learning Resources2014-07-31 16:26:06List of Learning Resources2014-07-31 11:54:12HotSpot Optionsby dleskov2014-07-08 01:59:08
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org