Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (754) Games in Android Showcase (229) games submitted by our members Games in WIP (842) 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 3904 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
 « 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

JGO Kernel

Medals: 517

 « 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

JGO Kernel

Medals: 517

 « 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: 167
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

 DesertCoockie (20 views) 2018-05-13 18:23:11 nelsongames (68 views) 2018-04-24 18:15:36 nelsongames (65 views) 2018-04-24 18:14:32 ivj94 (748 views) 2018-03-24 14:47:39 ivj94 (79 views) 2018-03-24 14:46:31 ivj94 (579 views) 2018-03-24 14:43:53 Solater (95 views) 2018-03-17 05:04:08 nelsongames (168 views) 2018-03-05 17:56:34 Gornova (362 views) 2018-03-02 22:15:33 buddyBro (1022 views) 2018-02-28 16:59:18
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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