Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (604) Games in Android Showcase (171) games submitted by our members Games in WIP (654) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Alpha max plus Beta min  (Read 2042 times) 0 Members and 1 Guest are viewing this topic.
StumpyStrust
 « Posted 2012-10-25 07:16:51 »

So I do not know if this is useful or not and I really don't know if it is worth anything but I read about it on the nets and decided to take a whack at it and see what happens.

Basically, an algorithm that approximates the sqrt. I have no idea if I am doing this right but here it is

http://en.wikipedia.org/wiki/Alpha_max_plus_beta_min_algorithm  love wiki

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21 `public float fastSqrt(Vector2f vec1)   {      float x = vec1.x*vec1.x;      float y = vec1.y*vec1.y;      if(x > y)      {         x = (x*.960434f)/vec1.x;         y = (y*.397825f)/vec1.y;      }      else      {         y = (y*.960434f)/vec1.y;         x = (x*.397825f)/vec1.x;      }      //when dividing your vector could have a negative component so need to check that.       if(x <0)         x *= -1;      if(y <0)         y *= -1;      return x+y;   }`

I know this is micro benchmarking which is basically useless but I found no speed improvement against Math.sqrt

I have to be doing something right as the result is only off from Math.sqrt by a little and in my particle prog there is almost no visual difference.

Anyone know something on this? I think it is a cool idea and would like to hear what other, more experienced, people have to say.

theagentd

« JGO Bitwise Duke »

Medals: 526
Projects: 3
Exp: 8 years

 « Reply #1 - Posted 2012-10-25 11:49:32 »

Math.sqrt() is almost never your bottleneck, but I still think that it might be useful. For example, it works on floats directly. Casting a double to a float is not free (and pretty slow in my experience). Just one question, exactly what is it calculating? The length of vec1?

Minor performance tip:
x *= -1 is the same as x = -x. I think simply using Math.abs() should be the fastest though.

 1 `return Math.abs(x) + Math.abs(y);`

Myomyomyo.
DrHalfway
 « Reply #2 - Posted 2012-10-25 13:01:09 »

Here is one based on the Carmacks constant

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 `   public static final float sqrt(final float num) {      int i = 0;      float x2, y;            x2 = num * 0.5f;      y = num;      i = Float.floatToIntBits(num);            // carmacks constant      i = 0x5f3759df - (i >> 1);      y = Float.intBitsToFloat(i);            // replicate for better accuracy      y = y * (1.5f - ( x2 * y * y));      //y = y * (1.5f - ( x2 * y * y));            return y * num;      }`

Never done benchmarking compared to java's sqrt(), perhaps someone else can take their time and do it

~DrHalfway

Roquen
 « Reply #3 - Posted 2012-10-25 13:05:40 »

My opinion is don't bother with software sqrt and 1/sqrt.  They're just too fast these days to goof around with (and very hard to approximate).  Maybe one day we'll have access to the SIMD approximations...sigh.
Roquen
 « Reply #4 - Posted 2012-10-25 14:45:10 »

I guess I should explicitly state that if the range is narrow (like say re-normalization) and/or high error is acceptable, then approximations of either is more than approp.
StumpyStrust
 « Reply #5 - Posted 2012-10-25 15:47:15 »

Yeah I know that this was not much of a slow down but thought it sounded fun so tried it. I generally try to stay away from the Math class as I have no idea what it does.

Nate

« JGO Bitwise Duke »

Medals: 165
Projects: 4
Exp: 14 years

Esoteric Software

 « Reply #6 - Posted 2012-10-25 20:32:23 »

Math.sqrt is fast, even on Android, not worth doing anything else.

Pages: [1]
 ignore  |  Print

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

 SHC (28 views) 2015-08-01 03:58:20 Jesse (20 views) 2015-07-29 04:35:27 Riven (40 views) 2015-07-27 16:38:00 Riven (22 views) 2015-07-27 15:35:20 Riven (25 views) 2015-07-27 12:26:13 Riven (15 views) 2015-07-27 12:23:39 BurntPizza (36 views) 2015-07-25 00:14:37 BurntPizza (46 views) 2015-07-24 22:06:39 BurntPizza (31 views) 2015-07-24 06:06:53 NoxInc (37 views) 2015-07-22 22:16:53
 theagentd 51x wessles 49x basil_ 35x KaiHH 26x orangepascal 25x Riven 20x ags1 20x mooman219 17x bornander 15x KudoDEV 13x CelestialCreator 11x princec 11x Jesse 10x klaus 10x pquiring 10x philfrei 8x
 List of Learning Resourcesby gouessej2015-07-09 11:29:36How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21
 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