Code: Select all

` // UNTESTED`

public static final float atan2( final float y, final float x) {

// float coeff_1 = PI/4;

// float coeff_2 = 3*coeff_1;

final float abs_y = abs( y) + .0000000001f; // kludge to prevent 0/0

// condition

float angle, r;

if ( x >= 0) {

r = (x - abs_y) / (x + abs_y);

// angle = coeff_1 - coeff_1 * r;

angle = 0.1963f * r * r * r - 0.9817f * r + PI / 4;

}

else {

r = (x + abs_y) / (abs_y - x);

// angle = coeff_2 - coeff_1 * r;

angle = 0.1963f * r * r * r - 0.9817f * r + 3 * PI / 4;

}

if ( y < 0) {

return -angle; // negate if in quad III or IV

}

else {

return angle;

}

}

/**

* Computes a fast approximation to <code>Math.pow(a, b)</code>. Adapted

* from <url>http://www.dctsystems.co.uk/Software/power.html</url>.

*

* @param a

* a positive number

* @param b

* a number

* @return a^b

*/

// UNTESTED

// I'm not sure if this is correct for floats, this was meant for doubles

public static final float pow( final float a, float b) {

// adapted from: http://www.dctsystems.co.uk/Software/power.html

float x = Float.floatToRawIntBits( a);

x *= 1.0f / (1 << 23);

x = x - 127;

float y = x - MathUtils.floor( x);

b *= x + (y - y * y) * 0.346607f;

y = b - MathUtils.floor( b);

y = (y - y * y) * 0.33971f;

return Float.intBitsToFloat( (int) ((b + 127 - y) * (1 << 23)));

}

// UNTESTED

public static final float sqrt( float x) {

x = invSqrt( x);

if ( x != 0.0f) {

return 1.0f / x;

}

else {

return 0;

}

}

// UNTESTED

public final static float invSqrt( float x) {

final float xhalf = 0.5f * x;

int i = Float.floatToRawIntBits( x);

i = 0x5f3759df - (i >> 1);

x = Float.intBitsToFloat( i);

x = x * (1.5f - xhalf * x * x);

return x;

}

This has some implications, like loss of accuracy. But you would still have identical results across machines (which is also why the Math library is so costly), so anything networked wouldn't have a problem.

What do you think? We could also just make it an option, although that would cost some overhead, as we would have to check for the option every call.